1<?php
2/**
3 * DokuWiki Plugin structat (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <it@rid.pl>
7 */
8
9// must be run within Dokuwiki
10use dokuwiki\plugin\struct\meta\AggregationTable;
11use dokuwiki\plugin\struct\meta\ConfigParser;
12use dokuwiki\plugin\struct\meta\StructException;
13use dokuwiki\plugin\structat\meta\SearchConfigAt;
14
15if (!defined('DOKU_INC')) {
16    die();
17}
18
19class syntax_plugin_structat_table extends DokuWiki_Syntax_Plugin
20{
21
22    /** @var string which class to use for output */
23    protected $tableclass = AggregationTable::class;
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('----+ *structat table *-+\n.*?\n----+', $mode, 'plugin_structat_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 SearchConfigAt($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);
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, applicable in child classes
130     *
131     * @param array $config
132     * @return array
133     */
134    protected function addTypeFilter($config)
135    {
136        return $config;
137    }
138}
139