diff --git a/NEWS b/NEWS index 7398a75f..1a221172 100644 --- a/NEWS +++ b/NEWS @@ -28,6 +28,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier tested on Linux. . Removed trailing whitespace from all text files, see remote-trailing-whitespace.php maintenance script. +. Convert configuration to use property list backend. 3.2.0, released 2008-10-31 # Using %Core.CollectErrors forces line number/column tracking on, whereas diff --git a/library/HTMLPurifier/Config.php b/library/HTMLPurifier/Config.php index 48848d19..299bc0f0 100644 --- a/library/HTMLPurifier/Config.php +++ b/library/HTMLPurifier/Config.php @@ -41,11 +41,6 @@ class HTMLPurifier_Config */ protected $serial; - /** - * Two-level associative array of configuration directives - */ - protected $conf; - /** * Parser for variables */ @@ -68,13 +63,18 @@ class HTMLPurifier_Config */ protected $finalized = false; + /** + * Property list containing configuration directives. + */ + protected $plist; + /** * @param $definition HTMLPurifier_ConfigSchema that defines what directives * are allowed. */ public function __construct($definition) { - $this->conf = $definition->defaults; // set up, copy in defaults - $this->def = $definition; // keep a copy around for checking + $this->plist = new HTMLPurifier_PropertyList($definition->defaultPlist); + $this->def = $definition; // keep a copy around for checking $this->parser = new HTMLPurifier_VarParser_Flexible(); } @@ -118,7 +118,7 @@ class HTMLPurifier_Config * @param $key String key */ public function get($namespace, $key) { - if (!$this->finalized && $this->autoFinalize) $this->finalize(); + if (!$this->finalized) $this->autoFinalize ? $this->finalize() : $this->plist->squash(true); if (!isset($this->def->info[$namespace][$key])) { // can't add % due to SimpleTest bug trigger_error('Cannot retrieve value of undefined directive ' . htmlspecialchars("$namespace.$key"), @@ -131,7 +131,7 @@ class HTMLPurifier_Config E_USER_ERROR); return; } - return $this->conf[$namespace][$key]; + return $this->plist->get("$namespace.$key"); } /** @@ -139,13 +139,14 @@ class HTMLPurifier_Config * @param $namespace String namespace */ public function getBatch($namespace) { - if (!$this->finalized && $this->autoFinalize) $this->finalize(); + if (!$this->finalized) $this->autoFinalize ? $this->finalize() : $this->plist->squash(true); if (!isset($this->def->info[$namespace])) { trigger_error('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace), E_USER_WARNING); return; } - return $this->conf[$namespace]; + $full = $this->getAll(); + return $full[$namespace]; } /** @@ -179,8 +180,13 @@ class HTMLPurifier_Config * Retrieves all directives, organized by namespace */ public function getAll() { - if (!$this->finalized && $this->autoFinalize) $this->finalize(); - return $this->conf; + if (!$this->finalized) $this->autoFinalize ? $this->finalize() : $this->plist->squash(true); + $ret = array(); + foreach ($this->plist->squash() as $name => $value) { + list($ns, $key) = explode('.', $name, 2); + $ret[$ns][$key] = $value; + } + return $ret; } /** @@ -240,7 +246,7 @@ class HTMLPurifier_Config return; } } - $this->conf[$namespace][$key] = $value; + $this->plist->set("$namespace.$key", $value); // reset definitions if the directives they depend on changed // this is a very costly process, so it's discouraged @@ -285,7 +291,7 @@ class HTMLPurifier_Config * @param $raw Whether or not definition should be returned raw */ public function getDefinition($type, $raw = false) { - if (!$this->finalized && $this->autoFinalize) $this->finalize(); + if (!$this->finalized) $this->autoFinalize ? $this->finalize() : $this->plist->squash(true); $factory = HTMLPurifier_DefinitionCacheFactory::instance(); $cache = $factory->create($type, $this); if (!$raw) { diff --git a/library/HTMLPurifier/ConfigSchema.php b/library/HTMLPurifier/ConfigSchema.php index 3e2563c2..340ed7db 100644 --- a/library/HTMLPurifier/ConfigSchema.php +++ b/library/HTMLPurifier/ConfigSchema.php @@ -11,6 +11,11 @@ class HTMLPurifier_ConfigSchema { */ public $defaults = array(); + /** + * The default property list. Do not edit this property list. + */ + public $defaultPlist; + /** * Definition of the directives. The structure of this is: * @@ -47,6 +52,10 @@ class HTMLPurifier_ConfigSchema { */ static protected $singleton; + public function __construct() { + $this->defaultPlist = new HTMLPurifier_PropertyList(); + } + /** * Unserializes the default ConfigSchema. */ @@ -84,6 +93,7 @@ class HTMLPurifier_ConfigSchema { if ($allow_null) $obj->allow_null = true; $this->info[$namespace][$name] = $obj; $this->defaults[$namespace][$name] = $default; + $this->defaultPlist->set("$namespace.$name", $default); } /** diff --git a/library/HTMLPurifier/ConfigSchema/schema.ser b/library/HTMLPurifier/ConfigSchema/schema.ser index 1ac5ff69..1eaecd11 100644 Binary files a/library/HTMLPurifier/ConfigSchema/schema.ser and b/library/HTMLPurifier/ConfigSchema/schema.ser differ diff --git a/library/HTMLPurifier/PropertyList.php b/library/HTMLPurifier/PropertyList.php index 247eb3b9..2b99fb7b 100644 --- a/library/HTMLPurifier/PropertyList.php +++ b/library/HTMLPurifier/PropertyList.php @@ -15,11 +15,18 @@ class HTMLPurifier_PropertyList */ protected $parent; + protected $cache; + + public function __construct($parent = null) { + $this->parent = $parent; + } + /** * Recursively retrieves the value for a key */ public function get($name) { if ($this->has($name)) return $this->data[$name]; + // possible performance bottleneck, convert to iterative if necessary if ($this->parent) return $this->parent->get($name); throw new HTMLPurifier_Exception("Key '$name' not found"); } @@ -48,12 +55,17 @@ class HTMLPurifier_PropertyList } /** - * Returns an iterator for traversing this array, optionally filtering - * for a certain prefix. + * Squashes this property list and all of its property lists into a single + * array, and returns the array. This value is cached by default. + * @param $force If true, ignores the cache and regenerates the array. */ - public function getIterator($filter = null) { - $a = new ArrayObject($this->data); - return new HTMLPurifier_PropertyListIterator($a->getIterator(), $filter); + public function squash($force = false) { + if ($this->cache !== null && !$force) return $this->cache; + if ($this->parent) { + return $this->cache = array_merge($this->parent->squash($force), $this->data); + } else { + return $this->cache = $this->data; + } } /** diff --git a/tests/HTMLPurifier/PropertyListTest.php b/tests/HTMLPurifier/PropertyListTest.php index 964a7313..a2d4811e 100644 --- a/tests/HTMLPurifier/PropertyListTest.php +++ b/tests/HTMLPurifier/PropertyListTest.php @@ -65,23 +65,29 @@ class HTMLPurifier_PropertyListTest extends UnitTestCase $this->assertIdentical($plist->has('key3'), false); } - function testIterator() { - $plist = new HTMLPurifier_PropertyList(); - $plist->set('nkey1', 'v'); - $plist->set('nkey2', 'v'); - $plist->set('rkey3', 'v'); - $a = array(); - foreach ($plist->getIterator() as $key => $value) { - $a[$key] = $value; - } - $this->assertIdentical($a, array('nkey1' => 'v', 'nkey2' => 'v', 'rkey3' => 'v')); - $a = array(); - foreach ($plist->getIterator('nkey') as $key => $value) { - $a[$key] = $value; - } - $this->assertIdentical($a, array('nkey1' => 'v', 'nkey2' => 'v')); + function testSquash() { + $parent = new HTMLPurifier_PropertyList(); + $parent->set('key1', 'hidden'); + $parent->set('key2', 2); + $plist = new HTMLPurifier_PropertyList($parent); + $plist->set('key1', 1); + $plist->set('key3', 3); + $this->assertIdentical( + $plist->squash(), + array('key1' => 1, 'key2' => 2, 'key3' => 3) + ); + // updates don't show up... + $plist->set('key2', 22); + $this->assertIdentical( + $plist->squash(), + array('key1' => 1, 'key2' => 2, 'key3' => 3) + ); + // until you force + $this->assertIdentical( + $plist->squash(true), + array('key1' => 1, 'key2' => 22, 'key3' => 3) + ); } - } // vim: et sw=4 sts=4