xref: /plugin/include/action.php (revision 7488c4d8b93a1b9c3b82261a059e31a8a7d7c339)
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     * plugin should use this method to register its handlers with the dokuwiki's event controller
31     */
32    function register(&$controller) {
33      $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
34      $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
35      $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
36      $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
37      $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect');
38      $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser');
39      $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_metadata');
40    }
41
42    /**
43     * Used for debugging purposes only
44     */
45    function handle_metadata(&$event, $param) {
46        global $conf;
47        if($conf['allowdebug']) {
48            dbglog('---- PLUGIN INCLUDE META DATA START ----');
49            dbglog($event->data);
50            dbglog('---- PLUGIN INCLUDE META DATA END ----');
51        }
52    }
53
54    /**
55     * Supplies the current section level to the include syntax plugin
56     *
57     * @author Michael Klier <chi@chimeric.de>
58     */
59    function handle_parser(&$event, $param) {
60        global $ID;
61
62        // check for stored toplevel ID in helper plugin
63        // if it's missing lets see if we have to do anything at all
64        if(!isset($this->helper->toplevel_id)) {
65            $ins =& $event->data->calls;
66            $num = count($ins);
67            for($i=0; $i<$num; $i++) {
68                if(($ins[$i][0] == 'plugin')) {
69                    switch($ins[$i][1][0]) {
70                        case 'include_include':
71                            if(!isset($this->helper->toplevel_id)) {
72                                $this->helper->toplevel_id = $ID;
73                            }
74                            $this->helper->parse_instructions($ID, $ins);
75                            break;
76                        // some plugins already close open sections
77                        // so we need to make sure we don't close them twice
78                        case 'box':
79                            $this->helper->sec_close = false;
80                            break;
81                    }
82                }
83            }
84        }
85    }
86
87    /**
88     * Add a hidden input to the form to preserve the redirect_id
89     */
90    function handle_form(&$event, $param) {
91      if (array_key_exists('redirect_id', $_REQUEST)) {
92        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
93      }
94    }
95
96    /**
97     * Modify the data for the redirect when there is a redirect_id set
98     */
99    function handle_redirect(&$event, $param) {
100      if (array_key_exists('redirect_id', $_REQUEST)) {
101        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
102        $event->data['title'] = '';
103      }
104    }
105
106    /**
107     * prepare the cache object for default _useCache action
108     */
109    function _cache_prepare(&$event, $param) {
110        global $ID;
111        global $INFO;
112        global $conf;
113
114        $cache =& $event->data;
115
116        // we're only interested in instructions of the current page
117        // without the ID check we'd get the cache objects for included pages as well
118        if(!isset($cache->page) || ($cache->page != $ID)) return;
119        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
120
121        if(!empty($INFO['userinfo'])) {
122            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
123        } else {
124            $include_key = '@ALL';
125        }
126
127        $depends = p_get_metadata($ID, 'plugin_include');
128
129        if($conf['allowdebug']) {
130            dbglog('---- PLUGIN INCLUDE INCLUDE KEY START ----');
131            dbglog($include_key);
132            dbglog('---- PLUGIN INCLUDE INCLUDE KEY END ----');
133            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
134            dbglog($depends);
135            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
136        }
137
138        if(is_array($depends)) {
139            $pages = array();
140            if(!isset($depends['keys'][$include_key])) {
141                $cache->depends['purge'] = true; // include key not set - request purge
142            } else {
143                $pages = $depends['pages'];
144            }
145        } else {
146            // nothing to do for us
147            return;
148        }
149
150        // add plugin.info.txt to depends for nicer upgrades
151        $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
152
153        $key = '';
154        foreach($pages as $page) {
155            $page = cleanID($this->helper->_apply_macro($page));
156            resolve_pageid(getNS($ID), $page, $exists);
157            $file = wikiFN($page);
158            if(!in_array($cache->depends['files'], array($file)) && @file_exists($file)) {
159                $cache->depends['files'][] = $file;
160                $key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
161            }
162        }
163
164        // empty $key implies no includes, so nothing to do
165        if(empty($key)) return;
166
167        // mark the cache as being modified by the include plugin
168        $cache->include = true;
169
170        // set new cache key & cache name
171        // now also dependent on included page ids and their ACL_READ status
172        $cache->key .= $key;
173        $cache->cache = getCacheName($cache->key, $cache->ext);
174    }
175
176}
177//vim:ts=4:sw=4:et:enc=utf-8:
178