1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 * DokuWiki Plugin notification (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Szymon Olewniczak <it@rid.pl>
12 */
13
14class action_plugin_notification_cache extends ActionPlugin
15{
16    /**
17     * Registers a callback function for a given event
18     *
19     * @param EventHandler $controller DokuWiki's event controller object
20     *
21     * @return void
22     */
23    public function register(EventHandler $controller)
24    {
25        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handleParserCacheUse');
26    }
27
28    /**
29     *
30     * @param Event $event event object by reference
31     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
32     *                           handler was registered]
33     *
34     * @return void
35     */
36    public function handleParserCacheUse(Event $event, $param)
37    {
38        /** @var cache_renderer $cache */
39        $cache = $event->data;
40
41        if (!$cache->page) return;
42        //purge only xhtml cache
43        if ($cache->mode != 'xhtml') return;
44
45        //Check if it is plugins
46        $notification = p_get_metadata($cache->page, 'plugin notification');
47        if (!$notification) return;
48
49        if ($notification['dynamic user']) {
50            $cache->_nocache = true;
51            return;
52        }
53
54        $data = [
55            'plugins' => $notification['plugins'],
56            'dependencies' => [],
57            '_nocache' => false
58        ];
59        Event::createAndTrigger('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', $data);
60
61        //add a media directories to dependencies
62        $cache->depends['files'] = array_merge($cache->depends['files'], $data['dependencies']);
63        $cache->_nocache = $data['_nocache'];
64    }
65}
66