PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle/lib/htmlpurifier/HTMLPurifier/DefinitionCache.php

https://bitbucket.org/geek745/moodle-db2
PHP | 131 lines | 57 code | 16 blank | 58 comment | 7 complexity | 7abf1693729adbb8a0264dac79192c54 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
  3. require_once 'HTMLPurifier/DefinitionCache/Null.php';
  4. require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
  5. require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
  6. require_once 'HTMLPurifier/DefinitionCache/Decorator/Cleanup.php';
  7. /**
  8. * Abstract class representing Definition cache managers that implements
  9. * useful common methods and is a factory.
  10. * @todo Get some sort of versioning variable so the library can easily
  11. * invalidate the cache with a new version
  12. * @todo Make the test runner cache aware and allow the user to easily
  13. * flush the cache
  14. * @todo Create a separate maintenance file advanced users can use to
  15. * cache their custom HTMLDefinition, which can be loaded
  16. * via a configuration directive
  17. * @todo Implement memcached
  18. */
  19. class HTMLPurifier_DefinitionCache
  20. {
  21. var $type;
  22. /**
  23. * @param $name Type of definition objects this instance of the
  24. * cache will handle.
  25. */
  26. function HTMLPurifier_DefinitionCache($type) {
  27. $this->type = $type;
  28. }
  29. /**
  30. * Generates a unique identifier for a particular configuration
  31. * @param Instance of HTMLPurifier_Config
  32. */
  33. function generateKey($config) {
  34. return $config->version . '-' . // possibly replace with function calls
  35. $config->getBatchSerial($this->type) . '-' .
  36. $config->get($this->type, 'DefinitionRev');
  37. }
  38. /**
  39. * Tests whether or not a key is old with respect to the configuration's
  40. * version and revision number.
  41. * @param $key Key to test
  42. * @param $config Instance of HTMLPurifier_Config to test against
  43. */
  44. function isOld($key, $config) {
  45. if (substr_count($key, '-') < 2) return true;
  46. list($version, $hash, $revision) = explode('-', $key, 3);
  47. $compare = version_compare($version, $config->version);
  48. // version mismatch, is always old
  49. if ($compare != 0) return true;
  50. // versions match, ids match, check revision number
  51. if (
  52. $hash == $config->getBatchSerial($this->type) &&
  53. $revision < $config->get($this->type, 'DefinitionRev')
  54. ) return true;
  55. return false;
  56. }
  57. /**
  58. * Checks if a definition's type jives with the cache's type
  59. * @note Throws an error on failure
  60. * @param $def Definition object to check
  61. * @return Boolean true if good, false if not
  62. */
  63. function checkDefType($def) {
  64. if ($def->type !== $this->type) {
  65. trigger_error("Cannot use definition of type {$def->type} in cache for {$this->type}");
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * Adds a definition object to the cache
  72. */
  73. function add($def, $config) {
  74. trigger_error('Cannot call abstract method', E_USER_ERROR);
  75. }
  76. /**
  77. * Unconditionally saves a definition object to the cache
  78. */
  79. function set($def, $config) {
  80. trigger_error('Cannot call abstract method', E_USER_ERROR);
  81. }
  82. /**
  83. * Replace an object in the cache
  84. */
  85. function replace($def, $config) {
  86. trigger_error('Cannot call abstract method', E_USER_ERROR);
  87. }
  88. /**
  89. * Retrieves a definition object from the cache
  90. */
  91. function get($config) {
  92. trigger_error('Cannot call abstract method', E_USER_ERROR);
  93. }
  94. /**
  95. * Removes a definition object to the cache
  96. */
  97. function remove($config) {
  98. trigger_error('Cannot call abstract method', E_USER_ERROR);
  99. }
  100. /**
  101. * Clears all objects from cache
  102. */
  103. function flush($config) {
  104. trigger_error('Cannot call abstract method', E_USER_ERROR);
  105. }
  106. /**
  107. * Clears all expired (older version or revision) objects from cache
  108. * @note Be carefuly implementing this method as flush. Flush must
  109. * not interfere with other Definition types, and cleanup()
  110. * should not be repeatedly called by userland code.
  111. */
  112. function cleanup($config) {
  113. trigger_error('Cannot call abstract method', E_USER_ERROR);
  114. }
  115. }