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