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