xref: /plugin/struct/syntax/table.php (revision 299ca8cc642c152679238a0fa0ee3d839791f6b6)
1549a0837SAndreas Gohr<?php
261356325SAnna Dabrowska
3549a0837SAndreas Gohr/**
4549a0837SAndreas Gohr * DokuWiki Plugin struct (Syntax Component)
5549a0837SAndreas Gohr *
6549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7549a0837SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8549a0837SAndreas Gohr */
9549a0837SAndreas Gohr
10d90aa848SAndreas Gohruse dokuwiki\plugin\struct\meta\Aggregation;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\AggregationTable;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ConfigParser;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SearchConfig;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1515929be2SAndreas Gohr
1661356325SAnna Dabrowskaclass syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin
1761356325SAnna Dabrowska{
183f2a8309SAndreas Gohr    /** @var string which class to use for output */
193f2a8309SAndreas Gohr    protected $tableclass = AggregationTable::class;
203f2a8309SAndreas Gohr
21d90aa848SAndreas Gohr    /** @var string Config options that are not allowed for this syntax mode */
22ce44c639SAndreas Gohr    protected $illegalOptions = ['nesting', 'index'];
23d90aa848SAndreas Gohr
24549a0837SAndreas Gohr    /**
25549a0837SAndreas Gohr     * @return string Syntax mode type
26549a0837SAndreas Gohr     */
2761356325SAnna Dabrowska    public function getType()
2861356325SAnna Dabrowska    {
2915929be2SAndreas Gohr        return 'substition';
30549a0837SAndreas Gohr    }
313f2a8309SAndreas Gohr
32549a0837SAndreas Gohr    /**
33549a0837SAndreas Gohr     * @return string Paragraph type
34549a0837SAndreas Gohr     */
3561356325SAnna Dabrowska    public function getPType()
3661356325SAnna Dabrowska    {
3715929be2SAndreas Gohr        return 'block';
38549a0837SAndreas Gohr    }
393f2a8309SAndreas Gohr
40549a0837SAndreas Gohr    /**
41549a0837SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
42549a0837SAndreas Gohr     */
4361356325SAnna Dabrowska    public function getSort()
4461356325SAnna Dabrowska    {
455511bd5bSAndreas Gohr        return 155;
46549a0837SAndreas Gohr    }
47549a0837SAndreas Gohr
48549a0837SAndreas Gohr    /**
49549a0837SAndreas Gohr     * Connect lookup pattern to lexer.
50549a0837SAndreas Gohr     *
51549a0837SAndreas Gohr     * @param string $mode Parser mode
52549a0837SAndreas Gohr     */
5361356325SAnna Dabrowska    public function connectTo($mode)
5461356325SAnna Dabrowska    {
555511bd5bSAndreas Gohr        $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table');
56549a0837SAndreas Gohr    }
57549a0837SAndreas Gohr
58549a0837SAndreas Gohr    /**
59549a0837SAndreas Gohr     * Handle matches of the struct syntax
60549a0837SAndreas Gohr     *
61549a0837SAndreas Gohr     * @param string $match The match of the syntax
62549a0837SAndreas Gohr     * @param int $state The state of the handler
63549a0837SAndreas Gohr     * @param int $pos The position in the document
64549a0837SAndreas Gohr     * @param Doku_Handler $handler The handler
65549a0837SAndreas Gohr     * @return array Data for the renderer
66549a0837SAndreas Gohr     */
6761356325SAnna Dabrowska    public function handle($match, $state, $pos, Doku_Handler $handler)
6861356325SAnna Dabrowska    {
69bd363da9SAndreas Gohr        global $conf;
70549a0837SAndreas Gohr
715511bd5bSAndreas Gohr        $lines = explode("\n", $match);
725511bd5bSAndreas Gohr        array_shift($lines);
735511bd5bSAndreas Gohr        array_pop($lines);
745511bd5bSAndreas Gohr
755511bd5bSAndreas Gohr        try {
765511bd5bSAndreas Gohr            $parser = new ConfigParser($lines);
770659dc64SMichael Grosse            $config = $parser->getConfig();
78d90aa848SAndreas Gohr            $this->checkForInvalidOptions($config);
790659dc64SMichael Grosse            return $config;
805511bd5bSAndreas Gohr        } catch (StructException $e) {
815511bd5bSAndreas Gohr            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
82bd363da9SAndreas Gohr            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
835511bd5bSAndreas Gohr            return null;
845511bd5bSAndreas Gohr        }
85549a0837SAndreas Gohr    }
86549a0837SAndreas Gohr
87549a0837SAndreas Gohr    /**
88549a0837SAndreas Gohr     * Render xhtml output or metadata
89549a0837SAndreas Gohr     *
9034ea6e10SAnna Dabrowska     * @param string $format Renderer mode (supported modes: xhtml)
91549a0837SAndreas Gohr     * @param Doku_Renderer $renderer The renderer
9230bf61e6SAndreas Gohr     * @param array $config The parsed config data from the handler() function
93549a0837SAndreas Gohr     * @return bool If rendering was successful.
94549a0837SAndreas Gohr     */
9530bf61e6SAndreas Gohr    public function render($format, Doku_Renderer $renderer, $config)
9661356325SAnna Dabrowska    {
9706fee43aSMichael Grosse        global $INFO;
98bd363da9SAndreas Gohr        global $conf;
9929877279SMichael Große
10030bf61e6SAndreas Gohr        if (!$config) return false;
10130bf61e6SAndreas Gohr        $config = $this->addTypeFilter($config); // add type specific filters
10230bf61e6SAndreas Gohr
10325ed0c0dSAndreas Gohr        // always use the main page's ID @todo might make sense as utility method somewhere
10425ed0c0dSAndreas Gohr        if ($INFO !== null) {
10525ed0c0dSAndreas Gohr            $mainId = $INFO['id'];
10625ed0c0dSAndreas Gohr        } else {
10725ed0c0dSAndreas Gohr            $mainId = getID();
10825ed0c0dSAndreas Gohr        }
10925ed0c0dSAndreas Gohr
11015929be2SAndreas Gohr        try {
111*299ca8ccSAndreas Gohr            $search = $this->getSearchConfig($config);
11234ea6e10SAnna Dabrowska            if ($format === 'struct_csv') {
1136ce83f43SAndreas Gohr                // no pagination in export
1146ce83f43SAndreas Gohr                $search->setLimit(0);
1156ce83f43SAndreas Gohr                $search->setOffset(0);
1166ce83f43SAndreas Gohr            }
1176ce83f43SAndreas Gohr
118d90aa848SAndreas Gohr            /** @var Aggregation $table */
11925ed0c0dSAndreas Gohr            $table = new $this->tableclass($mainId, $format, $renderer, $search);
120af0ce8d2SAndreas Gohr            $table->startScope();
121d90aa848SAndreas Gohr            $table->render(true);
122af0ce8d2SAndreas Gohr            $table->finishScope();
12316b7d914SAndreas Gohr
12434ea6e10SAnna Dabrowska            if ($format === 'metadata') {
12516b7d914SAndreas Gohr                /** @var Doku_Renderer_metadata $renderer */
12616b7d914SAndreas Gohr                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
12716b7d914SAndreas Gohr            }
1285511bd5bSAndreas Gohr        } catch (StructException $e) {
12915929be2SAndreas Gohr            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
130bd363da9SAndreas Gohr            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
13115929be2SAndreas Gohr        }
13215929be2SAndreas Gohr
133549a0837SAndreas Gohr        return true;
134549a0837SAndreas Gohr    }
1350ceefd5cSAnna Dabrowska
1360ceefd5cSAnna Dabrowska    /**
137*299ca8ccSAndreas Gohr     * Initialize a SearchConfig with the given parsed config
138*299ca8ccSAndreas Gohr     *
139*299ca8ccSAndreas Gohr     * @param array $config
140*299ca8ccSAndreas Gohr     * @return SearchConfig
141*299ca8ccSAndreas Gohr     */
142*299ca8ccSAndreas Gohr    protected function getSearchConfig($config)
143*299ca8ccSAndreas Gohr    {
144*299ca8ccSAndreas Gohr        return new SearchConfig($config);
145*299ca8ccSAndreas Gohr    }
146*299ca8ccSAndreas Gohr
147*299ca8ccSAndreas Gohr
148*299ca8ccSAndreas Gohr    /**
149c797933dSAnna Dabrowska     * Filter based on primary key columns, applicable in child classes
1500ceefd5cSAnna Dabrowska     *
1510ceefd5cSAnna Dabrowska     * @param array $config
1520ceefd5cSAnna Dabrowska     * @return array
1530ceefd5cSAnna Dabrowska     */
1540ceefd5cSAnna Dabrowska    protected function addTypeFilter($config)
1550ceefd5cSAnna Dabrowska    {
1560ceefd5cSAnna Dabrowska        return $config;
1570ceefd5cSAnna Dabrowska    }
158d90aa848SAndreas Gohr
159d90aa848SAndreas Gohr    /**
160d90aa848SAndreas Gohr     * Checks for options that do not work in this aggregation
161d90aa848SAndreas Gohr     *
162d90aa848SAndreas Gohr     * @param array $config
163d90aa848SAndreas Gohr     */
164d90aa848SAndreas Gohr    protected function checkForInvalidOptions($config)
165d90aa848SAndreas Gohr    {
166d90aa848SAndreas Gohr        foreach ($this->illegalOptions as $illegalOption) {
167d90aa848SAndreas Gohr            if (!empty($config[$illegalOption])) {
168d90aa848SAndreas Gohr                throw new StructException('illegal option', $illegalOption);
169d90aa848SAndreas Gohr            }
170d90aa848SAndreas Gohr        }
171d90aa848SAndreas Gohr    }
172549a0837SAndreas Gohr}
173