1<?php
2/**
3 * Tag Plugin, topic component: displays links to all wiki pages with a certain tag
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Esther Brunner <wikidesign@gmail.com>
7 */
8
9/**
10 * Topic syntax, displays links to all wiki pages with a certain tag
11 */
12class syntax_plugin_tag_topic extends DokuWiki_Syntax_Plugin {
13
14    /**
15     * @return string Syntax type
16     */
17    function getType() { return 'substition'; }
18
19    /**
20     * @return string Paragraph type
21     */
22    function getPType() { return 'block'; }
23
24    /**
25     * @return int Sort order
26     */
27    function getSort() { return 295; }
28
29    /**
30     * @param string $mode Parser mode
31     */
32    function connectTo($mode) {
33        $this->Lexer->addSpecialPattern('\{\{topic>.+?\}\}',$mode,'plugin_tag_topic');
34    }
35
36    /**
37     * Handle matches of the topic syntax
38     *
39     * @param string $match The match of the syntax
40     * @param int    $state The state of the handler
41     * @param int    $pos The position in the document
42     * @param Doku_Handler    $handler The handler
43     * @return array Data for the renderer
44     */
45    function handle($match, $state, $pos, Doku_Handler $handler) {
46        global $ID;
47
48        $match = substr($match, 8, -2); // strip {{topic> from start and }} from end
49        list($match, $flags) = explode('&', $match, 2);
50        $flags = explode('&', $flags);
51        list($ns, $tag) = explode('?', $match);
52
53        if (!$tag) {
54            $tag = $ns;
55            $ns   = '';
56        }
57
58        if (($ns == '*') || ($ns == ':')) $ns = '';
59        elseif ($ns == '.') $ns = getNS($ID);
60        else $ns = cleanID($ns);
61
62        return array($ns, trim($tag), $flags);
63    }
64
65    /**
66     * Render xhtml output or metadata
67     *
68     * @param string         $mode      Renderer mode (supported modes: xhtml and metadata)
69     * @param Doku_Renderer  $renderer  The renderer
70     * @param array          $data      The data from the handler function
71     * @return bool If rendering was successful.
72     */
73    function render($mode, Doku_Renderer $renderer, $data) {
74        list($ns, $tag, $flags) = $data;
75
76        /* @var helper_plugin_tag $my */
77        if ($my = $this->loadHelper('tag')) $pages = $my->getTopic($ns, '', $tag);
78        if (!isset($pages) || !$pages) return true; // nothing to display
79
80        if ($mode == 'xhtml') {
81            /* @var Doku_Renderer_xhtml $renderer */
82
83            // prevent caching to ensure content is always fresh
84            $renderer->nocache();
85
86            /* @var helper_plugin_pagelist $pagelist */
87            // let Pagelist Plugin do the work for us
88            if ((!$pagelist = $this->loadHelper('pagelist'))) {
89                return false;
90            }
91            $pagelist->sort = false;
92            $pagelist->rsort = false;
93
94            $configflags = explode(',', str_replace(" ", "", $this->getConf('pagelist_flags')));
95           	$flags = array_merge($configflags, $flags);
96           	foreach($flags as $key => $flag) {
97           		if($flag == "")	unset($flags[$key]);
98           	}
99
100            $pagelist->setFlags($flags);
101            $pagelist->startList();
102
103            // Sort pages by pagename if required by flags
104            if($pagelist->sort || $pagelist->rsort) {
105            	$keys = array();
106            	$fnc = create_function('$a, $b', 'return strcmp(noNS($a["id"]), noNS($b["id"])); ');
107            	usort($pages, $fnc);
108            	// rsort is true - revserse sort the pages
109            	if($pagelist->rsort) krsort($pages);
110            }
111
112            foreach ($pages as $page) {
113                $pagelist->addPage($page);
114            }
115            $renderer->doc .= $pagelist->finishList();
116            return true;
117
118        // for metadata renderer
119/*        } elseif ($mode == 'metadata') {
120            foreach ($pages as $page) {
121                $renderer->meta['relation']['references'][$page['id']] = true;
122            }
123
124            return true;*/ // causes issues with backlinks
125        }
126        return false;
127    }
128}
129// vim:ts=4:sw=4:et:
130