2007-05-25 01:32:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class representing Definition cache managers that implements
|
|
|
|
* useful common methods and is a factory.
|
|
|
|
* @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
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract class HTMLPurifier_DefinitionCache
|
2007-05-25 01:32:29 +00:00
|
|
|
{
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @type string
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $type;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $type Type of definition objects this instance of the
|
2007-05-25 01:32:29 +00:00
|
|
|
* cache will handle.
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __construct($type)
|
|
|
|
{
|
2007-05-25 01:32:29 +00:00
|
|
|
$this->type = $type;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Generates a unique identifier for a particular configuration
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
|
|
|
|
* @return string
|
2007-05-25 01:32:29 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function generateKey($config)
|
|
|
|
{
|
2008-04-26 19:28:14 +00:00
|
|
|
return $config->version . ',' . // possibly replace with function calls
|
|
|
|
$config->getBatchSerial($this->type) . ',' .
|
2009-02-20 00:17:49 +00:00
|
|
|
$config->get($this->type . '.DefinitionRev');
|
2007-05-29 18:19:42 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
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.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $key Key to test
|
|
|
|
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against
|
|
|
|
* @return bool
|
2007-05-29 18:19:42 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function isOld($key, $config)
|
|
|
|
{
|
|
|
|
if (substr_count($key, ',') < 2) {
|
|
|
|
return true;
|
|
|
|
}
|
2008-04-26 19:28:14 +00:00
|
|
|
list($version, $hash, $revision) = explode(',', $key, 3);
|
2007-05-29 18:19:42 +00:00
|
|
|
$compare = version_compare($version, $config->version);
|
2007-06-23 14:05:09 +00:00
|
|
|
// version mismatch, is always old
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($compare != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-06-23 14:05:09 +00:00
|
|
|
// versions match, ids match, check revision number
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($hash == $config->getBatchSerial($this->type) &&
|
|
|
|
$revision < $config->get($this->type . '.DefinitionRev')) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-06-23 14:05:09 +00:00
|
|
|
return false;
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +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
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Definition $def Definition object to check
|
|
|
|
* @return bool true if good, false if not
|
2007-05-27 13:25:54 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function checkDefType($def)
|
|
|
|
{
|
2007-05-27 13:25:54 +00:00
|
|
|
if ($def->type !== $this->type) {
|
|
|
|
trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Adds a definition object to the cache
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Definition $def
|
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-25 01:32:29 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function add($def, $config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Unconditionally saves a definition object to the cache
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Definition $def
|
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-25 01:32:29 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function set($def, $config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
/**
|
|
|
|
* Replace an object in the cache
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Definition $def
|
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-27 13:25:54 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function replace($def, $config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Retrieves a definition object from the cache
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-25 01:32:29 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function get($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Removes a definition object to the cache
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-25 01:32:29 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function remove($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
/**
|
|
|
|
* Clears all objects from cache
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-27 13:25:54 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function flush($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
/**
|
|
|
|
* Clears all expired (older version or revision) objects from cache
|
2016-03-27 21:53:31 +00:00
|
|
|
* @note Be careful implementing this method as flush. Flush must
|
2007-12-09 03:14:34 +00:00
|
|
|
* not interfere with other Definition types, and cleanup()
|
|
|
|
* should not be repeatedly called by userland code.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param HTMLPurifier_Config $config
|
2007-05-29 18:19:42 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
abstract public function cleanup($config);
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|