1<?php
2
3class action_plugin_instantpage_adjustPrefetchCaching extends DokuWiki_Action_Plugin
4{
5
6    /**
7     * Registers a callback function for a given event
8     *
9     * @param Doku_Event_Handler $controller DokuWiki's event controller object
10     *
11     * @return void
12     */
13    public function register(Doku_Event_Handler $controller)
14    {
15        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'adjustCaching');
16    }
17
18    /**
19     * Event: ACTION_HEADERS_SEND
20     *
21     * @param Doku_Event $event event object by reference
22     *
23     * @return void
24     */
25    public function adjustCaching(Doku_Event $event)
26    {
27        global $INPUT;
28        if (!$this->isPrefetchRequest()) {
29            return;
30        }
31        if (substr($INPUT->server->str('SCRIPT_NAME'), -8) !== 'doku.php') {
32            return;
33        }
34        $event->data[] = 'Cache-Control: max-age=60';
35        $event->data[] = 'Pragma: ';
36        $event->data[] = 'Expires: ';
37    }
38
39    private function isPrefetchRequest()
40    {
41        global $INPUT;
42        return $INPUT->server->str('HTTP_PURPOSE') === 'prefetch'
43            || $INPUT->server->str('HTTP_X_MOZ') === 'prefetch';
44    }
45}
46