xref: /plugin/include/action.php (revision e14109df25add262ba4bc17f1894814de07df7a4)
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');
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     * @author Michael Hamann <michael@content-space.de>
59     */
60    function handle_parser(&$event, $param) {
61        global $ID;
62
63        $level = 0;
64        $ins =& $event->data->calls;
65        $num = count($ins);
66        for($i=0; $i<$num; $i++) {
67            switch($ins[$i][0]) {
68            case 'plugin':
69                switch($ins[$i][1][0]) {
70                case 'include_include':
71                    $ins[$i][1][1][] = $level;
72                    break;
73                    /* FIXME: this doesn't work anymore that way with the new structure
74                    // some plugins already close open sections
75                    // so we need to make sure we don't close them twice
76                case 'box':
77                    $this->helper->sec_close = false;
78                    break;
79                     */
80                }
81                break;
82            case 'section_open':
83                $level = $ins[$i][1][0];
84                break;
85            }
86        }
87    }
88
89    /**
90     * Add a hidden input to the form to preserve the redirect_id
91     */
92    function handle_form(&$event, $param) {
93      if (array_key_exists('redirect_id', $_REQUEST)) {
94        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
95      }
96    }
97
98    /**
99     * Modify the data for the redirect when there is a redirect_id set
100     */
101    function handle_redirect(&$event, $param) {
102      if (array_key_exists('redirect_id', $_REQUEST)) {
103        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
104        $event->data['title'] = '';
105      }
106    }
107
108    /**
109     * prepare the cache object for default _useCache action
110     */
111    function _cache_prepare(&$event, $param) {
112        global $ID;
113        global $INFO;
114        global $conf;
115
116        $cache =& $event->data;
117
118        // we're only interested in instructions of the current page
119        // without the ID check we'd get the cache objects for included pages as well
120        if(!isset($cache->page) || ($cache->page != $ID)) return;
121        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
122
123        if(!empty($INFO['userinfo'])) {
124            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
125        } else {
126            $include_key = '@ALL';
127        }
128
129        $depends = p_get_metadata($ID, 'plugin_include');
130
131        if($conf['allowdebug']) {
132            dbglog('---- PLUGIN INCLUDE INCLUDE KEY START ----');
133            dbglog($include_key);
134            dbglog('---- PLUGIN INCLUDE INCLUDE KEY END ----');
135            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
136            dbglog($depends);
137            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
138        }
139
140        if(is_array($depends)) {
141            $pages = array();
142            if(!isset($depends['keys'][$include_key])) {
143                $cache->depends['purge'] = true; // include key not set - request purge
144            } else {
145                $pages = $depends['pages'];
146            }
147        } else {
148            // nothing to do for us
149            return;
150        }
151
152        // add plugin.info.txt to depends for nicer upgrades
153        $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
154
155        $key = '';
156        if (is_array($pages)) {
157            foreach($pages as $page) {
158                $page = cleanID($this->helper->_apply_macro($page));
159                resolve_pageid(getNS($ID), $page, $exists);
160                $file = wikiFN($page);
161                if(!in_array($cache->depends['files'], array($file)) && @file_exists($file)) {
162                    $cache->depends['files'][] = $file;
163                    $key .= '#' . $page . '|ACL' . auth_quickaclcheck($page);
164                }
165            }
166        }
167
168        // empty $key implies no includes, so nothing to do
169        if(empty($key)) return;
170
171        // mark the cache as being modified by the include plugin
172        $cache->include = true;
173
174        // set new cache key & cache name
175        // now also dependent on included page ids and their ACL_READ status
176        $cache->key .= $key;
177        $cache->cache = getCacheName($cache->key, $cache->ext);
178    }
179
180}
181//vim:ts=4:sw=4:et:enc=utf-8:
182