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