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