xref: /plugin/include/action.php (revision 9c0b18fdf8201e8b075b124d9e2d035d3f8c3858)
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_plugin_include $helper */
24    var $helper = null;
25
26    function action_plugin_include() {
27        $this->helper = plugin_load('helper', 'include');
28    }
29
30    /**
31     * plugin should use this method to register its handlers with the dokuwiki's event controller
32     */
33    function register(&$controller) {
34        /* @var Doku_event_handler $controller */
35      $controller->register_hook('PARSER_CACHE_USE','BEFORE', $this, '_cache_prepare');
36      $controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
37      $controller->register_hook('HTML_CONFLICTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
38      $controller->register_hook('HTML_DRAFTFORM_OUTPUT', 'BEFORE', $this, 'handle_form');
39      $controller->register_hook('ACTION_SHOW_REDIRECT', 'BEFORE', $this, 'handle_redirect');
40      $controller->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'handle_parser');
41      $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_metadata');
42      $controller->register_hook('HTML_SECEDIT_BUTTON', 'BEFORE', $this, 'handle_secedit_button');
43      $controller->register_hook('EDITX_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_editx_register');
44    }
45
46    /**
47     * Used for debugging purposes only
48     */
49    function handle_metadata(&$event, $param) {
50        global $conf;
51        if($conf['allowdebug']) {
52            dbglog('---- PLUGIN INCLUDE META DATA START ----');
53            dbglog($event->data);
54            dbglog('---- PLUGIN INCLUDE META DATA END ----');
55        }
56    }
57
58    /**
59     * Supplies the current section level to the include syntax plugin
60     *
61     * @author Michael Klier <chi@chimeric.de>
62     * @author Michael Hamann <michael@content-space.de>
63     */
64    function handle_parser(Doku_Event &$event, $param) {
65        global $ID;
66
67        $level = 0;
68        $ins =& $event->data->calls;
69        $num = count($ins);
70        for($i=0; $i<$num; $i++) {
71            switch($ins[$i][0]) {
72            case 'plugin':
73                switch($ins[$i][1][0]) {
74                case 'include_include':
75                    $ins[$i][1][1][] = $level;
76                    break;
77                    /* FIXME: this doesn't work anymore that way with the new structure
78                    // some plugins already close open sections
79                    // so we need to make sure we don't close them twice
80                case 'box':
81                    $this->helper->sec_close = false;
82                    break;
83                     */
84                }
85                break;
86            case 'section_open':
87                $level = $ins[$i][1][0];
88                break;
89            }
90        }
91    }
92
93    /**
94     * Add a hidden input to the form to preserve the redirect_id
95     */
96    function handle_form(Doku_Event &$event, $param) {
97      if (array_key_exists('redirect_id', $_REQUEST)) {
98        $event->data->addHidden('redirect_id', cleanID($_REQUEST['redirect_id']));
99      }
100    }
101
102    /**
103     * Modify the data for the redirect when there is a redirect_id set
104     */
105    function handle_redirect(Doku_Event &$event, $param) {
106      if (array_key_exists('redirect_id', $_REQUEST)) {
107        // Render metadata when this is an older DokuWiki version where
108        // metadata is not automatically re-rendered as the page has probably
109        // been changed but is not directly displayed
110        $versionData = getVersionData();
111        if ($versionData['date'] < '2010-11-23') {
112            p_set_metadata($event->data['id'], array(), true);
113        }
114        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
115        $event->data['title'] = '';
116      }
117    }
118
119    /**
120     * prepare the cache object for default _useCache action
121     */
122    function _cache_prepare(Doku_Event &$event, $param) {
123        global $conf;
124
125        /* @var cache_renderer $cache */
126        $cache =& $event->data;
127
128        if(!isset($cache->page)) return;
129        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
130
131        $depends = p_get_metadata($cache->page, 'plugin_include');
132
133        if($conf['allowdebug']) {
134            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
135            dbglog($depends);
136            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
137        }
138
139        if (!is_array($depends)) return; // nothing to do for us
140
141        if (!is_array($depends['pages']) ||
142            !is_array($depends['instructions']) ||
143            $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions']) ||
144            // the include_content url parameter may change the behavior for included pages
145            $depends['include_content'] != isset($_REQUEST['include_content'])) {
146
147            $cache->depends['purge'] = true; // included pages changed or old metadata - request purge.
148            if($conf['allowdebug']) {
149                dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----');
150                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----');
151                dbglog($depends['pages']);
152                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----');
153                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----');
154                dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions']));
155                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----');
156
157            }
158        } else {
159            // add plugin.info.txt to depends for nicer upgrades
160            $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
161            foreach ($depends['pages'] as $page) {
162                if (!$page['exists']) continue;
163                $file = wikiFN($page['id']);
164                if (!in_array($file, $cache->depends['files'])) {
165                    $cache->depends['files'][] = $file;
166                }
167            }
168        }
169    }
170
171    /**
172     * Handle special section edit buttons for the include plugin to get the current page
173     * and replace normal section edit buttons when the current page is different from the
174     * global $ID.
175     */
176    function handle_secedit_button(Doku_Event &$event, $params) {
177        // stack of included pages in the form ('id' => page, 'rev' => modification time, 'writable' => bool)
178        static $page_stack = array();
179
180        global $ID, $lang;
181
182        $data = $event->data;
183
184        if ($data['target'] == 'plugin_include_start' || $data['target'] == 'plugin_include_start_noredirect') {
185            // handle the "section edits" added by the include plugin
186            $fn = wikiFN($data['name']);
187            $perm = auth_quickaclcheck($data['name']);
188            array_unshift($page_stack, array(
189                'id' => $data['name'],
190                'rev' => @filemtime($fn),
191                'writable' => (page_exists($data['name']) ? (is_writable($fn) && $perm >= AUTH_EDIT) : $perm >= AUTH_CREATE),
192                'redirect' => ($data['target'] == 'plugin_include_start'),
193            ));
194        } elseif ($data['target'] == 'plugin_include_end') {
195            array_shift($page_stack);
196        } elseif ($data['target'] == 'plugin_include_editbtn') {
197            if ($page_stack[0]['writable']) {
198                $params = array('do' => 'edit',
199                    'id' => $page_stack[0]['id']);
200                if ($page_stack[0]['redirect'])
201                    $params['redirect_id'] = $ID;
202                $event->result = '<div class="secedit">' . DOKU_LF .
203                    html_btn('incledit', $page_stack[0]['id'], '',
204                        $params, 'post',
205                        $data['name'],
206                        $lang['btn_secedit'].' ('.$page_stack[0]['id'].')') .
207                    '</div>' . DOKU_LF;
208            }
209        } elseif (!empty($page_stack)) {
210
211            // Special handling for the edittable plugin
212            if ($data['target'] == 'table' && !plugin_isdisabled('edittable')) {
213                /* @var action_plugin_edittable $edittable */
214                $edittable =& plugin_load('action', 'edittable');
215                $data['name'] = $edittable->getLang('secedit_name');
216            }
217
218            if ($page_stack[0]['writable'] && isset($data['name']) && $data['name'] !== '') {
219                $name = $data['name'];
220                unset($data['name']);
221
222                $secid = $data['secid'];
223                unset($data['secid']);
224
225                if ($page_stack[0]['redirect'])
226                    $data['redirect_id'] = $ID;
227
228                $event->result = "<div class='secedit editbutton_" . $data['target'] .
229                    " editbutton_" . $secid . "'>" .
230                    html_btn('secedit', $page_stack[0]['id'], '',
231                        array_merge(array('do'  => 'edit',
232                        'rev' => $page_stack[0]['rev'],
233                        'summary' => '['.$name.'] '), $data),
234                        'post', $name) . '</div>';
235            } else {
236                $event->result = '';
237            }
238        } else {
239            return; // return so the event won't be stopped
240        }
241
242        $event->preventDefault();
243        $event->stopPropagation();
244    }
245
246    public function handle_editx_register(Doku_Event $event, $params) {
247        $event->data['handlers']['include_include'] = array($this, 'rewrite_include');
248    }
249
250    public function rewrite_include($match, $pos, $state, $plugin, action_plugin_editx_handler $handler) {
251        $syntax = substr($match, 2, -2); // strip markup
252        $replacers = explode('|', $syntax);
253        $syntax = array_shift($replacers);
254        list($syntax, $flags) = explode('&', $syntax, 2);
255
256        // break the pattern up into its parts
257        list($mode, $page, $sect) = preg_split('/>|#/u', $syntax, 3);
258        $newpage = $handler->adaptRelativeId($page);
259        if ($newpage == $page) {
260            return $match;
261        } else {
262            $result = '{{'.$mode.'>'.$newpage;
263            if ($sect) $result .= '#'.$sect;
264            if ($flags) $result .= '&'.$flags;
265            if ($replacers) $result .= '|'.$replacers;
266            $result .= '}}';
267            return $result;
268        }
269    }
270}
271// vim:ts=4:sw=4:et:
272