xref: /plugin/struct/syntax/table.php (revision d400a5f063580f6b7df696ef5e0e166d509b272c)
1549a0837SAndreas Gohr<?php
2549a0837SAndreas Gohr/**
3549a0837SAndreas Gohr * DokuWiki Plugin struct (Syntax Component)
4549a0837SAndreas Gohr *
5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6549a0837SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7549a0837SAndreas Gohr */
8549a0837SAndreas Gohr
9ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\AggregationTable;
10ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ConfigParser;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SearchConfig;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1315929be2SAndreas Gohr
1407993756SAndreas Gohr// must be run within Dokuwiki
15549a0837SAndreas Gohrif(!defined('DOKU_INC')) die();
16549a0837SAndreas Gohr
17549a0837SAndreas Gohrclass syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin {
183f2a8309SAndreas Gohr
193f2a8309SAndreas Gohr    /** @var string which class to use for output */
203f2a8309SAndreas Gohr    protected $tableclass = AggregationTable::class;
21*d400a5f0SAnna Dabrowska    protected $idColumn = 'pid';
223f2a8309SAndreas Gohr
23549a0837SAndreas Gohr    /**
24549a0837SAndreas Gohr     * @return string Syntax mode type
25549a0837SAndreas Gohr     */
26549a0837SAndreas Gohr    public function getType() {
2715929be2SAndreas Gohr        return 'substition';
28549a0837SAndreas Gohr    }
293f2a8309SAndreas Gohr
30549a0837SAndreas Gohr    /**
31549a0837SAndreas Gohr     * @return string Paragraph type
32549a0837SAndreas Gohr     */
33549a0837SAndreas Gohr    public function getPType() {
3415929be2SAndreas Gohr        return 'block';
35549a0837SAndreas Gohr    }
363f2a8309SAndreas Gohr
37549a0837SAndreas Gohr    /**
38549a0837SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
39549a0837SAndreas Gohr     */
40549a0837SAndreas Gohr    public function getSort() {
415511bd5bSAndreas Gohr        return 155;
42549a0837SAndreas Gohr    }
43549a0837SAndreas Gohr
44549a0837SAndreas Gohr    /**
45549a0837SAndreas Gohr     * Connect lookup pattern to lexer.
46549a0837SAndreas Gohr     *
47549a0837SAndreas Gohr     * @param string $mode Parser mode
48549a0837SAndreas Gohr     */
49549a0837SAndreas Gohr    public function connectTo($mode) {
505511bd5bSAndreas Gohr        $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table');
51549a0837SAndreas Gohr    }
52549a0837SAndreas Gohr
53549a0837SAndreas Gohr    /**
54549a0837SAndreas Gohr     * Handle matches of the struct syntax
55549a0837SAndreas Gohr     *
56549a0837SAndreas Gohr     * @param string $match The match of the syntax
57549a0837SAndreas Gohr     * @param int $state The state of the handler
58549a0837SAndreas Gohr     * @param int $pos The position in the document
59549a0837SAndreas Gohr     * @param Doku_Handler $handler The handler
60549a0837SAndreas Gohr     * @return array Data for the renderer
61549a0837SAndreas Gohr     */
62ab466032SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler) {
63bd363da9SAndreas Gohr        global $conf;
64549a0837SAndreas Gohr
655511bd5bSAndreas Gohr        $lines = explode("\n", $match);
665511bd5bSAndreas Gohr        array_shift($lines);
675511bd5bSAndreas Gohr        array_pop($lines);
685511bd5bSAndreas Gohr
695511bd5bSAndreas Gohr        try {
705511bd5bSAndreas Gohr            $parser = new ConfigParser($lines);
710659dc64SMichael Grosse            $config = $parser->getConfig();
720ceefd5cSAnna Dabrowska
730ceefd5cSAnna Dabrowska            $config = $this->addTypeFilter($config);
740ceefd5cSAnna Dabrowska
750659dc64SMichael Grosse            return $config;
765511bd5bSAndreas Gohr        } catch(StructException $e) {
775511bd5bSAndreas Gohr            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
78bd363da9SAndreas Gohr            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
795511bd5bSAndreas Gohr            return null;
805511bd5bSAndreas Gohr        }
81549a0837SAndreas Gohr    }
82549a0837SAndreas Gohr
83549a0837SAndreas Gohr    /**
84549a0837SAndreas Gohr     * Render xhtml output or metadata
85549a0837SAndreas Gohr     *
86549a0837SAndreas Gohr     * @param string $mode Renderer mode (supported modes: xhtml)
87549a0837SAndreas Gohr     * @param Doku_Renderer $renderer The renderer
88549a0837SAndreas Gohr     * @param array $data The data from the handler() function
89549a0837SAndreas Gohr     * @return bool If rendering was successful.
90549a0837SAndreas Gohr     */
91ab466032SAndreas Gohr    public function render($mode, Doku_Renderer $renderer, $data) {
925511bd5bSAndreas Gohr        if(!$data) return false;
9306fee43aSMichael Grosse        global $INFO;
94bd363da9SAndreas Gohr        global $conf;
9529877279SMichael Große
9615929be2SAndreas Gohr        try {
975511bd5bSAndreas Gohr            $search = new SearchConfig($data);
986ce83f43SAndreas Gohr            if($mode == 'struct_csv') {
996ce83f43SAndreas Gohr                // no pagination in export
1006ce83f43SAndreas Gohr                $search->setLimit(0);
1016ce83f43SAndreas Gohr                $search->setOffset(0);
1026ce83f43SAndreas Gohr            }
1036ce83f43SAndreas Gohr
1043f2a8309SAndreas Gohr            /** @var AggregationTable $table */
105*d400a5f0SAnna Dabrowska            $table = new $this->tableclass($INFO['id'], $mode, $renderer, $search, $this->idColumn);
10607993756SAndreas Gohr            $table->render();
10716b7d914SAndreas Gohr
10816b7d914SAndreas Gohr            if($mode == 'metadata') {
10916b7d914SAndreas Gohr                /** @var Doku_Renderer_metadata $renderer */
11016b7d914SAndreas Gohr                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
11116b7d914SAndreas Gohr            }
11216b7d914SAndreas Gohr
1135511bd5bSAndreas Gohr        } catch(StructException $e) {
11415929be2SAndreas Gohr            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
115bd363da9SAndreas Gohr            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
11615929be2SAndreas Gohr        }
11715929be2SAndreas Gohr
118549a0837SAndreas Gohr        return true;
119549a0837SAndreas Gohr    }
1200ceefd5cSAnna Dabrowska
1210ceefd5cSAnna Dabrowska    /**
1220ceefd5cSAnna Dabrowska     * Filter based on primary key columns
1230ceefd5cSAnna Dabrowska     *
1240ceefd5cSAnna Dabrowska     * @param array $config
1250ceefd5cSAnna Dabrowska     * @return array
1260ceefd5cSAnna Dabrowska     */
1270ceefd5cSAnna Dabrowska    protected function addTypeFilter($config)
1280ceefd5cSAnna Dabrowska    {
1290ceefd5cSAnna Dabrowska        $config['filter'][] = ['%rowid%', '=', (string)\dokuwiki\plugin\struct\meta\AccessTableData::DEFAULT_PAGE_RID, 'AND'];
1300ceefd5cSAnna Dabrowska        return $config;
1310ceefd5cSAnna Dabrowska    }
132549a0837SAndreas Gohr}
133