xref: /plugin/include/action.php (revision 1148b0fedd67533325f040fefe5efa5e28e0d8f8)
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')) {
56                    switch($ins[$i][1][0]) {
57                        case 'include_include':
58                            if(!isset($this->helper->toplevel_id)) {
59                                $this->helper->toplevel_id = $ID;
60                            }
61                            $this->helper->parse_instructions($ID, $ins);
62                            break;
63                        // some plugins already close open sections
64                        // so we need to make sure we don't close them twice
65                        case 'box':
66                            $this->helper->sec_close = false;
67                            break;
68                    }
69                }
70            }
71        }
72    }
73
74    /**
75     * Add a hidden input to the form to preserve the redirect_id
76     */
77    function handle_form(&$event, $param) {
78      if (array_key_exists('redirect_id', $_REQUEST)) {
79        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
80      }
81    }
82
83    /**
84     * Modify the data for the redirect when there is a redirect_id set
85     */
86    function handle_redirect(&$event, $param) {
87      if (array_key_exists('redirect_id', $_REQUEST)) {
88        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
89        $event->data['title'] = '';
90      }
91    }
92
93    /**
94     * prepare the cache object for default _useCache action
95     */
96    function _cache_prepare(&$event, $param) {
97        global $ID;
98        global $INFO;
99        global $conf;
100
101        $cache =& $event->data;
102
103        // we're only interested in instructions of the current page
104        // without the ID check we'd get the cache objects for included pages as well
105        if(!isset($cache->page) || ($cache->page != $ID)) return;
106        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
107
108        if(!empty($INFO['userinfo'])) {
109            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
110        } else {
111            $include_key = '@ALL';
112        }
113
114
115        $depends = p_get_metadata($ID, 'plugin_include');
116        if(is_array($depends)) {
117            $pages = array();
118            if(!isset($depends['keys'][$include_key])) {
119                $cache->depends['purge'] = true; // include key not set - request purge
120            } else {
121                $pages = $depends['pages'];
122            }
123        } else {
124            // nothing to do for us
125            return;
126        }
127
128        // add plugin VERSION file to depends for nicer upgrades
129        $cache->depends['files'][] = dirname(__FILE__) . '/VERSION';
130
131        $key = '';
132        foreach($pages as $page) {
133            $page = cleanID($this->helper->_apply_macro($page));
134            $file = wikiFN($page);
135            if(!in_array($cache->depends['files'], array($file)) && @file_exists($file)) {
136                $cache->depends['files'][] = $file;
137                $key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
138            }
139        }
140
141        // empty $key implies no includes, so nothing to do
142        if(empty($key)) return;
143
144        // mark the cache as being modified by the include plugin
145        $cache->include = true;
146
147        // set new cache key & cache name
148        // now also dependent on included page ids and their ACL_READ status
149        $cache->key .= $key;
150        $cache->cache = getCacheName($cache->key, $cache->ext);
151    }
152
153}
154//vim:ts=4:sw=4:et:enc=utf-8:
155