1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 * Changes Plugin: List the most recent changes of the wiki
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Andreas Gohr <andi@splitbrain.org>
12 * @author     Mykola Ostrovskyy <spambox03@mail.ru>
13 */
14
15/**
16 * Class action_plugin_changes
17 */
18class action_plugin_changes extends ActionPlugin
19{
20    /**
21     * Register callbacks
22     * @param EventHandler $controller
23     */
24    public function register(EventHandler $controller)
25    {
26        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'beforeParserCacheUse');
27    }
28
29    /**
30     * Handle PARSER_CACHE_USE:BEFORE event
31     * @param Event $event
32     */
33    public function beforeParserCacheUse($event)
34    {
35        global $ID;
36        $cache = $event->data;
37        if (isset($cache->mode) && ($cache->mode == 'xhtml')) {
38            $depends = p_get_metadata($ID, 'relation depends');
39            if (!empty($depends) && isset($depends['rendering'])) {
40                $this->addDependencies($cache, array_keys($depends['rendering']));
41            }
42        }
43    }
44
45    /**
46     * Add extra dependencies to the cache
47     */
48    protected function addDependencies($cache, $depends)
49    {
50        // Prevent "Warning: in_array() expects parameter 2 to be array, null given"
51        if (!is_array($cache->depends)) {
52            $cache->depends = [];
53        }
54        if (!array_key_exists('files', $cache->depends)) {
55            $cache->depends['files'] = [];
56        }
57
58        foreach ($depends as $file) {
59            if (!in_array($file, $cache->depends['files']) && @file_exists($file)) {
60                $cache->depends['files'][] = $file;
61            }
62        }
63    }
64}
65