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