xref: /plugin/struct/action/cache.php (revision 16b7d914c0b69bf27d1ddd8a08e05beddbeedf02)
1dbe5bc9dSAndreas Gohr<?php
2*16b7d914SAndreas Gohruse plugin\struct\meta\SearchConfig;
353ed3125SAndreas Gohruse plugin\struct\meta\SearchConfigParameters;
4dbe5bc9dSAndreas Gohr
5dbe5bc9dSAndreas Gohr/**
6dbe5bc9dSAndreas Gohr * Handle caching of pages containing struct aggregations
7dbe5bc9dSAndreas Gohr */
8dbe5bc9dSAndreas Gohrclass action_plugin_struct_cache extends DokuWiki_Action_Plugin {
9dbe5bc9dSAndreas Gohr
10dbe5bc9dSAndreas Gohr    /**
11dbe5bc9dSAndreas Gohr     * Registers a callback function for a given event
12dbe5bc9dSAndreas Gohr     *
13dbe5bc9dSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
14dbe5bc9dSAndreas Gohr     * @return void
15dbe5bc9dSAndreas Gohr     */
16dbe5bc9dSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
17dbe5bc9dSAndreas Gohr        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache_aggregation');
18dbe5bc9dSAndreas Gohr        $controller->register_hook('PARSER_CACHE_USE', 'AFTER', $this, 'handle_cache_dynamic');
19dbe5bc9dSAndreas Gohr    }
20dbe5bc9dSAndreas Gohr
21dbe5bc9dSAndreas Gohr    /**
22dbe5bc9dSAndreas Gohr     * For pages containing an aggregation, add the last modified date of the database itself
23dbe5bc9dSAndreas Gohr     * to the cache dependencies
24dbe5bc9dSAndreas Gohr     *
25dbe5bc9dSAndreas Gohr     * @param Doku_Event $event event object by reference
26dbe5bc9dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
27dbe5bc9dSAndreas Gohr     *                           handler was registered]
28dbe5bc9dSAndreas Gohr     * @return bool
29dbe5bc9dSAndreas Gohr     */
30dbe5bc9dSAndreas Gohr    public function handle_cache_aggregation(Doku_Event $event, $param) {
31*16b7d914SAndreas Gohr        global $INPUT;
32*16b7d914SAndreas Gohr
33dbe5bc9dSAndreas Gohr        /** @var \cache_parser $cache */
34dbe5bc9dSAndreas Gohr        $cache = $event->data;
35dbe5bc9dSAndreas Gohr        if($cache->mode != 'xhtml') return true;
36dbe5bc9dSAndreas Gohr        if(!$cache->page) return true; // not a page cache
37dbe5bc9dSAndreas Gohr
38dbe5bc9dSAndreas Gohr        $meta = p_get_metadata($cache->page, 'plugin struct');
39dbe5bc9dSAndreas Gohr        if(isset($meta['hasaggregation'])) {
40dbe5bc9dSAndreas Gohr            /** @var helper_plugin_struct_db $db */
41dbe5bc9dSAndreas Gohr            $db = plugin_load('helper', 'struct_db');
42dbe5bc9dSAndreas Gohr            $cache->depends['files'][] = $db->getDB()->getAdapter()->getDbFile();
43*16b7d914SAndreas Gohr
44*16b7d914SAndreas Gohr            // cache depends on today's date
45*16b7d914SAndreas Gohr            if($meta['hasaggregation'] & SearchConfig::$CACHE_DATE) {
46*16b7d914SAndreas Gohr                $oldage = $cache->depends['age'];
47*16b7d914SAndreas Gohr                $newage = time() - mktime(0, 0, 1); // time since first second today
48*16b7d914SAndreas Gohr                $cache->depends['age'] = min($oldage, $newage);
49*16b7d914SAndreas Gohr            }
50*16b7d914SAndreas Gohr
51*16b7d914SAndreas Gohr            // cache depends on current user
52*16b7d914SAndreas Gohr            if($meta['hasaggregation'] & SearchConfig::$CACHE_USER) {
53*16b7d914SAndreas Gohr                $cache->key .= ';'.$INPUT->server->str('REMOTE_USER');
54*16b7d914SAndreas Gohr                $cache->cache = getCacheName($cache->key, $cache->ext);
55*16b7d914SAndreas Gohr            }
56dbe5bc9dSAndreas Gohr        }
57dbe5bc9dSAndreas Gohr
58dbe5bc9dSAndreas Gohr        return true;
59dbe5bc9dSAndreas Gohr    }
60dbe5bc9dSAndreas Gohr
61dbe5bc9dSAndreas Gohr    /**
62dbe5bc9dSAndreas Gohr     * Disable cache when dymanic parameters are present
63dbe5bc9dSAndreas Gohr     *
64dbe5bc9dSAndreas Gohr     * @param Doku_Event $event event object by reference
65dbe5bc9dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
66dbe5bc9dSAndreas Gohr     *                           handler was registered]
67dbe5bc9dSAndreas Gohr     * @return bool
68dbe5bc9dSAndreas Gohr     */
69dbe5bc9dSAndreas Gohr    public function handle_cache_dynamic(Doku_Event $event, $param) {
70dbe5bc9dSAndreas Gohr        /** @var \cache_parser $cache */
71dbe5bc9dSAndreas Gohr        $cache = $event->data;
72dbe5bc9dSAndreas Gohr        if($cache->mode != 'xhtml') return true;
73dbe5bc9dSAndreas Gohr        if(!$cache->page) return true; // not a page cache
74dbe5bc9dSAndreas Gohr        global $INPUT;
75dbe5bc9dSAndreas Gohr
76dbe5bc9dSAndreas Gohr        // disable cache use when one of these parameters is present
7753ed3125SAndreas Gohr        foreach(array(
7853ed3125SAndreas Gohr                    SearchConfigParameters::$PARAM_FILTER,
7953ed3125SAndreas Gohr                    SearchConfigParameters::$PARAM_OFFSET,
8053ed3125SAndreas Gohr                    SearchConfigParameters::$PARAM_SORT
8153ed3125SAndreas Gohr                ) as $key) {
82dbe5bc9dSAndreas Gohr            if($INPUT->has($key)) {
83dbe5bc9dSAndreas Gohr                $event->result = false;
84dbe5bc9dSAndreas Gohr                return true;
85dbe5bc9dSAndreas Gohr            }
86dbe5bc9dSAndreas Gohr        }
87dbe5bc9dSAndreas Gohr
88dbe5bc9dSAndreas Gohr        return true;
89dbe5bc9dSAndreas Gohr    }
90dbe5bc9dSAndreas Gohr
91dbe5bc9dSAndreas Gohr}
92