xref: /plugin/dw2pdf/src/Cache.php (revision 5340eaffbcc1177667ac835e4e719d9eb8959315)
1293d84e6SAndreas Gohr<?php
2293d84e6SAndreas Gohr
3293d84e6SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf\src;
4293d84e6SAndreas Gohr
5293d84e6SAndreas Gohrclass Cache extends \dokuwiki\Cache\Cache
6293d84e6SAndreas Gohr{
7293d84e6SAndreas Gohr    protected AbstractCollector $collector;
8293d84e6SAndreas Gohr
9293d84e6SAndreas Gohr    /** @inheritdoc */
10293d84e6SAndreas Gohr    public function __construct(Config $config, AbstractCollector $collector)
11293d84e6SAndreas Gohr    {
12293d84e6SAndreas Gohr        $this->collector = $collector;
13293d84e6SAndreas Gohr
14f77f381aSAndreas Gohr        $pages = $collector->getPages();
15f77f381aSAndreas Gohr        sort($pages);
16*5340eaffSAndreas Gohr        $key = implode(':', [
17*5340eaffSAndreas Gohr            implode(',', $pages),
18293d84e6SAndreas Gohr            $config->getCacheKey(),
19293d84e6SAndreas Gohr            $collector->getTitle(),
20293d84e6SAndreas Gohr        ]);
21293d84e6SAndreas Gohr
22293d84e6SAndreas Gohr        parent::__construct($key, '.dw2.pdf');
23293d84e6SAndreas Gohr
24293d84e6SAndreas Gohr        $this->addDependencies();
25293d84e6SAndreas Gohr    }
26293d84e6SAndreas Gohr
27293d84e6SAndreas Gohr    /**
28293d84e6SAndreas Gohr     * When this was a cache for a specific revision, remove it on destruction
29293d84e6SAndreas Gohr     */
30293d84e6SAndreas Gohr    public function __destruct()
31293d84e6SAndreas Gohr    {
32293d84e6SAndreas Gohr        if (!$this->collector->getRev()) return;
33293d84e6SAndreas Gohr        $this->removeCache();
34293d84e6SAndreas Gohr    }
35293d84e6SAndreas Gohr
36293d84e6SAndreas Gohr    /** @inheritdoc */
37293d84e6SAndreas Gohr    public function useCache($depends = [])
38293d84e6SAndreas Gohr    {
39293d84e6SAndreas Gohr        // when a specific revision is requested, do not use the cache
40293d84e6SAndreas Gohr        if ($this->collector->getRev()) {
41293d84e6SAndreas Gohr            return false;
42293d84e6SAndreas Gohr        }
43293d84e6SAndreas Gohr
44293d84e6SAndreas Gohr        return parent::useCache($depends);
45293d84e6SAndreas Gohr    }
46293d84e6SAndreas Gohr
47293d84e6SAndreas Gohr    /**
48293d84e6SAndreas Gohr     * Note: we do not set up any dependencies to the plugin source code itself. On normal installation,
49293d84e6SAndreas Gohr     * the config files will be touched which will invalidate the cache. During development, the developer
50293d84e6SAndreas Gohr     * should manually purge the cache when changing the plugin code.
51293d84e6SAndreas Gohr     * @inheritdoc
52293d84e6SAndreas Gohr     */
53293d84e6SAndreas Gohr    protected function addDependencies()
54293d84e6SAndreas Gohr    {
55293d84e6SAndreas Gohr        parent::addDependencies();
56293d84e6SAndreas Gohr
57293d84e6SAndreas Gohr        // images and included pages
58293d84e6SAndreas Gohr        $dependencies = [];
59f77f381aSAndreas Gohr        foreach ($this->collector->getPages() as $pageid) {
60293d84e6SAndreas Gohr            $relations = p_get_metadata($pageid, 'relation');
61293d84e6SAndreas Gohr
62293d84e6SAndreas Gohr            if (is_array($relations)) {
63293d84e6SAndreas Gohr                if (array_key_exists('media', $relations) && is_array($relations['media'])) {
64293d84e6SAndreas Gohr                    foreach ($relations['media'] as $mediaid => $exists) {
65293d84e6SAndreas Gohr                        if ($exists) {
66293d84e6SAndreas Gohr                            $dependencies[] = mediaFN($mediaid);
67293d84e6SAndreas Gohr                        }
68293d84e6SAndreas Gohr                    }
69293d84e6SAndreas Gohr                }
70293d84e6SAndreas Gohr
71293d84e6SAndreas Gohr                if (array_key_exists('haspart', $relations) && is_array($relations['haspart'])) {
72293d84e6SAndreas Gohr                    foreach ($relations['haspart'] as $part_pageid => $exists) {
73293d84e6SAndreas Gohr                        if ($exists) {
74293d84e6SAndreas Gohr                            $dependencies[] = wikiFN($part_pageid);
75293d84e6SAndreas Gohr                        }
76293d84e6SAndreas Gohr                    }
77293d84e6SAndreas Gohr                }
78293d84e6SAndreas Gohr            }
79293d84e6SAndreas Gohr
80293d84e6SAndreas Gohr            $dependencies[] = metaFN($pageid, '.meta');
81293d84e6SAndreas Gohr        }
82293d84e6SAndreas Gohr
83293d84e6SAndreas Gohr        // set up the dependencies
84293d84e6SAndreas Gohr        $this->depends['files'] = array_merge(
85293d84e6SAndreas Gohr            $dependencies,
86293d84e6SAndreas Gohr            $this->collector->getFiles(),
87293d84e6SAndreas Gohr            getConfigFiles('main')
88293d84e6SAndreas Gohr        );
89293d84e6SAndreas Gohr    }
90293d84e6SAndreas Gohr}
91