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  Iain Hallam <iain@nineworlds.net>
8 */
9
10// phpcs:disable PSR1.Files.SideEffects
11use dokuwiki\Extension\SyntaxPlugin;
12use dokuwiki\plugin\struct\meta\AggregationValue;
13use dokuwiki\plugin\struct\meta\InlineConfigParser;
14use dokuwiki\plugin\struct\meta\SearchConfig;
15use dokuwiki\plugin\struct\meta\StructException;
16
17// phpcs:ignore PSR1.Classes.ClassDeclaration, Squiz.Classes.ValidClassName
18class syntax_plugin_struct_value extends SyntaxPlugin
19{
20    /**
21     * @return string Syntax mode type
22     */
23    public function getType()
24    {
25        return 'substition';
26    }
27
28    /**
29     * @return int Sort order - Low numbers go before high numbers
30     */
31    public function getSort()
32    {
33        /* 315 to place above Doku_Parser_Mode_media, which would
34         * otherwise take precedence. See
35         * https://www.dokuwiki.org/devel:parser:getsort_list
36         */
37        return 315;
38    }
39
40    /**
41     * Connect lookup pattern to lexer.
42     *
43     * @param string $mode Parser mode
44     */
45    public function connectTo($mode)
46    {
47        // {{$...}}
48        $this->Lexer->addSpecialPattern('\{\{\$[^}]+\}\}', $mode, 'plugin_struct_value');
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
64        try {
65            // strip {{$ and }} markers
66            $inline = substr($match, 3, -2);
67
68            // Parse inline syntax
69            $parser = new InlineConfigParser($inline);
70            $config = $parser->getConfig();
71
72            return $config;
73        } catch (StructException $e) {
74            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
75            if ($conf['allowdebug']) {
76                msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
77            }
78            return null;
79        }
80    }
81
82    /**
83     * Render xhtml output or metadata
84     *
85     * @param string $mode Renderer mode (supported modes: xhtml)
86     * @param Doku_Renderer $renderer The renderer
87     * @param array $data The data from the handler() function
88     * @return  bool                      If rendering was successful.
89     */
90    public function render($mode, Doku_Renderer $renderer, $data)
91    {
92        if (!$data) {
93            return false;
94        }
95        global $INFO;
96        global $conf;
97
98        // Get configuration
99        $show_not_found = $this->getConf('show_not_found');
100
101        try {
102            /** @var SearchConfig $search */
103            $search = new SearchConfig($data);
104
105            /** @var AggregationValue $value */
106            $value = new AggregationValue($INFO['id'], $mode, $renderer, $search);
107            $value->startScope();
108            $value->render($show_not_found);
109            $value->finishScope();
110
111            if ($mode == 'metadata') {
112                /** @var Doku_Renderer_metadata $renderer */
113                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
114            }
115        } catch (StructException $e) {
116            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
117            if ($conf['allowdebug']) {
118                msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
119            }
120        }
121
122        return true;
123    }
124}
125