DefinitionCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  13. * @type string
  14. */
  15. public $type;
  16. /**
  17. * @param string $type Type of definition objects this instance of the
  18. * cache will handle.
  19. */
  20. public function __construct($type)
  21. {
  22. $this->type = $type;
  23. }
  24. /**
  25. * Generates a unique identifier for a particular configuration
  26. * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config
  27. * @return string
  28. */
  29. public function generateKey($config)
  30. {
  31. return $config->version . ',' . // possibly replace with function calls
  32. $config->getBatchSerial($this->type) . ',' .
  33. $config->get($this->type . '.DefinitionRev');
  34. }
  35. /**
  36. * Tests whether or not a key is old with respect to the configuration's
  37. * version and revision number.
  38. * @param string $key Key to test
  39. * @param HTMLPurifier_Config $config Instance of HTMLPurifier_Config to test against
  40. * @return bool
  41. */
  42. public function isOld($key, $config)
  43. {
  44. if (substr_count($key, ',') < 2) {
  45. return true;
  46. }
  47. list($version, $hash, $revision) = explode(',', $key, 3);
  48. $compare = version_compare($version, $config->version);
  49. // version mismatch, is always old
  50. if ($compare != 0) {
  51. return true;
  52. }
  53. // versions match, ids match, check revision number
  54. if ($hash == $config->getBatchSerial($this->type) &&
  55. $revision < $config->get($this->type . '.DefinitionRev')) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. /**
  61. * Checks if a definition's type jives with the cache's type
  62. * @note Throws an error on failure
  63. * @param HTMLPurifier_Definition $def Definition object to check
  64. * @return bool true if good, false if not
  65. */
  66. public function checkDefType($def)
  67. {
  68. if ($def->type !== $this->type) {
  69. trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * Adds a definition object to the cache
  76. * @param HTMLPurifier_Definition $def
  77. * @param HTMLPurifier_Config $config
  78. */
  79. abstract public function add($def, $config);
  80. /**
  81. * Unconditionally saves a definition object to the cache
  82. * @param HTMLPurifier_Definition $def
  83. * @param HTMLPurifier_Config $config
  84. */
  85. abstract public function set($def, $config);
  86. /**
  87. * Replace an object in the cache
  88. * @param HTMLPurifier_Definition $def
  89. * @param HTMLPurifier_Config $config
  90. */
  91. abstract public function replace($def, $config);
  92. /**
  93. * Retrieves a definition object from the cache
  94. * @param HTMLPurifier_Config $config
  95. */
  96. abstract public function get($config);
  97. /**
  98. * Removes a definition object to the cache
  99. * @param HTMLPurifier_Config $config
  100. */
  101. abstract public function remove($config);
  102. /**
  103. * Clears all objects from cache
  104. * @param HTMLPurifier_Config $config
  105. */
  106. abstract public function flush($config);
  107. /**
  108. * Clears all expired (older version or revision) objects from cache
  109. * @note Be careful implementing this method as flush. Flush must
  110. * not interfere with other Definition types, and cleanup()
  111. * should not be repeatedly called by userland code.
  112. * @param HTMLPurifier_Config $config
  113. */
  114. abstract public function cleanup($config);
  115. }
  116. // vim: et sw=4 sts=4