Don't make it always fail.
[htmlpurifier-drupal.git] / HTMLPurifier_DefinitionCache_Drupal.php
bloba219cd2c1fc1de96bb94acad3b420b242bab1b71
1 <?php
3 require_once 'HTMLPurifier/DefinitionCache.php';
5 /**
6 * Cache handler that stores all data in drupals builtin cache
7 */
8 class HTMLPurifier_DefinitionCache_Drupal extends HTMLPurifier_DefinitionCache
10 /**
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)) {
18 // already cached
19 return false;
21 $this->storeInDrupalCache($def, $key);
22 return true;
25 /**
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);
33 return true;
36 /**
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
45 return false;
48 $this->storeInDrupalCache($def, $key);
49 return true;
52 /**
53 * Retrieve an object from the cache
55 function get($config) {
56 $key = $this->generateKey($config);
57 return $this->fetchFromDrupalCache($key);
60 /**
61 * Delete an object from the cache
63 function remove($config) {
64 $key = $this->generateKey($config);
65 cache_clear_all("htmlpurifier:$key", 'cache');
66 return true;
69 function flush($config) {
70 cache_clear_all("htmlpurifier:*", 'cache', true);
71 return 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);
87 return false;
90 function storeInDrupalCache($def, $key) {
91 cache_set("htmlpurifier:$key", serialize($def), 'cache', CACHE_PERMANENT);