1
0
Fork 0
friendica_2020-09-1_sharedH.../vendor/ezyang/htmlpurifier/library/HTMLPurifier/DefinitionCache.php

130 lines
3.8 KiB
PHP
Raw Normal View History

2010-09-09 05:14:17 +02: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
*/
abstract class HTMLPurifier_DefinitionCache
{
2016-02-09 11:06:17 +01:00
/**
* @type string
*/
2010-09-09 05:14:17 +02:00
public $type;
/**
2016-02-09 11:06:17 +01:00
* @param string $type Type of definition objects this instance of the
2010-09-09 05:14:17 +02:00
* cache will handle.
*/
2016-02-09 11:06:17 +01:00
public function __construct($type)
{
2010-09-09 05:14:17 +02:00
$this->type = $type;
}
/**
* Generates a unique identifier for a particular configuration
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
* @return string
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function generateKey($config)
{
2010-09-09 05:14:17 +02:00
return $config->version . ',' . // possibly replace with function calls
$config->getBatchSerial($this->type) . ',' .
$config->get($this->type . '.DefinitionRev');
}
/**
* Tests whether or not a key is old with respect to the configuration's
* version and revision number.
2016-02-09 11:06:17 +01:00
* @param string $key Key to test
* @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against
* @return bool
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function isOld($key, $config)
{
if (substr_count($key, ',') < 2) {
return true;
}
2010-09-09 05:14:17 +02:00
list($version, $hash, $revision) = explode(',', $key, 3);
$compare = version_compare($version, $config->version);
// version mismatch, is always old
2016-02-09 11:06:17 +01:00
if ($compare != 0) {
return true;
}
2010-09-09 05:14:17 +02:00
// versions match, ids match, check revision number
2016-02-09 11:06:17 +01:00
if ($hash == $config->getBatchSerial($this->type) &&
$revision < $config->get($this->type . '.DefinitionRev')) {
return true;
}
2010-09-09 05:14:17 +02:00
return false;
}
/**
* Checks if a definition's type jives with the cache's type
* @note Throws an error on failure
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Definition $def Definition object to check
* @return bool true if good, false if not
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function checkDefType($def)
{
2010-09-09 05:14:17 +02:00
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
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Definition $def
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function add($def, $config);
/**
* Unconditionally saves a definition object to the cache
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Definition $def
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function set($def, $config);
/**
* Replace an object in the cache
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Definition $def
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function replace($def, $config);
/**
* Retrieves a definition object from the cache
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function get($config);
/**
* Removes a definition object to the cache
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function remove($config);
/**
* Clears all objects from cache
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function flush($config);
/**
* Clears all expired (older version or revision) objects from cache
* @note Be carefuly implementing this method as flush. Flush must
* not interfere with other Definition types, and cleanup()
* should not be repeatedly called by userland code.
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
2010-09-09 05:14:17 +02:00
*/
abstract public function cleanup($config);
}
// vim: et sw=4 sts=4