xref: /plugin/struct/syntax/cloud.php (revision 9fbb0f8bd649f8c5791988a972dff0e0a6a95e6b)
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        global $INFO, $conf;
92        try {
93            $search = new SearchCloud($data);
94            $cloud = new AggregationCloud($INFO['id'], $mode, $renderer, $search);
95            $cloud->startScope();
96            $cloud->render();
97            $cloud->finishScope();
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