xref: /dokuwiki/inc/Cache/CacheRenderer.php (revision 7d34963b3e75ea04c63ec066a6b7a692e123cb53)
10db5771eSMichael Große<?php
20db5771eSMichael Große
30db5771eSMichael Großenamespace dokuwiki\Cache;
40db5771eSMichael Große
50db5771eSMichael Große/**
60db5771eSMichael Große * Caching of data of renderer
70db5771eSMichael Große */
80db5771eSMichael Großeclass CacheRenderer extends CacheParser
90db5771eSMichael Große{
100db5771eSMichael Große
110db5771eSMichael Große    /**
120db5771eSMichael Große     * method contains cache use decision logic
130db5771eSMichael Große     *
140db5771eSMichael Große     * @return bool               see useCache()
150db5771eSMichael Große     */
1672c2bae8SMichael Große    public function makeDefaultCacheDecision()
170db5771eSMichael Große    {
180db5771eSMichael Große        global $conf;
190db5771eSMichael Große
2072c2bae8SMichael Große        if (!parent::makeDefaultCacheDecision()) {
210db5771eSMichael Große            return false;
220db5771eSMichael Große        }
230db5771eSMichael Große
240db5771eSMichael Große        if (!isset($this->page)) {
250db5771eSMichael Große            return true;
260db5771eSMichael Große        }
270db5771eSMichael Große
280db5771eSMichael Große        // meta cache older than file it depends on?
29b4b0b31bSMichael Große        if ($this->_time < @filemtime(metaFN($this->page, '.meta'))) {
300db5771eSMichael Große            return false;
310db5771eSMichael Große        }
320db5771eSMichael Große
330db5771eSMichael Große        // check current link existence is consistent with cache version
340db5771eSMichael Große        // first check the purgefile
350db5771eSMichael Große        // - if the cache is more recent than the purgefile we know no links can have been updated
36b4b0b31bSMichael Große        if ($this->_time >= @filemtime($conf['cachedir'] . '/purgefile')) {
370db5771eSMichael Große            return true;
380db5771eSMichael Große        }
390db5771eSMichael Große
400db5771eSMichael Große        // for wiki pages, check metadata dependencies
410db5771eSMichael Große        $metadata = p_get_metadata($this->page);
420db5771eSMichael Große
43*7d34963bSAndreas Gohr        if (
44*7d34963bSAndreas Gohr            !isset($metadata['relation']['references']) ||
45*7d34963bSAndreas Gohr            empty($metadata['relation']['references'])
46*7d34963bSAndreas Gohr        ) {
470db5771eSMichael Große            return true;
480db5771eSMichael Große        }
490db5771eSMichael Große
500db5771eSMichael Große        foreach ($metadata['relation']['references'] as $id => $exists) {
510db5771eSMichael Große            if ($exists != page_exists($id, '', false)) {
520db5771eSMichael Große                return false;
530db5771eSMichael Große            }
540db5771eSMichael Große        }
550db5771eSMichael Große
560db5771eSMichael Große        return true;
570db5771eSMichael Große    }
580db5771eSMichael Große
5942c00b45SMichael Große    protected function addDependencies()
600db5771eSMichael Große    {
610db5771eSMichael Große        global $conf;
620db5771eSMichael Große
630db5771eSMichael Große        // default renderer cache file 'age' is dependent on 'cachetime' setting, two special values:
640db5771eSMichael Große        //    -1 : do not cache (should not be overridden)
650db5771eSMichael Große        //    0  : cache never expires (can be overridden) - no need to set depends['age']
660db5771eSMichael Große        if ($conf['cachetime'] == -1) {
67b4b0b31bSMichael Große            $this->_nocache = true;
680db5771eSMichael Große            return;
690db5771eSMichael Große        } elseif ($conf['cachetime'] > 0) {
700db5771eSMichael Große            $this->depends['age'] = isset($this->depends['age']) ?
710db5771eSMichael Große                min($this->depends['age'], $conf['cachetime']) : $conf['cachetime'];
720db5771eSMichael Große        }
730db5771eSMichael Große
740db5771eSMichael Große        // renderer cache file dependencies ...
75a95427a5SAndreas Gohr        $files = [DOKU_INC . 'inc/parser/' . $this->mode . '.php'];
760db5771eSMichael Große
770db5771eSMichael Große        // page implies metadata and possibly some other dependencies
780db5771eSMichael Große        if (isset($this->page)) {
790db5771eSMichael Große            // for xhtml this will render the metadata if needed
800db5771eSMichael Große            $valid = p_get_metadata($this->page, 'date valid');
810db5771eSMichael Große            if (!empty($valid['age'])) {
820db5771eSMichael Große                $this->depends['age'] = isset($this->depends['age']) ?
830db5771eSMichael Große                    min($this->depends['age'], $valid['age']) : $valid['age'];
840db5771eSMichael Große            }
850db5771eSMichael Große        }
860db5771eSMichael Große
87a95427a5SAndreas Gohr        $this->depends['files'] = empty($this->depends['files']) ?
88a95427a5SAndreas Gohr            $files :
89a95427a5SAndreas Gohr            array_merge($files, $this->depends['files']);
900db5771eSMichael Große
9142c00b45SMichael Große        parent::addDependencies();
920db5771eSMichael Große    }
930db5771eSMichael Große}
94