xref: /plugin/include/syntax/include.php (revision 61053b0410e7270611fe8e396b5a81b1441e1a07)
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 getInfo() {
30        return array(
31                'author' => 'Gina Häußge, Michael Klier, Esther Brunner',
32                'email'  => 'dokuwiki@chimeric.de',
33                'date'   => @file_get_contents(DOKU_PLUGIN . 'blog/VERSION'),
34                'name'   => 'Include Plugin',
35                'desc'   => 'Displays a wiki page (or a section thereof) within another',
36                'url'    => 'http://wiki.splitbrain.org/plugin:include',
37                );
38    }
39
40    function getType() { return 'substition'; }
41    function getSort() { return 303; }
42    function getPType() { return 'block'; }
43
44    function connectTo($mode) {
45        $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include');
46        $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include');
47    }
48
49    function handle($match, $state, $pos, &$handler) {
50
51        $match = substr($match, 2, -2); // strip markup
52        list($match, $flags) = explode('&', $match, 2);
53
54        // break the pattern up into its parts
55        list($include, $id, $section) = preg_split('/>|#/u', $match, 3);
56        return array($include, $id, cleanID($section), explode('&', $flags));
57    }
58
59    function render($format, &$renderer, $data) {
60        global $ID;
61
62        list($type, $raw_id, $section, $flags, $lvl, $toc) = $data;
63
64        $id = $this->_applyMacro($raw_id);
65        $nocache = ($id != $raw_id);
66
67        resolve_pageid(getNS($ID), $id, $exists); // resolve shortcuts
68
69        if ($nocache) $renderer->info['cache'] = false;                 // prevent caching
70        if (AUTH_READ > auth_quickaclcheck($id)) return true;           // check for permission
71
72        $this->helper =& plugin_load('helper', 'include');
73
74        $this->helper->setMode($type);
75        $this->helper->setFlags($flags);
76
77        if (!$this->helper->setPage(compact('type','id','section','exists'))) {
78            return false;
79        }
80
81        // handle render formats
82        switch($format) {
83            case 'xhtml':
84
85                // check for toc to prepend eventually
86                if(!empty($toc)) {
87                    foreach($toc as $data) {
88                        $item = array();
89                        $item['hid'] = $renderer->_headerToLink($data[0], 'true');
90                        $item['title'] = $data[0];
91                        $item['type'] = ul;
92                        $item['level'] = $data[1];
93                        array_push($this->helper->toc, $item);
94                    }
95                }
96
97
98                $this->helper->setLevel($lvl);
99
100                // close current section
101                if ($lvl && ($type == 'section')) $renderer->doc .= '</div>';
102
103                // include the page
104                $this->helper->renderXHTML($renderer, $info);
105
106                // propagate any cache prevention from included pages into this page
107                if ($info['cache'] == false) $renderer->info['cache'] = false;
108
109                // resume current section
110                if ($lvl && ($type == 'section')) $renderer->doc .= '<div class="level'.$lvl.'">';
111
112                return true;
113                break;
114
115            case 'odt':
116
117                // current section level
118                $clevel = 0;
119                preg_match_all('|<text:h text:style-name="Heading_20_\d" text:outline-level="(\d)">|i', $renderer->doc, $matches, PREG_SET_ORDER);
120                $n = count($matches) - 1;
121                if ($n > -1) $clevel = $matches[$n][1];
122                $this->helper->setLevel($clevel);
123
124                // include the page now
125                $this->helpeer->renderODT($renderer);
126
127                return true;
128                break;
129
130            case 'metadata':
131                if (!$nocache) {
132                    $renderer->meta['relation']['haspart'][$id] = @file_exists(wikiFN($id));
133                }
134                return true;
135                break;
136            default;
137                return false;
138                break;
139        }
140    }
141
142    /**
143     * Makes user or date dependent includes possible
144     */
145    function _applyMacro($id) {
146        global $INFO, $auth;
147
148        // if we don't have an auth object, do nothing
149        if (!$auth)
150        	return $id;
151
152        $user     = $_SERVER['REMOTE_USER'];
153        $userdata = $auth->getUserData($user);
154        $group    = $userdata['grps'][0];
155
156        $replace = array(
157                '@USER@'  => cleanID($user),
158                '@NAME@'  => cleanID($INFO['userinfo']['name']),
159                '@GROUP@' => cleanID($group),
160                '@YEAR@'  => date('Y'),
161                '@MONTH@' => date('m'),
162                '@DAY@'   => date('d'),
163                );
164        return str_replace(array_keys($replace), array_values($replace), $id);
165    }
166}
167// vim:ts=4:sw=4:et:enc=utf-8:
168