xref: /plugin/include/syntax/include.php (revision 9c0b18fdf8201e8b075b124d9e2d035d3f8c3858)
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, &$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 = null;
80        if (isset($sect)) $sect = sectionID($sect, $check);
81        return array($mode, $page, $sect, explode('&', $flags));
82    }
83
84    /**
85     * Renders the included page(s)
86     *
87     * @author Michael Hamann <michael@content-space.de>
88     */
89    function render($format, &$renderer, $data) {
90        global $ID, $conf;
91
92        // static stack that records all ancestors of the child pages
93        static $page_stack = array();
94
95        // when there is no id just assume the global $ID is the current id
96        if (empty($page_stack)) $page_stack[] = $ID;
97
98        $parent_id = $page_stack[count($page_stack)-1];
99        $root_id = $page_stack[0];
100
101        list($mode, $page, $sect, $flags, $level) = $data;
102
103        if (!$this->helper)
104            $this->helper =& plugin_load('helper', 'include');
105        $flags = $this->helper->get_flags($flags);
106
107        $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id);
108
109        if ($format == 'metadata') {
110            /** @var Doku_Renderer_metadata $renderer */
111
112            // remove old persistent metadata of previous versions of the include plugin
113            if (isset($renderer->persistent['plugin_include'])) {
114                unset($renderer->persistent['plugin_include']);
115                unset($renderer->meta['plugin_include']);
116            }
117
118            $renderer->meta['plugin_include']['instructions'][] = compact('mode', 'page', 'sect', 'parent_id');
119            if (!isset($renderer->meta['plugin_include']['pages']))
120               $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge
121            $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages);
122            $renderer->meta['plugin_include']['include_content'] = isset($_REQUEST['include_content']);
123        }
124
125        foreach ($pages as $page) {
126            extract($page);
127            $id = $page['id'];
128            $exists = $page['exists'];
129
130            if (in_array($id, $page_stack)) continue;
131            array_push($page_stack, $id);
132
133            // add references for backlink
134            if ($format == 'metadata') {
135                $renderer->meta['relation']['references'][$id] = $exists;
136                $renderer->meta['relation']['haspart'][$id]    = $exists;
137            }
138
139            $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id);
140
141            $renderer->nest($instructions);
142
143            array_pop($page_stack);
144        }
145
146        // When all includes have been handled remove the current id
147        // in order to allow the rendering of other pages
148        if (count($page_stack) == 1) array_pop($page_stack);
149
150        return true;
151    }
152}
153// vim:ts=4:sw=4:et:
154