xref: /plugin/struct/syntax/list.php (revision e6a2e26b57dd1411a582eba0bdba877d874c7fa3)
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\plugin\struct\meta\AggregationList;
11use dokuwiki\plugin\struct\meta\ConfigParser;
12use dokuwiki\plugin\struct\meta\SearchConfig;
13use dokuwiki\plugin\struct\meta\StructException;
14
15// must be run within Dokuwiki
16if (!defined('DOKU_INC')) die();
17
18class syntax_plugin_struct_list extends DokuWiki_Syntax_Plugin
19{
20
21    /** @var string which class to use for output */
22    protected $tableclass = AggregationList::class;
23
24    /**
25     * @return string Syntax mode type
26     */
27    public function getType()
28    {
29        return 'substition';
30    }
31    /**
32     * @return string Paragraph type
33     */
34    public function getPType()
35    {
36        return 'block';
37    }
38    /**
39     * @return int Sort order - Low numbers go before high numbers
40     */
41    public function getSort()
42    {
43        return 155;
44    }
45
46    /**
47     * Connect lookup pattern to lexer.
48     *
49     * @param string $mode Parser mode
50     */
51    public function connectTo($mode)
52    {
53        $this->Lexer->addSpecialPattern('----+ *struct list *-+\n.*?\n----+', $mode, 'plugin_struct_list');
54    }
55
56    /**
57     * Handle matches of the struct syntax
58     *
59     * @param string $match The match of the syntax
60     * @param int    $state The state of the handler
61     * @param int    $pos The position in the document
62     * @param Doku_Handler    $handler The handler
63     * @return array Data for the renderer
64     */
65    public function handle($match, $state, $pos, Doku_Handler $handler)
66    {
67        global $conf;
68
69        $lines = explode("\n", $match);
70        array_shift($lines);
71        array_pop($lines);
72
73        try {
74            $parser = new ConfigParser($lines);
75            $config = $parser->getConfig();
76            return $config;
77        } catch (StructException $e) {
78            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
79            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
80            return null;
81        }
82    }
83
84    /**
85     * Checks for options that do not work in a list aggregation
86     *
87     * @param array $config
88     */
89    protected function checkForInvalidOptions($config)
90    {
91        $illegal = ['dynfilters', 'summarize', 'rownumbers', 'widths', 'summary'];
92        foreach ($illegal as $illegalOption)
93        if (!empty($config[$illegalOption])) {
94            throw new StructException('illegal option', $illegalOption);
95        }
96    }
97
98    /**
99     * Render xhtml output or metadata
100     *
101     * @param string         $mode      Renderer mode (supported modes: xhtml)
102     * @param Doku_Renderer  $renderer  The renderer
103     * @param array          $data      The data from the handler() function
104     * @return bool If rendering was successful.
105     */
106    public function render($mode, Doku_Renderer $renderer, $data)
107    {
108        if ($mode != 'xhtml') return false;
109        if (!$data) return false;
110        global $INFO;
111        global $conf;
112
113        try {
114            $this->checkForInvalidOptions($data);
115            $search = new SearchConfig($data);
116
117            /** @var AggregationList $list */
118            $list = new $this->tableclass($INFO['id'], $mode, $renderer, $search);
119            $list->render();
120
121            if ($mode == 'metadata') {
122                /** @var Doku_Renderer_metadata $renderer */
123                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
124            }
125        } catch (StructException $e) {
126            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
127            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
128        }
129
130        return true;
131    }
132}
133
134// vim:ts=4:sw=4:et:
135