xref: /plugin/struct/syntax/table.php (revision 88ab55939ae8f8149117f65b768a3c0ae32951c5)
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        foreach (helper_plugin_struct::BLACKLIST_RENDERER as $blacklisted) {
99            if ($renderer instanceof $blacklisted) {
100                return true;
101            }
102        }
103
104        global $INFO;
105        global $conf;
106
107        try {
108            $search = new SearchConfig($data);
109            if ($format === 'struct_csv') {
110                // no pagination in export
111                $search->setLimit(0);
112                $search->setOffset(0);
113            }
114
115            /** @var AggregationTable $table */
116            $table = new $this->tableclass($INFO['id'], $format, $renderer, $search);
117            $table->render();
118
119            if ($format === 'metadata') {
120                /** @var Doku_Renderer_metadata $renderer */
121                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
122            }
123        } catch (StructException $e) {
124            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
125            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
126        }
127
128        return true;
129    }
130
131    /**
132     * Filter based on primary key columns, applicable in child classes
133     *
134     * @param array $config
135     * @return array
136     */
137    protected function addTypeFilter($config)
138    {
139        return $config;
140    }
141}
142