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 protected $config = array(); 15 16 /** 17 * Parser constructor. 18 * 19 * parses the given configuration lines 20 * 21 * @param $lines 22 */ 23 public function __construct($lines) 24 { 25 /** @var \helper_plugin_struct_config $helper */ 26 $helper = plugin_load('helper', 'struct_config'); 27 $this->config = array( 28 'limit' => 0, 29 'dynfilters' => false, 30 'summarize' => false, 31 'rownumbers' => false, 32 'sepbyheaders' => false, 33 'target' => '', 34 'align' => array(), 35 'headers' => array(), 36 'cols' => array(), 37 'widths' => array(), 38 'filter' => array(), 39 'schemas' => array(), 40 'sort' => array(), 41 'csv' => true, 42 ); 43 // parse info 44 foreach ($lines as $line) { 45 list($key, $val) = $this->splitLine($line); 46 if (!$key) continue; 47 48 $logic = 'OR'; 49 // handle line commands (we allow various aliases here) 50 switch ($key) { 51 case 'from': 52 case 'schema': 53 case 'tables': 54 $this->config['schemas'] = array_merge($this->config['schemas'], $this->parseSchema($val)); 55 break; 56 case 'select': 57 case 'cols': 58 case 'field': 59 case 'col': 60 $this->config['cols'] = $this->parseValues($val); 61 break; 62 case 'sepbyheaders': 63 $this->config['sepbyheaders'] = (bool)$val; 64 break; 65 case 'head': 66 case 'header': 67 case 'headers': 68 $this->config['headers'] = $this->parseValues($val); 69 break; 70 case 'align': 71 $this->config['align'] = $this->parseAlignments($val); 72 break; 73 case 'width': 74 case 'widths': 75 $this->config['widths'] = $this->parseWidths($val); 76 break; 77 case 'min': 78 $this->config['min'] = abs((int)$val); 79 break; 80 case 'limit': 81 case 'max': 82 $this->config['limit'] = abs((int)$val); 83 break; 84 case 'order': 85 case 'sort': 86 $sorts = $this->parseValues($val); 87 $sorts = array_map(array($helper, 'parseSort'), $sorts); 88 $this->config['sort'] = array_merge($this->config['sort'], $sorts); 89 break; 90 case 'where': 91 case 'filter': 92 case 'filterand': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment 93 /** @noinspection PhpMissingBreakStatementInspection */ 94 case 'and': // phpcs:ignore PSR2.ControlStructures.SwitchDeclaration.TerminatingComment 95 $logic = 'AND'; 96 case 'filteror': 97 case 'or': 98 $flt = $helper->parseFilterLine($logic, $val); 99 if ($flt) { 100 $this->config['filter'][] = $flt; 101 } 102 break; 103 case 'dynfilters': 104 $this->config['dynfilters'] = (bool)$val; 105 break; 106 case 'rownumbers': 107 $this->config['rownumbers'] = (bool)$val; 108 break; 109 case 'summarize': 110 $this->config['summarize'] = (bool)$val; 111 break; 112 case 'csv': 113 $this->config['csv'] = (bool)$val; 114 break; 115 case 'target': 116 case 'page': 117 $this->config['target'] = cleanID($val); 118 break; 119 default: 120 $data = array('config' => &$this->config, 'key' => $key, 'val' => $val); 121 $ev = new \Doku_Event('PLUGIN_STRUCT_CONFIGPARSER_UNKNOWNKEY', $data); 122 if ($ev->advise_before()) { 123 throw new StructException("unknown option '%s'", hsc($key)); 124 } 125 $ev->advise_after(); 126 } 127 } 128 129 // fill up headers - a NULL signifies that the column label is wanted 130 $this->config['headers'] = (array)$this->config['headers']; 131 $cnth = count($this->config['headers']); 132 $cntf = count($this->config['cols']); 133 for ($i = $cnth; $i < $cntf; $i++) { 134 $this->config['headers'][] = null; 135 } 136 // fill up alignments 137 $cnta = count($this->config['align']); 138 for ($i = $cnta; $i < $cntf; $i++) { 139 $this->config['align'][] = null; 140 } 141 } 142 143 /** 144 * Get the parsed configuration 145 * 146 * @return array 147 */ 148 public function getConfig() 149 { 150 return $this->config; 151 } 152 153 /** 154 * Splits the given line into key and value 155 * 156 * @param $line 157 * @return array returns ['',''] if the line is empty 158 */ 159 protected function splitLine($line) 160 { 161 // ignore comments 162 $line = preg_replace('/(?<![&\\\\])#.*$/', '', $line); 163 $line = str_replace('\\#', '#', $line); 164 $line = trim($line); 165 if (empty($line)) return ['', '']; 166 167 $line = preg_split('/\s*:\s*/', $line, 2); 168 $line[0] = strtolower($line[0]); 169 if (!isset($line[1])) $line[1] = ''; 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) = array_pad(explode(' ', trim($part)), 2, ''); 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