0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00
htmlpurifier/library/HTMLPurifier/DefinitionCache.php
Edward Z. Yang d1187ed331 [1.7.0] Add versioning to serializer cache
- Make some AttrDef member-variables lazy-loading to save serialization space, clean up others
- Refactor get*Definition() methods

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1116 48356398-32a2-884e-a903-53898d9a118a
2007-05-29 18:19:42 +00:00

130 lines
3.9 KiB
PHP

<?php
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
/**
* Abstract class representing Definition cache managers that implements
* useful common methods and is a factory.
* @note The configuration object is transformed into the key used by the cache
* @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) {
$version = $config->version;
$revision = $config->revision;
return $version . '-' . $revision . '-' . md5(serialize($config->getBatch($this->type)));
}
/**
* 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;
if ($compare == 0 && $revision >= $config->revision) return false;
return true;
}
/**
* Factory method that creates a cache object based on configuration
* @param $name Name of definitions handled by cache
* @param $config Instance of HTMLPurifier_Config
*/
function create($name, $config) {
// only one implementation as for right now, $config will
// be used to determine implementation
return new HTMLPurifier_DefinitionCache_Serializer($name);
}
/**
* 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;
}
/**
* 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);
}
/**
* Replace an object in the cache
*/
function replace($def, $config) {
trigger_error('Cannot call abstract method', E_USER_ERROR);
}
/**
* 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);
}
/**
* Clears all objects from cache
*/
function flush() {
trigger_error('Cannot call abstract method', E_USER_ERROR);
}
/**
* Clears all expired (older version or revision) objects from cache
*/
function cleanup($config) {
trigger_error('Cannot call abstract method', E_USER_ERROR);
}
}
?>