3 require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
4 require_once 'HTMLPurifier/DefinitionCache/Null.php';
6 require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
7 require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
8 require_once 'HTMLPurifier/DefinitionCache/Decorator/Cleanup.php';
11 * Abstract class representing Definition cache managers that implements
12 * useful common methods and is a factory.
13 * @todo Get some sort of versioning variable so the library can easily
14 * invalidate the cache with a new version
15 * @todo Make the test runner cache aware and allow the user to easily
17 * @todo Create a separate maintenance file advanced users can use to
18 * cache their custom HTMLDefinition, which can be loaded
19 * via a configuration directive
20 * @todo Implement memcached
22 class HTMLPurifier_DefinitionCache
28 * @param $name Type of definition objects this instance of the
31 function HTMLPurifier_DefinitionCache($type) {
36 * Generates a unique identifier for a particular configuration
37 * @param Instance of HTMLPurifier_Config
39 function generateKey($config) {
40 return $config->version
. '-' . // possibly replace with function calls
41 $config->getBatchSerial($this->type
) . '-' .
42 $config->get($this->type
, 'DefinitionRev');
46 * Tests whether or not a key is old with respect to the configuration's
47 * version and revision number.
48 * @param $key Key to test
49 * @param $config Instance of HTMLPurifier_Config to test against
51 function isOld($key, $config) {
52 if (substr_count($key, '-') < 2) return true;
53 list($version, $hash, $revision) = explode('-', $key, 3);
54 $compare = version_compare($version, $config->version
);
55 // version mismatch, is always old
56 if ($compare != 0) return true;
57 // versions match, ids match, check revision number
59 $hash == $config->getBatchSerial($this->type
) &&
60 $revision < $config->get($this->type
, 'DefinitionRev')
66 * Checks if a definition's type jives with the cache's type
67 * @note Throws an error on failure
68 * @param $def Definition object to check
69 * @return Boolean true if good, false if not
71 function checkDefType($def) {
72 if ($def->type
!== $this->type
) {
73 trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
80 * Adds a definition object to the cache
82 function add($def, $config) {
83 trigger_error('Cannot call abstract method', E_USER_ERROR
);
87 * Unconditionally saves a definition object to the cache
89 function set($def, $config) {
90 trigger_error('Cannot call abstract method', E_USER_ERROR
);
94 * Replace an object in the cache
96 function replace($def, $config) {
97 trigger_error('Cannot call abstract method', E_USER_ERROR
);
101 * Retrieves a definition object from the cache
103 function get($config) {
104 trigger_error('Cannot call abstract method', E_USER_ERROR
);
108 * Removes a definition object to the cache
110 function remove($config) {
111 trigger_error('Cannot call abstract method', E_USER_ERROR
);
115 * Clears all objects from cache
117 function flush($config) {
118 trigger_error('Cannot call abstract method', E_USER_ERROR
);
122 * Clears all expired (older version or revision) objects from cache
124 function cleanup($config) {
125 trigger_error('Cannot call abstract method', E_USER_ERROR
);