xref: /plugin/include/action.php (revision 993fccf38cd0351b20d2a1dea45a0629289eca85)
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        $event->data['id'] = cleanID($_REQUEST['redirect_id']);
105        $event->data['title'] = '';
106      }
107    }
108
109    /**
110     * prepare the cache object for default _useCache action
111     */
112    function _cache_prepare(&$event, $param) {
113        global $conf;
114
115        $cache =& $event->data;
116
117        if(!isset($cache->page)) return;
118        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
119
120        $depends = p_get_metadata($cache->page, 'plugin_include');
121
122        if($conf['allowdebug']) {
123            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
124            dbglog($depends);
125            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
126        }
127
128        if (!is_array($depends)) return; // nothing to do for us
129
130        if (!is_array($depends['pages']) ||
131            !is_array($depends['instructions']) ||
132            $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions'])) {
133
134            $cache->depends['purge'] = true; // included pages changed or old metadata - request purge.
135            if($conf['allowdebug']) {
136                dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----');
137                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----');
138                dbglog($depends['pages']);
139                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----');
140                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----');
141                dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions']));
142                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----');
143
144            }
145        } else {
146            // add plugin.info.txt to depends for nicer upgrades
147            $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
148            foreach ($depends['pages'] as $page) {
149                if (!$page['exists']) continue;
150                $file = wikiFN($page['id']);
151                if (!in_array($file, $cache->depends['files'])) {
152                    $cache->depends['files'][] = $file;
153                }
154            }
155        }
156    }
157
158    function handle_secedit_button(&$event, $params) {
159        // stack of included pages in the form ('id' => page, 'rev' => modification time, 'can_edit' => bool)
160        static $page_stack = array();
161
162        global $ID;
163
164        $data = $event->data;
165
166        if ($data['target'] == 'plugin_include_start') {
167            // handle the "section edits" added by the include plugin
168            $fn = wikiFN($data['name']);
169            array_unshift($page_stack, array(
170                'id' => $data['name'],
171                'rev' => @filemtime($fn),
172                'writable' => (is_writable($fn) && auth_quickaclcheck($data['name']) >= AUTH_EDIT)
173            ));
174        } elseif ($data['target'] == 'plugin_include_end') {
175            array_shift($page_stack);
176        } elseif (!empty($page_stack)) {
177            if ($page_stack[0]['writable']) {
178                $name = $data['name'];
179                unset($data['name']);
180
181                $secid = $data['secid'];
182                unset($data['secid']);
183
184                $data['redirect_id'] = $ID;
185
186                $event->result = "<div class='secedit editbutton_" . $data['target'] .
187                    " editbutton_" . $secid . "'>" .
188                    html_btn('secedit', $page_stack[0]['id'], '',
189                        array_merge(array('do'  => 'edit',
190                        'rev' => $page_stack[0]['rev'],
191                        'summary' => '['.$name.'] '), $data),
192                        'post', $name) . '</div>';
193            } else {
194                $event->result = '';
195            }
196        } else {
197            return; // return so the event won't be stopped
198        }
199
200        $event->preventDefault();
201        $event->stopPropagation();
202    }
203}
204// vim:ts=4:sw=4:et:
205