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