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        // always use the main page's ID @todo might make sense as utility method somewhere
99        if ($INFO !== null) {
100            $mainId = $INFO['id'];
101        } else {
102            $mainId = getID();
103        }
104
105        try {
106            $search = new SearchConfig($config);
107            if ($format === 'struct_csv') {
108                // no pagination in export
109                $search->setLimit(0);
110                $search->setOffset(0);
111            }
112
113            /** @var AggregationTable $table */
114            $table = new $this->tableclass($mainId, $format, $renderer, $search);
115            $table->render();
116
117            if ($format === 'metadata') {
118                /** @var Doku_Renderer_metadata $renderer */
119                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
120            }
121        } catch (StructException $e) {
122            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
123            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
124        }
125
126        return true;
127    }
128
129    /**
130     * Filter based on primary key columns, applicable in child classes
131     *
132     * @param array $config
133     * @return array
134     */
135    protected function addTypeFilter($config)
136    {
137        return $config;
138    }
139}
140