--- /dev/null
+<?php\r
+\r
+/**\r
+ * Abstract class representing Definition cache managers that implements\r
+ * useful common methods and is a factory.\r
+ * @todo Create a separate maintenance file advanced users can use to\r
+ * cache their custom HTMLDefinition, which can be loaded\r
+ * via a configuration directive\r
+ * @todo Implement memcached\r
+ */\r
+abstract class HTMLPurifier_DefinitionCache\r
+{\r
+ /**\r
+ * @type string\r
+ */\r
+ public $type;\r
+\r
+ /**\r
+ * @param string $type Type of definition objects this instance of the\r
+ * cache will handle.\r
+ */\r
+ public function __construct($type)\r
+ {\r
+ $this->type = $type;\r
+ }\r
+\r
+ /**\r
+ * Generates a unique identifier for a particular configuration\r
+ * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config\r
+ * @return string\r
+ */\r
+ public function generateKey($config)\r
+ {\r
+ return $config->version . ',' . // possibly replace with function calls\r
+ $config->getBatchSerial($this->type) . ',' .\r
+ $config->get($this->type . '.DefinitionRev');\r
+ }\r
+\r
+ /**\r
+ * Tests whether or not a key is old with respect to the configuration's\r
+ * version and revision number.\r
+ * @param string $key Key to test\r
+ * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against\r
+ * @return bool\r
+ */\r
+ public function isOld($key, $config)\r
+ {\r
+ if (substr_count($key, ',') < 2) {\r
+ return true;\r
+ }\r
+ list($version, $hash, $revision) = explode(',', $key, 3);\r
+ $compare = version_compare($version, $config->version);\r
+ // version mismatch, is always old\r
+ if ($compare != 0) {\r
+ return true;\r
+ }\r
+ // versions match, ids match, check revision number\r
+ if ($hash == $config->getBatchSerial($this->type) &&\r
+ $revision < $config->get($this->type . '.DefinitionRev')) {\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Checks if a definition's type jives with the cache's type\r
+ * @note Throws an error on failure\r
+ * @param HTMLPurifier_Definition $def Definition object to check\r
+ * @return bool true if good, false if not\r
+ */\r
+ public function checkDefType($def)\r
+ {\r
+ if ($def->type !== $this->type) {\r
+ trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Adds a definition object to the cache\r
+ * @param HTMLPurifier_Definition $def\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function add($def, $config);\r
+\r
+ /**\r
+ * Unconditionally saves a definition object to the cache\r
+ * @param HTMLPurifier_Definition $def\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function set($def, $config);\r
+\r
+ /**\r
+ * Replace an object in the cache\r
+ * @param HTMLPurifier_Definition $def\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function replace($def, $config);\r
+\r
+ /**\r
+ * Retrieves a definition object from the cache\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function get($config);\r
+\r
+ /**\r
+ * Removes a definition object to the cache\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function remove($config);\r
+\r
+ /**\r
+ * Clears all objects from cache\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function flush($config);\r
+\r
+ /**\r
+ * Clears all expired (older version or revision) objects from cache\r
+ * @note Be carefuly implementing this method as flush. Flush must\r
+ * not interfere with other Definition types, and cleanup()\r
+ * should not be repeatedly called by userland code.\r
+ * @param HTMLPurifier_Config $config\r
+ */\r
+ abstract public function cleanup($config);\r
+}\r
+\r
+// vim: et sw=4 sts=4\r