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