0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 08:21:52 +00:00

Implement schema extraction script; almost done except for version extraction. Also, some minor refinements.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1534 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-02-07 19:29:08 +00:00
parent 14ef0b75e5
commit b6c9dcefd7
4 changed files with 119 additions and 5 deletions

View File

@ -39,12 +39,22 @@ class ConfigSchema_StringHashReverseAdapter
trigger_error("Directive '$ns.$directive' doesn't exist in schema"); trigger_error("Directive '$ns.$directive' doesn't exist in schema");
return; return;
} }
$def = $this->schema->info[$ns][$directive]; $def = $this->schema->info[$ns][$directive];
if ($def instanceof HTMLPurifier_ConfigDef_DirectiveAlias) {
return false;
}
$ret['ID'] = "$ns.$directive"; $ret['ID'] = "$ns.$directive";
$ret['TYPE'] = $def->type; $ret['TYPE'] = $def->type;
$ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]); $ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]);
$ret['DESCRIPTION'] = $def->description;
if ($def->allowed !== null) { // Attempt to extract version information from description.
$ret['DESCRIPTION'] = wordwrap($this->normalize($def->description), 75, "\n");
if ($def->allowed !== true) {
$ret['ALLOWED'] = $this->exportLookup($def->allowed); $ret['ALLOWED'] = $this->exportLookup($def->allowed);
} }
if (!empty($def->aliases)) { if (!empty($def->aliases)) {
@ -59,14 +69,17 @@ class ConfigSchema_StringHashReverseAdapter
/** /**
* Exports a variable into a PHP-readable format * Exports a variable into a PHP-readable format
*/ */
protected function export($var) { public function export($var) {
if ($var === array()) return 'array()'; // single-line format
return var_export($var, true); return var_export($var, true);
} }
/** /**
* Exports a lookup array into the form 'key1', 'key2', ... * Exports a lookup array into the form 'key1', 'key2', ...
*/ */
protected function exportLookup($lookup) { public function exportLookup($lookup) {
if (!is_array($lookup)) return $this->export($lookup);
if (empty($lookup)) return '';
$keys = array_map(array($this, 'export'), array_keys($lookup)); $keys = array_map(array($this, 'export'), array_keys($lookup));
return implode(', ', $keys); return implode(', ', $keys);
} }
@ -74,7 +87,9 @@ class ConfigSchema_StringHashReverseAdapter
/** /**
* Exports a hash into the form 'key' => 'val',\n ... * Exports a hash into the form 'key' => 'val',\n ...
*/ */
protected function exportHash($hash) { public function exportHash($hash) {
if (!is_array($hash)) return $this->export($hash);
if (empty($hash)) return '';
$code = $this->export($hash); $code = $this->export($hash);
$lines = explode("\n", $code); $lines = explode("\n", $code);
$ret = ''; $ret = '';
@ -86,4 +101,11 @@ class ConfigSchema_StringHashReverseAdapter
return $ret; return $ret;
} }
/**
* Normalizes a string to Unix style newlines
*/
protected function normalize($string) {
return str_replace(array("\r\n", "\r"), "\n", $string);
}
} }

View File

@ -0,0 +1,65 @@
#!/usr/bin/php
<?php
chdir(dirname(__FILE__));
require_once 'common.php';
assertCli();
/**
* @file
* Extracts all definitions inside a configuration schema
* (HTMLPurifier_ConfigSchema) and exports them as plain text files.
*
* @todo Extract version numbers.
*/
define('HTMLPURIFIER_SCHEMA_STRICT', true); // description data needs to be collected
require_once dirname(__FILE__) . '/../library/HTMLPurifier.auto.php';
// We need includes to ensure all HTMLPurifier_ConfigSchema calls are
// performed.
require_once 'HTMLPurifier.includes.php';
// Also, these extra files will be necessary.
require_once 'HTMLPurifier/Filter/ExtractStyleBlocks.php';
/**
* Takes a hash and saves its contents to library/HTMLPurifier/ConfigSchema/
*/
function saveHash($hash) {
if ($hash === false) return;
$dir = realpath(dirname(__FILE__) . '/../library/HTMLPurifier/ConfigSchema/');
$name = $hash['ID'] . '.txt';
$file = $dir . $name;
if (file_exists($file)) {
//trigger_error("File already exists; skipped $name");
//return;
}
$file = new FSTools_File($file);
$file->open('w');
$multiline = false;
foreach ($hash as $key => $value) {
$multiline = $multiline || (strpos($value, "\n") !== false);
if ($multiline) {
$file->put("--$key--" . PHP_EOL);
$file->put(str_replace("\n", PHP_EOL, $value) . PHP_EOL);
} else {
if ($key == 'ID') {
$file->put("$value" . PHP_EOL);
} else {
$file->put("$key: $value" . PHP_EOL);
}
}
}
$file->close();
}
$schema = HTMLPurifier_ConfigSchema::instance();
$adapter = new ConfigSchema_StringHashReverseAdapter($schema);
foreach ($schema->info as $ns => $ns_array) {
saveHash($adapter->get($ns));
foreach ($ns_array as $dir => $x) {
saveHash($adapter->get($ns, $dir));
}
}

View File

@ -57,4 +57,30 @@ class ConfigSchema_StringHashReverseAdapterTest extends UnitTestCase
$adapter->get('BadNs', 'BadDir'); $adapter->get('BadNs', 'BadDir');
} }
function assertMethod($func, $input, $expect) {
$adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$result = $adapter->$func($input);
$this->assertIdentical($result, $expect);
}
function testExportEmptyHash() {
$this->assertMethod('exportHash', array(), '');
}
function testExportHash() {
$this->assertMethod('exportHash', array('foo' => 'bar'), "'foo' => 'bar',\n");
}
function testExportEmptyLookup() {
$this->assertMethod('exportLookup', array(), '');
}
function testExportSingleLookup() {
$this->assertMethod('exportLookup', array('key' => true), "'key'");
}
function testExportLookup() {
$this->assertMethod('exportLookup', array('key' => true, 'key2' => true, 3 => true), "'key', 'key2', 3");
}
} }

View File

@ -240,6 +240,7 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
$this->assertValid("foo\nbar", 'lookup', array('foo' => true, 'bar' => true)); $this->assertValid("foo\nbar", 'lookup', array('foo' => true, 'bar' => true));
$this->assertValid("foo\nbar,baz", 'lookup', array('foo' => true, 'bar' => true, 'baz' => true)); $this->assertValid("foo\nbar,baz", 'lookup', array('foo' => true, 'bar' => true, 'baz' => true));
$this->assertValid('', 'lookup', array()); $this->assertValid('', 'lookup', array());
$this->assertValid(array(), 'lookup');
$this->assertValid(array('foo' => 'bar'), 'hash'); $this->assertValid(array('foo' => 'bar'), 'hash');
$this->assertValid(array(1 => 'moo'), 'hash'); $this->assertValid(array(1 => 'moo'), 'hash');