File indexing completed on 2024-05-05 06:02:15

0001 <?php
0002 
0003 /**
0004  * Abstract class representing Definition cache managers that implements
0005  * useful common methods and is a factory.
0006  * @todo Create a separate maintenance file advanced users can use to
0007  *       cache their custom HTMLDefinition, which can be loaded
0008  *       via a configuration directive
0009  * @todo Implement memcached
0010  */
0011 abstract class HTMLPurifier_DefinitionCache
0012 {
0013     /**
0014      * @type string
0015      */
0016     public $type;
0017 
0018     /**
0019      * @param string $type Type of definition objects this instance of the
0020      *      cache will handle.
0021      */
0022     public function __construct($type)
0023     {
0024         $this->type = $type;
0025     }
0026 
0027     /**
0028      * Generates a unique identifier for a particular configuration
0029      * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
0030      * @return string
0031      */
0032     public function generateKey($config)
0033     {
0034         return $config->version . ',' . // possibly replace with function calls
0035                $config->getBatchSerial($this->type) . ',' .
0036                $config->get($this->type . '.DefinitionRev');
0037     }
0038 
0039     /**
0040      * Tests whether or not a key is old with respect to the configuration's
0041      * version and revision number.
0042      * @param string $key Key to test
0043      * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against
0044      * @return bool
0045      */
0046     public function isOld($key, $config)
0047     {
0048         if (substr_count($key, ',') < 2) {
0049             return true;
0050         }
0051         list($version, $hash, $revision) = explode(',', $key, 3);
0052         $compare = version_compare($version, $config->version);
0053         // version mismatch, is always old
0054         if ($compare != 0) {
0055             return true;
0056         }
0057         // versions match, ids match, check revision number
0058         if ($hash == $config->getBatchSerial($this->type) &&
0059             $revision < $config->get($this->type . '.DefinitionRev')) {
0060             return true;
0061         }
0062         return false;
0063     }
0064 
0065     /**
0066      * Checks if a definition's type jives with the cache's type
0067      * @note Throws an error on failure
0068      * @param HTMLPurifier_Definition $def Definition object to check
0069      * @return bool true if good, false if not
0070      */
0071     public function checkDefType($def)
0072     {
0073         if ($def->type !== $this->type) {
0074             trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
0075             return false;
0076         }
0077         return true;
0078     }
0079 
0080     /**
0081      * Adds a definition object to the cache
0082      * @param HTMLPurifier_Definition $def
0083      * @param HTMLPurifier_Config $config
0084      */
0085     abstract public function add($def, $config);
0086 
0087     /**
0088      * Unconditionally saves a definition object to the cache
0089      * @param HTMLPurifier_Definition $def
0090      * @param HTMLPurifier_Config $config
0091      */
0092     abstract public function set($def, $config);
0093 
0094     /**
0095      * Replace an object in the cache
0096      * @param HTMLPurifier_Definition $def
0097      * @param HTMLPurifier_Config $config
0098      */
0099     abstract public function replace($def, $config);
0100 
0101     /**
0102      * Retrieves a definition object from the cache
0103      * @param HTMLPurifier_Config $config
0104      */
0105     abstract public function get($config);
0106 
0107     /**
0108      * Removes a definition object to the cache
0109      * @param HTMLPurifier_Config $config
0110      */
0111     abstract public function remove($config);
0112 
0113     /**
0114      * Clears all objects from cache
0115      * @param HTMLPurifier_Config $config
0116      */
0117     abstract public function flush($config);
0118 
0119     /**
0120      * Clears all expired (older version or revision) objects from cache
0121      * @note Be careful implementing this method as flush. Flush must
0122      *       not interfere with other Definition types, and cleanup()
0123      *       should not be repeatedly called by userland code.
0124      * @param HTMLPurifier_Config $config
0125      */
0126     abstract public function cleanup($config);
0127 }
0128 
0129 // vim: et sw=4 sts=4