xref: /plugin/include/syntax/include.php (revision 5534e4eb838b0c903560952ad99ac9c875f0ae4a)
171ec1101SGina Haeussge<?php
271ec1101SGina Haeussge/**
371ec1101SGina Haeussge * Include Plugin: displays a wiki page within another
471ec1101SGina Haeussge * Usage:
571ec1101SGina Haeussge * {{page>page}} for "page" in same namespace
671ec1101SGina Haeussge * {{page>:page}} for "page" in top namespace
771ec1101SGina Haeussge * {{page>namespace:page}} for "page" in namespace "namespace"
871ec1101SGina Haeussge * {{page>.namespace:page}} for "page" in subnamespace "namespace"
971ec1101SGina Haeussge * {{page>page#section}} for a section of "page"
1071ec1101SGina Haeussge *
1171ec1101SGina Haeussge * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
1271ec1101SGina Haeussge * @author     Esther Brunner <wikidesign@gmail.com>
1371ec1101SGina Haeussge * @author     Christopher Smith <chris@jalakai.co.uk>
1471ec1101SGina Haeussge * @author     Gina Häußge, Michael Klier <dokuwiki@chimeric.de>
1571ec1101SGina Haeussge */
1671ec1101SGina Haeussge
1771ec1101SGina Haeussgeif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
1871ec1101SGina Haeussgeif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
1971ec1101SGina Haeussgerequire_once(DOKU_PLUGIN.'syntax.php');
2071ec1101SGina Haeussge
2171ec1101SGina Haeussge/**
2271ec1101SGina Haeussge * All DokuWiki plugins to extend the parser/rendering mechanism
2371ec1101SGina Haeussge * need to inherit from this class
2471ec1101SGina Haeussge */
2571ec1101SGina Haeussgeclass syntax_plugin_include_include extends DokuWiki_Syntax_Plugin {
2671ec1101SGina Haeussge
2761053b04SMichael Klier    var $helper = null;
2861053b04SMichael Klier
2971ec1101SGina Haeussge    function getType() { return 'substition'; }
3071ec1101SGina Haeussge    function getSort() { return 303; }
3171ec1101SGina Haeussge    function getPType() { return 'block'; }
3271ec1101SGina Haeussge
3371ec1101SGina Haeussge    function connectTo($mode) {
3471ec1101SGina Haeussge        $this->Lexer->addSpecialPattern("{{page>.+?}}", $mode, 'plugin_include_include');
3571ec1101SGina Haeussge        $this->Lexer->addSpecialPattern("{{section>.+?}}", $mode, 'plugin_include_include');
368b99501bSMichael Klier        $this->Lexer->addSpecialPattern("{{namespace>.+?}}", $mode, 'plugin_include_include');
37a0a6f8fbSMichael Klier        $this->Lexer->addSpecialPattern("{{tagtopic>.+?}}", $mode, 'plugin_include_include');
3871ec1101SGina Haeussge    }
3971ec1101SGina Haeussge
4071ec1101SGina Haeussge    function handle($match, $state, $pos, &$handler) {
4171ec1101SGina Haeussge
4271ec1101SGina Haeussge        $match = substr($match, 2, -2); // strip markup
4371ec1101SGina Haeussge        list($match, $flags) = explode('&', $match, 2);
4471ec1101SGina Haeussge
4561053b04SMichael Klier        // break the pattern up into its parts
466f0ad9d7SMichael Klier        list($mode, $page, $sect) = preg_split('/>|#/u', $match, 3);
477475482bSMichael Hamann        $check = null;
48*5534e4ebSMichael Hamann        if (isset($sect)) $sect = sectionID($sect, $check);
49*5534e4ebSMichael Hamann        return array($mode, $page, $sect, explode('&', $flags));
5071ec1101SGina Haeussge    }
5171ec1101SGina Haeussge
521a25f14bSMichael Hamann    /**
531a25f14bSMichael Hamann     * Renders the included page(s)
541a25f14bSMichael Hamann     *
551a25f14bSMichael Hamann     * @author Michael Hamann <michael@content-space.de>
561a25f14bSMichael Hamann     */
5771ec1101SGina Haeussge    function render($format, &$renderer, $data) {
581a25f14bSMichael Hamann        global $ID, $conf;
591a25f14bSMichael Hamann
601a25f14bSMichael Hamann        // static stack that records all ancestors of the child pages
611a25f14bSMichael Hamann        static $page_stack = array();
621a25f14bSMichael Hamann
631a25f14bSMichael Hamann        // when there is no id just assume the global $ID is the current id
641a25f14bSMichael Hamann        if (empty($page_stack)) $page_stack[] = $ID;
651a25f14bSMichael Hamann
661a25f14bSMichael Hamann        $parent_id = $page_stack[count($page_stack)-1];
671a25f14bSMichael Hamann        $root_id = $page_stack[0];
681a25f14bSMichael Hamann
691a25f14bSMichael Hamann        list($mode, $page, $sect, $flags, $level) = $data;
701a25f14bSMichael Hamann
711a25f14bSMichael Hamann        if (!$this->helper)
721a25f14bSMichael Hamann            $this->helper =& plugin_load('helper', 'include');
731a25f14bSMichael Hamann        $flags = $this->helper->get_flags($flags);
741a25f14bSMichael Hamann
75791e1550SMichael Hamann        $pages = $this->helper->_get_included_pages($mode, $page, $sect, $parent_id);
76791e1550SMichael Hamann
77791e1550SMichael Hamann        if ($format == 'metadata') {
78560e27a0SMichael Hamann
79560e27a0SMichael Hamann            // remove old persistent metadata of previous versions of the include plugin
80560e27a0SMichael Hamann            if (isset($renderer->persistent['plugin_include'])) {
81560e27a0SMichael Hamann                unset($renderer->persistent['plugin_include']);
82560e27a0SMichael Hamann                unset($renderer->meta['plugin_include']);
83560e27a0SMichael Hamann            }
84560e27a0SMichael Hamann
85d5b0d6ceSMichael Hamann            $renderer->meta['plugin_include']['instructions'][] = compact('mode', 'page', 'sect', 'parent_id');
86791e1550SMichael Hamann            if (!isset($renderer->meta['plugin_include']['pages']))
87791e1550SMichael Hamann               $renderer->meta['plugin_include']['pages'] = array(); // add an array for array_merge
88791e1550SMichael Hamann            $renderer->meta['plugin_include']['pages'] = array_merge($renderer->meta['plugin_include']['pages'], $pages);
89b11b2b9dSMichael Hamann            $renderer->meta['plugin_include']['include_content'] = isset($_REQUEST['include_content']);
901a25f14bSMichael Hamann        }
911a25f14bSMichael Hamann
921a25f14bSMichael Hamann        foreach ($pages as $page) {
93791e1550SMichael Hamann            extract($page);
941a25f14bSMichael Hamann
95791e1550SMichael Hamann            if (in_array($id, $page_stack)) continue;
96791e1550SMichael Hamann            array_push($page_stack, $id);
971a25f14bSMichael Hamann
986db19c84SMichael Hamann            // add references for backlink
996db19c84SMichael Hamann            if ($format == 'metadata')
1006db19c84SMichael Hamann                $renderer->meta['relation']['references'][$id] = $exists;
1016db19c84SMichael Hamann
102791e1550SMichael Hamann            $instructions = $this->helper->_get_instructions($id, $sect, $mode, $level, $flags, $root_id);
103993fccf3SMichael Hamann
1041a25f14bSMichael Hamann            $renderer->nest($instructions);
105993fccf3SMichael Hamann
1061a25f14bSMichael Hamann            array_pop($page_stack);
1071a25f14bSMichael Hamann        }
1081a25f14bSMichael Hamann
1091a25f14bSMichael Hamann        // When all includes have been handled remove the current id
1101a25f14bSMichael Hamann        // in order to allow the rendering of other pages
1111a25f14bSMichael Hamann        if (count($page_stack) == 1) array_pop($page_stack);
1121a25f14bSMichael Hamann
1131a25f14bSMichael Hamann        return true;
11461053b04SMichael Klier    }
11571ec1101SGina Haeussge}
1162524d407SMichael Hamann// vim:ts=4:sw=4:et:
117