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 */ 12*d6d97f60SAnna Dabrowskaclass ConfigParser 13*d6d97f60SAnna Dabrowska{ 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 */ 24*d6d97f60SAnna Dabrowska public function __construct($lines) 25*d6d97f60SAnna Dabrowska { 2629877279SMichael Große /** @var \helper_plugin_struct_config $helper */ 2729877279SMichael Große $helper = plugin_load('helper', 'struct_config'); 28d930c197SMichael Große $this->config = array( 295511bd5bSAndreas Gohr 'limit' => 0, 305511bd5bSAndreas Gohr 'dynfilters' => false, 315511bd5bSAndreas Gohr 'summarize' => false, 325511bd5bSAndreas Gohr 'rownumbers' => false, 335511bd5bSAndreas Gohr 'sepbyheaders' => false, 3485edf4f2SMichael Grosse 'target' => '', 359cda5805SAndreas Gohr 'align' => array(), 365511bd5bSAndreas Gohr 'headers' => array(), 37d93ddfc7SSzymon Olewniczak 'cols' => array(), 385511bd5bSAndreas Gohr 'widths' => array(), 395511bd5bSAndreas Gohr 'filter' => array(), 407051cfa1SMichael Große 'schemas' => array(), 417b240ca8SAndreas Gohr 'sort' => array(), 427b240ca8SAndreas Gohr 'csv' => true, 435511bd5bSAndreas Gohr ); 445511bd5bSAndreas Gohr // parse info 455511bd5bSAndreas Gohr foreach ($lines as $line) { 465511bd5bSAndreas Gohr list($key, $val) = $this->splitLine($line); 475511bd5bSAndreas Gohr if (!$key) continue; 485511bd5bSAndreas Gohr 495511bd5bSAndreas Gohr $logic = 'OR'; 505511bd5bSAndreas Gohr // handle line commands (we allow various aliases here) 515511bd5bSAndreas Gohr switch ($key) { 525511bd5bSAndreas Gohr case 'from': 535511bd5bSAndreas Gohr case 'schema': 545511bd5bSAndreas Gohr case 'tables': 555511bd5bSAndreas Gohr $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val)); 565511bd5bSAndreas Gohr break; 575511bd5bSAndreas Gohr case 'select': 585511bd5bSAndreas Gohr case 'cols': 595511bd5bSAndreas Gohr case 'field': 605511bd5bSAndreas Gohr case 'col': 615511bd5bSAndreas Gohr $this->config['cols'] = $this->parseValues($val); 625511bd5bSAndreas Gohr break; 63ea5ad12aSMichael Grosse case 'sepbyheaders': 64ea5ad12aSMichael Grosse $this->config['sepbyheaders'] = (bool) $val; 65ea5ad12aSMichael Grosse break; 665511bd5bSAndreas Gohr case 'head': 675511bd5bSAndreas Gohr case 'header': 685511bd5bSAndreas Gohr case 'headers': 695511bd5bSAndreas Gohr $this->config['headers'] = $this->parseValues($val); 705511bd5bSAndreas Gohr break; 715511bd5bSAndreas Gohr case 'align': 725511bd5bSAndreas Gohr $this->config['align'] = $this->parseAlignments($val); 735511bd5bSAndreas Gohr break; 74e0216289SAndreas Gohr case 'width': 755511bd5bSAndreas Gohr case 'widths': 76e0216289SAndreas Gohr $this->config['widths'] = $this->parseWidths($val); 775511bd5bSAndreas Gohr break; 785511bd5bSAndreas Gohr case 'min': 795511bd5bSAndreas Gohr $this->config['min'] = abs((int) $val); 805511bd5bSAndreas Gohr break; 815511bd5bSAndreas Gohr case 'limit': 825511bd5bSAndreas Gohr case 'max': 835511bd5bSAndreas Gohr $this->config['limit'] = abs((int) $val); 845511bd5bSAndreas Gohr break; 855511bd5bSAndreas Gohr case 'order': 865511bd5bSAndreas Gohr case 'sort': 876047d2b8SAndreas Gohr $sorts = $this->parseValues($val); 886047d2b8SAndreas Gohr $sorts = array_map(array($helper, 'parseSort'), $sorts); 896047d2b8SAndreas Gohr $this->config['sort'] = array_merge($this->config['sort'], $sorts); 905511bd5bSAndreas Gohr break; 915511bd5bSAndreas Gohr case 'where': 925511bd5bSAndreas Gohr case 'filter': 935511bd5bSAndreas Gohr case 'filterand': 945511bd5bSAndreas Gohr /** @noinspection PhpMissingBreakStatementInspection */ 955511bd5bSAndreas Gohr case 'and': 965511bd5bSAndreas Gohr $logic = 'AND'; 975511bd5bSAndreas Gohr case 'filteror': 985511bd5bSAndreas Gohr case 'or': 9929877279SMichael Große $flt = $helper->parseFilterLine($logic, $val); 1005511bd5bSAndreas Gohr if ($flt) { 1015511bd5bSAndreas Gohr $this->config['filter'][] = $flt; 1025511bd5bSAndreas Gohr } 1035511bd5bSAndreas Gohr break; 1045511bd5bSAndreas Gohr case 'dynfilters': 1055511bd5bSAndreas Gohr $this->config['dynfilters'] = (bool) $val; 1065511bd5bSAndreas Gohr break; 1075511bd5bSAndreas Gohr case 'rownumbers': 1085511bd5bSAndreas Gohr $this->config['rownumbers'] = (bool) $val; 1095511bd5bSAndreas Gohr break; 1105511bd5bSAndreas Gohr case 'summarize': 1115511bd5bSAndreas Gohr $this->config['summarize'] = (bool) $val; 1125511bd5bSAndreas Gohr break; 1137b240ca8SAndreas Gohr case 'csv': 1147b240ca8SAndreas Gohr $this->config['csv'] = (bool) $val; 1157b240ca8SAndreas Gohr break; 11685edf4f2SMichael Grosse case 'target': 11785edf4f2SMichael Grosse case 'page': 11885edf4f2SMichael Grosse $this->config['target'] = cleanID($val); 11985edf4f2SMichael Grosse break; 1205511bd5bSAndreas Gohr default: 12129a2b794SAndreas Gohr $data = array('config' => &$this->config, 'key' => $key, 'val' => $val); 12229a2b794SAndreas Gohr $ev = new \Doku_Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data); 12329a2b794SAndreas Gohr if ($ev->advise_before()) { 1247d88e7edSAndreas Gohr throw new StructException("unknown option '%s'", hsc($key)); 1255511bd5bSAndreas Gohr } 12629a2b794SAndreas Gohr $ev->advise_after(); 12729a2b794SAndreas Gohr } 1285511bd5bSAndreas Gohr } 12901dd90deSAndreas Gohr 13001dd90deSAndreas Gohr // fill up headers - a NULL signifies that the column label is wanted 1315511bd5bSAndreas Gohr $this->config['headers'] = (array) $this->config['headers']; 1325511bd5bSAndreas Gohr $cnth = count($this->config['headers']); 1335511bd5bSAndreas Gohr $cntf = count($this->config['cols']); 1345511bd5bSAndreas Gohr for ($i = $cnth; $i < $cntf; $i++) { 13501dd90deSAndreas Gohr $this->config['headers'][] = null; 1365511bd5bSAndreas Gohr } 13795eef580SAndreas Gohr // fill up alignments 13895eef580SAndreas Gohr $cnta = count($this->config['align']); 13995eef580SAndreas Gohr for ($i = $cnta; $i < $cntf; $i++) { 14095eef580SAndreas Gohr $this->config['align'][] = null; 14195eef580SAndreas Gohr } 1425511bd5bSAndreas Gohr } 1435511bd5bSAndreas Gohr 1445511bd5bSAndreas Gohr /** 1455511bd5bSAndreas Gohr * Get the parsed configuration 1465511bd5bSAndreas Gohr * 1475511bd5bSAndreas Gohr * @return array 1485511bd5bSAndreas Gohr */ 149*d6d97f60SAnna Dabrowska public function getConfig() 150*d6d97f60SAnna Dabrowska { 1515511bd5bSAndreas Gohr return $this->config; 1525511bd5bSAndreas Gohr } 1535511bd5bSAndreas Gohr 1545511bd5bSAndreas Gohr /** 1555511bd5bSAndreas Gohr * Splits the given line into key and value 1565511bd5bSAndreas Gohr * 1575511bd5bSAndreas Gohr * @param $line 1585511bd5bSAndreas Gohr * @return bool|array returns false for empty lines 1595511bd5bSAndreas Gohr */ 160*d6d97f60SAnna Dabrowska protected function splitLine($line) 161*d6d97f60SAnna Dabrowska { 1625511bd5bSAndreas Gohr // ignore comments 1635511bd5bSAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line); 1645511bd5bSAndreas Gohr $line = str_replace('\\#', '#', $line); 1655511bd5bSAndreas Gohr $line = trim($line); 1665511bd5bSAndreas Gohr if (empty($line)) return false; 1675511bd5bSAndreas Gohr 1685511bd5bSAndreas Gohr $line = preg_split('/\s*:\s*/', $line, 2); 1695511bd5bSAndreas Gohr $line[0] = strtolower($line[0]); 1705511bd5bSAndreas Gohr 1715511bd5bSAndreas Gohr return $line; 1725511bd5bSAndreas Gohr } 1735511bd5bSAndreas Gohr 1745511bd5bSAndreas Gohr /** 1755511bd5bSAndreas Gohr * parses schema config and aliases 1765511bd5bSAndreas Gohr * 1775511bd5bSAndreas Gohr * @param $val 1785511bd5bSAndreas Gohr * @return array 1795511bd5bSAndreas Gohr */ 180*d6d97f60SAnna Dabrowska protected function parseSchema($val) 181*d6d97f60SAnna Dabrowska { 1825511bd5bSAndreas Gohr $schemas = array(); 1835511bd5bSAndreas Gohr $parts = explode(',', $val); 1845511bd5bSAndreas Gohr foreach ($parts as $part) { 1859007da58SMichael Große @list($table, $alias) = explode(' ', trim($part)); 1865511bd5bSAndreas Gohr $table = trim($table); 1875511bd5bSAndreas Gohr $alias = trim($alias); 1885511bd5bSAndreas Gohr if (!$table) continue; 1895511bd5bSAndreas Gohr 19029877279SMichael Große $schemas[] = array($table, $alias,); 1915511bd5bSAndreas Gohr } 1925511bd5bSAndreas Gohr return $schemas; 1935511bd5bSAndreas Gohr } 1945511bd5bSAndreas Gohr 1955511bd5bSAndreas Gohr /** 1965511bd5bSAndreas Gohr * Parse alignment data 1975511bd5bSAndreas Gohr * 1985511bd5bSAndreas Gohr * @param string $val 1995511bd5bSAndreas Gohr * @return string[] 2005511bd5bSAndreas Gohr */ 201*d6d97f60SAnna Dabrowska protected function parseAlignments($val) 202*d6d97f60SAnna Dabrowska { 2035511bd5bSAndreas Gohr $cols = explode(',', $val); 2045511bd5bSAndreas Gohr $data = array(); 2055511bd5bSAndreas Gohr foreach ($cols as $col) { 2065511bd5bSAndreas Gohr $col = trim(strtolower($col)); 2075511bd5bSAndreas Gohr if ($col[0] == 'c') { 2085511bd5bSAndreas Gohr $align = 'center'; 2095511bd5bSAndreas Gohr } elseif ($col[0] == 'r') { 2105511bd5bSAndreas Gohr $align = 'right'; 2119cda5805SAndreas Gohr } elseif ($col[0] == 'l') { 2125511bd5bSAndreas Gohr $align = 'left'; 2139cda5805SAndreas Gohr } else { 2149cda5805SAndreas Gohr $align = null; 2155511bd5bSAndreas Gohr } 2165511bd5bSAndreas Gohr $data[] = $align; 2175511bd5bSAndreas Gohr } 2185511bd5bSAndreas Gohr 2195511bd5bSAndreas Gohr return $data; 2205511bd5bSAndreas Gohr } 2215511bd5bSAndreas Gohr 2225511bd5bSAndreas Gohr /** 223e0216289SAndreas Gohr * Parse width data 224e0216289SAndreas Gohr * 225e0216289SAndreas Gohr * @param $val 226e0216289SAndreas Gohr * @return array 227e0216289SAndreas Gohr */ 228*d6d97f60SAnna Dabrowska protected function parseWidths($val) 229*d6d97f60SAnna Dabrowska { 230e0216289SAndreas Gohr $vals = explode(',', $val); 231e0216289SAndreas Gohr $vals = array_map('trim', $vals); 232e0216289SAndreas Gohr $len = count($vals); 233e0216289SAndreas Gohr for ($i = 0; $i < $len; $i++) { 234e0216289SAndreas Gohr $val = trim(strtolower($vals[$i])); 235e0216289SAndreas Gohr 236e0216289SAndreas Gohr if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) { 237e0216289SAndreas Gohr // proper CSS unit? 238e0216289SAndreas Gohr $vals[$i] = $val; 239e0216289SAndreas Gohr } elseif (preg_match('/^\d+$/', $val)) { 240e0216289SAndreas Gohr // decimal only? 241e0216289SAndreas Gohr $vals[$i] = $val . 'px'; 242e0216289SAndreas Gohr } else { 243e0216289SAndreas Gohr // invalid 244e0216289SAndreas Gohr $vals[$i] = ''; 245e0216289SAndreas Gohr } 246e0216289SAndreas Gohr } 247e0216289SAndreas Gohr return $vals; 248e0216289SAndreas Gohr } 249e0216289SAndreas Gohr 250e0216289SAndreas Gohr /** 2515511bd5bSAndreas Gohr * Split values at the commas, 2525511bd5bSAndreas Gohr * - Wrap with quotes to escape comma, quotes escaped by two quotes 2535511bd5bSAndreas Gohr * - Within quotes spaces are stored. 2545511bd5bSAndreas Gohr * 2555511bd5bSAndreas Gohr * @param string $line 2565511bd5bSAndreas Gohr * @return array 2575511bd5bSAndreas Gohr */ 258*d6d97f60SAnna Dabrowska protected function parseValues($line) 259*d6d97f60SAnna Dabrowska { 2605511bd5bSAndreas Gohr $values = array(); 2615511bd5bSAndreas Gohr $inQuote = false; 2625511bd5bSAndreas Gohr $escapedQuote = false; 2635511bd5bSAndreas Gohr $value = ''; 2645511bd5bSAndreas Gohr $len = strlen($line); 2655511bd5bSAndreas Gohr for ($i = 0; $i < $len; $i++) { 266d982cb29SMichael Große if ($line[$i] == '"') { 2675511bd5bSAndreas Gohr if ($inQuote) { 2685511bd5bSAndreas Gohr if ($escapedQuote) { 2695511bd5bSAndreas Gohr $value .= '"'; 2705511bd5bSAndreas Gohr $escapedQuote = false; 2715511bd5bSAndreas Gohr continue; 2725511bd5bSAndreas Gohr } 273d982cb29SMichael Große if ($line[$i + 1] == '"') { 2745511bd5bSAndreas Gohr $escapedQuote = true; 2755511bd5bSAndreas Gohr continue; 2765511bd5bSAndreas Gohr } 2775511bd5bSAndreas Gohr array_push($values, $value); 2785511bd5bSAndreas Gohr $inQuote = false; 2795511bd5bSAndreas Gohr $value = ''; 2805511bd5bSAndreas Gohr continue; 2815511bd5bSAndreas Gohr } else { 2825511bd5bSAndreas Gohr $inQuote = true; 2835511bd5bSAndreas Gohr $value = ''; //don't store stuff before the opening quote 2845511bd5bSAndreas Gohr continue; 2855511bd5bSAndreas Gohr } 286d982cb29SMichael Große } elseif ($line[$i] == ',') { 2875511bd5bSAndreas Gohr if ($inQuote) { 2885511bd5bSAndreas Gohr $value .= ','; 2895511bd5bSAndreas Gohr continue; 2905511bd5bSAndreas Gohr } else { 2915511bd5bSAndreas Gohr if (strlen($value) < 1) { 2925511bd5bSAndreas Gohr continue; 2935511bd5bSAndreas Gohr } 2945511bd5bSAndreas Gohr array_push($values, trim($value)); 2955511bd5bSAndreas Gohr $value = ''; 2965511bd5bSAndreas Gohr continue; 2975511bd5bSAndreas Gohr } 2985511bd5bSAndreas Gohr } 299d982cb29SMichael Große $value .= $line[$i]; 3005511bd5bSAndreas Gohr } 3015511bd5bSAndreas Gohr if (strlen($value) > 0) { 3025511bd5bSAndreas Gohr array_push($values, trim($value)); 3035511bd5bSAndreas Gohr } 3045511bd5bSAndreas Gohr return $values; 3055511bd5bSAndreas Gohr } 3065511bd5bSAndreas Gohr} 307