xref: /plugin/struct/meta/ConfigParser.php (revision 9fbb0f8bd649f8c5791988a972dff0e0a6a95e6b)
15511bd5bSAndreas Gohr<?php
25511bd5bSAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
45511bd5bSAndreas Gohr
57234bfb1Ssplitbrainuse dokuwiki\Extension\Event;
67234bfb1Ssplitbrain
75511bd5bSAndreas Gohr/**
85511bd5bSAndreas Gohr * Class ConfigParser
95511bd5bSAndreas Gohr *
105511bd5bSAndreas Gohr * Utilities to parse the configuration syntax into an array
115511bd5bSAndreas Gohr *
12ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
135511bd5bSAndreas Gohr */
14d6d97f60SAnna Dabrowskaclass ConfigParser
15d6d97f60SAnna Dabrowska{
167fe2cdf2SAndreas Gohr    protected $config = [
177fe2cdf2SAndreas Gohr        'limit' => 0,
187fe2cdf2SAndreas Gohr        'dynfilters' => false,
197fe2cdf2SAndreas Gohr        'summarize' => false,
207fe2cdf2SAndreas Gohr        'rownumbers' => false,
217fe2cdf2SAndreas Gohr        'sepbyheaders' => false,
227fe2cdf2SAndreas Gohr        'target' => '',
237fe2cdf2SAndreas Gohr        'align' => [],
247fe2cdf2SAndreas Gohr        'headers' => [],
257fe2cdf2SAndreas Gohr        'cols' => [],
267fe2cdf2SAndreas Gohr        'widths' => [],
277fe2cdf2SAndreas Gohr        'filter' => [],
287fe2cdf2SAndreas Gohr        'schemas' => [],
297fe2cdf2SAndreas Gohr        'sort' => [],
307fe2cdf2SAndreas Gohr        'csv' => true,
317fe2cdf2SAndreas Gohr        'nesting' => 0,
327fe2cdf2SAndreas Gohr        'index' => 0,
33*9fbb0f8bSAndreas Gohr        'classes' => [],
34*9fbb0f8bSAndreas Gohr        'actcol' => -1,
357fe2cdf2SAndreas Gohr    ];
365511bd5bSAndreas Gohr
375511bd5bSAndreas Gohr    /**
385511bd5bSAndreas Gohr     * Parser constructor.
395511bd5bSAndreas Gohr     *
405511bd5bSAndreas Gohr     * parses the given configuration lines
415511bd5bSAndreas Gohr     *
425511bd5bSAndreas Gohr     * @param $lines
435511bd5bSAndreas Gohr     */
44d6d97f60SAnna Dabrowska    public function __construct($lines)
45d6d97f60SAnna Dabrowska    {
4629877279SMichael Große        /** @var \helper_plugin_struct_config $helper */
4729877279SMichael Große        $helper = plugin_load('helper', 'struct_config');
485511bd5bSAndreas Gohr        // parse info
495511bd5bSAndreas Gohr        foreach ($lines as $line) {
507234bfb1Ssplitbrain            [$key, $val] = $this->splitLine($line);
515511bd5bSAndreas Gohr            if (!$key) continue;
525511bd5bSAndreas Gohr
535511bd5bSAndreas Gohr            $logic = 'OR';
545511bd5bSAndreas Gohr            // handle line commands (we allow various aliases here)
555511bd5bSAndreas Gohr            switch ($key) {
565511bd5bSAndreas Gohr                case 'from':
575511bd5bSAndreas Gohr                case 'schema':
585511bd5bSAndreas Gohr                case 'tables':
595511bd5bSAndreas Gohr                    $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val));
605511bd5bSAndreas Gohr                    break;
615511bd5bSAndreas Gohr                case 'select':
625511bd5bSAndreas Gohr                case 'cols':
635511bd5bSAndreas Gohr                case 'field':
645511bd5bSAndreas Gohr                case 'col':
655511bd5bSAndreas Gohr                    $this->config['cols'] = $this->parseValues($val);
665511bd5bSAndreas Gohr                    break;
67ea5ad12aSMichael Grosse                case 'sepbyheaders':
68ea5ad12aSMichael Grosse                    $this->config['sepbyheaders'] = (bool)$val;
69ea5ad12aSMichael Grosse                    break;
705511bd5bSAndreas Gohr                case 'head':
715511bd5bSAndreas Gohr                case 'header':
725511bd5bSAndreas Gohr                case 'headers':
735511bd5bSAndreas Gohr                    $this->config['headers'] = $this->parseValues($val);
745511bd5bSAndreas Gohr                    break;
755511bd5bSAndreas Gohr                case 'align':
765511bd5bSAndreas Gohr                    $this->config['align'] = $this->parseAlignments($val);
775511bd5bSAndreas Gohr                    break;
78e0216289SAndreas Gohr                case 'width':
795511bd5bSAndreas Gohr                case 'widths':
80e0216289SAndreas Gohr                    $this->config['widths'] = $this->parseWidths($val);
815511bd5bSAndreas Gohr                    break;
825511bd5bSAndreas Gohr                case 'min':
835511bd5bSAndreas Gohr                    $this->config['min'] = abs((int)$val);
845511bd5bSAndreas Gohr                    break;
855511bd5bSAndreas Gohr                case 'limit':
865511bd5bSAndreas Gohr                case 'max':
875511bd5bSAndreas Gohr                    $this->config['limit'] = abs((int)$val);
885511bd5bSAndreas Gohr                    break;
895511bd5bSAndreas Gohr                case 'order':
905511bd5bSAndreas Gohr                case 'sort':
916047d2b8SAndreas Gohr                    $sorts = $this->parseValues($val);
927234bfb1Ssplitbrain                    $sorts = array_map([$helper, 'parseSort'], $sorts);
936047d2b8SAndreas Gohr                    $this->config['sort'] = array_merge($this->config['sort'], $sorts);
945511bd5bSAndreas Gohr                    break;
955511bd5bSAndreas Gohr                case 'where':
965511bd5bSAndreas Gohr                case 'filter':
97748e747fSAnna Dabrowska                case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
985511bd5bSAndreas Gohr                    /** @noinspection PhpMissingBreakStatementInspection */
99748e747fSAnna Dabrowska                case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
1005511bd5bSAndreas Gohr                    $logic = 'AND';
1015511bd5bSAndreas Gohr                case 'filteror':
1025511bd5bSAndreas Gohr                case 'or':
10329877279SMichael Große                    $flt = $helper->parseFilterLine($logic, $val);
1045511bd5bSAndreas Gohr                    if ($flt) {
1055511bd5bSAndreas Gohr                        $this->config['filter'][] = $flt;
1065511bd5bSAndreas Gohr                    }
1075511bd5bSAndreas Gohr                    break;
1085511bd5bSAndreas Gohr                case 'dynfilters':
1095511bd5bSAndreas Gohr                    $this->config['dynfilters'] = (bool)$val;
1105511bd5bSAndreas Gohr                    break;
1115511bd5bSAndreas Gohr                case 'rownumbers':
1125511bd5bSAndreas Gohr                    $this->config['rownumbers'] = (bool)$val;
1135511bd5bSAndreas Gohr                    break;
1145511bd5bSAndreas Gohr                case 'summarize':
1155511bd5bSAndreas Gohr                    $this->config['summarize'] = (bool)$val;
1165511bd5bSAndreas Gohr                    break;
1177b240ca8SAndreas Gohr                case 'csv':
1187b240ca8SAndreas Gohr                    $this->config['csv'] = (bool)$val;
1197b240ca8SAndreas Gohr                    break;
12085edf4f2SMichael Grosse                case 'target':
12185edf4f2SMichael Grosse                case 'page':
12285edf4f2SMichael Grosse                    $this->config['target'] = cleanID($val);
12385edf4f2SMichael Grosse                    break;
1245bc00e11SAndreas Gohr                case 'nesting':
1255bc00e11SAndreas Gohr                case 'nest':
1265bc00e11SAndreas Gohr                    $this->config['nesting'] = (int) $val;
1275bc00e11SAndreas Gohr                    break;
128ce44c639SAndreas Gohr                case 'index':
129ce44c639SAndreas Gohr                    $this->config['index'] = (int) $val;
130ce44c639SAndreas Gohr                    break;
131af0ce8d2SAndreas Gohr                case 'class':
132af0ce8d2SAndreas Gohr                case 'classes':
133af0ce8d2SAndreas Gohr                    $this->config['classes'] = $this->parseClasses($val);
134af0ce8d2SAndreas Gohr                    break;
135*9fbb0f8bSAndreas Gohr                case 'actcol':
136*9fbb0f8bSAndreas Gohr                case 'actioncol':
137*9fbb0f8bSAndreas Gohr                case 'actioncolumn':
138*9fbb0f8bSAndreas Gohr                    $this->config['actcol'] = (int) $val;
139*9fbb0f8bSAndreas Gohr                    break;
1405511bd5bSAndreas Gohr                default:
1417234bfb1Ssplitbrain                    $data = ['config' => &$this->config, 'key' => $key, 'val' => $val];
1427234bfb1Ssplitbrain                    $ev = new Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data);
14329a2b794SAndreas Gohr                    if ($ev->advise_before()) {
1447d88e7edSAndreas Gohr                        throw new StructException("unknown option '%s'", hsc($key));
1455511bd5bSAndreas Gohr                    }
14629a2b794SAndreas Gohr                    $ev->advise_after();
14729a2b794SAndreas Gohr            }
1485511bd5bSAndreas Gohr        }
14901dd90deSAndreas Gohr
15001dd90deSAndreas Gohr        // fill up headers - a NULL signifies that the column label is wanted
1515511bd5bSAndreas Gohr        $this->config['headers'] = (array)$this->config['headers'];
1525511bd5bSAndreas Gohr        $cnth = count($this->config['headers']);
1535511bd5bSAndreas Gohr        $cntf = count($this->config['cols']);
1545511bd5bSAndreas Gohr        for ($i = $cnth; $i < $cntf; $i++) {
15501dd90deSAndreas Gohr            $this->config['headers'][] = null;
1565511bd5bSAndreas Gohr        }
15795eef580SAndreas Gohr        // fill up alignments
15895eef580SAndreas Gohr        $cnta = count($this->config['align']);
15995eef580SAndreas Gohr        for ($i = $cnta; $i < $cntf; $i++) {
16095eef580SAndreas Gohr            $this->config['align'][] = null;
16195eef580SAndreas Gohr        }
1625511bd5bSAndreas Gohr    }
1635511bd5bSAndreas Gohr
1645511bd5bSAndreas Gohr    /**
1655511bd5bSAndreas Gohr     * Get the parsed configuration
1665511bd5bSAndreas Gohr     *
1675511bd5bSAndreas Gohr     * @return array
1685511bd5bSAndreas Gohr     */
169d6d97f60SAnna Dabrowska    public function getConfig()
170d6d97f60SAnna Dabrowska    {
1715511bd5bSAndreas Gohr        return $this->config;
1725511bd5bSAndreas Gohr    }
1735511bd5bSAndreas Gohr
1745511bd5bSAndreas Gohr    /**
1755511bd5bSAndreas Gohr     * Splits the given line into key and value
1765511bd5bSAndreas Gohr     *
1775511bd5bSAndreas Gohr     * @param $line
17842d332d7SAndreas Gohr     * @return array returns ['',''] if the line is empty
1795511bd5bSAndreas Gohr     */
180d6d97f60SAnna Dabrowska    protected function splitLine($line)
181d6d97f60SAnna Dabrowska    {
1825511bd5bSAndreas Gohr        // ignore comments
1835511bd5bSAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
1845511bd5bSAndreas Gohr        $line = str_replace('\\#', '#', $line);
1855511bd5bSAndreas Gohr        $line = trim($line);
18642d332d7SAndreas Gohr        if (empty($line)) return ['', ''];
1875511bd5bSAndreas Gohr
1885511bd5bSAndreas Gohr        $line = preg_split('/\s*:\s*/', $line, 2);
1895511bd5bSAndreas Gohr        $line[0] = strtolower($line[0]);
19042d332d7SAndreas Gohr        if (!isset($line[1])) $line[1] = '';
1915511bd5bSAndreas Gohr
1925511bd5bSAndreas Gohr        return $line;
1935511bd5bSAndreas Gohr    }
1945511bd5bSAndreas Gohr
1955511bd5bSAndreas Gohr    /**
1965511bd5bSAndreas Gohr     * parses schema config and aliases
1975511bd5bSAndreas Gohr     *
1985511bd5bSAndreas Gohr     * @param $val
1995511bd5bSAndreas Gohr     * @return array
2005511bd5bSAndreas Gohr     */
201d6d97f60SAnna Dabrowska    protected function parseSchema($val)
202d6d97f60SAnna Dabrowska    {
2037234bfb1Ssplitbrain        $schemas = [];
2045511bd5bSAndreas Gohr        $parts = explode(',', $val);
2055511bd5bSAndreas Gohr        foreach ($parts as $part) {
2067fe2cdf2SAndreas Gohr            [$table, $alias] = sexplode(' ', trim($part), 2, '');
2075511bd5bSAndreas Gohr            $table = trim($table);
2085511bd5bSAndreas Gohr            $alias = trim($alias);
2095511bd5bSAndreas Gohr            if (!$table) continue;
2105511bd5bSAndreas Gohr
2117234bfb1Ssplitbrain            $schemas[] = [$table, $alias];
2125511bd5bSAndreas Gohr        }
2135511bd5bSAndreas Gohr        return $schemas;
2145511bd5bSAndreas Gohr    }
2155511bd5bSAndreas Gohr
2165511bd5bSAndreas Gohr    /**
2175511bd5bSAndreas Gohr     * Parse alignment data
2185511bd5bSAndreas Gohr     *
2195511bd5bSAndreas Gohr     * @param string $val
2205511bd5bSAndreas Gohr     * @return string[]
2215511bd5bSAndreas Gohr     */
222d6d97f60SAnna Dabrowska    protected function parseAlignments($val)
223d6d97f60SAnna Dabrowska    {
2245511bd5bSAndreas Gohr        $cols = explode(',', $val);
2257234bfb1Ssplitbrain        $data = [];
2265511bd5bSAndreas Gohr        foreach ($cols as $col) {
2275511bd5bSAndreas Gohr            $col = trim(strtolower($col));
2285511bd5bSAndreas Gohr            if ($col[0] == 'c') {
2295511bd5bSAndreas Gohr                $align = 'center';
2305511bd5bSAndreas Gohr            } elseif ($col[0] == 'r') {
2315511bd5bSAndreas Gohr                $align = 'right';
2329cda5805SAndreas Gohr            } elseif ($col[0] == 'l') {
2335511bd5bSAndreas Gohr                $align = 'left';
2349cda5805SAndreas Gohr            } else {
2359cda5805SAndreas Gohr                $align = null;
2365511bd5bSAndreas Gohr            }
2375511bd5bSAndreas Gohr            $data[] = $align;
2385511bd5bSAndreas Gohr        }
2395511bd5bSAndreas Gohr
2405511bd5bSAndreas Gohr        return $data;
2415511bd5bSAndreas Gohr    }
2425511bd5bSAndreas Gohr
2435511bd5bSAndreas Gohr    /**
244e0216289SAndreas Gohr     * Parse width data
245e0216289SAndreas Gohr     *
246e0216289SAndreas Gohr     * @param $val
247e0216289SAndreas Gohr     * @return array
248e0216289SAndreas Gohr     */
249d6d97f60SAnna Dabrowska    protected function parseWidths($val)
250d6d97f60SAnna Dabrowska    {
251e0216289SAndreas Gohr        $vals = explode(',', $val);
252e0216289SAndreas Gohr        $vals = array_map('trim', $vals);
2537234bfb1Ssplitbrain
254e0216289SAndreas Gohr        $len = count($vals);
255e0216289SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
256e0216289SAndreas Gohr            $val = trim(strtolower($vals[$i]));
257e0216289SAndreas Gohr
258e0216289SAndreas Gohr            if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) {
259e0216289SAndreas Gohr                // proper CSS unit?
260e0216289SAndreas Gohr                $vals[$i] = $val;
261e0216289SAndreas Gohr            } elseif (preg_match('/^\d+$/', $val)) {
262e0216289SAndreas Gohr                // decimal only?
263e0216289SAndreas Gohr                $vals[$i] = $val . 'px';
264e0216289SAndreas Gohr            } else {
265e0216289SAndreas Gohr                // invalid
266e0216289SAndreas Gohr                $vals[$i] = '';
267e0216289SAndreas Gohr            }
268e0216289SAndreas Gohr        }
269e0216289SAndreas Gohr        return $vals;
270e0216289SAndreas Gohr    }
271e0216289SAndreas Gohr
272e0216289SAndreas Gohr    /**
2735511bd5bSAndreas Gohr     * Split values at the commas,
2745511bd5bSAndreas Gohr     * - Wrap with quotes to escape comma, quotes escaped by two quotes
2755511bd5bSAndreas Gohr     * - Within quotes spaces are stored.
2765511bd5bSAndreas Gohr     *
2775511bd5bSAndreas Gohr     * @param string $line
2785511bd5bSAndreas Gohr     * @return array
2795511bd5bSAndreas Gohr     */
280d6d97f60SAnna Dabrowska    protected function parseValues($line)
281d6d97f60SAnna Dabrowska    {
2827234bfb1Ssplitbrain        $values = [];
2835511bd5bSAndreas Gohr        $inQuote = false;
2845511bd5bSAndreas Gohr        $escapedQuote = false;
2855511bd5bSAndreas Gohr        $value = '';
2865511bd5bSAndreas Gohr        $len = strlen($line);
2875511bd5bSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
28862c804ccSAnna Dabrowska            if ($line[$i] === '"') {
2895511bd5bSAndreas Gohr                if ($inQuote) {
2905511bd5bSAndreas Gohr                    if ($escapedQuote) {
2915511bd5bSAndreas Gohr                        $value .= '"';
2925511bd5bSAndreas Gohr                        $escapedQuote = false;
2935511bd5bSAndreas Gohr                        continue;
2945511bd5bSAndreas Gohr                    }
29562c804ccSAnna Dabrowska                    if (isset($line[$i + 1]) && $line[$i + 1] === '"') {
2965511bd5bSAndreas Gohr                        $escapedQuote = true;
2975511bd5bSAndreas Gohr                        continue;
2985511bd5bSAndreas Gohr                    }
2997234bfb1Ssplitbrain                    $values[] = $value;
3005511bd5bSAndreas Gohr                    $inQuote = false;
3015511bd5bSAndreas Gohr                    $value = '';
3025511bd5bSAndreas Gohr                    continue;
3035511bd5bSAndreas Gohr                } else {
3045511bd5bSAndreas Gohr                    $inQuote = true;
3055511bd5bSAndreas Gohr                    $value = ''; //don't store stuff before the opening quote
3065511bd5bSAndreas Gohr                    continue;
3075511bd5bSAndreas Gohr                }
30862c804ccSAnna Dabrowska            } elseif ($line[$i] === ',') {
3095511bd5bSAndreas Gohr                if ($inQuote) {
3105511bd5bSAndreas Gohr                    $value .= ',';
3115511bd5bSAndreas Gohr                    continue;
3125511bd5bSAndreas Gohr                } else {
3135511bd5bSAndreas Gohr                    if (strlen($value) < 1) {
3145511bd5bSAndreas Gohr                        continue;
3155511bd5bSAndreas Gohr                    }
3167234bfb1Ssplitbrain                    $values[] = trim($value);
3175511bd5bSAndreas Gohr                    $value = '';
3185511bd5bSAndreas Gohr                    continue;
3195511bd5bSAndreas Gohr                }
3205511bd5bSAndreas Gohr            }
321d982cb29SMichael Große            $value .= $line[$i];
3225511bd5bSAndreas Gohr        }
3235511bd5bSAndreas Gohr        if (strlen($value) > 0) {
3247234bfb1Ssplitbrain            $values[] = trim($value);
3255511bd5bSAndreas Gohr        }
3265511bd5bSAndreas Gohr        return $values;
3275511bd5bSAndreas Gohr    }
328af0ce8d2SAndreas Gohr
329af0ce8d2SAndreas Gohr    /**
330af0ce8d2SAndreas Gohr     * Ensure custom classes are valid and don't clash
331af0ce8d2SAndreas Gohr     *
332af0ce8d2SAndreas Gohr     * @param string $line
333af0ce8d2SAndreas Gohr     * @return string[]
334af0ce8d2SAndreas Gohr     */
335af0ce8d2SAndreas Gohr    protected function parseClasses($line)
336af0ce8d2SAndreas Gohr    {
337af0ce8d2SAndreas Gohr        $classes = $this->parseValues($line);
338af0ce8d2SAndreas Gohr        $classes = array_map(function ($class) {
339af0ce8d2SAndreas Gohr            $class = str_replace(' ', '_', $class);
340af0ce8d2SAndreas Gohr            $class = preg_replace('/[^a-zA-Z0-9_]/', '', $class);
341af0ce8d2SAndreas Gohr            return 'struct-custom-' . $class;
342af0ce8d2SAndreas Gohr        }, $classes);
343af0ce8d2SAndreas Gohr        return $classes;
344af0ce8d2SAndreas Gohr    }
3455511bd5bSAndreas Gohr}
346