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