xref: /plugin/struct/meta/ConfigParser.php (revision ce44c6393fcd559a7f07942c55824e5b8379912c)
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,
43*ce44c639SAndreas Gohr            'index' => 0,
445511bd5bSAndreas Gohr        );
455511bd5bSAndreas Gohr        // parse info
465511bd5bSAndreas Gohr        foreach ($lines as $line) {
4742d332d7SAndreas Gohr            list($key, $val) = $this->splitLine($line);
485511bd5bSAndreas Gohr            if (!$key) continue;
495511bd5bSAndreas Gohr
505511bd5bSAndreas Gohr            $logic = 'OR';
515511bd5bSAndreas Gohr            // handle line commands (we allow various aliases here)
525511bd5bSAndreas Gohr            switch ($key) {
535511bd5bSAndreas Gohr                case 'from':
545511bd5bSAndreas Gohr                case 'schema':
555511bd5bSAndreas Gohr                case 'tables':
565511bd5bSAndreas Gohr                    $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val));
575511bd5bSAndreas Gohr                    break;
585511bd5bSAndreas Gohr                case 'select':
595511bd5bSAndreas Gohr                case 'cols':
605511bd5bSAndreas Gohr                case 'field':
615511bd5bSAndreas Gohr                case 'col':
625511bd5bSAndreas Gohr                    $this->config['cols'] = $this->parseValues($val);
635511bd5bSAndreas Gohr                    break;
64ea5ad12aSMichael Grosse                case 'sepbyheaders':
65ea5ad12aSMichael Grosse                    $this->config['sepbyheaders'] = (bool)$val;
66ea5ad12aSMichael Grosse                    break;
675511bd5bSAndreas Gohr                case 'head':
685511bd5bSAndreas Gohr                case 'header':
695511bd5bSAndreas Gohr                case 'headers':
705511bd5bSAndreas Gohr                    $this->config['headers'] = $this->parseValues($val);
715511bd5bSAndreas Gohr                    break;
725511bd5bSAndreas Gohr                case 'align':
735511bd5bSAndreas Gohr                    $this->config['align'] = $this->parseAlignments($val);
745511bd5bSAndreas Gohr                    break;
75e0216289SAndreas Gohr                case 'width':
765511bd5bSAndreas Gohr                case 'widths':
77e0216289SAndreas Gohr                    $this->config['widths'] = $this->parseWidths($val);
785511bd5bSAndreas Gohr                    break;
795511bd5bSAndreas Gohr                case 'min':
805511bd5bSAndreas Gohr                    $this->config['min'] = abs((int)$val);
815511bd5bSAndreas Gohr                    break;
825511bd5bSAndreas Gohr                case 'limit':
835511bd5bSAndreas Gohr                case 'max':
845511bd5bSAndreas Gohr                    $this->config['limit'] = abs((int)$val);
855511bd5bSAndreas Gohr                    break;
865511bd5bSAndreas Gohr                case 'order':
875511bd5bSAndreas Gohr                case 'sort':
886047d2b8SAndreas Gohr                    $sorts = $this->parseValues($val);
896047d2b8SAndreas Gohr                    $sorts = array_map(array($helper, 'parseSort'), $sorts);
906047d2b8SAndreas Gohr                    $this->config['sort'] = array_merge($this->config['sort'], $sorts);
915511bd5bSAndreas Gohr                    break;
925511bd5bSAndreas Gohr                case 'where':
935511bd5bSAndreas Gohr                case 'filter':
94748e747fSAnna Dabrowska                case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
955511bd5bSAndreas Gohr                    /** @noinspection PhpMissingBreakStatementInspection */
96748e747fSAnna Dabrowska                case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment
975511bd5bSAndreas Gohr                    $logic = 'AND';
985511bd5bSAndreas Gohr                case 'filteror':
995511bd5bSAndreas Gohr                case 'or':
10029877279SMichael Große                    $flt = $helper->parseFilterLine($logic, $val);
1015511bd5bSAndreas Gohr                    if ($flt) {
1025511bd5bSAndreas Gohr                        $this->config['filter'][] = $flt;
1035511bd5bSAndreas Gohr                    }
1045511bd5bSAndreas Gohr                    break;
1055511bd5bSAndreas Gohr                case 'dynfilters':
1065511bd5bSAndreas Gohr                    $this->config['dynfilters'] = (bool)$val;
1075511bd5bSAndreas Gohr                    break;
1085511bd5bSAndreas Gohr                case 'rownumbers':
1095511bd5bSAndreas Gohr                    $this->config['rownumbers'] = (bool)$val;
1105511bd5bSAndreas Gohr                    break;
1115511bd5bSAndreas Gohr                case 'summarize':
1125511bd5bSAndreas Gohr                    $this->config['summarize'] = (bool)$val;
1135511bd5bSAndreas Gohr                    break;
1147b240ca8SAndreas Gohr                case 'csv':
1157b240ca8SAndreas Gohr                    $this->config['csv'] = (bool)$val;
1167b240ca8SAndreas Gohr                    break;
11785edf4f2SMichael Grosse                case 'target':
11885edf4f2SMichael Grosse                case 'page':
11985edf4f2SMichael Grosse                    $this->config['target'] = cleanID($val);
12085edf4f2SMichael Grosse                    break;
1215bc00e11SAndreas Gohr                case 'nesting':
1225bc00e11SAndreas Gohr                case 'nest':
1235bc00e11SAndreas Gohr                    $this->config['nesting'] = (int) $val;
1245bc00e11SAndreas Gohr                    break;
125*ce44c639SAndreas Gohr                case 'index':
126*ce44c639SAndreas Gohr                    $this->config['index'] = (int) $val;
127*ce44c639SAndreas Gohr                    break;
1285511bd5bSAndreas Gohr                default:
12929a2b794SAndreas Gohr                    $data = array('config' => &$this->config, 'key' => $key, 'val' => $val);
13029a2b794SAndreas Gohr                    $ev = new \Doku_Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data);
13129a2b794SAndreas Gohr                    if ($ev->advise_before()) {
1327d88e7edSAndreas Gohr                        throw new StructException("unknown option '%s'", hsc($key));
1335511bd5bSAndreas Gohr                    }
13429a2b794SAndreas Gohr                    $ev->advise_after();
13529a2b794SAndreas Gohr            }
1365511bd5bSAndreas Gohr        }
13701dd90deSAndreas Gohr
13801dd90deSAndreas Gohr        // fill up headers - a NULL signifies that the column label is wanted
1395511bd5bSAndreas Gohr        $this->config['headers'] = (array)$this->config['headers'];
1405511bd5bSAndreas Gohr        $cnth = count($this->config['headers']);
1415511bd5bSAndreas Gohr        $cntf = count($this->config['cols']);
1425511bd5bSAndreas Gohr        for ($i = $cnth; $i < $cntf; $i++) {
14301dd90deSAndreas Gohr            $this->config['headers'][] = null;
1445511bd5bSAndreas Gohr        }
14595eef580SAndreas Gohr        // fill up alignments
14695eef580SAndreas Gohr        $cnta = count($this->config['align']);
14795eef580SAndreas Gohr        for ($i = $cnta; $i < $cntf; $i++) {
14895eef580SAndreas Gohr            $this->config['align'][] = null;
14995eef580SAndreas Gohr        }
1505511bd5bSAndreas Gohr    }
1515511bd5bSAndreas Gohr
1525511bd5bSAndreas Gohr    /**
1535511bd5bSAndreas Gohr     * Get the parsed configuration
1545511bd5bSAndreas Gohr     *
1555511bd5bSAndreas Gohr     * @return array
1565511bd5bSAndreas Gohr     */
157d6d97f60SAnna Dabrowska    public function getConfig()
158d6d97f60SAnna Dabrowska    {
1595511bd5bSAndreas Gohr        return $this->config;
1605511bd5bSAndreas Gohr    }
1615511bd5bSAndreas Gohr
1625511bd5bSAndreas Gohr    /**
1635511bd5bSAndreas Gohr     * Splits the given line into key and value
1645511bd5bSAndreas Gohr     *
1655511bd5bSAndreas Gohr     * @param $line
16642d332d7SAndreas Gohr     * @return array returns ['',''] if the line is empty
1675511bd5bSAndreas Gohr     */
168d6d97f60SAnna Dabrowska    protected function splitLine($line)
169d6d97f60SAnna Dabrowska    {
1705511bd5bSAndreas Gohr        // ignore comments
1715511bd5bSAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
1725511bd5bSAndreas Gohr        $line = str_replace('\\#', '#', $line);
1735511bd5bSAndreas Gohr        $line = trim($line);
17442d332d7SAndreas Gohr        if (empty($line)) return ['', ''];
1755511bd5bSAndreas Gohr
1765511bd5bSAndreas Gohr        $line = preg_split('/\s*:\s*/', $line, 2);
1775511bd5bSAndreas Gohr        $line[0] = strtolower($line[0]);
17842d332d7SAndreas Gohr        if (!isset($line[1])) $line[1] = '';
1795511bd5bSAndreas Gohr
1805511bd5bSAndreas Gohr        return $line;
1815511bd5bSAndreas Gohr    }
1825511bd5bSAndreas Gohr
1835511bd5bSAndreas Gohr    /**
1845511bd5bSAndreas Gohr     * parses schema config and aliases
1855511bd5bSAndreas Gohr     *
1865511bd5bSAndreas Gohr     * @param $val
1875511bd5bSAndreas Gohr     * @return array
1885511bd5bSAndreas Gohr     */
189d6d97f60SAnna Dabrowska    protected function parseSchema($val)
190d6d97f60SAnna Dabrowska    {
1915511bd5bSAndreas Gohr        $schemas = array();
1925511bd5bSAndreas Gohr        $parts = explode(',', $val);
1935511bd5bSAndreas Gohr        foreach ($parts as $part) {
19425ed0c0dSAndreas Gohr            @list($table, $alias) = array_pad(explode(' ', trim($part)), 2, '');
1955511bd5bSAndreas Gohr            $table = trim($table);
1965511bd5bSAndreas Gohr            $alias = trim($alias);
1975511bd5bSAndreas Gohr            if (!$table) continue;
1985511bd5bSAndreas Gohr
19929877279SMichael Große            $schemas[] = array($table, $alias,);
2005511bd5bSAndreas Gohr        }
2015511bd5bSAndreas Gohr        return $schemas;
2025511bd5bSAndreas Gohr    }
2035511bd5bSAndreas Gohr
2045511bd5bSAndreas Gohr    /**
2055511bd5bSAndreas Gohr     * Parse alignment data
2065511bd5bSAndreas Gohr     *
2075511bd5bSAndreas Gohr     * @param string $val
2085511bd5bSAndreas Gohr     * @return string[]
2095511bd5bSAndreas Gohr     */
210d6d97f60SAnna Dabrowska    protected function parseAlignments($val)
211d6d97f60SAnna Dabrowska    {
2125511bd5bSAndreas Gohr        $cols = explode(',', $val);
2135511bd5bSAndreas Gohr        $data = array();
2145511bd5bSAndreas Gohr        foreach ($cols as $col) {
2155511bd5bSAndreas Gohr            $col = trim(strtolower($col));
2165511bd5bSAndreas Gohr            if ($col[0] == 'c') {
2175511bd5bSAndreas Gohr                $align = 'center';
2185511bd5bSAndreas Gohr            } elseif ($col[0] == 'r') {
2195511bd5bSAndreas Gohr                $align = 'right';
2209cda5805SAndreas Gohr            } elseif ($col[0] == 'l') {
2215511bd5bSAndreas Gohr                $align = 'left';
2229cda5805SAndreas Gohr            } else {
2239cda5805SAndreas Gohr                $align = null;
2245511bd5bSAndreas Gohr            }
2255511bd5bSAndreas Gohr            $data[] = $align;
2265511bd5bSAndreas Gohr        }
2275511bd5bSAndreas Gohr
2285511bd5bSAndreas Gohr        return $data;
2295511bd5bSAndreas Gohr    }
2305511bd5bSAndreas Gohr
2315511bd5bSAndreas Gohr    /**
232e0216289SAndreas Gohr     * Parse width data
233e0216289SAndreas Gohr     *
234e0216289SAndreas Gohr     * @param $val
235e0216289SAndreas Gohr     * @return array
236e0216289SAndreas Gohr     */
237d6d97f60SAnna Dabrowska    protected function parseWidths($val)
238d6d97f60SAnna Dabrowska    {
239e0216289SAndreas Gohr        $vals = explode(',', $val);
240e0216289SAndreas Gohr        $vals = array_map('trim', $vals);
241e0216289SAndreas Gohr        $len = count($vals);
242e0216289SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
243e0216289SAndreas Gohr            $val = trim(strtolower($vals[$i]));
244e0216289SAndreas Gohr
245e0216289SAndreas Gohr            if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) {
246e0216289SAndreas Gohr                // proper CSS unit?
247e0216289SAndreas Gohr                $vals[$i] = $val;
248e0216289SAndreas Gohr            } elseif (preg_match('/^\d+$/', $val)) {
249e0216289SAndreas Gohr                // decimal only?
250e0216289SAndreas Gohr                $vals[$i] = $val . 'px';
251e0216289SAndreas Gohr            } else {
252e0216289SAndreas Gohr                // invalid
253e0216289SAndreas Gohr                $vals[$i] = '';
254e0216289SAndreas Gohr            }
255e0216289SAndreas Gohr        }
256e0216289SAndreas Gohr        return $vals;
257e0216289SAndreas Gohr    }
258e0216289SAndreas Gohr
259e0216289SAndreas Gohr    /**
2605511bd5bSAndreas Gohr     * Split values at the commas,
2615511bd5bSAndreas Gohr     * - Wrap with quotes to escape comma, quotes escaped by two quotes
2625511bd5bSAndreas Gohr     * - Within quotes spaces are stored.
2635511bd5bSAndreas Gohr     *
2645511bd5bSAndreas Gohr     * @param string $line
2655511bd5bSAndreas Gohr     * @return array
2665511bd5bSAndreas Gohr     */
267d6d97f60SAnna Dabrowska    protected function parseValues($line)
268d6d97f60SAnna Dabrowska    {
2695511bd5bSAndreas Gohr        $values = array();
2705511bd5bSAndreas Gohr        $inQuote = false;
2715511bd5bSAndreas Gohr        $escapedQuote = false;
2725511bd5bSAndreas Gohr        $value = '';
2735511bd5bSAndreas Gohr        $len = strlen($line);
2745511bd5bSAndreas Gohr        for ($i = 0; $i < $len; $i++) {
275d982cb29SMichael Große            if ($line[$i] == '"') {
2765511bd5bSAndreas Gohr                if ($inQuote) {
2775511bd5bSAndreas Gohr                    if ($escapedQuote) {
2785511bd5bSAndreas Gohr                        $value .= '"';
2795511bd5bSAndreas Gohr                        $escapedQuote = false;
2805511bd5bSAndreas Gohr                        continue;
2815511bd5bSAndreas Gohr                    }
282d982cb29SMichael Große                    if ($line[$i + 1] == '"') {
2835511bd5bSAndreas Gohr                        $escapedQuote = true;
2845511bd5bSAndreas Gohr                        continue;
2855511bd5bSAndreas Gohr                    }
2865511bd5bSAndreas Gohr                    array_push($values, $value);
2875511bd5bSAndreas Gohr                    $inQuote = false;
2885511bd5bSAndreas Gohr                    $value = '';
2895511bd5bSAndreas Gohr                    continue;
2905511bd5bSAndreas Gohr                } else {
2915511bd5bSAndreas Gohr                    $inQuote = true;
2925511bd5bSAndreas Gohr                    $value = ''; //don't store stuff before the opening quote
2935511bd5bSAndreas Gohr                    continue;
2945511bd5bSAndreas Gohr                }
295d982cb29SMichael Große            } elseif ($line[$i] == ',') {
2965511bd5bSAndreas Gohr                if ($inQuote) {
2975511bd5bSAndreas Gohr                    $value .= ',';
2985511bd5bSAndreas Gohr                    continue;
2995511bd5bSAndreas Gohr                } else {
3005511bd5bSAndreas Gohr                    if (strlen($value) < 1) {
3015511bd5bSAndreas Gohr                        continue;
3025511bd5bSAndreas Gohr                    }
3035511bd5bSAndreas Gohr                    array_push($values, trim($value));
3045511bd5bSAndreas Gohr                    $value = '';
3055511bd5bSAndreas Gohr                    continue;
3065511bd5bSAndreas Gohr                }
3075511bd5bSAndreas Gohr            }
308d982cb29SMichael Große            $value .= $line[$i];
3095511bd5bSAndreas Gohr        }
3105511bd5bSAndreas Gohr        if (strlen($value) > 0) {
3115511bd5bSAndreas Gohr            array_push($values, trim($value));
3125511bd5bSAndreas Gohr        }
3135511bd5bSAndreas Gohr        return $values;
3145511bd5bSAndreas Gohr    }
3155511bd5bSAndreas Gohr}
316