xref: /plugin/include/action.php (revision 84629dd2f7fc0eb7a5c22103363a26dd5b086cf0)
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    }
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 $conf;
113
114        $cache =& $event->data;
115
116        if(!isset($cache->page)) return;
117        if(!isset($cache->mode) || !in_array($cache->mode, $this->supportedModes)) return;
118
119        $depends = p_get_metadata($cache->page, 'plugin_include');
120
121        if($conf['allowdebug']) {
122            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS START ----');
123            dbglog($depends);
124            dbglog('---- PLUGIN INCLUDE CACHE DEPENDS END ----');
125        }
126
127        if (!is_array($depends)) return; // nothing to do for us
128
129        if (!is_array($depends['pages']) ||
130            !is_array($depends['instructions']) ||
131            $depends['pages'] != $this->helper->_get_included_pages_from_meta_instructions($depends['instructions'])) {
132
133            $cache->depends['purge'] = true; // included pages changed or old metadata - request purge.
134            if($conf['allowdebug']) {
135                dbglog('---- PLUGIN INCLUDE: REQUESTING CACHE PURGE ----');
136                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META START ----');
137                dbglog($depends['pages']);
138                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META END ----');
139                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS START ----');
140                dbglog($this->helper->_get_included_pages_from_meta_instructions($depends['instructions']));
141                dbglog('---- PLUGIN INCLUDE CACHE PAGES FROM META_INSTRUCTIONS END ----');
142
143            }
144        } else {
145            // add plugin.info.txt to depends for nicer upgrades
146            $cache->depends['files'][] = dirname(__FILE__) . '/plugin.info.txt';
147            foreach ($depends['pages'] as $page) {
148                if (!$page['exists']) continue;
149                $file = wikiFN($page['id']);
150                if (!in_array($file, $cache->depends['files'])) {
151                    $cache->depends['files'][] = $file;
152                }
153            }
154        }
155    }
156
157}
158// vim:ts=4:sw=4:et:
159