1<?php
2
3/**
4 * Select Template Pages for your Content
5 * The templates Pages have to have the entry @@CONTENT@@
6 * the template per page can be defined using the META plugin
7 *
8 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     i-net software <tools@inetsoftware.de>
10 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
11 */
12
13// must be run within Dokuwiki
14if (!defined('DOKU_INC'))
15    die();
16
17if (!defined('DOKU_LF'))
18    define('DOKU_LF', "\n");
19if (!defined('DOKU_TAB'))
20    define('DOKU_TAB', "\t");
21if (!defined('DOKU_PLUGIN'))
22    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
23
24require_once (DOKU_PLUGIN . 'action.php');
25require_once(DOKU_INC . 'inc/pageutils.php');
26
27class action_plugin_pagetemplater extends DokuWiki_Action_Plugin {
28
29    /**
30     * Register the eventhandlers.
31     */
32    function register(Doku_Event_Handler $controller) {
33        $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_content_display', array ());
34        $controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'handle_meta_data', array ());
35    }
36
37    function handle_content_display(& $event, $params) {
38        global $ACT, $INFO, $TOC, $ID;
39
40        $template = $this->resolve_template();
41        if ( !$template || $ACT != 'show' ) { return; }
42
43        $oldtoc = $TOC;
44        $template = p_wiki_xhtml( $template );
45
46        // set the replacements
47        $replace = $INFO['meta']['templater'];
48        unset($replace['page']);
49        $replace['content'] = $event->data;
50        $replace['page'] = $ID;
51        $replace['namespace'] = getNS($ID);
52
53        $new = $template;
54        foreach (array_keys($replace) as $key) {
55            if ( $new != $template ) { $template = $new; }
56            if ( $key != 'content' && substr($key, 0, 1) == '!' ) {
57                $rkey = substr($key, 1);
58                $replace[$key] = p_render('xhtml', p_get_instructions($replace[$key]),$info);
59            } else { $rkey = $key; }
60            $new = str_replace('@@' . strtoupper(trim($rkey)) . '@@', $replace[$key], $template);
61            $new = str_replace(urlencode('@@') . strtoupper(trim($rkey)) . urlencode('@@'), $replace[$key], $new);
62        }
63
64        if ( $new != $event->data ) {
65            $event->data = $new;
66        }
67
68        $TOC = $oldtoc;
69
70        $data = array('xhtml',& $event->data);
71        trigger_event('RENDERER_CONTENT_POSTPROCESS',$data);
72
73        return true;
74    }
75
76    function handle_meta_data(& $event, $params) {
77        global $ACT;
78
79        $id = getId();
80        if ( $id != $event->data['page'] ) { return true; }
81        $template = $this->resolve_template( $event->data['current']['templater'] );
82        if ( empty( $template) || in_array($template, array( $id, $event->data['page']) ) ) { return true; }
83
84        $meta = p_get_metadata( $template, '', METADATA_RENDER_UNLIMITED );
85
86
87        if ( !$event->data['current']['internal'] || !is_array($event->data['current']['internal']) ) $event->data['current']['internal'] = array();
88        if ( !is_array($meta['internal']) ) $meta['internal'] = array();
89        $event->data['current']['internal'] = array_merge($event->data['current']['internal'], $meta['internal']);
90
91        if ( !$event->data['current']['toc'] || !is_array($event->data['current']['toc']) ) $event->data['current']['toc'] = array();
92        if ( !is_array($meta['toc']) ) $meta['toc'] = array();
93        $event->data['current']['toc'] = array_merge($event->data['current']['toc'], $meta['toc']);
94
95        return true;
96    }
97
98    private function resolve_template( $templater = array() ) {
99        global $INFO;
100
101        $page = empty($INFO['meta']['templater']['page']) ? $templater['page'] : $INFO['meta']['templater']['page'];
102
103        // are we in an avtive Namespace?
104        $template = $this->_getActiveNamespace();
105        if ( !$template && empty( $page ) ) return;
106
107        return empty( $page ) ? $template : $page;
108    }
109
110    function _getActiveNamespace() {
111        global $ID;
112        global $INFO;
113
114        $pattern = $this->getConf('excluded_pages');
115        if (strlen($pattern) > 0 && preg_match($pattern, $ID)) {
116            return false;
117        }
118
119        $this->loadPages();
120        foreach ($this->pages as $namespace) {
121            $space = cleanID($namespace[0]);
122            if (trim($space) && (strpos($ID, $space . ':') === 0)) {
123                return resolve_id($namespace[0], $namespace[1]);
124            }
125        }
126
127        return false;
128    }
129
130    private static $pages = null;
131    private function loadPages() {
132        if ( $this->pages != null ) {
133            return;
134        }
135
136        $this->pages = array();
137        $namespaces = explode("\n", $this->getConf('enabled_namespaces'));
138        foreach( $namespaces as $namespace ) {
139
140            if ( strlen(trim($namespace)) == 0 ) { continue; }
141            $this->pages[] = explode("=>", $namespace);
142        }
143    }
144}
145
146//Setup VIM: ex: et ts=4 enc=utf-8 :
147