xref: /plugin/include/action.php (revision babe403e0bc835bad18105b5eb8c9df3f7e0246e)
1<?php
2/**
3 * Include Plugin:  Display a wiki page within another wiki page
4 *
5 * Action plugin component, for cache validity determination
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Christopher Smith <chris@jalakai.co.uk>
9 * @author     Michael Klier <chi@chimeric.de>
10 */
11if(!defined('DOKU_INC')) die();  // no Dokuwiki, no go
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14require_once(DOKU_PLUGIN.'action.php');
15
16/**
17 * All DokuWiki plugins to extend the parser/rendering mechanism
18 * need to inherit from this class
19 */
20class action_plugin_include extends DokuWiki_Action_Plugin {
21
22    var $supportedModes = array('xhtml', 'i');
23    var $helper = null;
24
25    function action_plugin_include() {
26        $this->helper = plugin_load('helper', 'include');
27    }
28
29    /**
30     * return some info
31     */
32    function getInfo() {
33      return array(
34        'author' => 'Gina Häußge, Michael Klier, Christopher Smith',
35        'email'  => 'dokuwiki@chimeric.de',
36        'date'   => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'),
37        'name'   => 'Include Plugin',
38        'desc'   => 'Improved cache handling for included pages and redirect-handling',
39        'url'    => 'http://dokuwiki.org/plugin:include',
40      );
41    }
42
43    /**
44     * plugin should use this method to register its handlers with the dokuwiki's event controller
45     */
46    function register(&$controller) {
47      $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
48      $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
49      $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
50      $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
51      $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect');
52      $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser');
53    }
54
55    /**
56     * Supplies the current section level to the include syntax plugin
57     *
58     * @author Michael Klier <chi@chimeric.de>
59     */
60    function handle_parser(&$event, $param) {
61        global $ID;
62
63        // check for stored toplevel ID in helper plugin
64        // if it's missing lets see if we have to do anything at all
65        if(!isset($this->helper->toplevel_id)) {
66            $ins =& $event->data->calls;
67            $num = count($ins);
68            for($i=0; $i<$num; $i++) {
69                if(($ins[$i][0] == 'plugin') && ($ins[$i][1][0] == 'include_include')) {
70                    if(!isset($this->helper->toplevel_id)) $this->helper->toplevel_id = $ID;
71                    $this->helper->parse_instructions($ID, $ins);
72                }
73            }
74        }
75    }
76
77    /**
78     * Add a hidden input to the form to preserve the redirect_id
79     */
80    function handle_form(&$event, $param) {
81      if (array_key_exists('redirect_id', $_REQUEST)) {
82        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
83      }
84    }
85
86    /**
87     * Modify the data for the redirect when there is a redirect_id set
88     */
89    function handle_redirect(&$event, $param) {
90      if (array_key_exists('redirect_id', $_REQUEST)) {
91        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
92        $event->data['title'] = '';
93      }
94    }
95
96    /**
97     * prepare the cache object for default _useCache action
98     */
99    function _cache_prepare(&$event, $param) {
100        global $ID;
101        global $INFO;
102        global $conf;
103
104        $cache =& $event->data;
105
106        // we're only interested in instructions of the current page
107        // without the ID check we'd get the cache objects for included pages as well
108        if(!isset($cache->page) || ($cache->page != $ID)) return;
109        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
110
111        if(!empty($INFO['userinfo'])) {
112            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
113        } else {
114            $include_key = '@ALL';
115        }
116
117        $depends = p_get_metadata($ID, 'plugin_include');
118        if(is_array($depends)) {
119            $pages = array();
120            if(!isset($depends['keys'][$include_key])) {
121                $cache->depends['purge'] = true; // include key not set - request purge
122            } else {
123                $pages = $depends['pages'];
124            }
125        } else {
126            // nothing to do for us
127            return;
128        }
129
130        // add plugin VERSION file to depends for nicer upgrades
131        $cache->depends['files'][] = dirname(__FILE__) . '/VERSION';
132
133        $key = '';
134        foreach($pages as $page) {
135            $page = $this->helper->_apply_macro($page);
136            if(strpos($page,'/') || cleanID($page) != $page) {
137                continue;
138            } else {
139                $file = wikiFN($page);
140                if(!in_array($cache->depends['files'], array($file)) && @file_exists($file)) {
141                    $cache->depends['files'][] = $file;
142                    $key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
143                }
144            }
145        }
146
147        // empty $key implies no includes, so nothing to do
148        if(empty($key)) return;
149
150        // mark the cache as being modified by the include plugin
151        $cache->include = true;
152
153        // set new cache key & cache name
154        // now also dependent on included page ids and their ACL_READ status
155        $cache->key .= $key;
156        $cache->cache = getCacheName($cache->key, $cache->ext);
157    }
158
159}
160//vim:ts=4:sw=4:et:enc=utf-8:
161