xref: /plugin/struct/meta/ConfigParser.php (revision 25ed0c0dbf1e045a4e27c1c3a21e99786851c54d)
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,
425511bd5bSAndreas Gohr        );
435511bd5bSAndreas Gohr        // parse info
445511bd5bSAndreas Gohr        foreach ($lines as $line) {
455511bd5bSAndreas Gohr            list($key, $val) = $this->splitLine($line);
465511bd5bSAndreas Gohr            if (!$key) continue;
475511bd5bSAndreas Gohr
485511bd5bSAndreas Gohr            $logic = 'OR';
495511bd5bSAndreas Gohr            // handle line commands (we allow various aliases here)
505511bd5bSAndreas Gohr            switch ($key) {
515511bd5bSAndreas Gohr                case 'from':
525511bd5bSAndreas Gohr                case 'schema':
535511bd5bSAndreas Gohr                case 'tables':
545511bd5bSAndreas Gohr                    $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val));
555511bd5bSAndreas Gohr                    break;
565511bd5bSAndreas Gohr                case 'select':
575511bd5bSAndreas Gohr                case 'cols':
585511bd5bSAndreas Gohr                case 'field':
595511bd5bSAndreas Gohr                case 'col':
605511bd5bSAndreas Gohr                    $this->config['cols'] = $this->parseValues($val);
615511bd5bSAndreas Gohr                    break;
62ea5ad12aSMichael Grosse                case 'sepbyheaders':
63ea5ad12aSMichael Grosse                    $this->config['sepbyheaders'] = (bool)$val;
64ea5ad12aSMichael Grosse                    break;
655511bd5bSAndreas Gohr                case 'head':
665511bd5bSAndreas Gohr                case 'header':
675511bd5bSAndreas Gohr                case 'headers':
685511bd5bSAndreas Gohr                    $this->config['headers'] = $this->parseValues($val);
695511bd5bSAndreas Gohr                    break;
705511bd5bSAndreas Gohr                case 'align':
715511bd5bSAndreas Gohr                    $this->config['align'] = $this->parseAlignments($val);
725511bd5bSAndreas Gohr                    break;
73e0216289SAndreas Gohr                case 'width':
745511bd5bSAndreas Gohr                case 'widths':
75e0216289SAndreas Gohr                    $this->config['widths'] = $this->parseWidths($val);
765511bd5bSAndreas Gohr                    break;
775511bd5bSAndreas Gohr                case 'min':
785511bd5bSAndreas Gohr                    $this->config['min'] = abs((int)$val);
795511bd5bSAndreas Gohr                    break;
805511bd5bSAndreas Gohr                case 'limit':
815511bd5bSAndreas Gohr                case 'max':
825511bd5bSAndreas Gohr                    $this->config['limit'] = abs((int)$val);
835511bd5bSAndreas Gohr                    break;
845511bd5bSAndreas Gohr                case 'order':
855511bd5bSAndreas Gohr                case 'sort':
866047d2b8SAndreas Gohr                    $sorts = $this->parseValues($val);
876047d2b8SAndreas Gohr                    $sorts = array_map(array($helper, 'parseSort'), $sorts);
886047d2b8SAndreas Gohr                    $this->config['sort'] = array_merge($this->config['sort'], $sorts);
895511bd5bSAndreas Gohr                    break;
905511bd5bSAndreas Gohr                case 'where':
915511bd5bSAndreas Gohr                case 'filter':
92748e747fSAnna Dabrowska                case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
935511bd5bSAndreas Gohr                    /** @noinspection PhpMissingBreakStatementInspection */
94748e747fSAnna Dabrowska                case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
955511bd5bSAndreas Gohr                    $logic = 'AND';
965511bd5bSAndreas Gohr                case 'filteror':
975511bd5bSAndreas Gohr                case 'or':
9829877279SMichael Große                    $flt = $helper->parseFilterLine($logic, $val);
995511bd5bSAndreas Gohr                    if ($flt) {
1005511bd5bSAndreas Gohr                        $this->config['filter'][] = $flt;
1015511bd5bSAndreas Gohr                    }
1025511bd5bSAndreas Gohr                    break;
1035511bd5bSAndreas Gohr                case 'dynfilters':
1045511bd5bSAndreas Gohr                    $this->config['dynfilters'] = (bool)$val;
1055511bd5bSAndreas Gohr                    break;
1065511bd5bSAndreas Gohr                case 'rownumbers':
1075511bd5bSAndreas Gohr                    $this->config['rownumbers'] = (bool)$val;
1085511bd5bSAndreas Gohr                    break;
1095511bd5bSAndreas Gohr                case 'summarize':
1105511bd5bSAndreas Gohr                    $this->config['summarize'] = (bool)$val;
1115511bd5bSAndreas Gohr                    break;
1127b240ca8SAndreas Gohr                case 'csv':
1137b240ca8SAndreas Gohr                    $this->config['csv'] = (bool)$val;
1147b240ca8SAndreas Gohr                    break;
11585edf4f2SMichael Grosse                case 'target':
11685edf4f2SMichael Grosse                case 'page':
11785edf4f2SMichael Grosse                    $this->config['target'] = cleanID($val);
11885edf4f2SMichael Grosse                    break;
1195511bd5bSAndreas Gohr                default:
12029a2b794SAndreas Gohr                    $data = array('config' => &$this->config, 'key' => $key, 'val' => $val);
12129a2b794SAndreas Gohr                    $ev = new \Doku_Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data);
12229a2b794SAndreas Gohr                    if ($ev->advise_before()) {
1237d88e7edSAndreas Gohr                        throw new StructException("unknown option '%s'", hsc($key));
1245511bd5bSAndreas Gohr                    }
12529a2b794SAndreas Gohr                    $ev->advise_after();
12629a2b794SAndreas Gohr            }
1275511bd5bSAndreas Gohr        }
12801dd90deSAndreas Gohr
12901dd90deSAndreas Gohr        // fill up headers - a NULL signifies that the column label is wanted
1305511bd5bSAndreas Gohr        $this->config['headers'] = (array)$this->config['headers'];
1315511bd5bSAndreas Gohr        $cnth = count($this->config['headers']);
1325511bd5bSAndreas Gohr        $cntf = count($this->config['cols']);
1335511bd5bSAndreas Gohr        for ($i = $cnth; $i < $cntf; $i++) {
13401dd90deSAndreas Gohr            $this->config['headers'][] = null;
1355511bd5bSAndreas Gohr        }
13695eef580SAndreas Gohr        // fill up alignments
13795eef580SAndreas Gohr        $cnta = count($this->config['align']);
13895eef580SAndreas Gohr        for ($i = $cnta; $i < $cntf; $i++) {
13995eef580SAndreas Gohr            $this->config['align'][] = null;
14095eef580SAndreas Gohr        }
1415511bd5bSAndreas Gohr    }
1425511bd5bSAndreas Gohr
1435511bd5bSAndreas Gohr    /**
1445511bd5bSAndreas Gohr     * Get the parsed configuration
1455511bd5bSAndreas Gohr     *
1465511bd5bSAndreas Gohr     * @return array
1475511bd5bSAndreas Gohr     */
148d6d97f60SAnna Dabrowska    public function getConfig()
149d6d97f60SAnna Dabrowska    {
1505511bd5bSAndreas Gohr        return $this->config;
1515511bd5bSAndreas Gohr    }
1525511bd5bSAndreas Gohr
1535511bd5bSAndreas Gohr    /**
1545511bd5bSAndreas Gohr     * Splits the given line into key and value
1555511bd5bSAndreas Gohr     *
1565511bd5bSAndreas Gohr     * @param $line
1575511bd5bSAndreas Gohr     * @return bool|array returns false for empty lines
1585511bd5bSAndreas Gohr     */
159d6d97f60SAnna Dabrowska    protected function splitLine($line)
160d6d97f60SAnna Dabrowska    {
1615511bd5bSAndreas Gohr        // ignore comments
1625511bd5bSAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
1635511bd5bSAndreas Gohr        $line = str_replace('\\#', '#', $line);
1645511bd5bSAndreas Gohr        $line = trim($line);
1655511bd5bSAndreas Gohr        if (empty($line)) return false;
1665511bd5bSAndreas Gohr
1675511bd5bSAndreas Gohr        $line = preg_split('/\s*:\s*/', $line, 2);
1685511bd5bSAndreas Gohr        $line[0] = strtolower($line[0]);
1695511bd5bSAndreas Gohr
1705511bd5bSAndreas Gohr        return $line;
1715511bd5bSAndreas Gohr    }
1725511bd5bSAndreas Gohr
1735511bd5bSAndreas Gohr    /**
1745511bd5bSAndreas Gohr     * parses schema config and aliases
1755511bd5bSAndreas Gohr     *
1765511bd5bSAndreas Gohr     * @param $val
1775511bd5bSAndreas Gohr     * @return array
1785511bd5bSAndreas Gohr     */
179d6d97f60SAnna Dabrowska    protected function parseSchema($val)
180d6d97f60SAnna Dabrowska    {
1815511bd5bSAndreas Gohr        $schemas = array();
1825511bd5bSAndreas Gohr        $parts = explode(',', $val);
1835511bd5bSAndreas Gohr        foreach ($parts as $part) {
184*25ed0c0dSAndreas Gohr            @list($table, $alias) = array_pad(explode(' ', trim($part)), 2, '');
1855511bd5bSAndreas Gohr            $table = trim($table);
1865511bd5bSAndreas Gohr            $alias = trim($alias);
1875511bd5bSAndreas Gohr            if (!$table) continue;
1885511bd5bSAndreas Gohr
18929877279SMichael Große            $schemas[] = array($table, $alias,);
1905511bd5bSAndreas Gohr        }
1915511bd5bSAndreas Gohr        return $schemas;
1925511bd5bSAndreas Gohr    }
1935511bd5bSAndreas Gohr
1945511bd5bSAndreas Gohr    /**
1955511bd5bSAndreas Gohr     * Parse alignment data
1965511bd5bSAndreas Gohr     *
1975511bd5bSAndreas Gohr     * @param string $val
1985511bd5bSAndreas Gohr     * @return string[]
1995511bd5bSAndreas Gohr     */
200d6d97f60SAnna Dabrowska    protected function parseAlignments($val)
201d6d97f60SAnna Dabrowska    {
2025511bd5bSAndreas Gohr        $cols = explode(',', $val);
2035511bd5bSAndreas Gohr        $data = array();
2045511bd5bSAndreas Gohr        foreach ($cols as $col) {
2055511bd5bSAndreas Gohr            $col = trim(strtolower($col));
2065511bd5bSAndreas Gohr            if ($col[0] == 'c') {
2075511bd5bSAndreas Gohr                $align = 'center';
2085511bd5bSAndreas Gohr            } elseif ($col[0] == 'r') {
2095511bd5bSAndreas Gohr                $align = 'right';
2109cda5805SAndreas Gohr            } elseif ($col[0] == 'l') {
2115511bd5bSAndreas Gohr                $align = 'left';
2129cda5805SAndreas Gohr            } else {
2139cda5805SAndreas Gohr                $align = null;
2145511bd5bSAndreas Gohr            }
2155511bd5bSAndreas Gohr            $data[] = $align;
2165511bd5bSAndreas Gohr        }
2175511bd5bSAndreas Gohr
2185511bd5bSAndreas Gohr        return $data;
2195511bd5bSAndreas Gohr    }
2205511bd5bSAndreas Gohr
2215511bd5bSAndreas Gohr    /**
222e0216289SAndreas Gohr     * Parse width data
223e0216289SAndreas Gohr     *
224e0216289SAndreas Gohr     * @param $val
225e0216289SAndreas Gohr     * @return array
226e0216289SAndreas Gohr     */
227d6d97f60SAnna Dabrowska    protected function parseWidths($val)
228d6d97f60SAnna Dabrowska    {
229e0216289SAndreas Gohr        $vals = explode(',', $val);
230e0216289SAndreas Gohr        $vals = array_map('trim', $vals);
231e0216289SAndreas Gohr        $len = count($vals);
232e0216289SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
233e0216289SAndreas Gohr            $val = trim(strtolower($vals[$i]));
234e0216289SAndreas Gohr
235e0216289SAndreas Gohr            if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) {
236e0216289SAndreas Gohr                // proper CSS unit?
237e0216289SAndreas Gohr                $vals[$i] = $val;
238e0216289SAndreas Gohr            } elseif (preg_match('/^\d+$/', $val)) {
239e0216289SAndreas Gohr                // decimal only?
240e0216289SAndreas Gohr                $vals[$i] = $val . 'px';
241e0216289SAndreas Gohr            } else {
242e0216289SAndreas Gohr                // invalid
243e0216289SAndreas Gohr                $vals[$i] = '';
244e0216289SAndreas Gohr            }
245e0216289SAndreas Gohr        }
246e0216289SAndreas Gohr        return $vals;
247e0216289SAndreas Gohr    }
248e0216289SAndreas Gohr
249e0216289SAndreas Gohr    /**
2505511bd5bSAndreas Gohr     * Split values at the commas,
2515511bd5bSAndreas Gohr     * - Wrap with quotes to escape comma, quotes escaped by two quotes
2525511bd5bSAndreas Gohr     * - Within quotes spaces are stored.
2535511bd5bSAndreas Gohr     *
2545511bd5bSAndreas Gohr     * @param string $line
2555511bd5bSAndreas Gohr     * @return array
2565511bd5bSAndreas Gohr     */
257d6d97f60SAnna Dabrowska    protected function parseValues($line)
258d6d97f60SAnna Dabrowska    {
2595511bd5bSAndreas Gohr        $values = array();
2605511bd5bSAndreas Gohr        $inQuote = false;
2615511bd5bSAndreas Gohr        $escapedQuote = false;
2625511bd5bSAndreas Gohr        $value = '';
2635511bd5bSAndreas Gohr        $len = strlen($line);
2645511bd5bSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
265d982cb29SMichael Große            if ($line[$i] == '"') {
2665511bd5bSAndreas Gohr                if ($inQuote) {
2675511bd5bSAndreas Gohr                    if ($escapedQuote) {
2685511bd5bSAndreas Gohr                        $value .= '"';
2695511bd5bSAndreas Gohr                        $escapedQuote = false;
2705511bd5bSAndreas Gohr                        continue;
2715511bd5bSAndreas Gohr                    }
272d982cb29SMichael Große                    if ($line[$i + 1] == '"') {
2735511bd5bSAndreas Gohr                        $escapedQuote = true;
2745511bd5bSAndreas Gohr                        continue;
2755511bd5bSAndreas Gohr                    }
2765511bd5bSAndreas Gohr                    array_push($values, $value);
2775511bd5bSAndreas Gohr                    $inQuote = false;
2785511bd5bSAndreas Gohr                    $value = '';
2795511bd5bSAndreas Gohr                    continue;
2805511bd5bSAndreas Gohr                } else {
2815511bd5bSAndreas Gohr                    $inQuote = true;
2825511bd5bSAndreas Gohr                    $value = ''; //don't store stuff before the opening quote
2835511bd5bSAndreas Gohr                    continue;
2845511bd5bSAndreas Gohr                }
285d982cb29SMichael Große            } elseif ($line[$i] == ',') {
2865511bd5bSAndreas Gohr                if ($inQuote) {
2875511bd5bSAndreas Gohr                    $value .= ',';
2885511bd5bSAndreas Gohr                    continue;
2895511bd5bSAndreas Gohr                } else {
2905511bd5bSAndreas Gohr                    if (strlen($value) < 1) {
2915511bd5bSAndreas Gohr                        continue;
2925511bd5bSAndreas Gohr                    }
2935511bd5bSAndreas Gohr                    array_push($values, trim($value));
2945511bd5bSAndreas Gohr                    $value = '';
2955511bd5bSAndreas Gohr                    continue;
2965511bd5bSAndreas Gohr                }
2975511bd5bSAndreas Gohr            }
298d982cb29SMichael Große            $value .= $line[$i];
2995511bd5bSAndreas Gohr        }
3005511bd5bSAndreas Gohr        if (strlen($value) > 0) {
3015511bd5bSAndreas Gohr            array_push($values, trim($value));
3025511bd5bSAndreas Gohr        }
3035511bd5bSAndreas Gohr        return $values;
3045511bd5bSAndreas Gohr    }
3055511bd5bSAndreas Gohr}
306