DefinitionCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Abstract class representing Definition cache managers that implements
  4. * useful common methods and is a factory.
  5. * @todo Create a separate maintenance file advanced users can use to
  6. * cache their custom HTMLDefinition, which can be loaded
  7. * via a configuration directive
  8. * @todo Implement memcached
  9. */
  10. abstract class HTMLPurifier_DefinitionCache
  11. {
  12. public $type;
  13. /**
  14. * @param $name Type of definition objects this instance of the
  15. * cache will handle.
  16. */
  17. public function __construct($type) {
  18. $this->type = $type;
  19. }
  20. /**
  21. * Generates a unique identifier for a particular configuration
  22. * @param Instance of HTMLPurifier_Config
  23. */
  24. public function generateKey($config) {
  25. return $config->version . ',' . // possibly replace with function calls
  26. $config->getBatchSerial($this->type) . ',' .
  27. $config->get($this->type . '.DefinitionRev');
  28. }
  29. /**
  30. * Tests whether or not a key is old with respect to the configuration's
  31. * version and revision number.
  32. * @param $key Key to test
  33. * @param $config Instance of HTMLPurifier_Config to test against
  34. */
  35. public function isOld($key, $config) {
  36. if (substr_count($key, ',') < 2) return true;
  37. list($version, $hash, $revision) = explode(',', $key, 3);
  38. $compare = version_compare($version, $config->version);
  39. // version mismatch, is always old
  40. if ($compare != 0) return true;
  41. // versions match, ids match, check revision number
  42. if (
  43. $hash == $config->getBatchSerial($this->type) &&
  44. $revision < $config->get($this->type . '.DefinitionRev')
  45. ) return true;
  46. return false;
  47. }
  48. /**
  49. * Checks if a definition's type jives with the cache's type
  50. * @note Throws an error on failure
  51. * @param $def Definition object to check
  52. * @return Boolean true if good, false if not
  53. */
  54. public function checkDefType($def) {
  55. if ($def->type !== $this->type) {
  56. trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * Adds a definition object to the cache
  63. */
  64. abstract public function add($def, $config);
  65. /**
  66. * Unconditionally saves a definition object to the cache
  67. */
  68. abstract public function set($def, $config);
  69. /**
  70. * Replace an object in the cache
  71. */
  72. abstract public function replace($def, $config);
  73. /**
  74. * Retrieves a definition object from the cache
  75. */
  76. abstract public function get($config);
  77. /**
  78. * Removes a definition object to the cache
  79. */
  80. abstract public function remove($config);
  81. /**
  82. * Clears all objects from cache
  83. */
  84. abstract public function flush($config);
  85. /**
  86. * Clears all expired (older version or revision) objects from cache
  87. * @note Be carefuly implementing this method as flush. Flush must
  88. * not interfere with other Definition types, and cleanup()
  89. * should not be repeatedly called by userland code.
  90. */
  91. abstract public function cleanup($config);
  92. }
  93. // vim: et sw=4 sts=4