1<?php
2
3/**
4 * DokuWiki Plugin struct (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\plugin\struct\meta\AggregationCloud;
11use dokuwiki\plugin\struct\meta\ConfigParser;
12use dokuwiki\plugin\struct\meta\SearchCloud;
13use dokuwiki\plugin\struct\meta\StructException;
14
15class syntax_plugin_struct_cloud extends DokuWiki_Syntax_Plugin
16{
17    /**
18     * @return string Syntax mode type
19     */
20    public function getType()
21    {
22        return 'substition';
23    }
24
25    /**
26     * @return string Paragraph type
27     */
28    public function getPType()
29    {
30        return 'block';
31    }
32
33    /**
34     * @return int Sort order - Low numbers go before high numbers
35     */
36    public function getSort()
37    {
38        return 151;
39    }
40
41    /**
42     * Connect lookup pattern to lexer.
43     *
44     * @param string $mode Parser mode
45     */
46    public function connectTo($mode)
47    {
48        $this->Lexer->addSpecialPattern('----+ *struct cloud *-+\n.*?\n----+', $mode, 'plugin_struct_cloud');
49    }
50
51    /**
52     * Handle matches of the struct syntax
53     *
54     * @param string $match The match of the syntax
55     * @param int $state The state of the handler
56     * @param int $pos The position in the document
57     * @param Doku_Handler $handler The handler
58     * @return array Data for the renderer
59     */
60    public function handle($match, $state, $pos, Doku_Handler $handler)
61    {
62        global $conf;
63        $lines = explode("\n", $match);
64        array_shift($lines);
65        array_pop($lines);
66
67        try {
68            $parser = new ConfigParser($lines);
69            $config = $parser->getConfig();
70            return $config;
71        } catch (StructException $e) {
72            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
73            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
74            return null;
75        }
76    }
77
78    /**
79     * Render xhtml output or metadata
80     *
81     * @param string $mode Renderer mode (supported modes: xhtml)
82     * @param Doku_Renderer $renderer The renderer
83     * @param array $data The data from the handler() function
84     * @return bool If rendering was successful.
85     */
86    public function render($mode, Doku_Renderer $renderer, $data)
87    {
88        if ($mode != 'xhtml') return false;
89        if (!$data) return false;
90        if (!empty($data['filter'])) {
91            msg($this->getLang('Warning: no filters for cloud'), -1);
92        }
93        global $INFO, $conf;
94        try {
95            $search = new SearchCloud($data);
96            $cloud = new AggregationCloud($INFO['id'], $mode, $renderer, $search);
97            $cloud->render();
98            if ($mode == 'metadata') {
99                /** @var Doku_Renderer_metadata $renderer */
100                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
101            }
102        } catch (StructException $e) {
103            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
104            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
105        }
106
107        return true;
108    }
109}
110
111// vim:ts=4:sw=4:et:
112