xref: /plugin/include/action.php (revision 1403dce04142d4874c515f4f0c6b9b4a88d51d29)
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', 'metadata');
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      $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handle_secedit_button');
41    }
42
43    /**
44     * Used for debugging purposes only
45     */
46    function handle_metadata(&$event, $param) {
47        global $conf;
48        if($conf['allowdebug']) {
49            dbglog('---- PLUGIN INCLUDE META DATA START ----');
50            dbglog($event->data);
51            dbglog('---- PLUGIN INCLUDE META DATA END ----');
52        }
53    }
54
55    /**
56     * Supplies the current section level to the include syntax plugin
57     *
58     * @author Michael Klier <chi@chimeric.de>
59     * @author Michael Hamann <michael@content-space.de>
60     */
61    function handle_parser(&$event, $param) {
62        global $ID;
63
64        $level = 0;
65        $ins =& $event->data->calls;
66        $num = count($ins);
67        for($i=0; $i<$num; $i++) {
68            switch($ins[$i][0]) {
69            case 'plugin':
70                switch($ins[$i][1][0]) {
71                case 'include_include':
72                    $ins[$i][1][1][] = $level;
73                    break;
74                    /* FIXME: this doesn't work anymore that way with the new structure
75                    // some plugins already close open sections
76                    // so we need to make sure we don't close them twice
77                case 'box':
78                    $this->helper->sec_close = false;
79                    break;
80                     */
81                }
82                break;
83            case 'section_open':
84                $level = $ins[$i][1][0];
85                break;
86            }
87        }
88    }
89
90    /**
91     * Add a hidden input to the form to preserve the redirect_id
92     */
93    function handle_form(&$event, $param) {
94      if (array_key_exists('redirect_id', $_REQUEST)) {
95        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
96      }
97    }
98
99    /**
100     * Modify the data for the redirect when there is a redirect_id set
101     */
102    function handle_redirect(&$event, $param) {
103      if (array_key_exists('redirect_id', $_REQUEST)) {
104        // Render metadata when this is an older DokuWiki version where
105        // metadata is not automatically re-rendered as the page has probably
106        // been changed but is not directly displayed
107        $versionData = getVersionData();
108        if ($versionData['date'] < '2010-11-23') {
109            p_set_metadata($event->data['id'], array(), true);
110        }
111        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
112        $event->data['title'] = '';
113      }
114    }
115
116    /**
117     * prepare the cache object for default _useCache action
118     */
119    function _cache_prepare(&$event, $param) {
120        global $conf;
121
122        $cache =& $event->data;
123
124        if(!isset($cache->page)) return;
125        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
126
127        $depends = p_get_metadata($cache->page, 'plugin_include');
128
129        if($conf['allowdebug']) {
130            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
131            dbglog($depends);
132            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
133        }
134
135        if (!is_array($depends)) return; // nothing to do for us
136
137        if (!is_array($depends['pages']) ||
138            !is_array($depends['instructions']) ||
139            $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions']) ||
140            // the include_content url parameter may change the behavior for included pages
141            $depends['include_content'] != isset($_REQUEST['include_content'])) {
142
143            $cache->depends['purge'] = true; // included pages changed or old metadata - request purge.
144            if($conf['allowdebug']) {
145                dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----');
146                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----');
147                dbglog($depends['pages']);
148                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----');
149                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----');
150                dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions']));
151                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----');
152
153            }
154        } else {
155            // add plugin.info.txt to depends for nicer upgrades
156            $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
157            foreach ($depends['pages'] as $page) {
158                if (!$page['exists']) continue;
159                $file = wikiFN($page['id']);
160                if (!in_array($file, $cache->depends['files'])) {
161                    $cache->depends['files'][] = $file;
162                }
163            }
164        }
165    }
166
167    /**
168     * Handle special section edit buttons for the include plugin to get the current page
169     * and replace normal section edit buttons when the current page is different from the
170     * global $ID.
171     */
172    function handle_secedit_button(&$event, $params) {
173        // stack of included pages in the form ('id' => page, 'rev' => modification time, 'writable' => bool)
174        static $page_stack = array();
175
176        global $ID;
177
178        $data = $event->data;
179
180        if ($data['target'] == 'plugin_include_start' || $data['target'] == 'plugin_include_start_noredirect') {
181            // handle the "section edits" added by the include plugin
182            $fn = wikiFN($data['name']);
183            array_unshift($page_stack, array(
184                'id' => $data['name'],
185                'rev' => @filemtime($fn),
186                'writable' => (is_writable($fn) && auth_quickaclcheck($data['name']) >= AUTH_EDIT),
187                'redirect' => ($data['target'] == 'plugin_include_start'),
188            ));
189        } elseif ($data['target'] == 'plugin_include_end') {
190            array_shift($page_stack);
191        } elseif (!empty($page_stack)) {
192
193            // Special handling for the edittable plugin
194            if ($data['target'] == 'table' && !plugin_isdisabled('edittable')) {
195                $edittable =& plugin_load('action', 'edittable');
196                $data['name'] = $edittable->getLang('secedit_name');
197            }
198
199            if ($page_stack[0]['writable'] && isset($data['name']) && $data['name'] !== '') {
200                $name = $data['name'];
201                unset($data['name']);
202
203                $secid = $data['secid'];
204                unset($data['secid']);
205
206                if ($page_stack[0]['redirect'])
207                    $data['redirect_id'] = $ID;
208
209                $event->result = "<div class='secedit editbutton_" . $data['target'] .
210                    " editbutton_" . $secid . "'>" .
211                    html_btn('secedit', $page_stack[0]['id'], '',
212                        array_merge(array('do'  => 'edit',
213                        'rev' => $page_stack[0]['rev'],
214                        'summary' => '['.$name.'] '), $data),
215                        'post', $name) . '</div>';
216            } else {
217                $event->result = '';
218            }
219        } else {
220            return; // return so the event won't be stopped
221        }
222
223        $event->preventDefault();
224        $event->stopPropagation();
225    }
226}
227// vim:ts=4:sw=4:et:
228