1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 *
9 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
10 */
11class action_plugin_pagequery extends ActionPlugin
12{
13    public function register(EventHandler $controller)
14    {
15        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'purgecache');
16    }
17
18    /**
19     * Check for pages changes and eventually purge cache.
20     *
21     * @param Event $event
22     * @param mixed      $param not defined
23     * @author Samuele Tognini <samuele@samuele.netsons.org>
24     */
25    public function purgecache(Event $event, $param)
26    {
27        global $ID;
28        global $conf;
29        /** @var cache_parser $cache */
30        $cache = $event->data;
31
32        if (!isset($cache->page)) {
33            return;
34        }
35        //purge only xhtml cache
36        if ($cache->mode !== "xhtml") {
37            return;
38        }
39        //Check if it is an pagequery page
40        if (!p_get_metadata($ID, 'pagequery')) {
41            return;
42        }
43        $aclcache = $this->getConf('aclcache');
44        if ($conf['useacl']) {
45            $newkey = false;
46            if ($aclcache === 'user') {
47                //Cache per user
48                if ($_SERVER['REMOTE_USER']) {
49                    $newkey = $_SERVER['REMOTE_USER'];
50                }
51            } elseif ($aclcache === 'groups') {
52                //Cache per groups
53                global $INFO;
54                if ($INFO['userinfo']['grps']) {
55                    $newkey = implode('#', $INFO['userinfo']['grps']);
56                }
57            }
58            if ($newkey) {
59                $cache->key   .= "#" . $newkey;
60                $cache->cache = getCacheName($cache->key, $cache->ext);
61            }
62        }
63        //Check if a page is more recent than purgefile.
64        if (@filemtime($cache->cache) < @filemtime($conf['cachedir'] . '/purgefile')) {
65            $event->preventDefault();
66            $event->stopPropagation();
67            $event->result = false;
68        }
69    }
70}
71