xref: /plugin/include/syntax/include.php (revision 993fccf38cd0351b20d2a1dea45a0629289eca85)
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 = null;
28
29    function getType() { return 'substition'; }
30    function getSort() { return 303; }
31    function getPType() { return 'block'; }
32
33    function connectTo($mode) {
34        $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include');
35        $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include');
36        $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include');
37        $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include');
38    }
39
40    function handle($match, $state, $pos, &$handler) {
41
42        $match = substr($match, 2, -2); // strip markup
43        list($match, $flags) = explode('&', $match, 2);
44
45        // break the pattern up into its parts
46        list($mode, $page, $sect) = preg_split('/>|#/u', $match, 3);
47        return array($mode, $page, cleanID($sect), explode('&', $flags));
48    }
49
50    /**
51     * Renders the included page(s)
52     *
53     * @author Michael Hamann <michael@content-space.de>
54     */
55    function render($format, &$renderer, $data) {
56        global $ID, $conf;
57
58        // static stack that records all ancestors of the child pages
59        static $page_stack = array();
60
61        // when there is no id just assume the global $ID is the current id
62        if (empty($page_stack)) $page_stack[] = $ID;
63
64        $parent_id = $page_stack[count($page_stack)-1];
65        $root_id = $page_stack[0];
66
67        list($mode, $page, $sect, $flags, $level) = $data;
68
69        if (!$this->helper)
70            $this->helper =& plugin_load('helper', 'include');
71        $flags = $this->helper->get_flags($flags);
72
73        $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id);
74
75        if ($format == 'metadata') {
76            $renderer->meta['plugin_include']['instructions'][] = array('mode' => $mode, 'page' => $page, 'sect' => $sect, 'parent_id' => $parent_id);
77            if (!isset($renderer->meta['plugin_include']['pages']))
78               $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge
79            $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages);
80        }
81
82        foreach ($pages as $page) {
83            extract($page);
84
85            if (in_array($id, $page_stack)) continue;
86            array_push($page_stack, $id);
87
88            if(!$exists) {
89                if($flags['footer']) {
90                    $footer_ins = array();
91                    if($flags['editbtn'] && (auth_quickaclcheck($id) >= AUTH_EDIT)) {
92                        $this->helper->_editbtn($footer_ins, $id, $sect, '', $root_id);
93                    }
94                    $footer_ins[] = $this->helper->_footer($id, $sect, '', $flags, $level, $root_id);
95                    $renderer->nest($footer_ins);
96                }
97            } else {
98                $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id);
99
100                if ($format == 'xhtml') {
101                    $renderer->startSectionEdit(0, 'plugin_include_start', $id);
102                    $renderer->finishSectionEdit();
103                    // Start a new section with type != section so headers in the included page
104                    // won't print section edit buttons of the parent page
105                    $renderer->startSectionEdit(0, 'plugin_include_end', $id);
106                }
107
108                $renderer->nest($instructions);
109
110                if ($format == 'xhtml') {
111                   $renderer->finishSectionEdit();
112                }
113            }
114
115            array_pop($page_stack);
116        }
117
118        // When all includes have been handled remove the current id
119        // in order to allow the rendering of other pages
120        if (count($page_stack) == 1) array_pop($page_stack);
121
122        return true;
123    }
124}
125// vim:ts=4:sw=4:et:
126