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