xref: /dokuwiki/inc/Cache/CacheParser.php (revision 6a8e48eda246f872402bf5b85763f276cd4c319d)
1<?php
2
3namespace dokuwiki\Cache;
4
5/**
6 * Parser caching
7 */
8class CacheParser extends Cache
9{
10    public $file = '';       // source file for cache
11    public $mode = '';       // input mode (represents the processing the input file will undergo)
12    public $page = '';
13
14    /**
15     *
16     * @param string $id page id
17     * @param string $file source file for cache
18     * @param string $mode input mode
19     * @param string|null $syntax syntax flavour the file is parsed under;
20     *     when non-null it enters the cache key so the same file rendered
21     *     under two syntaxes in one request does not collide. null leaves
22     *     the key unchanged.
23     */
24    public function __construct($id, $file, $mode, $syntax = null)
25    {
26        if ($id) {
27            $this->page = $id;
28        }
29        $this->file = $file;
30        $this->mode = $mode;
31
32        $this->setEvent('PARSER_CACHE_USE');
33        parent::__construct(
34            $file . ($syntax ?? '') . $this->getEnvironmentKey(),
35            '.' . $mode
36        );
37    }
38
39    /**
40     * Environment-dependent fragment to append to the cache key
41     *
42     * The returned fragment is appended to the cache key in the constructor. Child classes can override
43     * this to include environment-dependent information like DOKU_BASE.
44     *
45     * @return string
46     */
47    protected function getEnvironmentKey()
48    {
49        return '';
50    }
51
52    /**
53     * method contains cache use decision logic
54     *
55     * @return bool see useCache()
56     */
57    public function makeDefaultCacheDecision()
58    {
59        if (!file_exists($this->file)) {
60            // source doesn't exist
61            return false;
62        }
63        return parent::makeDefaultCacheDecision();
64    }
65
66    protected function addDependencies()
67    {
68        // parser cache file dependencies ...
69        $files = [
70            $this->file, // source
71            DOKU_INC . 'inc/Parsing/Parser.php', // parser
72            DOKU_INC . 'inc/Parsing/Handler.php', // handler
73        ];
74        $files = array_merge($files, getConfigFiles('main')); // wiki settings
75
76        $this->depends['files'] = empty($this->depends['files']) ?
77            $files :
78            array_merge($files, $this->depends['files']);
79        parent::addDependencies();
80    }
81}
82