xref: /plugin/struct/action/cache.php (revision ba76620163eb4cb2b8d6042c6bec7725074f508c)
1dbe5bc9dSAndreas Gohr<?php
2*ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SearchConfig;
3*ba766201SAndreas Gohruse dokuwiki\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) {
3116b7d914SAndreas Gohr        global $INPUT;
3216b7d914SAndreas 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');
421dc771ecSAndreas Gohr            // cache depends on last database save
43dbe5bc9dSAndreas Gohr            $cache->depends['files'][] = $db->getDB()->getAdapter()->getDbFile();
4416b7d914SAndreas Gohr
451dc771ecSAndreas Gohr            // dynamic renders should never overwrite the default page cache
461dc771ecSAndreas Gohr            // we need this in additon to handle_cache_dynamic() below because we can only
471dc771ecSAndreas Gohr            // influence if a cache is used, not that it will be written
481dc771ecSAndreas Gohr            if(
491dc771ecSAndreas Gohr                $INPUT->has(SearchConfigParameters::$PARAM_FILTER) ||
501dc771ecSAndreas Gohr                $INPUT->has(SearchConfigParameters::$PARAM_OFFSET) ||
511dc771ecSAndreas Gohr                $INPUT->has(SearchConfigParameters::$PARAM_SORT)
521dc771ecSAndreas Gohr            ) {
531dc771ecSAndreas Gohr                $cache->key .= 'dynamic';
541dc771ecSAndreas Gohr            }
551dc771ecSAndreas Gohr
5616b7d914SAndreas Gohr            // cache depends on today's date
5716b7d914SAndreas Gohr            if($meta['hasaggregation'] & SearchConfig::$CACHE_DATE) {
5816b7d914SAndreas Gohr                $oldage = $cache->depends['age'];
5916b7d914SAndreas Gohr                $newage = time() - mktime(0, 0, 1); // time since first second today
6016b7d914SAndreas Gohr                $cache->depends['age'] = min($oldage, $newage);
6116b7d914SAndreas Gohr            }
6216b7d914SAndreas Gohr
6316b7d914SAndreas Gohr            // cache depends on current user
6416b7d914SAndreas Gohr            if($meta['hasaggregation'] & SearchConfig::$CACHE_USER) {
6516b7d914SAndreas Gohr                $cache->key .= ';'.$INPUT->server->str('REMOTE_USER');
661dc771ecSAndreas Gohr
6716b7d914SAndreas Gohr            }
681dc771ecSAndreas Gohr
691dc771ecSAndreas Gohr            // rebuild cachename
701dc771ecSAndreas Gohr            $cache->cache = getCacheName($cache->key, $cache->ext);
71dbe5bc9dSAndreas Gohr        }
72dbe5bc9dSAndreas Gohr
73dbe5bc9dSAndreas Gohr        return true;
74dbe5bc9dSAndreas Gohr    }
75dbe5bc9dSAndreas Gohr
76dbe5bc9dSAndreas Gohr    /**
77dbe5bc9dSAndreas Gohr     * Disable cache when dymanic parameters are present
78dbe5bc9dSAndreas Gohr     *
79dbe5bc9dSAndreas Gohr     * @param Doku_Event $event event object by reference
80dbe5bc9dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
81dbe5bc9dSAndreas Gohr     *                           handler was registered]
82dbe5bc9dSAndreas Gohr     * @return bool
83dbe5bc9dSAndreas Gohr     */
84dbe5bc9dSAndreas Gohr    public function handle_cache_dynamic(Doku_Event $event, $param) {
85dbe5bc9dSAndreas Gohr        /** @var \cache_parser $cache */
86dbe5bc9dSAndreas Gohr        $cache = $event->data;
87dbe5bc9dSAndreas Gohr        if($cache->mode != 'xhtml') return true;
88dbe5bc9dSAndreas Gohr        if(!$cache->page) return true; // not a page cache
89dbe5bc9dSAndreas Gohr        global $INPUT;
90dbe5bc9dSAndreas Gohr
91dbe5bc9dSAndreas Gohr        // disable cache use when one of these parameters is present
9253ed3125SAndreas Gohr        foreach(array(
9353ed3125SAndreas Gohr                    SearchConfigParameters::$PARAM_FILTER,
9453ed3125SAndreas Gohr                    SearchConfigParameters::$PARAM_OFFSET,
9553ed3125SAndreas Gohr                    SearchConfigParameters::$PARAM_SORT
9653ed3125SAndreas Gohr                ) as $key) {
97dbe5bc9dSAndreas Gohr            if($INPUT->has($key)) {
98dbe5bc9dSAndreas Gohr                $event->result = false;
99dbe5bc9dSAndreas Gohr                return true;
100dbe5bc9dSAndreas Gohr            }
101dbe5bc9dSAndreas Gohr        }
102dbe5bc9dSAndreas Gohr
103dbe5bc9dSAndreas Gohr        return true;
104dbe5bc9dSAndreas Gohr    }
105dbe5bc9dSAndreas Gohr
106dbe5bc9dSAndreas Gohr}
107