xref: /plugin/tagging/syntax.php (revision fc7056c454bec7cd85c8c385ec81bacf17b192b5)
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
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'syntax.php';
17require_once DOKU_PLUGIN.'tagging/common.php';
18
19class syntax_plugin_tagging extends DokuWiki_Syntax_Plugin {
20
21    function getType() {
22        return 'substition';
23    }
24
25    function getPType() {
26        return 'block';
27    }
28
29    function getSort() {
30        return 13;
31    }
32
33    function connectTo($mode) {
34        $this->Lexer->addSpecialPattern('{{tagging::\w+(?:>[^}]+)?}}',$mode,'plugin_tagging');
35    }
36
37    function handle($match, $state, $pos, &$handler){
38        $data = array();
39        $matches = array();
40        preg_match('/{{tagging::(\w+)(?:>([^}]+))?}}/', $match, $matches);
41        $data['cmd'] = $matches[1];
42
43        switch($data['cmd']) {
44        case 'user':
45            if (count($matches) > 2) {
46                $data['user'] = trim($matches[2]);
47            }
48            break;
49        }
50
51        return $data;
52    }
53
54    function render($mode, &$renderer, $data) {
55        if ($mode !== 'xhtml') {
56            return false;
57        }
58
59        $pte = tagging_get_pte($this);
60
61        switch($data['cmd']) {
62        case 'user':
63            $renderer->info['cache'] = false;
64            if (!isset($data['user'])) {
65                $data['user'] = $_SERVER['REMOTE_USER'];
66            }
67            if (is_null($pte)) return;
68            list($min, $max, $data_arr) = $pte->user_tagcloud($data['user'], 10);
69
70            cloud_weight($data_arr, $min, $max, 10);
71
72            $renderer->doc .= '<ul class="tagcloud" id="tagging_tagcloud">';
73            if (count($data_arr) === 0) {
74                // Produce valid XHTML (ul needs a child)
75                $this->setupLocale();
76                $renderer->doc .=  '<li>' . $this->lang['js']['notags'] . '</li>';
77            }
78            foreach ($data_arr as $tag => $size) {
79                $renderer->doc .=  '<li class="t' .
80                     $size . '">' .
81                     '<a href="' . $pte->tag_browse_url($tag) . '">' .
82                     $tag . '</a>' . '</li> ';
83            }
84            $renderer->doc .= '</ul>';
85
86            break;
87        }
88
89        return true;
90    }
91}
92
93// vim:ts=4:sw=4:et:enc=utf-8:
94