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