xref: /plugin/struct/meta/ConfigParser.php (revision 74461852bb3d2f1d8303b85e43da7f86db2e99a4)
15511bd5bSAndreas Gohr<?php
25511bd5bSAndreas Gohr
35511bd5bSAndreas Gohrnamespace 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 *
105511bd5bSAndreas Gohr * @package plugin\struct\meta
115511bd5bSAndreas Gohr */
125511bd5bSAndreas Gohrclass ConfigParser {
135511bd5bSAndreas Gohr
145511bd5bSAndreas Gohr
155511bd5bSAndreas Gohr    protected $config = array();
165511bd5bSAndreas Gohr
175511bd5bSAndreas Gohr    /**
185511bd5bSAndreas Gohr     * Parser constructor.
195511bd5bSAndreas Gohr     *
205511bd5bSAndreas Gohr     * parses the given configuration lines
215511bd5bSAndreas Gohr     *
225511bd5bSAndreas Gohr     * @param $lines
235511bd5bSAndreas Gohr     */
245511bd5bSAndreas Gohr    public function __construct($lines) {
255511bd5bSAndreas Gohr        $data = array(
265511bd5bSAndreas Gohr            'limit' => 0,
275511bd5bSAndreas Gohr            'dynfilters' => false,
285511bd5bSAndreas Gohr            'summarize' => false,
295511bd5bSAndreas Gohr            'rownumbers' => false,
305511bd5bSAndreas Gohr            'sepbyheaders' => false,
315511bd5bSAndreas Gohr            'headers' => array(),
325511bd5bSAndreas Gohr            'widths' => array(),
335511bd5bSAndreas Gohr            'filter' => array(),
345511bd5bSAndreas Gohr            'schemas' => array()
355511bd5bSAndreas Gohr        );
365511bd5bSAndreas Gohr        // parse info
375511bd5bSAndreas Gohr        foreach($lines as $line) {
385511bd5bSAndreas Gohr            list($key, $val) = $this->splitLine($line);
395511bd5bSAndreas Gohr            if(!$key) continue;
405511bd5bSAndreas Gohr
415511bd5bSAndreas Gohr            $logic = 'OR';
425511bd5bSAndreas Gohr            // handle line commands (we allow various aliases here)
435511bd5bSAndreas Gohr            switch($key) {
445511bd5bSAndreas Gohr                case 'from':
455511bd5bSAndreas Gohr                case 'schema':
465511bd5bSAndreas Gohr                case 'tables':
475511bd5bSAndreas Gohr                    $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val));
485511bd5bSAndreas Gohr                    break;
495511bd5bSAndreas Gohr                case 'select':
505511bd5bSAndreas Gohr                case 'cols':
515511bd5bSAndreas Gohr                case 'field':
525511bd5bSAndreas Gohr                case 'col':
535511bd5bSAndreas Gohr                    $this->config['cols'] = $this->parseValues($val);
545511bd5bSAndreas Gohr                    break;
555511bd5bSAndreas Gohr                case 'title':
565511bd5bSAndreas Gohr                    $this->config['title'] = $val;
575511bd5bSAndreas Gohr                    break;
585511bd5bSAndreas Gohr                case 'head':
595511bd5bSAndreas Gohr                case 'header':
605511bd5bSAndreas Gohr                case 'headers':
615511bd5bSAndreas Gohr                    $this->config['headers'] = $this->parseValues($val);
625511bd5bSAndreas Gohr                    break;
635511bd5bSAndreas Gohr                case 'align':
645511bd5bSAndreas Gohr                    $this->config['align'] = $this->parseAlignments($val);
655511bd5bSAndreas Gohr                    break;
665511bd5bSAndreas Gohr                case 'widths':
675511bd5bSAndreas Gohr                    $this->config['width'] = $this->parseValues($val);
685511bd5bSAndreas Gohr                    break;
695511bd5bSAndreas Gohr                case 'min':
705511bd5bSAndreas Gohr                    $this->config['min'] = abs((int) $val);
715511bd5bSAndreas Gohr                    break;
725511bd5bSAndreas Gohr                case 'limit':
735511bd5bSAndreas Gohr                case 'max':
745511bd5bSAndreas Gohr                    $this->config['limit'] = abs((int) $val);
755511bd5bSAndreas Gohr                    break;
765511bd5bSAndreas Gohr                case 'order':
775511bd5bSAndreas Gohr                case 'sort':
785511bd5bSAndreas Gohr                    // FIXME multiple values!?
795511bd5bSAndreas Gohr                    if(substr($val, 0, 1) == '^') {
805511bd5bSAndreas Gohr                        $this->config['sort'] = array(substr($val, 1), 'DESC');
815511bd5bSAndreas Gohr                    } else {
825511bd5bSAndreas Gohr                        $this->config['sort'] = array($val, 'ASC');
835511bd5bSAndreas Gohr                    }
845511bd5bSAndreas Gohr                    break;
855511bd5bSAndreas Gohr                case 'where':
865511bd5bSAndreas Gohr                case 'filter':
875511bd5bSAndreas Gohr                case 'filterand':
885511bd5bSAndreas Gohr                    /** @noinspection PhpMissingBreakStatementInspection */
895511bd5bSAndreas Gohr                case 'and':
905511bd5bSAndreas Gohr                    $logic = 'AND';
915511bd5bSAndreas Gohr                case 'filteror':
925511bd5bSAndreas Gohr                case 'or':
935511bd5bSAndreas Gohr                    $flt = $this->parseFilter($val);
945511bd5bSAndreas Gohr                    if($flt) {
955511bd5bSAndreas Gohr                        $flt[] = $logic;
965511bd5bSAndreas Gohr                        $this->config['filter'][] = $flt;
975511bd5bSAndreas Gohr                    }
985511bd5bSAndreas Gohr                    break;
995511bd5bSAndreas Gohr                case 'page':
1005511bd5bSAndreas Gohr                case 'target':
1015511bd5bSAndreas Gohr                    $this->config['page'] = cleanID($val);
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;
1125511bd5bSAndreas Gohr                case 'sepbyheaders':
1135511bd5bSAndreas Gohr                    $this->config['sepbyheaders'] = (bool) $val;
1145511bd5bSAndreas Gohr                    break;
1155511bd5bSAndreas Gohr                default:
1165511bd5bSAndreas Gohr                    throw new StructException("unknown option '%s'", hsc($val));
1175511bd5bSAndreas Gohr            }
1185511bd5bSAndreas Gohr        }
1195511bd5bSAndreas Gohr        // we need at least one column to display
1205511bd5bSAndreas Gohr        // fill up headers with field names if necessary
1215511bd5bSAndreas Gohr        $this->config['headers'] = (array) $this->config['headers'];
1225511bd5bSAndreas Gohr        $cnth = count($this->config['headers']);
1235511bd5bSAndreas Gohr        $cntf = count($this->config['cols']);
1245511bd5bSAndreas Gohr        for($i = $cnth; $i < $cntf; $i++) {
1255511bd5bSAndreas Gohr            $column = array_slice($this->config['cols'], $i, 1);
1265511bd5bSAndreas Gohr            $columnprops = array_pop($column);
1275511bd5bSAndreas Gohr            $this->config['headers'][] = $columnprops['title'];
1285511bd5bSAndreas Gohr        }
1295511bd5bSAndreas Gohr
1305511bd5bSAndreas Gohr        return $data;
1315511bd5bSAndreas Gohr    }
1325511bd5bSAndreas Gohr
1335511bd5bSAndreas Gohr    /**
1345511bd5bSAndreas Gohr     * Get the parsed configuration
1355511bd5bSAndreas Gohr     *
1365511bd5bSAndreas Gohr     * @return array
1375511bd5bSAndreas Gohr     */
1385511bd5bSAndreas Gohr    public function getConfig() {
1395511bd5bSAndreas Gohr        return $this->config;
1405511bd5bSAndreas Gohr    }
1415511bd5bSAndreas Gohr
1425511bd5bSAndreas Gohr    /**
1435511bd5bSAndreas Gohr     * Splits the given line into key and value
1445511bd5bSAndreas Gohr     *
1455511bd5bSAndreas Gohr     * @param $line
1465511bd5bSAndreas Gohr     * @return bool|array returns false for empty lines
1475511bd5bSAndreas Gohr     */
1485511bd5bSAndreas Gohr    protected function splitLine($line) {
1495511bd5bSAndreas Gohr        // ignore comments
1505511bd5bSAndreas Gohr        $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line);
1515511bd5bSAndreas Gohr        $line = str_replace('\\#', '#', $line);
1525511bd5bSAndreas Gohr        $line = trim($line);
1535511bd5bSAndreas Gohr        if(empty($line)) return false;
1545511bd5bSAndreas Gohr
1555511bd5bSAndreas Gohr        $line = preg_split('/\s*:\s*/', $line, 2);
1565511bd5bSAndreas Gohr        $line[0] = strtolower($line[0]);
1575511bd5bSAndreas Gohr
1585511bd5bSAndreas Gohr        return $line;
1595511bd5bSAndreas Gohr    }
1605511bd5bSAndreas Gohr
1615511bd5bSAndreas Gohr    /**
1625511bd5bSAndreas Gohr     * parses schema config and aliases
1635511bd5bSAndreas Gohr     *
1645511bd5bSAndreas Gohr     * @param $val
1655511bd5bSAndreas Gohr     * @return array
1665511bd5bSAndreas Gohr     */
1675511bd5bSAndreas Gohr    protected function parseSchema($val){
1685511bd5bSAndreas Gohr        $schemas = array();
1695511bd5bSAndreas Gohr        $parts = explode(',', $val);
1705511bd5bSAndreas Gohr        foreach($parts as $part) {
1715511bd5bSAndreas Gohr            list($table, $alias) = explode(' ', $part);
1725511bd5bSAndreas Gohr            $table = trim($table);
1735511bd5bSAndreas Gohr            $alias = trim($alias);
1745511bd5bSAndreas Gohr            if(!$table) continue;
1755511bd5bSAndreas Gohr
1765511bd5bSAndreas Gohr            $schemas[] = array($table, $alias);
1775511bd5bSAndreas Gohr        }
1785511bd5bSAndreas Gohr        return $schemas;
1795511bd5bSAndreas Gohr    }
1805511bd5bSAndreas Gohr
1815511bd5bSAndreas Gohr    /**
1825511bd5bSAndreas Gohr     * Parse a filter
1835511bd5bSAndreas Gohr     *
1845511bd5bSAndreas Gohr     * @param string $val
1855511bd5bSAndreas Gohr     * @return array ($col, $comp, $value)
1865511bd5bSAndreas Gohr     */
1875511bd5bSAndreas Gohr    protected function parseFilter($val) {
1885511bd5bSAndreas Gohr
189*74461852SAndreas Gohr        $comps = Search::$COMPARATORS;
1905511bd5bSAndreas Gohr        $comps = array_map('preg_quote_cb', $comps);
1915511bd5bSAndreas Gohr        $comps = join('|', $comps);
1925511bd5bSAndreas Gohr
1935511bd5bSAndreas Gohr        if(!preg_match('/^(.*?)('.$comps.')(.*)$/', $val, $match)) {
1945511bd5bSAndreas Gohr            throw new StructException('Invalid search filter %s', hsc($val));
1955511bd5bSAndreas Gohr        }
1965511bd5bSAndreas Gohr        array_shift($match); // we don't need the zeroth match
1975511bd5bSAndreas Gohr        return $match;
1985511bd5bSAndreas Gohr    }
1995511bd5bSAndreas Gohr
2005511bd5bSAndreas Gohr
2015511bd5bSAndreas Gohr    /**
2025511bd5bSAndreas Gohr     * Parse alignment data
2035511bd5bSAndreas Gohr     *
2045511bd5bSAndreas Gohr     * @param string $val
2055511bd5bSAndreas Gohr     * @return string[]
2065511bd5bSAndreas Gohr     */
2075511bd5bSAndreas Gohr    protected function parseAlignments($val) {
2085511bd5bSAndreas Gohr        $cols = explode(',', $val);
2095511bd5bSAndreas Gohr        $data = array();
2105511bd5bSAndreas Gohr        foreach($cols as $col) {
2115511bd5bSAndreas Gohr            $col = trim(strtolower($col));
2125511bd5bSAndreas Gohr            if($col[0] == 'c') {
2135511bd5bSAndreas Gohr                $align = 'center';
2145511bd5bSAndreas Gohr            } elseif($col[0] == 'r') {
2155511bd5bSAndreas Gohr                $align = 'right';
2165511bd5bSAndreas Gohr            } else {
2175511bd5bSAndreas Gohr                $align = 'left';
2185511bd5bSAndreas Gohr            }
2195511bd5bSAndreas Gohr            $data[] = $align;
2205511bd5bSAndreas Gohr        }
2215511bd5bSAndreas Gohr
2225511bd5bSAndreas Gohr        return $data;
2235511bd5bSAndreas Gohr    }
2245511bd5bSAndreas Gohr
2255511bd5bSAndreas Gohr    /**
2265511bd5bSAndreas Gohr     * Split values at the commas,
2275511bd5bSAndreas Gohr     * - Wrap with quotes to escape comma, quotes escaped by two quotes
2285511bd5bSAndreas Gohr     * - Within quotes spaces are stored.
2295511bd5bSAndreas Gohr     *
2305511bd5bSAndreas Gohr     * @param string $line
2315511bd5bSAndreas Gohr     * @return array
2325511bd5bSAndreas Gohr     */
2335511bd5bSAndreas Gohr    protected function parseValues($line) {
2345511bd5bSAndreas Gohr        $values = array();
2355511bd5bSAndreas Gohr        $inQuote = false;
2365511bd5bSAndreas Gohr        $escapedQuote = false;
2375511bd5bSAndreas Gohr        $value = '';
2385511bd5bSAndreas Gohr        $len = strlen($line);
2395511bd5bSAndreas Gohr        for($i = 0; $i < $len; $i++) {
2405511bd5bSAndreas Gohr            if($line{$i} == '"') {
2415511bd5bSAndreas Gohr                if($inQuote) {
2425511bd5bSAndreas Gohr                    if($escapedQuote) {
2435511bd5bSAndreas Gohr                        $value .= '"';
2445511bd5bSAndreas Gohr                        $escapedQuote = false;
2455511bd5bSAndreas Gohr                        continue;
2465511bd5bSAndreas Gohr                    }
2475511bd5bSAndreas Gohr                    if($line{$i + 1} == '"') {
2485511bd5bSAndreas Gohr                        $escapedQuote = true;
2495511bd5bSAndreas Gohr                        continue;
2505511bd5bSAndreas Gohr                    }
2515511bd5bSAndreas Gohr                    array_push($values, $value);
2525511bd5bSAndreas Gohr                    $inQuote = false;
2535511bd5bSAndreas Gohr                    $value = '';
2545511bd5bSAndreas Gohr                    continue;
2555511bd5bSAndreas Gohr                } else {
2565511bd5bSAndreas Gohr                    $inQuote = true;
2575511bd5bSAndreas Gohr                    $value = ''; //don't store stuff before the opening quote
2585511bd5bSAndreas Gohr                    continue;
2595511bd5bSAndreas Gohr                }
2605511bd5bSAndreas Gohr            } else if($line{$i} == ',') {
2615511bd5bSAndreas Gohr                if($inQuote) {
2625511bd5bSAndreas Gohr                    $value .= ',';
2635511bd5bSAndreas Gohr                    continue;
2645511bd5bSAndreas Gohr                } else {
2655511bd5bSAndreas Gohr                    if(strlen($value) < 1) {
2665511bd5bSAndreas Gohr                        continue;
2675511bd5bSAndreas Gohr                    }
2685511bd5bSAndreas Gohr                    array_push($values, trim($value));
2695511bd5bSAndreas Gohr                    $value = '';
2705511bd5bSAndreas Gohr                    continue;
2715511bd5bSAndreas Gohr                }
2725511bd5bSAndreas Gohr            }
2735511bd5bSAndreas Gohr            $value .= $line{$i};
2745511bd5bSAndreas Gohr        }
2755511bd5bSAndreas Gohr        if(strlen($value) > 0) {
2765511bd5bSAndreas Gohr            array_push($values, trim($value));
2775511bd5bSAndreas Gohr        }
2785511bd5bSAndreas Gohr        return $values;
2795511bd5bSAndreas Gohr    }
2805511bd5bSAndreas Gohr
2815511bd5bSAndreas Gohr}
282