xref: /plugin/struct/action/cache.php (revision dbe5bc9dd7816db788eaee8abda1d10fe9e8415d) !
1*dbe5bc9dSAndreas Gohr<?php
2*dbe5bc9dSAndreas Gohr
3*dbe5bc9dSAndreas Gohr/**
4*dbe5bc9dSAndreas Gohr * Handle caching of pages containing struct aggregations
5*dbe5bc9dSAndreas Gohr */
6*dbe5bc9dSAndreas Gohrclass action_plugin_struct_cache extends DokuWiki_Action_Plugin {
7*dbe5bc9dSAndreas Gohr
8*dbe5bc9dSAndreas Gohr    /**
9*dbe5bc9dSAndreas Gohr     * Registers a callback function for a given event
10*dbe5bc9dSAndreas Gohr     *
11*dbe5bc9dSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
12*dbe5bc9dSAndreas Gohr     * @return void
13*dbe5bc9dSAndreas Gohr     */
14*dbe5bc9dSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
15*dbe5bc9dSAndreas Gohr        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache_aggregation');
16*dbe5bc9dSAndreas Gohr        $controller->register_hook('PARSER_CACHE_USE', 'AFTER', $this, 'handle_cache_dynamic');
17*dbe5bc9dSAndreas Gohr    }
18*dbe5bc9dSAndreas Gohr
19*dbe5bc9dSAndreas Gohr    /**
20*dbe5bc9dSAndreas Gohr     * For pages containing an aggregation, add the last modified date of the database itself
21*dbe5bc9dSAndreas Gohr     * to the cache dependencies
22*dbe5bc9dSAndreas Gohr     *
23*dbe5bc9dSAndreas Gohr     * @param Doku_Event $event event object by reference
24*dbe5bc9dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
25*dbe5bc9dSAndreas Gohr     *                           handler was registered]
26*dbe5bc9dSAndreas Gohr     * @return bool
27*dbe5bc9dSAndreas Gohr     */
28*dbe5bc9dSAndreas Gohr    public function handle_cache_aggregation(Doku_Event $event, $param) {
29*dbe5bc9dSAndreas Gohr        /** @var \cache_parser $cache */
30*dbe5bc9dSAndreas Gohr        $cache = $event->data;
31*dbe5bc9dSAndreas Gohr        if($cache->mode != 'xhtml') return true;
32*dbe5bc9dSAndreas Gohr        if(!$cache->page) return true; // not a page cache
33*dbe5bc9dSAndreas Gohr
34*dbe5bc9dSAndreas Gohr        $meta = p_get_metadata($cache->page, 'plugin struct');
35*dbe5bc9dSAndreas Gohr        if(isset($meta['hasaggregation'])) {
36*dbe5bc9dSAndreas Gohr            /** @var helper_plugin_struct_db $db */
37*dbe5bc9dSAndreas Gohr            $db = plugin_load('helper', 'struct_db');
38*dbe5bc9dSAndreas Gohr            $cache->depends['files'][] = $db->getDB()->getAdapter()->getDbFile();
39*dbe5bc9dSAndreas Gohr        }
40*dbe5bc9dSAndreas Gohr
41*dbe5bc9dSAndreas Gohr        return true;
42*dbe5bc9dSAndreas Gohr    }
43*dbe5bc9dSAndreas Gohr
44*dbe5bc9dSAndreas Gohr    /**
45*dbe5bc9dSAndreas Gohr     * Disable cache when dymanic parameters are present
46*dbe5bc9dSAndreas Gohr     *
47*dbe5bc9dSAndreas Gohr     * @param Doku_Event $event event object by reference
48*dbe5bc9dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
49*dbe5bc9dSAndreas Gohr     *                           handler was registered]
50*dbe5bc9dSAndreas Gohr     * @return bool
51*dbe5bc9dSAndreas Gohr     */
52*dbe5bc9dSAndreas Gohr    public function handle_cache_dynamic(Doku_Event $event, $param) {
53*dbe5bc9dSAndreas Gohr        /** @var \cache_parser $cache */
54*dbe5bc9dSAndreas Gohr        $cache = $event->data;
55*dbe5bc9dSAndreas Gohr        if($cache->mode != 'xhtml') return true;
56*dbe5bc9dSAndreas Gohr        if(!$cache->page) return true; // not a page cache
57*dbe5bc9dSAndreas Gohr        global $INPUT;
58*dbe5bc9dSAndreas Gohr
59*dbe5bc9dSAndreas Gohr        // disable cache use when one of these parameters is present
60*dbe5bc9dSAndreas Gohr        foreach(array('dataflt', 'dataofs', 'datasrt') as $key) {
61*dbe5bc9dSAndreas Gohr            if($INPUT->has($key)) {
62*dbe5bc9dSAndreas Gohr                $event->result = false;
63*dbe5bc9dSAndreas Gohr                return true;
64*dbe5bc9dSAndreas Gohr            }
65*dbe5bc9dSAndreas Gohr        }
66*dbe5bc9dSAndreas Gohr
67*dbe5bc9dSAndreas Gohr        return true;
68*dbe5bc9dSAndreas Gohr    }
69*dbe5bc9dSAndreas Gohr
70*dbe5bc9dSAndreas Gohr}
71