xref: /dokuwiki/inc/Cache/CacheParser.php (revision 884caed926ca0aa0af6ce3f34ae3aa7317a3361a)
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        global $INPUT;
27
28        if ($id) {
29            $this->page = $id;
30        }
31        $this->file = $file;
32        $this->mode = $mode;
33
34        $this->setEvent('PARSER_CACHE_USE');
35        parent::__construct(
36            $file . $INPUT->server->str('HTTP_HOST') . $INPUT->server->str('SERVER_PORT') . ($syntax ?? ''),
37            '.' . $mode
38        );
39    }
40
41    /**
42     * method contains cache use decision logic
43     *
44     * @return bool see useCache()
45     */
46    public function makeDefaultCacheDecision()
47    {
48        if (!file_exists($this->file)) {
49            // source doesn't exist
50            return false;
51        }
52        return parent::makeDefaultCacheDecision();
53    }
54
55    protected function addDependencies()
56    {
57        // parser cache file dependencies ...
58        $files = [
59            $this->file, // source
60            DOKU_INC . 'inc/Parsing/Parser.php', // parser
61            DOKU_INC . 'inc/Parsing/Handler.php', // handler
62        ];
63        $files = array_merge($files, getConfigFiles('main')); // wiki settings
64
65        $this->depends['files'] = empty($this->depends['files']) ?
66            $files :
67            array_merge($files, $this->depends['files']);
68        parent::addDependencies();
69    }
70}
71