1<?php
2/**
3 * DokuWiki Plugin top (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class syntax_plugin_top extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'protected';
18    }
19
20    /**
21     * @return int Sort order - Low numbers go before high numbers
22     */
23    public function getSort() {
24        return 200;
25    }
26
27    /**
28     * Connect lookup pattern to lexer.
29     *
30     * @param string $mode Parser mode
31     */
32    public function connectTo($mode) {
33        $this->Lexer->addSpecialPattern('\\{\\{top(?:\|.+?)?\\}\\}', $mode, 'plugin_top');
34    }
35
36    /**
37     * Handle matches of the top 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    public function handle($match, $state, $pos, Doku_Handler $handler) {
46        if ($state==DOKU_LEXER_SPECIAL) {
47            $options = array('lang' => null, 'month' => null, 'tag' => 'ul', 'score' => 'false' );
48            $match = rtrim($match,'\}');
49            $match = substr($match,5);
50            if ($match != '') {
51                $match = ltrim($match,'\|');
52                $match = explode(",", $match);
53                foreach($match as $option) {
54                    list($key, $val) = explode('=', $option);
55                    $options[$key] = $val;
56                }
57            }
58            return array($state, $options);
59        } else {
60            return array($state, '');
61        }
62    }
63
64    /**
65     * Render xhtml output or metadata
66     *
67     * @param string $mode Renderer mode (supported modes: xhtml)
68     * @param Doku_Renderer $renderer The renderer
69     * @param array $data The data from the handler() function
70     * @return bool If rendering was successful.
71     */
72    public function render($mode, Doku_Renderer $renderer, $data) {
73        if($mode == 'metadata') return false;
74        if($data[0] != DOKU_LEXER_SPECIAL) return false;
75
76        /** @var helper_plugin_top $hlp */
77        $hlp  = plugin_load('helper', 'top');
78        $list = $hlp->best($data[1]['lang'],$data[1]['month'], 20);
79
80        if($data[1]['tag'] == 'ol') {
81            $renderer->listo_open();
82        } else {
83            $renderer->listu_open();
84        }
85
86        $num_items=0;
87        foreach($list as $item) {
88            if ($this->getConf('show_only_public')) {
89                if (auth_aclcheck($item['page'],'',null) < AUTH_READ) continue;
90            } else {
91                if (auth_quickaclcheck($item['page']) < AUTH_READ) continue;
92            }
93            if (!page_exists($item['page'])) continue;
94            if (isHiddenPage($item['page'])) continue;
95            $num_items = $num_items +1;
96            $renderer->listitem_open(1);
97            if (strpos($item['page'],':') === false) {
98                $item['page'] = ':' . $item['page'];
99            }
100            $renderer->internallink($item['page']);
101            if ($data[1]['score'] === 'true') $renderer->cdata(' (' . $item['value'] . ')');
102            $renderer->listitem_close();
103            if ($num_items >= 10) break;
104        }
105
106        if($data[1]['tag'] == 'ol') {
107            $renderer->listo_close();
108        } else {
109            $renderer->listu_close();
110        }
111        return true;
112    }
113}
114
115// vim:ts=4:sw=4:et:
116