]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * Abstract class representing Definition cache managers that implements\r | |
5 | * useful common methods and is a factory.\r | |
6 | * @todo Create a separate maintenance file advanced users can use to\r | |
7 | * cache their custom HTMLDefinition, which can be loaded\r | |
8 | * via a configuration directive\r | |
9 | * @todo Implement memcached\r | |
10 | */\r | |
11 | abstract class HTMLPurifier_DefinitionCache\r | |
12 | {\r | |
13 | /**\r | |
14 | * @type string\r | |
15 | */\r | |
16 | public $type;\r | |
17 | \r | |
18 | /**\r | |
19 | * @param string $type Type of definition objects this instance of the\r | |
20 | * cache will handle.\r | |
21 | */\r | |
22 | public function __construct($type)\r | |
23 | {\r | |
24 | $this->type = $type;\r | |
25 | }\r | |
26 | \r | |
27 | /**\r | |
28 | * Generates a unique identifier for a particular configuration\r | |
29 | * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config\r | |
30 | * @return string\r | |
31 | */\r | |
32 | public function generateKey($config)\r | |
33 | {\r | |
34 | return $config->version . ',' . // possibly replace with function calls\r | |
35 | $config->getBatchSerial($this->type) . ',' .\r | |
36 | $config->get($this->type . '.DefinitionRev');\r | |
37 | }\r | |
38 | \r | |
39 | /**\r | |
40 | * Tests whether or not a key is old with respect to the configuration's\r | |
41 | * version and revision number.\r | |
42 | * @param string $key Key to test\r | |
43 | * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against\r | |
44 | * @return bool\r | |
45 | */\r | |
46 | public function isOld($key, $config)\r | |
47 | {\r | |
48 | if (substr_count($key, ',') < 2) {\r | |
49 | return true;\r | |
50 | }\r | |
51 | list($version, $hash, $revision) = explode(',', $key, 3);\r | |
52 | $compare = version_compare($version, $config->version);\r | |
53 | // version mismatch, is always old\r | |
54 | if ($compare != 0) {\r | |
55 | return true;\r | |
56 | }\r | |
57 | // versions match, ids match, check revision number\r | |
58 | if ($hash == $config->getBatchSerial($this->type) &&\r | |
59 | $revision < $config->get($this->type . '.DefinitionRev')) {\r | |
60 | return true;\r | |
61 | }\r | |
62 | return false;\r | |
63 | }\r | |
64 | \r | |
65 | /**\r | |
66 | * Checks if a definition's type jives with the cache's type\r | |
67 | * @note Throws an error on failure\r | |
68 | * @param HTMLPurifier_Definition $def Definition object to check\r | |
69 | * @return bool true if good, false if not\r | |
70 | */\r | |
71 | public function checkDefType($def)\r | |
72 | {\r | |
73 | if ($def->type !== $this->type) {\r | |
74 | trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");\r | |
75 | return false;\r | |
76 | }\r | |
77 | return true;\r | |
78 | }\r | |
79 | \r | |
80 | /**\r | |
81 | * Adds a definition object to the cache\r | |
82 | * @param HTMLPurifier_Definition $def\r | |
83 | * @param HTMLPurifier_Config $config\r | |
84 | */\r | |
85 | abstract public function add($def, $config);\r | |
86 | \r | |
87 | /**\r | |
88 | * Unconditionally saves a definition object to the cache\r | |
89 | * @param HTMLPurifier_Definition $def\r | |
90 | * @param HTMLPurifier_Config $config\r | |
91 | */\r | |
92 | abstract public function set($def, $config);\r | |
93 | \r | |
94 | /**\r | |
95 | * Replace an object in the cache\r | |
96 | * @param HTMLPurifier_Definition $def\r | |
97 | * @param HTMLPurifier_Config $config\r | |
98 | */\r | |
99 | abstract public function replace($def, $config);\r | |
100 | \r | |
101 | /**\r | |
102 | * Retrieves a definition object from the cache\r | |
103 | * @param HTMLPurifier_Config $config\r | |
104 | */\r | |
105 | abstract public function get($config);\r | |
106 | \r | |
107 | /**\r | |
108 | * Removes a definition object to the cache\r | |
109 | * @param HTMLPurifier_Config $config\r | |
110 | */\r | |
111 | abstract public function remove($config);\r | |
112 | \r | |
113 | /**\r | |
114 | * Clears all objects from cache\r | |
115 | * @param HTMLPurifier_Config $config\r | |
116 | */\r | |
117 | abstract public function flush($config);\r | |
118 | \r | |
119 | /**\r | |
120 | * Clears all expired (older version or revision) objects from cache\r | |
121 | * @note Be carefuly implementing this method as flush. Flush must\r | |
122 | * not interfere with other Definition types, and cleanup()\r | |
123 | * should not be repeatedly called by userland code.\r | |
124 | * @param HTMLPurifier_Config $config\r | |
125 | */\r | |
126 | abstract public function cleanup($config);\r | |
127 | }\r | |
128 | \r | |
129 | // vim: et sw=4 sts=4\r |