xref: /plugin/tagging/syntax.php (revision 8ed4f4c425b5570b43f6a78b2b790fd057acae70)
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 'ns':
49                if (count($matches) > 2) {
50                    $data['ns'] = trim($matches[2]);
51                }
52                break;
53        }
54
55        return $data;
56    }
57
58    function render($mode, Doku_Renderer $renderer, $data) {
59        if ($mode !== 'xhtml') {
60            return false;
61        }
62
63        /** @var helper_plugin_tagging $hlp */
64        $hlp = plugin_load('helper', 'tagging');
65
66        switch ($data['cmd']) {
67            case 'user':
68                $renderer->info['cache'] = false;
69                if (!isset($data['user'])) {
70                    $data['user'] = $_SERVER['REMOTE_USER'];
71                }
72                $tags = $hlp->findItems(array('tagger' => $data['user']), 'tag', $data['limit']);
73                $renderer->doc .= $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), true, true);
74
75                break;
76            case 'ns':
77                $renderer->info['cache'] = false;
78                if (!isset($data['ns'])) {
79                    global $INFO;
80                    $data['ns'] = $INFO['namespace'];
81                }
82                global $ID;
83                $data['ns'] = resolve_id(getNS($ID), $data['ns'] . ':');
84                if ($data['ns'] !== '') {
85                    // Do not match nsbla, only ns:bla
86                    $data['ns'] .= ':';
87                }
88                $tags = $hlp->findItems(array('pid' => $data['ns'] . '%'), 'tag', $data['limit']);
89                $renderer->doc .= $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), true, true, $data['ns']);
90
91                break;
92            case 'input':
93                $renderer->nocache();
94                $renderer->doc .= $hlp->tpl_tags(false);
95                break;
96        }
97
98        return true;
99    }
100}
101
102// vim:ts=4:sw=4:et:enc=utf-8:
103