3 require_once 'HTMLPurifier/DefinitionCache.php';
6 * Cache handler that stores all data in drupals builtin cache
8 class HTMLPurifier_DefinitionCache_Drupal
extends HTMLPurifier_DefinitionCache
11 * Add an object to the cache without overwriting
13 function add($def, $config) {
14 if (!$this->checkDefType($def)) return;
15 $key = $this->generateKey($config);
17 if ($this->fetchFromDrupalCache($key)) {
21 $this->storeInDrupalCache($def, $key);
26 * Unconditionally add an object to the cache, overwrites any existing object.
28 function set($def, $config) {
29 if (!$this->checkDefType($def)) return;
30 $key = $this->generateKey($config);
32 $this->storeInDrupalCache($def, $key);
37 * Replace an object that already exists in the cache.
39 function replace($def, $config) {
40 if (!$this->checkDefType($def)) return;
41 $key = $this->generateKey($config);
43 if (!$this->fetchFromDrupalCache($key)) {
44 // object does not exist in cache
48 $this->storeInDrupalCache($def, $key);
53 * Retrieve an object from the cache
55 function get($config) {
56 $key = $this->generateKey($config);
57 return $this->fetchFromDrupalCache($key);
61 * Delete an object from the cache
63 function remove($config) {
64 $key = $this->generateKey($config);
65 cache_clear_all("htmlpurifier:$key", 'cache');
69 function flush($config) {
70 cache_clear_all("htmlpurifier:*", 'cache', true);
74 function cleanup($config) {
75 $res = db_query("SELECT cid FROM {cache} WHERE cid LIKE '%s%%'", 'htmlpurifier:');
76 while ($row = db_fetch_object($res)) {
77 $key = substr($row->cid
, 13); // 13 == strlen('htmlpurifier:')
78 if ($this->isOld($key, $config)) {
79 cache_clear_all($row->cid
, 'cache');
84 function fetchFromDrupalCache($key) {
85 $cached = cache_get("htmlpurifier:$key");
86 if ($cached) return unserialize($cached->data
);
90 function storeInDrupalCache($def, $key) {
91 cache_set("htmlpurifier:$key", serialize($def), 'cache', CACHE_PERMANENT
);