| 1 | <?php |
|---|
| 2 | |
|---|
| 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 |
|---|
| 10 | */ |
|---|
| 11 | abstract class HTMLPurifier_DefinitionCache |
|---|
| 12 | { |
|---|
| 13 | |
|---|
| 14 | public $type; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * @param $name Type of definition objects this instance of the |
|---|
| 18 | * cache will handle. |
|---|
| 19 | */ |
|---|
| 20 | public function __construct($type) { |
|---|
| 21 | $this->type = $type; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Generates a unique identifier for a particular configuration |
|---|
| 26 | * @param Instance of HTMLPurifier_Config |
|---|
| 27 | */ |
|---|
| 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'); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 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 |
|---|
| 39 | */ |
|---|
| 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; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 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 |
|---|
| 59 | */ |
|---|
| 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; |
|---|
| 64 | } |
|---|
| 65 | return true; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Adds a definition object to the cache |
|---|
| 70 | */ |
|---|
| 71 | abstract public function add($def, $config); |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Unconditionally saves a definition object to the cache |
|---|
| 75 | */ |
|---|
| 76 | abstract public function set($def, $config); |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Replace an object in the cache |
|---|
| 80 | */ |
|---|
| 81 | abstract public function replace($def, $config); |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Retrieves a definition object from the cache |
|---|
| 85 | */ |
|---|
| 86 | abstract public function get($config); |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Removes a definition object to the cache |
|---|
| 90 | */ |
|---|
| 91 | abstract public function remove($config); |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * Clears all objects from cache |
|---|
| 95 | */ |
|---|
| 96 | abstract public function flush($config); |
|---|
| 97 | |
|---|
| 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. |
|---|
| 103 | */ |
|---|
| 104 | abstract public function cleanup($config); |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|