1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\Cache\CacheParser;
7
8/**
9 * Double caching of pages containing struct aggregations:
10 * one for regular users, one for publishers/approvers
11 *
12 * @see action_plugin_struct_cache
13 */
14class action_plugin_structpublish_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     * @return void
21     */
22    public function register(EventHandler $controller)
23    {
24        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handleCacheAggregation');
25    }
26
27    /**
28     * For pages containing an aggregation, add structpublish flag to cache key
29     * to differentiate between caches for regular and privileged users
30     *
31     * @param Event $event event object by reference
32     * @return bool
33     */
34    public function handleCacheAggregation(Event $event)
35    {
36        /** @var CacheParser $cache */
37        $cache = $event->data;
38        if ($cache->mode != 'xhtml') return true;
39        if (!$cache->page) return true; // not a page cache
40
41        $meta = p_get_metadata($cache->page, 'plugin struct');
42        if (isset($meta['hasaggregation'])) {
43            // separate caches for publishers/approvers and others
44             $cache->key .= ';' . helper_plugin_structpublish_db::userHasRole($cache->page, '', []);
45
46            // rebuild cache name
47            $cache->cache = getCacheName($cache->key, $cache->ext);
48        }
49
50        return true;
51    }
52}
53