xref: /plugin/struct/meta/ConfigParser.php (revision af0ce8d2fb493ed201ed374daddbc04d257baa9c)
15511bd5bSAndreas Gohr<?php
25511bd5bSAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
45511bd5bSAndreas Gohr
55511bd5bSAndreas Gohr/**
65511bd5bSAndreas Gohr * Class ConfigParser
75511bd5bSAndreas Gohr *
85511bd5bSAndreas Gohr * Utilities to parse the configuration syntax into an array
95511bd5bSAndreas Gohr *
10ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
115511bd5bSAndreas Gohr */
12d6d97f60SAnna Dabrowskaclass ConfigParser
13d6d97f60SAnna Dabrowska{
145511bd5bSAndreas Gohr    protected $config = array();
155511bd5bSAndreas Gohr
165511bd5bSAndreas Gohr    /**
175511bd5bSAndreas Gohr     * Parser constructor.
185511bd5bSAndreas Gohr     *
195511bd5bSAndreas Gohr     * parses the given configuration lines
205511bd5bSAndreas Gohr     *
215511bd5bSAndreas Gohr     * @param $lines
225511bd5bSAndreas Gohr     */
23d6d97f60SAnna Dabrowska    public function __construct($lines)
24d6d97f60SAnna Dabrowska    {
2529877279SMichael Große        /** @var \helper_plugin_struct_config $helper */
2629877279SMichael Große        $helper = plugin_load('helper', 'struct_config');
27d930c197SMichael Große        $this->config = array(
285511bd5bSAndreas Gohr            'limit' => 0,
295511bd5bSAndreas Gohr            'dynfilters' => false,
305511bd5bSAndreas Gohr            'summarize' => false,
315511bd5bSAndreas Gohr            'rownumbers' => false,
325511bd5bSAndreas Gohr            'sepbyheaders' => false,
3385edf4f2SMichael Grosse            'target' => '',
349cda5805SAndreas Gohr            'align' => array(),
355511bd5bSAndreas Gohr            'headers' => array(),
36d93ddfc7SSzymon Olewniczak            'cols' => array(),
375511bd5bSAndreas Gohr            'widths' => array(),
385511bd5bSAndreas Gohr            'filter' => array(),
397051cfa1SMichael Große            'schemas' => array(),
407b240ca8SAndreas Gohr            'sort' => array(),
417b240ca8SAndreas Gohr            'csv' => true,
425bc00e11SAndreas Gohr            'nesting' => 0,
43ce44c639SAndreas Gohr            'index' => 0,
44*af0ce8d2SAndreas Gohr            'classes' => array(),
455511bd5bSAndreas Gohr        );
465511bd5bSAndreas Gohr        // parse info
475511bd5bSAndreas Gohr        foreach ($lines as $line) {
4842d332d7SAndreas Gohr            list($key, $val) = $this->splitLine($line);
495511bd5bSAndreas Gohr            if (!$key) continue;
505511bd5bSAndreas Gohr
515511bd5bSAndreas Gohr            $logic = 'OR';
525511bd5bSAndreas Gohr            // handle line commands (we allow various aliases here)
535511bd5bSAndreas Gohr            switch ($key) {
545511bd5bSAndreas Gohr                case 'from':
555511bd5bSAndreas Gohr                case 'schema':
565511bd5bSAndreas Gohr                case 'tables':
575511bd5bSAndreas Gohr                    $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val));
585511bd5bSAndreas Gohr                    break;
595511bd5bSAndreas Gohr                case 'select':
605511bd5bSAndreas Gohr                case 'cols':
615511bd5bSAndreas Gohr                case 'field':
625511bd5bSAndreas Gohr                case 'col':
635511bd5bSAndreas Gohr                    $this->config['cols'] = $this->parseValues($val);
645511bd5bSAndreas Gohr                    break;
65ea5ad12aSMichael Grosse                case 'sepbyheaders':
66ea5ad12aSMichael Grosse                    $this->config['sepbyheaders'] = (bool)$val;
67ea5ad12aSMichael Grosse                    break;
685511bd5bSAndreas Gohr                case 'head':
695511bd5bSAndreas Gohr                case 'header':
705511bd5bSAndreas Gohr                case 'headers':
715511bd5bSAndreas Gohr                    $this->config['headers'] = $this->parseValues($val);
725511bd5bSAndreas Gohr                    break;
735511bd5bSAndreas Gohr                case 'align':
745511bd5bSAndreas Gohr                    $this->config['align'] = $this->parseAlignments($val);
755511bd5bSAndreas Gohr                    break;
76e0216289SAndreas Gohr                case 'width':
775511bd5bSAndreas Gohr                case 'widths':
78e0216289SAndreas Gohr                    $this->config['widths'] = $this->parseWidths($val);
795511bd5bSAndreas Gohr                    break;
805511bd5bSAndreas Gohr                case 'min':
815511bd5bSAndreas Gohr                    $this->config['min'] = abs((int)$val);
825511bd5bSAndreas Gohr                    break;
835511bd5bSAndreas Gohr                case 'limit':
845511bd5bSAndreas Gohr                case 'max':
855511bd5bSAndreas Gohr                    $this->config['limit'] = abs((int)$val);
865511bd5bSAndreas Gohr                    break;
875511bd5bSAndreas Gohr                case 'order':
885511bd5bSAndreas Gohr                case 'sort':
896047d2b8SAndreas Gohr                    $sorts = $this->parseValues($val);
906047d2b8SAndreas Gohr                    $sorts = array_map(array($helper, 'parseSort'), $sorts);
916047d2b8SAndreas Gohr                    $this->config['sort'] = array_merge($this->config['sort'], $sorts);
925511bd5bSAndreas Gohr                    break;
935511bd5bSAndreas Gohr                case 'where':
945511bd5bSAndreas Gohr                case 'filter':
95748e747fSAnna Dabrowska                case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
965511bd5bSAndreas Gohr                    /** @noinspection PhpMissingBreakStatementInspection */
97748e747fSAnna Dabrowska                case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
985511bd5bSAndreas Gohr                    $logic = 'AND';
995511bd5bSAndreas Gohr                case 'filteror':
1005511bd5bSAndreas Gohr                case 'or':
10129877279SMichael Große                    $flt = $helper->parseFilterLine($logic, $val);
1025511bd5bSAndreas Gohr                    if ($flt) {
1035511bd5bSAndreas Gohr                        $this->config['filter'][] = $flt;
1045511bd5bSAndreas Gohr                    }
1055511bd5bSAndreas Gohr                    break;
1065511bd5bSAndreas Gohr                case 'dynfilters':
1075511bd5bSAndreas Gohr                    $this->config['dynfilters'] = (bool)$val;
1085511bd5bSAndreas Gohr                    break;
1095511bd5bSAndreas Gohr                case 'rownumbers':
1105511bd5bSAndreas Gohr                    $this->config['rownumbers'] = (bool)$val;
1115511bd5bSAndreas Gohr                    break;
1125511bd5bSAndreas Gohr                case 'summarize':
1135511bd5bSAndreas Gohr                    $this->config['summarize'] = (bool)$val;
1145511bd5bSAndreas Gohr                    break;
1157b240ca8SAndreas Gohr                case 'csv':
1167b240ca8SAndreas Gohr                    $this->config['csv'] = (bool)$val;
1177b240ca8SAndreas Gohr                    break;
11885edf4f2SMichael Grosse                case 'target':
11985edf4f2SMichael Grosse                case 'page':
12085edf4f2SMichael Grosse                    $this->config['target'] = cleanID($val);
12185edf4f2SMichael Grosse                    break;
1225bc00e11SAndreas Gohr                case 'nesting':
1235bc00e11SAndreas Gohr                case 'nest':
1245bc00e11SAndreas Gohr                    $this->config['nesting'] = (int) $val;
1255bc00e11SAndreas Gohr                    break;
126ce44c639SAndreas Gohr                case 'index':
127ce44c639SAndreas Gohr                    $this->config['index'] = (int) $val;
128ce44c639SAndreas Gohr                    break;
129*af0ce8d2SAndreas Gohr                case 'class':
130*af0ce8d2SAndreas Gohr                case 'classes':
131*af0ce8d2SAndreas Gohr                    $this->config['classes'] = $this->parseClasses($val);
132*af0ce8d2SAndreas Gohr                    break;
1335511bd5bSAndreas Gohr                default:
13429a2b794SAndreas Gohr                    $data = array('config' => &$this->config, 'key' => $key, 'val' => $val);
13529a2b794SAndreas Gohr                    $ev = new \Doku_Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data);
13629a2b794SAndreas Gohr                    if ($ev->advise_before()) {
1377d88e7edSAndreas Gohr                        throw new StructException("unknown option '%s'", hsc($key));
1385511bd5bSAndreas Gohr                    }
13929a2b794SAndreas Gohr                    $ev->advise_after();
14029a2b794SAndreas Gohr            }
1415511bd5bSAndreas Gohr        }
14201dd90deSAndreas Gohr
14301dd90deSAndreas Gohr        // fill up headers - a NULL signifies that the column label is wanted
1445511bd5bSAndreas Gohr        $this->config['headers'] = (array)$this->config['headers'];
1455511bd5bSAndreas Gohr        $cnth = count($this->config['headers']);
1465511bd5bSAndreas Gohr        $cntf = count($this->config['cols']);
1475511bd5bSAndreas Gohr        for ($i = $cnth; $i < $cntf; $i++) {
14801dd90deSAndreas Gohr            $this->config['headers'][] = null;
1495511bd5bSAndreas Gohr        }
15095eef580SAndreas Gohr        // fill up alignments
15195eef580SAndreas Gohr        $cnta = count($this->config['align']);
15295eef580SAndreas Gohr        for ($i = $cnta; $i < $cntf; $i++) {
15395eef580SAndreas Gohr            $this->config['align'][] = null;
15495eef580SAndreas Gohr        }
1555511bd5bSAndreas Gohr    }
1565511bd5bSAndreas Gohr
1575511bd5bSAndreas Gohr    /**
1585511bd5bSAndreas Gohr     * Get the parsed configuration
1595511bd5bSAndreas Gohr     *
1605511bd5bSAndreas Gohr     * @return array
1615511bd5bSAndreas Gohr     */
162d6d97f60SAnna Dabrowska    public function getConfig()
163d6d97f60SAnna Dabrowska    {
1645511bd5bSAndreas Gohr        return $this->config;
1655511bd5bSAndreas Gohr    }
1665511bd5bSAndreas Gohr
1675511bd5bSAndreas Gohr    /**
1685511bd5bSAndreas Gohr     * Splits the given line into key and value
1695511bd5bSAndreas Gohr     *
1705511bd5bSAndreas Gohr     * @param $line
17142d332d7SAndreas Gohr     * @return array returns ['',''] if the line is empty
1725511bd5bSAndreas Gohr     */
173d6d97f60SAnna Dabrowska    protected function splitLine($line)
174d6d97f60SAnna Dabrowska    {
1755511bd5bSAndreas Gohr        // ignore comments
1765511bd5bSAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
1775511bd5bSAndreas Gohr        $line = str_replace('\\#', '#', $line);
1785511bd5bSAndreas Gohr        $line = trim($line);
17942d332d7SAndreas Gohr        if (empty($line)) return ['', ''];
1805511bd5bSAndreas Gohr
1815511bd5bSAndreas Gohr        $line = preg_split('/\s*:\s*/', $line, 2);
1825511bd5bSAndreas Gohr        $line[0] = strtolower($line[0]);
18342d332d7SAndreas Gohr        if (!isset($line[1])) $line[1] = '';
1845511bd5bSAndreas Gohr
1855511bd5bSAndreas Gohr        return $line;
1865511bd5bSAndreas Gohr    }
1875511bd5bSAndreas Gohr
1885511bd5bSAndreas Gohr    /**
1895511bd5bSAndreas Gohr     * parses schema config and aliases
1905511bd5bSAndreas Gohr     *
1915511bd5bSAndreas Gohr     * @param $val
1925511bd5bSAndreas Gohr     * @return array
1935511bd5bSAndreas Gohr     */
194d6d97f60SAnna Dabrowska    protected function parseSchema($val)
195d6d97f60SAnna Dabrowska    {
1965511bd5bSAndreas Gohr        $schemas = array();
1975511bd5bSAndreas Gohr        $parts = explode(',', $val);
1985511bd5bSAndreas Gohr        foreach ($parts as $part) {
19925ed0c0dSAndreas Gohr            @list($table, $alias) = array_pad(explode(' ', trim($part)), 2, '');
2005511bd5bSAndreas Gohr            $table = trim($table);
2015511bd5bSAndreas Gohr            $alias = trim($alias);
2025511bd5bSAndreas Gohr            if (!$table) continue;
2035511bd5bSAndreas Gohr
20429877279SMichael Große            $schemas[] = array($table, $alias,);
2055511bd5bSAndreas Gohr        }
2065511bd5bSAndreas Gohr        return $schemas;
2075511bd5bSAndreas Gohr    }
2085511bd5bSAndreas Gohr
2095511bd5bSAndreas Gohr    /**
2105511bd5bSAndreas Gohr     * Parse alignment data
2115511bd5bSAndreas Gohr     *
2125511bd5bSAndreas Gohr     * @param string $val
2135511bd5bSAndreas Gohr     * @return string[]
2145511bd5bSAndreas Gohr     */
215d6d97f60SAnna Dabrowska    protected function parseAlignments($val)
216d6d97f60SAnna Dabrowska    {
2175511bd5bSAndreas Gohr        $cols = explode(',', $val);
2185511bd5bSAndreas Gohr        $data = array();
2195511bd5bSAndreas Gohr        foreach ($cols as $col) {
2205511bd5bSAndreas Gohr            $col = trim(strtolower($col));
2215511bd5bSAndreas Gohr            if ($col[0] == 'c') {
2225511bd5bSAndreas Gohr                $align = 'center';
2235511bd5bSAndreas Gohr            } elseif ($col[0] == 'r') {
2245511bd5bSAndreas Gohr                $align = 'right';
2259cda5805SAndreas Gohr            } elseif ($col[0] == 'l') {
2265511bd5bSAndreas Gohr                $align = 'left';
2279cda5805SAndreas Gohr            } else {
2289cda5805SAndreas Gohr                $align = null;
2295511bd5bSAndreas Gohr            }
2305511bd5bSAndreas Gohr            $data[] = $align;
2315511bd5bSAndreas Gohr        }
2325511bd5bSAndreas Gohr
2335511bd5bSAndreas Gohr        return $data;
2345511bd5bSAndreas Gohr    }
2355511bd5bSAndreas Gohr
2365511bd5bSAndreas Gohr    /**
237e0216289SAndreas Gohr     * Parse width data
238e0216289SAndreas Gohr     *
239e0216289SAndreas Gohr     * @param $val
240e0216289SAndreas Gohr     * @return array
241e0216289SAndreas Gohr     */
242d6d97f60SAnna Dabrowska    protected function parseWidths($val)
243d6d97f60SAnna Dabrowska    {
244e0216289SAndreas Gohr        $vals = explode(',', $val);
245e0216289SAndreas Gohr        $vals = array_map('trim', $vals);
246e0216289SAndreas Gohr        $len = count($vals);
247e0216289SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
248e0216289SAndreas Gohr            $val = trim(strtolower($vals[$i]));
249e0216289SAndreas Gohr
250e0216289SAndreas Gohr            if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) {
251e0216289SAndreas Gohr                // proper CSS unit?
252e0216289SAndreas Gohr                $vals[$i] = $val;
253e0216289SAndreas Gohr            } elseif (preg_match('/^\d+$/', $val)) {
254e0216289SAndreas Gohr                // decimal only?
255e0216289SAndreas Gohr                $vals[$i] = $val . 'px';
256e0216289SAndreas Gohr            } else {
257e0216289SAndreas Gohr                // invalid
258e0216289SAndreas Gohr                $vals[$i] = '';
259e0216289SAndreas Gohr            }
260e0216289SAndreas Gohr        }
261e0216289SAndreas Gohr        return $vals;
262e0216289SAndreas Gohr    }
263e0216289SAndreas Gohr
264e0216289SAndreas Gohr    /**
2655511bd5bSAndreas Gohr     * Split values at the commas,
2665511bd5bSAndreas Gohr     * - Wrap with quotes to escape comma, quotes escaped by two quotes
2675511bd5bSAndreas Gohr     * - Within quotes spaces are stored.
2685511bd5bSAndreas Gohr     *
2695511bd5bSAndreas Gohr     * @param string $line
2705511bd5bSAndreas Gohr     * @return array
2715511bd5bSAndreas Gohr     */
272d6d97f60SAnna Dabrowska    protected function parseValues($line)
273d6d97f60SAnna Dabrowska    {
2745511bd5bSAndreas Gohr        $values = array();
2755511bd5bSAndreas Gohr        $inQuote = false;
2765511bd5bSAndreas Gohr        $escapedQuote = false;
2775511bd5bSAndreas Gohr        $value = '';
2785511bd5bSAndreas Gohr        $len = strlen($line);
2795511bd5bSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
280d982cb29SMichael Große            if ($line[$i] == '"') {
2815511bd5bSAndreas Gohr                if ($inQuote) {
2825511bd5bSAndreas Gohr                    if ($escapedQuote) {
2835511bd5bSAndreas Gohr                        $value .= '"';
2845511bd5bSAndreas Gohr                        $escapedQuote = false;
2855511bd5bSAndreas Gohr                        continue;
2865511bd5bSAndreas Gohr                    }
287d982cb29SMichael Große                    if ($line[$i + 1] == '"') {
2885511bd5bSAndreas Gohr                        $escapedQuote = true;
2895511bd5bSAndreas Gohr                        continue;
2905511bd5bSAndreas Gohr                    }
2915511bd5bSAndreas Gohr                    array_push($values, $value);
2925511bd5bSAndreas Gohr                    $inQuote = false;
2935511bd5bSAndreas Gohr                    $value = '';
2945511bd5bSAndreas Gohr                    continue;
2955511bd5bSAndreas Gohr                } else {
2965511bd5bSAndreas Gohr                    $inQuote = true;
2975511bd5bSAndreas Gohr                    $value = ''; //don't store stuff before the opening quote
2985511bd5bSAndreas Gohr                    continue;
2995511bd5bSAndreas Gohr                }
300d982cb29SMichael Große            } elseif ($line[$i] == ',') {
3015511bd5bSAndreas Gohr                if ($inQuote) {
3025511bd5bSAndreas Gohr                    $value .= ',';
3035511bd5bSAndreas Gohr                    continue;
3045511bd5bSAndreas Gohr                } else {
3055511bd5bSAndreas Gohr                    if (strlen($value) < 1) {
3065511bd5bSAndreas Gohr                        continue;
3075511bd5bSAndreas Gohr                    }
3085511bd5bSAndreas Gohr                    array_push($values, trim($value));
3095511bd5bSAndreas Gohr                    $value = '';
3105511bd5bSAndreas Gohr                    continue;
3115511bd5bSAndreas Gohr                }
3125511bd5bSAndreas Gohr            }
313d982cb29SMichael Große            $value .= $line[$i];
3145511bd5bSAndreas Gohr        }
3155511bd5bSAndreas Gohr        if (strlen($value) > 0) {
3165511bd5bSAndreas Gohr            array_push($values, trim($value));
3175511bd5bSAndreas Gohr        }
3185511bd5bSAndreas Gohr        return $values;
3195511bd5bSAndreas Gohr    }
320*af0ce8d2SAndreas Gohr
321*af0ce8d2SAndreas Gohr    /**
322*af0ce8d2SAndreas Gohr     * Ensure custom classes are valid and don't clash
323*af0ce8d2SAndreas Gohr     *
324*af0ce8d2SAndreas Gohr     * @param string $line
325*af0ce8d2SAndreas Gohr     * @return string[]
326*af0ce8d2SAndreas Gohr     */
327*af0ce8d2SAndreas Gohr    protected function parseClasses($line)
328*af0ce8d2SAndreas Gohr    {
329*af0ce8d2SAndreas Gohr        $classes = $this->parseValues($line);
330*af0ce8d2SAndreas Gohr        $classes = array_map(function ($class) {
331*af0ce8d2SAndreas Gohr            $class = str_replace(' ', '_', $class);
332*af0ce8d2SAndreas Gohr            $class = preg_replace('/[^a-zA-Z0-9_]/', '', $class);
333*af0ce8d2SAndreas Gohr            return 'struct-custom-' . $class;
334*af0ce8d2SAndreas Gohr        }, $classes);
335*af0ce8d2SAndreas Gohr        return $classes;
336*af0ce8d2SAndreas Gohr    }
3375511bd5bSAndreas Gohr}
338