xref: /plugin/struct/syntax/list.php (revision 127d6bacbaa5831b9606d5ae91b03cfbe9108473)
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\AggregationList;
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_list extends DokuWiki_Syntax_Plugin {
18
19    /** @var string which class to use for output */
20    protected $tableclass = AggregationList::class;
21
22    /**
23     * @return string Syntax mode type
24     */
25    public function getType() {
26        return 'substition';
27    }
28    /**
29     * @return string Paragraph type
30     */
31    public function getPType() {
32        return 'block';
33    }
34    /**
35     * @return int Sort order - Low numbers go before high numbers
36     */
37    public function getSort() {
38        return 155;
39    }
40
41    /**
42     * Connect lookup pattern to lexer.
43     *
44     * @param string $mode Parser mode
45     */
46    public function connectTo($mode) {
47        $this->Lexer->addSpecialPattern('----+ *struct list *-+\n.*?\n----+',$mode,'plugin_struct_list');
48    }
49
50    /**
51     * Handle matches of the struct syntax
52     *
53     * @param string $match The match of the syntax
54     * @param int    $state The state of the handler
55     * @param int    $pos The position in the document
56     * @param Doku_Handler    $handler The handler
57     * @return array Data for the renderer
58     */
59    public function handle($match, $state, $pos, Doku_Handler $handler){
60        global $conf;
61
62        $lines = explode("\n", $match);
63        array_shift($lines);
64        array_pop($lines);
65
66        try {
67            $parser = new ConfigParser($lines);
68            $config = $parser->getConfig();
69            return $config;
70        } catch(StructException $e) {
71            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
72            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
73            return null;
74        }
75    }
76
77    /**
78     * Checks for options that do not work in a list aggregation
79     *
80     * @param array $config
81     */
82    protected function checkForInvalidOptions($config) {
83        $illegal = ['dynfilters', 'summarize', 'rownumbers', 'widths', 'summary'];
84        foreach ($illegal as $illegalOption)
85        if (!empty($config[$illegalOption])) {
86            throw new StructException('illegal option', $illegalOption);
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        if($mode != 'xhtml') return false;
100        if(!$data) return false;
101        global $INFO;
102        global $conf;
103
104        try {
105            $this->checkForInvalidOptions($data);
106            $search = new SearchConfig($data);
107
108            /** @var AggregationList $list */
109            $list = new $this->tableclass($INFO['id'], $mode, $renderer, $search);
110            $list->render();
111
112            if($mode == 'metadata') {
113                /** @var Doku_Renderer_metadata $renderer */
114                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
115            }
116
117        } catch(StructException $e) {
118            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
119            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
120        }
121
122        return true;
123    }
124}
125
126// vim:ts=4:sw=4:et:
127