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