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