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 1 : {
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 1 : $this->type = $type;
22 1 : }
23 :
24 : /**
25 : * Generates a unique identifier for a particular configuration
26 : * @param Instance of HTMLPurifier_Config
27 : */
28 : public function generateKey($config) {
29 2 : return $config->version . ',' . // possibly replace with function calls
30 2 : $config->getBatchSerial($this->type) . ',' .
31 2 : $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 0 : if (substr_count($key, ',') < 2) return true;
42 0 : list($version, $hash, $revision) = explode(',', $key, 3);
43 0 : $compare = version_compare($version, $config->version);
44 : // version mismatch, is always old
45 0 : if ($compare != 0) return true;
46 : // versions match, ids match, check revision number
47 : if (
48 0 : $hash == $config->getBatchSerial($this->type) &&
49 0 : $revision < $config->get($this->type, 'DefinitionRev')
50 0 : ) return true;
51 0 : 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 0 : if ($def->type !== $this->type) {
62 0 : trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
63 0 : return false;
64 : }
65 0 : 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 :
|