xref: /plugin/include/syntax/include.php (revision 1a25f14b28693cdcc54f732630b8204c660d5108)
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    var $taghelper = null;
29
30    function getType() { return 'substition'; }
31    function getSort() { return 303; }
32    function getPType() { return 'block'; }
33
34    function connectTo($mode) {
35        $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include');
36        $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include');
37        $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include');
38        $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include');
39    }
40
41    function handle($match, $state, $pos, &$handler) {
42
43        $match = substr($match, 2, -2); // strip markup
44        list($match, $flags) = explode('&', $match, 2);
45
46        // break the pattern up into its parts
47        list($mode, $page, $sect) = preg_split('/>|#/u', $match, 3);
48        return array($mode, $page, cleanID($sect), explode('&', $flags));
49    }
50
51    /**
52     * Renders the included page(s)
53     *
54     * @author Michael Hamann <michael@content-space.de>
55     */
56    function render($format, &$renderer, $data) {
57        global $ID, $conf;
58
59        // static stack that records all ancestors of the child pages
60        static $page_stack = array();
61
62        // when there is no id just assume the global $ID is the current id
63        if (empty($page_stack)) $page_stack[] = $ID;
64
65        $parent_id = $page_stack[count($page_stack)-1];
66        $root_id = $page_stack[0];
67
68        list($mode, $page, $sect, $flags, $level) = $data;
69
70        if (!$this->helper)
71            $this->helper =& plugin_load('helper', 'include');
72        $flags = $this->helper->get_flags($flags);
73
74        $pages = array();
75        switch($mode) {
76        case 'namespace':
77            $ns    = str_replace(':', '/', cleanID($page));
78            search($pagearrays, $conf['datadir'], 'search_list', '', $ns);
79            if (is_array($pagearrays)) {
80                foreach ($pagearrays as $pagearray) {
81                    $pages[] = $pagearray['id'];
82                }
83            }
84            sort($pages);
85            break;
86        case 'tagtopic':
87            if (!$this->taghelper)
88                $this->taghelper =& plugin_load('helper', 'tag');
89            if(!$this->taghelper) {
90                msg('You have to install the tag plugin to use this functionality!', -1);
91                return;
92            }
93            $tag   = $page;
94            $sect  = '';
95            $pagearrays = $this->taghelper->getTopic('', null, $tag);
96            foreach ($pagearrays as $pagearray) {
97                $pages[] = $pagearray['id'];
98            }
99            break;
100        default:
101            $page = cleanID($this->helper->_apply_macro($page));
102            resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts
103            $pages[] = $page;
104        }
105
106        foreach ($pages as $page) {
107            if (in_array($page, $page_stack)) continue;
108            array_push($page_stack, $page);
109
110            if($format == 'metadata') {
111                $renderer->meta['plugin_include']['pages'][] = $page; // FIXME: record raw id so caching can check if the current replacements still match
112                // recording all included pages might make sense for metadata cache updates, though really knowing all included pages
113                // probably means we just need to always purge the metadata cache or move rendered metadata to other cache files that depend on user/groups
114                // for namespace/tag includes we probably need to purge the cache everytime so they should be recorded in the metadata so we know when that's necessary
115            }
116
117            $perm = auth_quickaclcheck($page);
118
119            if($perm < AUTH_READ) continue;
120
121            if(!page_exists($page)) {
122                if($flags['footer']) {
123                    $renderer->nest(array($this->helper->_footer($page, $sect, '', $flags, $level, $root_id)));
124                }
125            } else {
126                $instructions = $this->helper->_get_instructions($page, $sect, $mode, $level, $flags, $root_id);
127                $renderer->nest($instructions);
128            }
129
130            array_pop($page_stack);
131        }
132
133        // When all includes have been handled remove the current id
134        // in order to allow the rendering of other pages
135        if (count($page_stack) == 1) array_pop($page_stack);
136
137        return true;
138    }
139}
140// vim:ts=4:sw=4:et:enc=utf-8:
141