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')) {
11    die();
12}
13
14class syntax_plugin_tagging extends DokuWiki_Syntax_Plugin {
15
16    function getType() {
17        return 'substition';
18    }
19
20    function getPType() {
21        return 'block';
22    }
23
24    function getSort() {
25        return 13;
26    }
27
28    function connectTo($mode) {
29        $this->Lexer->addSpecialPattern('{{tagging::\w+(?:>[^}\?]+)?(?:\?[0-9]+)?}}', $mode, 'plugin_tagging');
30    }
31
32    function handle($match, $state, $pos, Doku_Handler $handler) {
33        $data = array();
34        $matches = array();
35        preg_match('/{{tagging::(\w+)(?:>([^}\?]+))?(\?[0-9]+)?}}/', $match, $matches);
36        $data['cmd'] = $matches[1];
37        $data['limit'] = (int)ltrim($matches[3], '?');
38        if (!$data['limit']) {
39            $data['limit'] = $this->getConf('cloudlimit');
40        }
41
42        switch ($data['cmd']) {
43            case 'user':
44                if (count($matches) > 2) {
45                    $data['user'] = trim($matches[2]);
46                }
47                break;
48            case 'tag':
49                if (count($matches) > 2) {
50                    $data['tag'] = trim($matches[2]);
51                }
52                break;
53            case 'ns':
54                if (count($matches) > 2) {
55                    $data['ns'] = trim($matches[2]);
56                }
57                break;
58            case 'manage':
59                if (count($matches) > 2) {
60                    $data['manage'] = trim($matches[2]);
61                }
62                break;
63        }
64
65        return $data;
66    }
67
68    function render($mode, Doku_Renderer $renderer, $data) {
69        if ($mode !== 'xhtml') {
70            return false;
71        }
72
73        /** @var helper_plugin_tagging $hlp */
74        $hlp = plugin_load('helper', 'tagging');
75
76        switch ($data['cmd']) {
77            case 'user':
78                $renderer->info['cache'] = false;
79                if (!isset($data['user'])) {
80                    $data['user'] = $_SERVER['REMOTE_USER'];
81                }
82                $tags = $hlp->findItems(array('tagger' => $data['user']), 'tag', $data['limit']);
83
84                $renderer->doc .= $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), true, true);
85
86                break;
87            case 'tag':
88                $renderer->info['cache'] = false;
89
90                $pids = $hlp->findItems(array('tag' => $data['tag']), 'pid', $data['limit']);
91
92                $renderer->doc .= $hlp->html_page_list($pids);
93
94                break;
95            case 'ns':
96                $renderer->info['cache'] = false;
97                if (!isset($data['ns'])) {
98                    global $INFO;
99                    $data['ns'] = $INFO['namespace'];
100                }
101                global $ID;
102                $data['ns'] = resolve_id(getNS($ID), $data['ns'] . ':');
103                if ($data['ns'] !== '') {
104                    // Do not match nsbla, only ns:bla
105                    $data['ns'] .= ':';
106                }
107                $tags = $hlp->findItems(['pid' => $hlp->globNamespace($data['ns'])], 'tag', $data['limit']);
108                $renderer->doc .= $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), true, true, $data['ns']);
109
110                break;
111            case 'input':
112                $renderer->nocache();
113                $renderer->doc .= $hlp->tpl_tags(false);
114                break;
115            case 'manage':
116                $renderer->nocache();
117                $ns = $data['manage'] ?: '';
118                $renderer->doc .= $hlp->manageTags($ns);
119                break;
120        }
121
122        return true;
123    }
124}
125
126// vim:ts=4:sw=4:et:enc=utf-8:
127