Add a little bit of documentation about contexts for URIFilters.
[htmlpurifier.git] / library / HTMLPurifier / DefinitionCache.php
blobc6e1e388c6513251112fcb4d911d14983298f79b
1 <?php
3 /**
4 * Abstract class representing Definition cache managers that implements
5 * useful common methods and is a factory.
6 * @todo Create a separate maintenance file advanced users can use to
7 * cache their custom HTMLDefinition, which can be loaded
8 * via a configuration directive
9 * @todo Implement memcached
11 abstract class HTMLPurifier_DefinitionCache
14 public $type;
16 /**
17 * @param $name Type of definition objects this instance of the
18 * cache will handle.
20 public function __construct($type) {
21 $this->type = $type;
24 /**
25 * Generates a unique identifier for a particular configuration
26 * @param Instance of HTMLPurifier_Config
28 public function generateKey($config) {
29 return $config->version . ',' . // possibly replace with function calls
30 $config->getBatchSerial($this->type) . ',' .
31 $config->get($this->type . '.DefinitionRev');
34 /**
35 * Tests whether or not a key is old with respect to the configuration's
36 * version and revision number.
37 * @param $key Key to test
38 * @param $config Instance of HTMLPurifier_Config to test against
40 public function isOld($key, $config) {
41 if (substr_count($key, ',') < 2) return true;
42 list($version, $hash, $revision) = explode(',', $key, 3);
43 $compare = version_compare($version, $config->version);
44 // version mismatch, is always old
45 if ($compare != 0) return true;
46 // versions match, ids match, check revision number
47 if (
48 $hash == $config->getBatchSerial($this->type) &&
49 $revision < $config->get($this->type . '.DefinitionRev')
50 ) return true;
51 return false;
54 /**
55 * Checks if a definition's type jives with the cache's type
56 * @note Throws an error on failure
57 * @param $def Definition object to check
58 * @return Boolean true if good, false if not
60 public function checkDefType($def) {
61 if ($def->type !== $this->type) {
62 trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
63 return false;
65 return true;
68 /**
69 * Adds a definition object to the cache
71 abstract public function add($def, $config);
73 /**
74 * Unconditionally saves a definition object to the cache
76 abstract public function set($def, $config);
78 /**
79 * Replace an object in the cache
81 abstract public function replace($def, $config);
83 /**
84 * Retrieves a definition object from the cache
86 abstract public function get($config);
88 /**
89 * Removes a definition object to the cache
91 abstract public function remove($config);
93 /**
94 * Clears all objects from cache
96 abstract public function flush($config);
98 /**
99 * Clears all expired (older version or revision) objects from cache
100 * @note Be carefuly implementing this method as flush. Flush must
101 * not interfere with other Definition types, and cleanup()
102 * should not be repeatedly called by userland code.
104 abstract public function cleanup($config);
108 // vim: et sw=4 sts=4