xref: /plugin/include/action.php (revision 5c293ea23f4709f7c25f491bf8f51410a2213f93)
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    }
40
41    /**
42     * Supplies the current section level to the include syntax plugin
43     *
44     * @author Michael Klier <chi@chimeric.de>
45     */
46    function handle_parser(&$event, $param) {
47        global $ID;
48
49        // check for stored toplevel ID in helper plugin
50        // if it's missing lets see if we have to do anything at all
51        if(!isset($this->helper->toplevel_id)) {
52            $ins =& $event->data->calls;
53            $num = count($ins);
54            for($i=0; $i<$num; $i++) {
55                if(($ins[$i][0] == 'plugin') && ($ins[$i][1][0] == 'include_include')) {
56                    if(!isset($this->helper->toplevel_id)) $this->helper->toplevel_id = $ID;
57                    $this->helper->parse_instructions($ID, $ins);
58                }
59            }
60        }
61    }
62
63    /**
64     * Add a hidden input to the form to preserve the redirect_id
65     */
66    function handle_form(&$event, $param) {
67      if (array_key_exists('redirect_id', $_REQUEST)) {
68        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
69      }
70    }
71
72    /**
73     * Modify the data for the redirect when there is a redirect_id set
74     */
75    function handle_redirect(&$event, $param) {
76      if (array_key_exists('redirect_id', $_REQUEST)) {
77        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
78        $event->data['title'] = '';
79      }
80    }
81
82    /**
83     * prepare the cache object for default _useCache action
84     */
85    function _cache_prepare(&$event, $param) {
86        global $ID;
87        global $INFO;
88        global $conf;
89
90        $cache =& $event->data;
91
92        // we're only interested in instructions of the current page
93        // without the ID check we'd get the cache objects for included pages as well
94        if(!isset($cache->page) || ($cache->page != $ID)) return;
95        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
96
97        if(!empty($INFO['userinfo'])) {
98            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
99        } else {
100            $include_key = '@ALL';
101        }
102
103        $depends = p_get_metadata($ID, 'plugin_include');
104        if(is_array($depends)) {
105            $pages = array();
106            if(!isset($depends['keys'][$include_key])) {
107                $cache->depends['purge'] = true; // include key not set - request purge
108            } else {
109                $pages = $depends['pages'];
110            }
111        } else {
112            // nothing to do for us
113            return;
114        }
115
116        // add plugin VERSION file to depends for nicer upgrades
117        $cache->depends['files'][] = dirname(__FILE__) . '/VERSION';
118
119        $key = '';
120        foreach($pages as $page) {
121            $page = $this->helper->_apply_macro($page);
122            $file = wikiFN($page);
123            if(!in_array($cache->depends['files'], array($file)) && @file_exists($file)) {
124                $cache->depends['files'][] = $file;
125                $key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
126            }
127        }
128
129        // empty $key implies no includes, so nothing to do
130        if(empty($key)) return;
131
132        // mark the cache as being modified by the include plugin
133        $cache->include = true;
134
135        // set new cache key & cache name
136        // now also dependent on included page ids and their ACL_READ status
137        $cache->key .= $key;
138        $cache->cache = getCacheName($cache->key, $cache->ext);
139    }
140
141}
142//vim:ts=4:sw=4:et:enc=utf-8:
143