xref: /plugin/tagging/syntax.php (revision 0a518a116de248fb2e4d610ff7d903adbfd13d2e)
1<?php
2/**
3 * DokuWiki Plugin tagging (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Adrian Lang <lang@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_tagging extends DokuWiki_Syntax_Plugin {
13
14    function getType() {
15        return 'substition';
16    }
17
18    function getPType() {
19        return 'block';
20    }
21
22    function getSort() {
23        return 13;
24    }
25
26    function connectTo($mode) {
27        $this->Lexer->addSpecialPattern('{{tagging::\w+(?:>[^}]+)?}}',$mode,'plugin_tagging');
28    }
29
30    function handle($match, $state, $pos, &$handler){
31        $data = array();
32        $matches = array();
33        preg_match('/{{tagging::(\w+)(?:>([^}]+))?}}/', $match, $matches);
34        $data['cmd'] = $matches[1];
35
36        switch($data['cmd']) {
37        case 'user':
38            if (count($matches) > 2) {
39                $data['user'] = trim($matches[2]);
40            }
41            break;
42        case 'ns':
43            if (count($matches) > 2) {
44                $data['ns'] = trim($matches[2]);
45            }
46            break;
47        }
48
49        return $data;
50    }
51
52    function render($mode, &$renderer, $data) {
53        if ($mode !== 'xhtml') {
54            return false;
55        }
56
57        /** @var helper_plugin_tagging $hlp */
58        $hlp = plugin_load('helper', 'tagging');
59
60        switch($data['cmd']) {
61        case 'user':
62            $renderer->info['cache'] = false;
63            if (!isset($data['user'])) {
64                $data['user'] = $_SERVER['REMOTE_USER'];
65            }
66            $tags = $hlp->getTags(array('tagger' => $data['user']), 'tag');
67            $renderer->doc .= $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), true, true);
68
69            break;
70        case 'ns':
71            $renderer->info['cache'] = false;
72            if (!isset($data['ns'])) {
73                global $INFO;
74                $data['ns'] = $INFO['namespace'];
75            }
76            global $ID;
77            $data['ns'] = resolve_id(getNS($ID), $data['ns'] . ':');
78            if ($data['ns'] !== '') {
79                // Do not match nsbla, only ns:bla
80                $data['ns'] .= ':';
81            }
82            $data['ns'] .= '%';
83            $tags = $hlp->getTags(array('pid' => $data['ns']), 'tag');
84            $renderer->doc .= $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), true, true);
85
86            break;
87        }
88
89        return true;
90    }
91}
92
93// vim:ts=4:sw=4:et:enc=utf-8:
94