CacheRenderer.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace dokuwiki\Cache;
  3. /**
  4. * Caching of data of renderer
  5. */
  6. class CacheRenderer extends CacheParser
  7. {
  8. /**
  9. * method contains cache use decision logic
  10. *
  11. * @return bool see useCache()
  12. */
  13. public function makeDefaultCacheDecision()
  14. {
  15. global $conf;
  16. if (!parent::makeDefaultCacheDecision()) {
  17. return false;
  18. }
  19. if (!isset($this->page)) {
  20. return true;
  21. }
  22. // meta cache older than file it depends on?
  23. if ($this->_time < @filemtime(metaFN($this->page, '.meta'))) {
  24. return false;
  25. }
  26. // check current link existence is consistent with cache version
  27. // first check the purgefile
  28. // - if the cache is more recent than the purgefile we know no links can have been updated
  29. if ($this->_time >= @filemtime($conf['cachedir'] . '/purgefile')) {
  30. return true;
  31. }
  32. // for wiki pages, check metadata dependencies
  33. $metadata = p_get_metadata($this->page);
  34. if (!isset($metadata['relation']['references']) ||
  35. empty($metadata['relation']['references'])) {
  36. return true;
  37. }
  38. foreach ($metadata['relation']['references'] as $id => $exists) {
  39. if ($exists != page_exists($id, '', false)) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. protected function addDependencies()
  46. {
  47. global $conf;
  48. // default renderer cache file 'age' is dependent on 'cachetime' setting, two special values:
  49. // -1 : do not cache (should not be overridden)
  50. // 0 : cache never expires (can be overridden) - no need to set depends['age']
  51. if ($conf['cachetime'] == -1) {
  52. $this->_nocache = true;
  53. return;
  54. } elseif ($conf['cachetime'] > 0) {
  55. $this->depends['age'] = isset($this->depends['age']) ?
  56. min($this->depends['age'], $conf['cachetime']) : $conf['cachetime'];
  57. }
  58. // renderer cache file dependencies ...
  59. $files = array(
  60. DOKU_INC . 'inc/parser/' . $this->mode . '.php', // ... the renderer
  61. );
  62. // page implies metadata and possibly some other dependencies
  63. if (isset($this->page)) {
  64. // for xhtml this will render the metadata if needed
  65. $valid = p_get_metadata($this->page, 'date valid');
  66. if (!empty($valid['age'])) {
  67. $this->depends['age'] = isset($this->depends['age']) ?
  68. min($this->depends['age'], $valid['age']) : $valid['age'];
  69. }
  70. }
  71. $this->depends['files'] = !empty($this->depends['files']) ?
  72. array_merge($files, $this->depends['files']) :
  73. $files;
  74. parent::addDependencies();
  75. }
  76. }