2007-05-25 01:32:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
|
2007-05-29 20:21:33 +00:00
|
|
|
require_once 'HTMLPurifier/DefinitionCache/Null.php';
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
|
2007-06-21 23:56:19 +00:00
|
|
|
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
|
|
|
|
require_once 'HTMLPurifier/DefinitionCache/Decorator/Cleanup.php';
|
2007-05-25 01:32:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class representing Definition cache managers that implements
|
|
|
|
* useful common methods and is a factory.
|
|
|
|
* @todo Get some sort of versioning variable so the library can easily
|
|
|
|
* invalidate the cache with a new version
|
|
|
|
* @todo Make the test runner cache aware and allow the user to easily
|
|
|
|
* flush the cache
|
|
|
|
* @todo Create a separate maintenance file advanced users can use to
|
|
|
|
* cache their custom HTMLDefinition, which can be loaded
|
|
|
|
* via a configuration directive
|
|
|
|
* @todo Implement memcached
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_DefinitionCache
|
|
|
|
{
|
|
|
|
|
|
|
|
var $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name Type of definition objects this instance of the
|
|
|
|
* cache will handle.
|
|
|
|
*/
|
|
|
|
function HTMLPurifier_DefinitionCache($type) {
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a unique identifier for a particular configuration
|
|
|
|
* @param Instance of HTMLPurifier_Config
|
|
|
|
*/
|
|
|
|
function generateKey($config) {
|
2007-05-29 20:21:33 +00:00
|
|
|
return $config->version . '-' . // possibly replace with function calls
|
2007-06-20 02:43:43 +00:00
|
|
|
$config->get($this->type, 'DefinitionRev') . '-' .
|
2007-05-29 20:21:33 +00:00
|
|
|
$config->getBatchSerial($this->type);
|
2007-05-29 18:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether or not a key is old with respect to the configuration's
|
|
|
|
* version and revision number.
|
|
|
|
* @param $key Key to test
|
|
|
|
* @param $config Instance of HTMLPurifier_Config to test against
|
|
|
|
*/
|
|
|
|
function isOld($key, $config) {
|
|
|
|
list($version, $revision, $hash) = explode('-', $key, 3);
|
|
|
|
$compare = version_compare($version, $config->version);
|
|
|
|
if ($compare > 0) return false;
|
2007-06-20 02:43:43 +00:00
|
|
|
if ($compare == 0 && $revision >= $config->get($this->type, 'DefinitionRev')) return false;
|
2007-05-29 18:19:42 +00:00
|
|
|
return true;
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
/**
|
|
|
|
* Checks if a definition's type jives with the cache's type
|
|
|
|
* @note Throws an error on failure
|
|
|
|
* @param $def Definition object to check
|
|
|
|
* @return Boolean true if good, false if not
|
|
|
|
*/
|
|
|
|
function checkDefType($def) {
|
|
|
|
if ($def->type !== $this->type) {
|
|
|
|
trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Adds a definition object to the cache
|
|
|
|
*/
|
|
|
|
function add($def, $config) {
|
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unconditionally saves a definition object to the cache
|
|
|
|
*/
|
|
|
|
function set($def, $config) {
|
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
/**
|
|
|
|
* Replace an object in the cache
|
|
|
|
*/
|
|
|
|
function replace($def, $config) {
|
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a definition object from the cache
|
|
|
|
*/
|
|
|
|
function get($config) {
|
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a definition object to the cache
|
|
|
|
*/
|
|
|
|
function remove($config) {
|
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
/**
|
|
|
|
* Clears all objects from cache
|
|
|
|
*/
|
2007-06-16 20:46:44 +00:00
|
|
|
function flush($config) {
|
2007-05-27 13:25:54 +00:00
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
/**
|
|
|
|
* Clears all expired (older version or revision) objects from cache
|
|
|
|
*/
|
|
|
|
function cleanup($config) {
|
|
|
|
trigger_error('Cannot call abstract method', E_USER_ERROR);
|
|
|
|
}
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|