15511bd5bSAndreas Gohr<?php 25511bd5bSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 45511bd5bSAndreas Gohr 57234bfb1Ssplitbrainuse dokuwiki\Extension\Event; 67234bfb1Ssplitbrain 75511bd5bSAndreas Gohr/** 85511bd5bSAndreas Gohr * Class ConfigParser 95511bd5bSAndreas Gohr * 105511bd5bSAndreas Gohr * Utilities to parse the configuration syntax into an array 115511bd5bSAndreas Gohr * 12ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 135511bd5bSAndreas Gohr */ 14d6d97f60SAnna Dabrowskaclass ConfigParser 15d6d97f60SAnna Dabrowska{ 167fe2cdf2SAndreas Gohr protected $config = [ 177fe2cdf2SAndreas Gohr 'limit' => 0, 187fe2cdf2SAndreas Gohr 'dynfilters' => false, 197fe2cdf2SAndreas Gohr 'summarize' => false, 207fe2cdf2SAndreas Gohr 'rownumbers' => false, 217fe2cdf2SAndreas Gohr 'sepbyheaders' => false, 227fe2cdf2SAndreas Gohr 'target' => '', 237fe2cdf2SAndreas Gohr 'align' => [], 247fe2cdf2SAndreas Gohr 'headers' => [], 257fe2cdf2SAndreas Gohr 'cols' => [], 267fe2cdf2SAndreas Gohr 'widths' => [], 277fe2cdf2SAndreas Gohr 'filter' => [], 287fe2cdf2SAndreas Gohr 'schemas' => [], 297fe2cdf2SAndreas Gohr 'sort' => [], 307fe2cdf2SAndreas Gohr 'csv' => true, 317fe2cdf2SAndreas Gohr 'nesting' => 0, 327fe2cdf2SAndreas Gohr 'index' => 0, 337fe2cdf2SAndreas Gohr 'classes' => [] 347fe2cdf2SAndreas Gohr ]; 355511bd5bSAndreas Gohr 365511bd5bSAndreas Gohr /** 375511bd5bSAndreas Gohr * Parser constructor. 385511bd5bSAndreas Gohr * 395511bd5bSAndreas Gohr * parses the given configuration lines 405511bd5bSAndreas Gohr * 415511bd5bSAndreas Gohr * @param $lines 425511bd5bSAndreas Gohr */ 43d6d97f60SAnna Dabrowska public function __construct($lines) 44d6d97f60SAnna Dabrowska { 4529877279SMichael Große /** @var \helper_plugin_struct_config $helper */ 4629877279SMichael Große $helper = plugin_load('helper', 'struct_config'); 475511bd5bSAndreas Gohr // parse info 485511bd5bSAndreas Gohr foreach ($lines as $line) { 497234bfb1Ssplitbrain [$key, $val] = $this->splitLine($line); 505511bd5bSAndreas Gohr if (!$key) continue; 515511bd5bSAndreas Gohr 525511bd5bSAndreas Gohr $logic = 'OR'; 535511bd5bSAndreas Gohr // handle line commands (we allow various aliases here) 545511bd5bSAndreas Gohr switch ($key) { 555511bd5bSAndreas Gohr case 'from': 565511bd5bSAndreas Gohr case 'schema': 575511bd5bSAndreas Gohr case 'tables': 585511bd5bSAndreas Gohr $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val)); 595511bd5bSAndreas Gohr break; 605511bd5bSAndreas Gohr case 'select': 615511bd5bSAndreas Gohr case 'cols': 625511bd5bSAndreas Gohr case 'field': 635511bd5bSAndreas Gohr case 'col': 645511bd5bSAndreas Gohr $this->config['cols'] = $this->parseValues($val); 655511bd5bSAndreas Gohr break; 66ea5ad12aSMichael Grosse case 'sepbyheaders': 67ea5ad12aSMichael Grosse $this->config['sepbyheaders'] = (bool)$val; 68ea5ad12aSMichael Grosse break; 695511bd5bSAndreas Gohr case 'head': 705511bd5bSAndreas Gohr case 'header': 715511bd5bSAndreas Gohr case 'headers': 725511bd5bSAndreas Gohr $this->config['headers'] = $this->parseValues($val); 735511bd5bSAndreas Gohr break; 745511bd5bSAndreas Gohr case 'align': 755511bd5bSAndreas Gohr $this->config['align'] = $this->parseAlignments($val); 765511bd5bSAndreas Gohr break; 77e0216289SAndreas Gohr case 'width': 785511bd5bSAndreas Gohr case 'widths': 79e0216289SAndreas Gohr $this->config['widths'] = $this->parseWidths($val); 805511bd5bSAndreas Gohr break; 815511bd5bSAndreas Gohr case 'min': 825511bd5bSAndreas Gohr $this->config['min'] = abs((int)$val); 835511bd5bSAndreas Gohr break; 845511bd5bSAndreas Gohr case 'limit': 855511bd5bSAndreas Gohr case 'max': 865511bd5bSAndreas Gohr $this->config['limit'] = abs((int)$val); 875511bd5bSAndreas Gohr break; 885511bd5bSAndreas Gohr case 'order': 895511bd5bSAndreas Gohr case 'sort': 906047d2b8SAndreas Gohr $sorts = $this->parseValues($val); 917234bfb1Ssplitbrain $sorts = array_map([$helper, 'parseSort'], $sorts); 926047d2b8SAndreas Gohr $this->config['sort'] = array_merge($this->config['sort'], $sorts); 935511bd5bSAndreas Gohr break; 945511bd5bSAndreas Gohr case 'where': 955511bd5bSAndreas Gohr case 'filter': 96748e747fSAnna Dabrowska case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment 975511bd5bSAndreas Gohr /** @noinspection PhpMissingBreakStatementInspection */ 98748e747fSAnna Dabrowska case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment 995511bd5bSAndreas Gohr $logic = 'AND'; 1005511bd5bSAndreas Gohr case 'filteror': 1015511bd5bSAndreas Gohr case 'or': 10229877279SMichael Große $flt = $helper->parseFilterLine($logic, $val); 1035511bd5bSAndreas Gohr if ($flt) { 1045511bd5bSAndreas Gohr $this->config['filter'][] = $flt; 1055511bd5bSAndreas Gohr } 1065511bd5bSAndreas Gohr break; 1075511bd5bSAndreas Gohr case 'dynfilters': 1085511bd5bSAndreas Gohr $this->config['dynfilters'] = (bool)$val; 1095511bd5bSAndreas Gohr break; 1105511bd5bSAndreas Gohr case 'rownumbers': 1115511bd5bSAndreas Gohr $this->config['rownumbers'] = (bool)$val; 1125511bd5bSAndreas Gohr break; 1135511bd5bSAndreas Gohr case 'summarize': 1145511bd5bSAndreas Gohr $this->config['summarize'] = (bool)$val; 1155511bd5bSAndreas Gohr break; 1167b240ca8SAndreas Gohr case 'csv': 1177b240ca8SAndreas Gohr $this->config['csv'] = (bool)$val; 1187b240ca8SAndreas Gohr break; 11985edf4f2SMichael Grosse case 'target': 12085edf4f2SMichael Grosse case 'page': 12185edf4f2SMichael Grosse $this->config['target'] = cleanID($val); 12285edf4f2SMichael Grosse break; 1235bc00e11SAndreas Gohr case 'nesting': 1245bc00e11SAndreas Gohr case 'nest': 1255bc00e11SAndreas Gohr $this->config['nesting'] = (int) $val; 1265bc00e11SAndreas Gohr break; 127ce44c639SAndreas Gohr case 'index': 128ce44c639SAndreas Gohr $this->config['index'] = (int) $val; 129ce44c639SAndreas Gohr break; 130af0ce8d2SAndreas Gohr case 'class': 131af0ce8d2SAndreas Gohr case 'classes': 132af0ce8d2SAndreas Gohr $this->config['classes'] = $this->parseClasses($val); 133af0ce8d2SAndreas Gohr break; 1345511bd5bSAndreas Gohr default: 1357234bfb1Ssplitbrain $data = ['config' => &$this->config, 'key' => $key, 'val' => $val]; 1367234bfb1Ssplitbrain $ev = new Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data); 13729a2b794SAndreas Gohr if ($ev->advise_before()) { 1387d88e7edSAndreas Gohr throw new StructException("unknown option '%s'", hsc($key)); 1395511bd5bSAndreas Gohr } 14029a2b794SAndreas Gohr $ev->advise_after(); 14129a2b794SAndreas Gohr } 1425511bd5bSAndreas Gohr } 14301dd90deSAndreas Gohr 14401dd90deSAndreas Gohr // fill up headers - a NULL signifies that the column label is wanted 1455511bd5bSAndreas Gohr $this->config['headers'] = (array)$this->config['headers']; 1465511bd5bSAndreas Gohr $cnth = count($this->config['headers']); 1475511bd5bSAndreas Gohr $cntf = count($this->config['cols']); 1485511bd5bSAndreas Gohr for ($i = $cnth; $i < $cntf; $i++) { 14901dd90deSAndreas Gohr $this->config['headers'][] = null; 1505511bd5bSAndreas Gohr } 15195eef580SAndreas Gohr // fill up alignments 15295eef580SAndreas Gohr $cnta = count($this->config['align']); 15395eef580SAndreas Gohr for ($i = $cnta; $i < $cntf; $i++) { 15495eef580SAndreas Gohr $this->config['align'][] = null; 15595eef580SAndreas Gohr } 1565511bd5bSAndreas Gohr } 1575511bd5bSAndreas Gohr 1585511bd5bSAndreas Gohr /** 1595511bd5bSAndreas Gohr * Get the parsed configuration 1605511bd5bSAndreas Gohr * 1615511bd5bSAndreas Gohr * @return array 1625511bd5bSAndreas Gohr */ 163d6d97f60SAnna Dabrowska public function getConfig() 164d6d97f60SAnna Dabrowska { 1655511bd5bSAndreas Gohr return $this->config; 1665511bd5bSAndreas Gohr } 1675511bd5bSAndreas Gohr 1685511bd5bSAndreas Gohr /** 1695511bd5bSAndreas Gohr * Splits the given line into key and value 1705511bd5bSAndreas Gohr * 1715511bd5bSAndreas Gohr * @param $line 17242d332d7SAndreas Gohr * @return array returns ['',''] if the line is empty 1735511bd5bSAndreas Gohr */ 174d6d97f60SAnna Dabrowska protected function splitLine($line) 175d6d97f60SAnna Dabrowska { 1765511bd5bSAndreas Gohr // ignore comments 1775511bd5bSAndreas Gohr $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line); 1785511bd5bSAndreas Gohr $line = str_replace('\\#', '#', $line); 1795511bd5bSAndreas Gohr $line = trim($line); 18042d332d7SAndreas Gohr if (empty($line)) return ['', '']; 1815511bd5bSAndreas Gohr 1825511bd5bSAndreas Gohr $line = preg_split('/\s*:\s*/', $line, 2); 1835511bd5bSAndreas Gohr $line[0] = strtolower($line[0]); 18442d332d7SAndreas Gohr if (!isset($line[1])) $line[1] = ''; 1855511bd5bSAndreas Gohr 1865511bd5bSAndreas Gohr return $line; 1875511bd5bSAndreas Gohr } 1885511bd5bSAndreas Gohr 1895511bd5bSAndreas Gohr /** 1905511bd5bSAndreas Gohr * parses schema config and aliases 1915511bd5bSAndreas Gohr * 1925511bd5bSAndreas Gohr * @param $val 1935511bd5bSAndreas Gohr * @return array 1945511bd5bSAndreas Gohr */ 195d6d97f60SAnna Dabrowska protected function parseSchema($val) 196d6d97f60SAnna Dabrowska { 1977234bfb1Ssplitbrain $schemas = []; 1985511bd5bSAndreas Gohr $parts = explode(',', $val); 1995511bd5bSAndreas Gohr foreach ($parts as $part) { 2007fe2cdf2SAndreas Gohr [$table, $alias] = sexplode(' ', trim($part), 2, ''); 2015511bd5bSAndreas Gohr $table = trim($table); 2025511bd5bSAndreas Gohr $alias = trim($alias); 2035511bd5bSAndreas Gohr if (!$table) continue; 2045511bd5bSAndreas Gohr 2057234bfb1Ssplitbrain $schemas[] = [$table, $alias]; 2065511bd5bSAndreas Gohr } 2075511bd5bSAndreas Gohr return $schemas; 2085511bd5bSAndreas Gohr } 2095511bd5bSAndreas Gohr 2105511bd5bSAndreas Gohr /** 2115511bd5bSAndreas Gohr * Parse alignment data 2125511bd5bSAndreas Gohr * 2135511bd5bSAndreas Gohr * @param string $val 2145511bd5bSAndreas Gohr * @return string[] 2155511bd5bSAndreas Gohr */ 216d6d97f60SAnna Dabrowska protected function parseAlignments($val) 217d6d97f60SAnna Dabrowska { 2185511bd5bSAndreas Gohr $cols = explode(',', $val); 2197234bfb1Ssplitbrain $data = []; 2205511bd5bSAndreas Gohr foreach ($cols as $col) { 2215511bd5bSAndreas Gohr $col = trim(strtolower($col)); 2225511bd5bSAndreas Gohr if ($col[0] == 'c') { 2235511bd5bSAndreas Gohr $align = 'center'; 2245511bd5bSAndreas Gohr } elseif ($col[0] == 'r') { 2255511bd5bSAndreas Gohr $align = 'right'; 2269cda5805SAndreas Gohr } elseif ($col[0] == 'l') { 2275511bd5bSAndreas Gohr $align = 'left'; 2289cda5805SAndreas Gohr } else { 2299cda5805SAndreas Gohr $align = null; 2305511bd5bSAndreas Gohr } 2315511bd5bSAndreas Gohr $data[] = $align; 2325511bd5bSAndreas Gohr } 2335511bd5bSAndreas Gohr 2345511bd5bSAndreas Gohr return $data; 2355511bd5bSAndreas Gohr } 2365511bd5bSAndreas Gohr 2375511bd5bSAndreas Gohr /** 238e0216289SAndreas Gohr * Parse width data 239e0216289SAndreas Gohr * 240e0216289SAndreas Gohr * @param $val 241e0216289SAndreas Gohr * @return array 242e0216289SAndreas Gohr */ 243d6d97f60SAnna Dabrowska protected function parseWidths($val) 244d6d97f60SAnna Dabrowska { 245e0216289SAndreas Gohr $vals = explode(',', $val); 246e0216289SAndreas Gohr $vals = array_map('trim', $vals); 2477234bfb1Ssplitbrain 248e0216289SAndreas Gohr $len = count($vals); 249e0216289SAndreas Gohr for ($i = 0; $i < $len; $i++) { 250e0216289SAndreas Gohr $val = trim(strtolower($vals[$i])); 251e0216289SAndreas Gohr 252e0216289SAndreas Gohr if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) { 253e0216289SAndreas Gohr // proper CSS unit? 254e0216289SAndreas Gohr $vals[$i] = $val; 255e0216289SAndreas Gohr } elseif (preg_match('/^\d+$/', $val)) { 256e0216289SAndreas Gohr // decimal only? 257e0216289SAndreas Gohr $vals[$i] = $val . 'px'; 258e0216289SAndreas Gohr } else { 259e0216289SAndreas Gohr // invalid 260e0216289SAndreas Gohr $vals[$i] = ''; 261e0216289SAndreas Gohr } 262e0216289SAndreas Gohr } 263e0216289SAndreas Gohr return $vals; 264e0216289SAndreas Gohr } 265e0216289SAndreas Gohr 266e0216289SAndreas Gohr /** 2675511bd5bSAndreas Gohr * Split values at the commas, 2685511bd5bSAndreas Gohr * - Wrap with quotes to escape comma, quotes escaped by two quotes 2695511bd5bSAndreas Gohr * - Within quotes spaces are stored. 2705511bd5bSAndreas Gohr * 2715511bd5bSAndreas Gohr * @param string $line 2725511bd5bSAndreas Gohr * @return array 2735511bd5bSAndreas Gohr */ 274d6d97f60SAnna Dabrowska protected function parseValues($line) 275d6d97f60SAnna Dabrowska { 2767234bfb1Ssplitbrain $values = []; 2775511bd5bSAndreas Gohr $inQuote = false; 2785511bd5bSAndreas Gohr $escapedQuote = false; 2795511bd5bSAndreas Gohr $value = ''; 2805511bd5bSAndreas Gohr $len = strlen($line); 2815511bd5bSAndreas Gohr for ($i = 0; $i < $len; $i++) { 282*62c804ccSAnna Dabrowska if ($line[$i] === '"') { 2835511bd5bSAndreas Gohr if ($inQuote) { 2845511bd5bSAndreas Gohr if ($escapedQuote) { 2855511bd5bSAndreas Gohr $value .= '"'; 2865511bd5bSAndreas Gohr $escapedQuote = false; 2875511bd5bSAndreas Gohr continue; 2885511bd5bSAndreas Gohr } 289*62c804ccSAnna Dabrowska if (isset($line[$i + 1]) && $line[$i + 1] === '"') { 2905511bd5bSAndreas Gohr $escapedQuote = true; 2915511bd5bSAndreas Gohr continue; 2925511bd5bSAndreas Gohr } 2937234bfb1Ssplitbrain $values[] = $value; 2945511bd5bSAndreas Gohr $inQuote = false; 2955511bd5bSAndreas Gohr $value = ''; 2965511bd5bSAndreas Gohr continue; 2975511bd5bSAndreas Gohr } else { 2985511bd5bSAndreas Gohr $inQuote = true; 2995511bd5bSAndreas Gohr $value = ''; //don't store stuff before the opening quote 3005511bd5bSAndreas Gohr continue; 3015511bd5bSAndreas Gohr } 302*62c804ccSAnna Dabrowska } elseif ($line[$i] === ',') { 3035511bd5bSAndreas Gohr if ($inQuote) { 3045511bd5bSAndreas Gohr $value .= ','; 3055511bd5bSAndreas Gohr continue; 3065511bd5bSAndreas Gohr } else { 3075511bd5bSAndreas Gohr if (strlen($value) < 1) { 3085511bd5bSAndreas Gohr continue; 3095511bd5bSAndreas Gohr } 3107234bfb1Ssplitbrain $values[] = trim($value); 3115511bd5bSAndreas Gohr $value = ''; 3125511bd5bSAndreas Gohr continue; 3135511bd5bSAndreas Gohr } 3145511bd5bSAndreas Gohr } 315d982cb29SMichael Große $value .= $line[$i]; 3165511bd5bSAndreas Gohr } 3175511bd5bSAndreas Gohr if (strlen($value) > 0) { 3187234bfb1Ssplitbrain $values[] = trim($value); 3195511bd5bSAndreas Gohr } 3205511bd5bSAndreas Gohr return $values; 3215511bd5bSAndreas Gohr } 322af0ce8d2SAndreas Gohr 323af0ce8d2SAndreas Gohr /** 324af0ce8d2SAndreas Gohr * Ensure custom classes are valid and don't clash 325af0ce8d2SAndreas Gohr * 326af0ce8d2SAndreas Gohr * @param string $line 327af0ce8d2SAndreas Gohr * @return string[] 328af0ce8d2SAndreas Gohr */ 329af0ce8d2SAndreas Gohr protected function parseClasses($line) 330af0ce8d2SAndreas Gohr { 331af0ce8d2SAndreas Gohr $classes = $this->parseValues($line); 332af0ce8d2SAndreas Gohr $classes = array_map(function ($class) { 333af0ce8d2SAndreas Gohr $class = str_replace(' ', '_', $class); 334af0ce8d2SAndreas Gohr $class = preg_replace('/[^a-zA-Z0-9_]/', '', $class); 335af0ce8d2SAndreas Gohr return 'struct-custom-' . $class; 336af0ce8d2SAndreas Gohr }, $classes); 337af0ce8d2SAndreas Gohr return $classes; 338af0ce8d2SAndreas Gohr } 3395511bd5bSAndreas Gohr} 340