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 */ 125511bd5bSAndreas Gohrclass ConfigParser { 135511bd5bSAndreas Gohr 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 */ 235511bd5bSAndreas Gohr public function __construct($lines) { 2429877279SMichael Große /** @var \helper_plugin_struct_config $helper */ 2529877279SMichael Große $helper = plugin_load('helper', 'struct_config'); 26d930c197SMichael Große $this->config = array( 275511bd5bSAndreas Gohr 'limit' => 0, 285511bd5bSAndreas Gohr 'dynfilters' => false, 295511bd5bSAndreas Gohr 'summarize' => false, 305511bd5bSAndreas Gohr 'rownumbers' => false, 315511bd5bSAndreas Gohr 'sepbyheaders' => false, 325511bd5bSAndreas Gohr 'headers' => array(), 335511bd5bSAndreas Gohr 'widths' => array(), 345511bd5bSAndreas Gohr 'filter' => array(), 357051cfa1SMichael Große 'schemas' => array(), 367051cfa1SMichael Große 'sort' => array() 375511bd5bSAndreas Gohr ); 385511bd5bSAndreas Gohr // parse info 395511bd5bSAndreas Gohr foreach($lines as $line) { 405511bd5bSAndreas Gohr list($key, $val) = $this->splitLine($line); 415511bd5bSAndreas Gohr if(!$key) continue; 425511bd5bSAndreas Gohr 435511bd5bSAndreas Gohr $logic = 'OR'; 445511bd5bSAndreas Gohr // handle line commands (we allow various aliases here) 455511bd5bSAndreas Gohr switch($key) { 465511bd5bSAndreas Gohr case 'from': 475511bd5bSAndreas Gohr case 'schema': 485511bd5bSAndreas Gohr case 'tables': 495511bd5bSAndreas Gohr $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val)); 505511bd5bSAndreas Gohr break; 515511bd5bSAndreas Gohr case 'select': 525511bd5bSAndreas Gohr case 'cols': 535511bd5bSAndreas Gohr case 'field': 545511bd5bSAndreas Gohr case 'col': 555511bd5bSAndreas Gohr $this->config['cols'] = $this->parseValues($val); 565511bd5bSAndreas Gohr break; 575511bd5bSAndreas Gohr case 'head': 585511bd5bSAndreas Gohr case 'header': 595511bd5bSAndreas Gohr case 'headers': 605511bd5bSAndreas Gohr $this->config['headers'] = $this->parseValues($val); 615511bd5bSAndreas Gohr break; 625511bd5bSAndreas Gohr case 'align': 635511bd5bSAndreas Gohr $this->config['align'] = $this->parseAlignments($val); 645511bd5bSAndreas Gohr break; 65e0216289SAndreas Gohr case 'width': 665511bd5bSAndreas Gohr case 'widths': 67e0216289SAndreas Gohr $this->config['widths'] = $this->parseWidths($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': 78*6047d2b8SAndreas Gohr $sorts = $this->parseValues($val); 79*6047d2b8SAndreas Gohr $sorts = array_map(array($helper, 'parseSort'), $sorts); 80*6047d2b8SAndreas Gohr $this->config['sort'] = array_merge($this->config['sort'], $sorts); 815511bd5bSAndreas Gohr break; 825511bd5bSAndreas Gohr case 'where': 835511bd5bSAndreas Gohr case 'filter': 845511bd5bSAndreas Gohr case 'filterand': 855511bd5bSAndreas Gohr /** @noinspection PhpMissingBreakStatementInspection */ 865511bd5bSAndreas Gohr case 'and': 875511bd5bSAndreas Gohr $logic = 'AND'; 885511bd5bSAndreas Gohr case 'filteror': 895511bd5bSAndreas Gohr case 'or': 9029877279SMichael Große $flt = $helper->parseFilterLine($logic, $val); 915511bd5bSAndreas Gohr if($flt) { 925511bd5bSAndreas Gohr $this->config['filter'][] = $flt; 935511bd5bSAndreas Gohr } 945511bd5bSAndreas Gohr break; 955511bd5bSAndreas Gohr case 'dynfilters': 965511bd5bSAndreas Gohr $this->config['dynfilters'] = (bool) $val; 975511bd5bSAndreas Gohr break; 985511bd5bSAndreas Gohr case 'rownumbers': 995511bd5bSAndreas Gohr $this->config['rownumbers'] = (bool) $val; 1005511bd5bSAndreas Gohr break; 1015511bd5bSAndreas Gohr case 'summarize': 1025511bd5bSAndreas Gohr $this->config['summarize'] = (bool) $val; 1035511bd5bSAndreas Gohr break; 1045511bd5bSAndreas Gohr default: 1057d88e7edSAndreas Gohr throw new StructException("unknown option '%s'", hsc($key)); 1065511bd5bSAndreas Gohr } 1075511bd5bSAndreas Gohr } 10801dd90deSAndreas Gohr 10901dd90deSAndreas Gohr // fill up headers - a NULL signifies that the column label is wanted 1105511bd5bSAndreas Gohr $this->config['headers'] = (array) $this->config['headers']; 1115511bd5bSAndreas Gohr $cnth = count($this->config['headers']); 1125511bd5bSAndreas Gohr $cntf = count($this->config['cols']); 1135511bd5bSAndreas Gohr for($i = $cnth; $i < $cntf; $i++) { 11401dd90deSAndreas Gohr $this->config['headers'][] = null; 1155511bd5bSAndreas Gohr } 1165511bd5bSAndreas Gohr } 1175511bd5bSAndreas Gohr 1185511bd5bSAndreas Gohr /** 1195511bd5bSAndreas Gohr * Get the parsed configuration 1205511bd5bSAndreas Gohr * 1215511bd5bSAndreas Gohr * @return array 1225511bd5bSAndreas Gohr */ 1235511bd5bSAndreas Gohr public function getConfig() { 1245511bd5bSAndreas Gohr return $this->config; 1255511bd5bSAndreas Gohr } 1265511bd5bSAndreas Gohr 1275511bd5bSAndreas Gohr /** 1285511bd5bSAndreas Gohr * Splits the given line into key and value 1295511bd5bSAndreas Gohr * 1305511bd5bSAndreas Gohr * @param $line 1315511bd5bSAndreas Gohr * @return bool|array returns false for empty lines 1325511bd5bSAndreas Gohr */ 1335511bd5bSAndreas Gohr protected function splitLine($line) { 1345511bd5bSAndreas Gohr // ignore comments 1355511bd5bSAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line); 1365511bd5bSAndreas Gohr $line = str_replace('\\#', '#', $line); 1375511bd5bSAndreas Gohr $line = trim($line); 1385511bd5bSAndreas Gohr if(empty($line)) return false; 1395511bd5bSAndreas Gohr 1405511bd5bSAndreas Gohr $line = preg_split('/\s*:\s*/', $line, 2); 1415511bd5bSAndreas Gohr $line[0] = strtolower($line[0]); 1425511bd5bSAndreas Gohr 1435511bd5bSAndreas Gohr return $line; 1445511bd5bSAndreas Gohr } 1455511bd5bSAndreas Gohr 1465511bd5bSAndreas Gohr /** 1475511bd5bSAndreas Gohr * parses schema config and aliases 1485511bd5bSAndreas Gohr * 1495511bd5bSAndreas Gohr * @param $val 1505511bd5bSAndreas Gohr * @return array 1515511bd5bSAndreas Gohr */ 1525511bd5bSAndreas Gohr protected function parseSchema($val) { 1535511bd5bSAndreas Gohr $schemas = array(); 1545511bd5bSAndreas Gohr $parts = explode(',', $val); 1555511bd5bSAndreas Gohr foreach($parts as $part) { 1564e7c197cSAndreas Gohr list($table, $alias) = explode(' ', trim($part)); 1575511bd5bSAndreas Gohr $table = trim($table); 1585511bd5bSAndreas Gohr $alias = trim($alias); 1595511bd5bSAndreas Gohr if(!$table) continue; 1605511bd5bSAndreas Gohr 16129877279SMichael Große $schemas[] = array($table, $alias,); 1625511bd5bSAndreas Gohr } 1635511bd5bSAndreas Gohr return $schemas; 1645511bd5bSAndreas Gohr } 1655511bd5bSAndreas Gohr 1665511bd5bSAndreas Gohr /** 1675511bd5bSAndreas Gohr * Parse alignment data 1685511bd5bSAndreas Gohr * 1695511bd5bSAndreas Gohr * @param string $val 1705511bd5bSAndreas Gohr * @return string[] 1715511bd5bSAndreas Gohr */ 1725511bd5bSAndreas Gohr protected function parseAlignments($val) { 1735511bd5bSAndreas Gohr $cols = explode(',', $val); 1745511bd5bSAndreas Gohr $data = array(); 1755511bd5bSAndreas Gohr foreach($cols as $col) { 1765511bd5bSAndreas Gohr $col = trim(strtolower($col)); 1775511bd5bSAndreas Gohr if($col[0] == 'c') { 1785511bd5bSAndreas Gohr $align = 'center'; 1795511bd5bSAndreas Gohr } elseif($col[0] == 'r') { 1805511bd5bSAndreas Gohr $align = 'right'; 1815511bd5bSAndreas Gohr } else { 1825511bd5bSAndreas Gohr $align = 'left'; 1835511bd5bSAndreas Gohr } 1845511bd5bSAndreas Gohr $data[] = $align; 1855511bd5bSAndreas Gohr } 1865511bd5bSAndreas Gohr 1875511bd5bSAndreas Gohr return $data; 1885511bd5bSAndreas Gohr } 1895511bd5bSAndreas Gohr 1905511bd5bSAndreas Gohr /** 191e0216289SAndreas Gohr * Parse width data 192e0216289SAndreas Gohr * 193e0216289SAndreas Gohr * @param $val 194e0216289SAndreas Gohr * @return array 195e0216289SAndreas Gohr */ 196e0216289SAndreas Gohr protected function parseWidths($val) { 197e0216289SAndreas Gohr $vals = explode(',', $val); 198e0216289SAndreas Gohr $vals = array_map('trim', $vals); 199e0216289SAndreas Gohr $len = count($vals); 200e0216289SAndreas Gohr for($i = 0; $i < $len; $i++) { 201e0216289SAndreas Gohr $val = trim(strtolower($vals[$i])); 202e0216289SAndreas Gohr 203e0216289SAndreas Gohr if(preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) { 204e0216289SAndreas Gohr // proper CSS unit? 205e0216289SAndreas Gohr $vals[$i] = $val; 206e0216289SAndreas Gohr } else if(preg_match('/^\d+$/', $val)) { 207e0216289SAndreas Gohr // decimal only? 208e0216289SAndreas Gohr $vals[$i] = $val . 'px'; 209e0216289SAndreas Gohr } else { 210e0216289SAndreas Gohr // invalid 211e0216289SAndreas Gohr $vals[$i] = ''; 212e0216289SAndreas Gohr } 213e0216289SAndreas Gohr } 214e0216289SAndreas Gohr return $vals; 215e0216289SAndreas Gohr } 216e0216289SAndreas Gohr 217e0216289SAndreas Gohr /** 2185511bd5bSAndreas Gohr * Split values at the commas, 2195511bd5bSAndreas Gohr * - Wrap with quotes to escape comma, quotes escaped by two quotes 2205511bd5bSAndreas Gohr * - Within quotes spaces are stored. 2215511bd5bSAndreas Gohr * 2225511bd5bSAndreas Gohr * @param string $line 2235511bd5bSAndreas Gohr * @return array 2245511bd5bSAndreas Gohr */ 2255511bd5bSAndreas Gohr protected function parseValues($line) { 2265511bd5bSAndreas Gohr $values = array(); 2275511bd5bSAndreas Gohr $inQuote = false; 2285511bd5bSAndreas Gohr $escapedQuote = false; 2295511bd5bSAndreas Gohr $value = ''; 2305511bd5bSAndreas Gohr $len = strlen($line); 2315511bd5bSAndreas Gohr for($i = 0; $i < $len; $i++) { 2325511bd5bSAndreas Gohr if($line{$i} == '"') { 2335511bd5bSAndreas Gohr if($inQuote) { 2345511bd5bSAndreas Gohr if($escapedQuote) { 2355511bd5bSAndreas Gohr $value .= '"'; 2365511bd5bSAndreas Gohr $escapedQuote = false; 2375511bd5bSAndreas Gohr continue; 2385511bd5bSAndreas Gohr } 2395511bd5bSAndreas Gohr if($line{$i + 1} == '"') { 2405511bd5bSAndreas Gohr $escapedQuote = true; 2415511bd5bSAndreas Gohr continue; 2425511bd5bSAndreas Gohr } 2435511bd5bSAndreas Gohr array_push($values, $value); 2445511bd5bSAndreas Gohr $inQuote = false; 2455511bd5bSAndreas Gohr $value = ''; 2465511bd5bSAndreas Gohr continue; 2475511bd5bSAndreas Gohr } else { 2485511bd5bSAndreas Gohr $inQuote = true; 2495511bd5bSAndreas Gohr $value = ''; //don't store stuff before the opening quote 2505511bd5bSAndreas Gohr continue; 2515511bd5bSAndreas Gohr } 2525511bd5bSAndreas Gohr } else if($line{$i} == ',') { 2535511bd5bSAndreas Gohr if($inQuote) { 2545511bd5bSAndreas Gohr $value .= ','; 2555511bd5bSAndreas Gohr continue; 2565511bd5bSAndreas Gohr } else { 2575511bd5bSAndreas Gohr if(strlen($value) < 1) { 2585511bd5bSAndreas Gohr continue; 2595511bd5bSAndreas Gohr } 2605511bd5bSAndreas Gohr array_push($values, trim($value)); 2615511bd5bSAndreas Gohr $value = ''; 2625511bd5bSAndreas Gohr continue; 2635511bd5bSAndreas Gohr } 2645511bd5bSAndreas Gohr } 2655511bd5bSAndreas Gohr $value .= $line{$i}; 2665511bd5bSAndreas Gohr } 2675511bd5bSAndreas Gohr if(strlen($value) > 0) { 2685511bd5bSAndreas Gohr array_push($values, trim($value)); 2695511bd5bSAndreas Gohr } 2705511bd5bSAndreas Gohr return $values; 2715511bd5bSAndreas Gohr } 2725511bd5bSAndreas Gohr 2735511bd5bSAndreas Gohr} 274