xref: /plugin/include/syntax/include.php (revision 227524822951ff25892f1267dc2b91b59ee5c3d8)
1<?php
2/**
3 * Include Plugin: displays a wiki page within another
4 * Usage:
5 * {{page>page}} for "page" in same namespace
6 * {{page>:page}} for "page" in top namespace
7 * {{page>namespace:page}} for "page" in namespace "namespace"
8 * {{page>.namespace:page}} for "page" in subnamespace "namespace"
9 * {{page>page#section}} for a section of "page"
10 *
11 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
12 * @author     Esther Brunner <wikidesign@gmail.com>
13 * @author     Christopher Smith <chris@jalakai.co.uk>
14 * @author     Gina Häußge, Michael Klier <dokuwiki@chimeric.de>
15 */
16
17if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
18if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19require_once(DOKU_PLUGIN.'syntax.php');
20
21/**
22 * All DokuWiki plugins to extend the parser/rendering mechanism
23 * need to inherit from this class
24 */
25class syntax_plugin_include_include extends DokuWiki_Syntax_Plugin {
26
27    /** @var $helper helper_plugin_include */
28    var $helper = null;
29
30    /**
31     * Get syntax plugin type.
32     *
33     * @return string The plugin type.
34     */
35    function getType() { return 'substition'; }
36
37    /**
38     * Get sort order of syntax plugin.
39     *
40     * @return int The sort order.
41     */
42    function getSort() { return 303; }
43
44    /**
45     * Get paragraph type.
46     *
47     * @return string The paragraph type.
48     */
49    function getPType() { return 'block'; }
50
51    /**
52     * Connect patterns/modes
53     *
54     * @param $mode mixed The current mode
55     */
56    function connectTo($mode) {
57        $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include');
58        $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include');
59        $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include');
60        $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include');
61    }
62
63    /**
64     * Handle syntax matches
65     *
66     * @param string       $match   The current match
67     * @param int          $state   The match state
68     * @param int          $pos     The position of the match
69     * @param Doku_Handler $handler The hanlder object
70     * @return array The instructions of the plugin
71     */
72    function handle($match, $state, $pos, Doku_Handler $handler) {
73
74        $match = substr($match, 2, -2); // strip markup
75        list($match, $flags) = explode('&', $match, 2);
76
77        // break the pattern up into its parts
78        list($mode, $page, $sect) = preg_split('/>|#/u', $match, 3);
79        $check = false;
80        if (isset($sect)) $sect = sectionID($sect, $check);
81        $level = NULL;
82        return array($mode, $page, $sect, explode('&', $flags), $level, $pos);
83    }
84
85    /**
86     * Renders the included page(s)
87     *
88     * @author Michael Hamann <michael@content-space.de>
89     */
90    function render($format, Doku_Renderer $renderer, $data) {
91        global $ID;
92
93        // static stack that records all ancestors of the child pages
94        static $page_stack = array();
95
96        // when there is no id just assume the global $ID is the current id
97        if (empty($page_stack)) $page_stack[] = $ID;
98
99        $parent_id = $page_stack[count($page_stack)-1];
100        $root_id = $page_stack[0];
101
102        list($mode, $page, $sect, $flags, $level, $pos) = $data;
103
104        if (!$this->helper)
105            $this->helper = plugin_load('helper', 'include');
106        $flags = $this->helper->get_flags($flags);
107
108        $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id, $flags);
109
110        if ($format == 'metadata') {
111            /** @var Doku_Renderer_metadata $renderer */
112
113            // remove old persistent metadata of previous versions of the include plugin
114            if (isset($renderer->persistent['plugin_include'])) {
115                unset($renderer->persistent['plugin_include']);
116                unset($renderer->meta['plugin_include']);
117            }
118
119            $renderer->meta['plugin_include']['instructions'][] = compact('mode', 'page', 'sect', 'parent_id', $flags);
120            if (!isset($renderer->meta['plugin_include']['pages']))
121               $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge
122            $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages);
123            $renderer->meta['plugin_include']['include_content'] = isset($_REQUEST['include_content']);
124        }
125
126        $secids = array();
127        if ($format == 'xhtml' || $format == 'odt') {
128            $secids = p_get_metadata($ID, 'plugin_include secids');
129        }
130
131        foreach ($pages as $page) {
132            extract($page);
133            $id = $page['id'];
134            $exists = $page['exists'];
135
136            if (in_array($id, $page_stack)) continue;
137            array_push($page_stack, $id);
138
139            // add references for backlink
140            if ($format == 'metadata') {
141                $renderer->meta['relation']['references'][$id] = $exists;
142                $renderer->meta['relation']['haspart'][$id]    = $exists;
143                if (!$sect && !$flags['firstsec'] && !$flags['linkonly'] && !isset($renderer->meta['plugin_include']['secids'][$id])) {
144                    $renderer->meta['plugin_include']['secids'][$id] = array('hid' => 'plugin_include__'.str_replace(':', '__', $id), 'pos' => $pos);
145                }
146            }
147
148            if (isset($secids[$id]) && $pos === $secids[$id]['pos']) {
149                $flags['include_secid'] = $secids[$id]['hid'];
150            } else {
151                unset($flags['include_secid']);
152            }
153
154            $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id, $secids);
155
156            $renderer->nest($instructions);
157
158            array_pop($page_stack);
159        }
160
161        // When all includes have been handled remove the current id
162        // in order to allow the rendering of other pages
163        if (count($page_stack) == 1) array_pop($page_stack);
164
165        return true;
166    }
167}
168// vim:ts=4:sw=4:et:
169