xref: /plugin/dw2pdf/src/Cache.php (revision 5340eaffbcc1177667ac835e4e719d9eb8959315)
1<?php
2
3namespace dokuwiki\plugin\dw2pdf\src;
4
5class Cache extends \dokuwiki\Cache\Cache
6{
7    protected AbstractCollector $collector;
8
9    /** @inheritdoc */
10    public function __construct(Config $config, AbstractCollector $collector)
11    {
12        $this->collector = $collector;
13
14        $pages = $collector->getPages();
15        sort($pages);
16        $key = implode(':', [
17            implode(',', $pages),
18            $config->getCacheKey(),
19            $collector->getTitle(),
20        ]);
21
22        parent::__construct($key, '.dw2.pdf');
23
24        $this->addDependencies();
25    }
26
27    /**
28     * When this was a cache for a specific revision, remove it on destruction
29     */
30    public function __destruct()
31    {
32        if (!$this->collector->getRev()) return;
33        $this->removeCache();
34    }
35
36    /** @inheritdoc */
37    public function useCache($depends = [])
38    {
39        // when a specific revision is requested, do not use the cache
40        if ($this->collector->getRev()) {
41            return false;
42        }
43
44        return parent::useCache($depends);
45    }
46
47    /**
48     * Note: we do not set up any dependencies to the plugin source code itself. On normal installation,
49     * the config files will be touched which will invalidate the cache. During development, the developer
50     * should manually purge the cache when changing the plugin code.
51     * @inheritdoc
52     */
53    protected function addDependencies()
54    {
55        parent::addDependencies();
56
57        // images and included pages
58        $dependencies = [];
59        foreach ($this->collector->getPages() as $pageid) {
60            $relations = p_get_metadata($pageid, 'relation');
61
62            if (is_array($relations)) {
63                if (array_key_exists('media', $relations) && is_array($relations['media'])) {
64                    foreach ($relations['media'] as $mediaid => $exists) {
65                        if ($exists) {
66                            $dependencies[] = mediaFN($mediaid);
67                        }
68                    }
69                }
70
71                if (array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
72                    foreach ($relations['haspart'] as $part_pageid => $exists) {
73                        if ($exists) {
74                            $dependencies[] = wikiFN($part_pageid);
75                        }
76                    }
77                }
78            }
79
80            $dependencies[] = metaFN($pageid, '.meta');
81        }
82
83        // set up the dependencies
84        $this->depends['files'] = array_merge(
85            $dependencies,
86            $this->collector->getFiles(),
87            getConfigFiles('main')
88        );
89    }
90}
91