1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Class ConfigParser 7 * 8 * Utilities to parse the configuration syntax into an array 9 * 10 * @package dokuwiki\plugin\struct\meta 11 */ 12class ConfigParser 13{ 14 15 protected $config = array(); 16 17 /** 18 * Parser constructor. 19 * 20 * parses the given configuration lines 21 * 22 * @param $lines 23 */ 24 public function __construct($lines) 25 { 26 /** @var \helper_plugin_struct_config $helper */ 27 $helper = plugin_load('helper', 'struct_config'); 28 $this->config = array( 29 'limit' => 0, 30 'dynfilters' => false, 31 'summarize' => false, 32 'rownumbers' => false, 33 'sepbyheaders' => false, 34 'target' => '', 35 'align' => array(), 36 'headers' => array(), 37 'cols' => array(), 38 'widths' => array(), 39 'filter' => array(), 40 'schemas' => array(), 41 'sort' => array(), 42 'csv' => true, 43 ); 44 // parse info 45 foreach ($lines as $line) { 46 list($key, $val) = $this->splitLine($line); 47 if (!$key) continue; 48 49 $logic = 'OR'; 50 // handle line commands (we allow various aliases here) 51 switch ($key) { 52 case 'from': 53 case 'schema': 54 case 'tables': 55 $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val)); 56 break; 57 case 'select': 58 case 'cols': 59 case 'field': 60 case 'col': 61 $this->config['cols'] = $this->parseValues($val); 62 break; 63 case 'sepbyheaders': 64 $this->config['sepbyheaders'] = (bool) $val; 65 break; 66 case 'head': 67 case 'header': 68 case 'headers': 69 $this->config['headers'] = $this->parseValues($val); 70 break; 71 case 'align': 72 $this->config['align'] = $this->parseAlignments($val); 73 break; 74 case 'width': 75 case 'widths': 76 $this->config['widths'] = $this->parseWidths($val); 77 break; 78 case 'min': 79 $this->config['min'] = abs((int) $val); 80 break; 81 case 'limit': 82 case 'max': 83 $this->config['limit'] = abs((int) $val); 84 break; 85 case 'order': 86 case 'sort': 87 $sorts = $this->parseValues($val); 88 $sorts = array_map(array($helper, 'parseSort'), $sorts); 89 $this->config['sort'] = array_merge($this->config['sort'], $sorts); 90 break; 91 case 'where': 92 case 'filter': 93 case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment 94 /** @noinspection PhpMissingBreakStatementInspection */ 95 case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment 96 $logic = 'AND'; 97 case 'filteror': 98 case 'or': 99 $flt = $helper->parseFilterLine($logic, $val); 100 if ($flt) { 101 $this->config['filter'][] = $flt; 102 } 103 break; 104 case 'dynfilters': 105 $this->config['dynfilters'] = (bool) $val; 106 break; 107 case 'rownumbers': 108 $this->config['rownumbers'] = (bool) $val; 109 break; 110 case 'summarize': 111 $this->config['summarize'] = (bool) $val; 112 break; 113 case 'csv': 114 $this->config['csv'] = (bool) $val; 115 break; 116 case 'target': 117 case 'page': 118 $this->config['target'] = cleanID($val); 119 break; 120 default: 121 $data = array('config' => &$this->config, 'key' => $key, 'val' => $val); 122 $ev = new \Doku_Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data); 123 if ($ev->advise_before()) { 124 throw new StructException("unknown option '%s'", hsc($key)); 125 } 126 $ev->advise_after(); 127 } 128 } 129 130 // fill up headers - a NULL signifies that the column label is wanted 131 $this->config['headers'] = (array) $this->config['headers']; 132 $cnth = count($this->config['headers']); 133 $cntf = count($this->config['cols']); 134 for ($i = $cnth; $i < $cntf; $i++) { 135 $this->config['headers'][] = null; 136 } 137 // fill up alignments 138 $cnta = count($this->config['align']); 139 for ($i = $cnta; $i < $cntf; $i++) { 140 $this->config['align'][] = null; 141 } 142 } 143 144 /** 145 * Get the parsed configuration 146 * 147 * @return array 148 */ 149 public function getConfig() 150 { 151 return $this->config; 152 } 153 154 /** 155 * Splits the given line into key and value 156 * 157 * @param $line 158 * @return bool|array returns false for empty lines 159 */ 160 protected function splitLine($line) 161 { 162 // ignore comments 163 $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line); 164 $line = str_replace('\\#', '#', $line); 165 $line = trim($line); 166 if (empty($line)) return false; 167 168 $line = preg_split('/\s*:\s*/', $line, 2); 169 $line[0] = strtolower($line[0]); 170 171 return $line; 172 } 173 174 /** 175 * parses schema config and aliases 176 * 177 * @param $val 178 * @return array 179 */ 180 protected function parseSchema($val) 181 { 182 $schemas = array(); 183 $parts = explode(',', $val); 184 foreach ($parts as $part) { 185 @list($table, $alias) = explode(' ', trim($part)); 186 $table = trim($table); 187 $alias = trim($alias); 188 if (!$table) continue; 189 190 $schemas[] = array($table, $alias,); 191 } 192 return $schemas; 193 } 194 195 /** 196 * Parse alignment data 197 * 198 * @param string $val 199 * @return string[] 200 */ 201 protected function parseAlignments($val) 202 { 203 $cols = explode(',', $val); 204 $data = array(); 205 foreach ($cols as $col) { 206 $col = trim(strtolower($col)); 207 if ($col[0] == 'c') { 208 $align = 'center'; 209 } elseif ($col[0] == 'r') { 210 $align = 'right'; 211 } elseif ($col[0] == 'l') { 212 $align = 'left'; 213 } else { 214 $align = null; 215 } 216 $data[] = $align; 217 } 218 219 return $data; 220 } 221 222 /** 223 * Parse width data 224 * 225 * @param $val 226 * @return array 227 */ 228 protected function parseWidths($val) 229 { 230 $vals = explode(',', $val); 231 $vals = array_map('trim', $vals); 232 $len = count($vals); 233 for ($i = 0; $i < $len; $i++) { 234 $val = trim(strtolower($vals[$i])); 235 236 if (preg_match('/^\d+.?(\d+)?(px|em|ex|ch|rem|%|in|cm|mm|q|pt|pc)$/', $val)) { 237 // proper CSS unit? 238 $vals[$i] = $val; 239 } elseif (preg_match('/^\d+$/', $val)) { 240 // decimal only? 241 $vals[$i] = $val . 'px'; 242 } else { 243 // invalid 244 $vals[$i] = ''; 245 } 246 } 247 return $vals; 248 } 249 250 /** 251 * Split values at the commas, 252 * - Wrap with quotes to escape comma, quotes escaped by two quotes 253 * - Within quotes spaces are stored. 254 * 255 * @param string $line 256 * @return array 257 */ 258 protected function parseValues($line) 259 { 260 $values = array(); 261 $inQuote = false; 262 $escapedQuote = false; 263 $value = ''; 264 $len = strlen($line); 265 for ($i = 0; $i < $len; $i++) { 266 if ($line[$i] == '"') { 267 if ($inQuote) { 268 if ($escapedQuote) { 269 $value .= '"'; 270 $escapedQuote = false; 271 continue; 272 } 273 if ($line[$i + 1] == '"') { 274 $escapedQuote = true; 275 continue; 276 } 277 array_push($values, $value); 278 $inQuote = false; 279 $value = ''; 280 continue; 281 } else { 282 $inQuote = true; 283 $value = ''; //don't store stuff before the opening quote 284 continue; 285 } 286 } elseif ($line[$i] == ',') { 287 if ($inQuote) { 288 $value .= ','; 289 continue; 290 } else { 291 if (strlen($value) < 1) { 292 continue; 293 } 294 array_push($values, trim($value)); 295 $value = ''; 296 continue; 297 } 298 } 299 $value .= $line[$i]; 300 } 301 if (strlen($value) > 0) { 302 array_push($values, trim($value)); 303 } 304 return $values; 305 } 306} 307