1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5use dokuwiki\plugin\struct\types\Date; 6use dokuwiki\plugin\struct\types\DateTime; 7use dokuwiki\plugin\struct\types\Page; 8 9class Search { 10 /** 11 * This separator will be used to concat multi values to flatten them in the result set 12 */ 13 const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 14 15 /** 16 * The list of known and allowed comparators 17 * (order matters) 18 */ 19 static public $COMPARATORS = array( 20 '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', 21 ); 22 23 /** @var \helper_plugin_sqlite */ 24 protected $sqlite; 25 26 /** @var Schema[] list of schemas to query */ 27 protected $schemas = array(); 28 29 /** @var Column[] list of columns to select */ 30 protected $columns = array(); 31 32 /** @var array the sorting of the result */ 33 protected $sortby = array(); 34 35 /** @var array the filters */ 36 protected $filter = array(); 37 38 /** @var array list of aliases tables can be referenced by */ 39 protected $aliases = array(); 40 41 /** @var int begin results from here */ 42 protected $range_begin = 0; 43 44 /** @var int end results here */ 45 protected $range_end = 0; 46 47 /** @var int the number of results */ 48 protected $count = -1; 49 /** @var string[] the PIDs of the result rows */ 50 protected $result_pids = null; 51 52 /** 53 * Search constructor. 54 */ 55 public function __construct() { 56 /** @var \helper_plugin_struct_db $plugin */ 57 $plugin = plugin_load('helper', 'struct_db'); 58 $this->sqlite = $plugin->getDB(); 59 } 60 61 /** 62 * Add a schema to be searched 63 * 64 * Call multiple times for multiple schemas. 65 * 66 * @param string $table 67 * @param string $alias 68 */ 69 public function addSchema($table, $alias = '') { 70 $this->schemas[$table] = new Schema($table); 71 if($alias) $this->aliases[$alias] = $table; 72 } 73 74 /** 75 * Add a column to be returned by the search 76 * 77 * Call multiple times for multiple columns. Be sure the referenced tables have been 78 * added before 79 * 80 * @param string $colname may contain an alias 81 */ 82 public function addColumn($colname) { 83 if($this->processWildcard($colname)) return; // wildcard? 84 $col = $this->findColumn($colname); 85 if(!$col) return; //FIXME do we really want to ignore missing columns? 86 $this->columns[] = $col; 87 } 88 89 /** 90 * Add sorting options 91 * 92 * Call multiple times for multiple columns. Be sure the referenced tables have been 93 * added before 94 * 95 * @param string $colname may contain an alias 96 * @param bool $asc sort direction (ASC = true, DESC = false) 97 */ 98 public function addSort($colname, $asc = true) { 99 $col = $this->findColumn($colname); 100 if(!$col) return; //FIXME do we really want to ignore missing columns? 101 102 $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc); 103 } 104 105 /** 106 * Returns all set sort columns 107 * 108 * @return array 109 */ 110 public function getSorts() { 111 return $this->sortby; 112 } 113 114 /** 115 * Adds a filter 116 * 117 * @param string $colname may contain an alias 118 * @param string|string[] $value 119 * @param string $comp @see self::COMPARATORS 120 * @param string $op either 'OR' or 'AND' 121 */ 122 public function addFilter($colname, $value, $comp, $op = 'OR') { 123 /* Convert certain filters into others 124 * this reduces the number of supported filters to implement in types */ 125 if ($comp == '*~') { 126 $value = $this->filterWrapAsterisks($value); 127 $comp = '~'; 128 } elseif ($comp == '<>') { 129 $comp = '!='; 130 } 131 132 if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 133 if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 134 135 $col = $this->findColumn($colname); 136 if(!$col) return; // ignore missing columns, filter might have been for different schema 137 138 // map filter operators to SQL syntax 139 switch($comp) { 140 case '~': 141 $comp = 'LIKE'; 142 break; 143 case '!~': 144 $comp = 'NOT LIKE'; 145 break; 146 case '=*': 147 $comp = 'REGEXP'; 148 break; 149 } 150 151 // we use asterisks, but SQL wants percents 152 if($comp == 'LIKE' || $comp == 'NOT LIKE') { 153 $value = $this->filterChangeToLike($value); 154 } 155 156 // add the filter 157 $this->filter[] = array($col, $value, $comp, $op); 158 } 159 160 /** 161 * Wrap given value in asterisks 162 * 163 * @param string|string[] $value 164 * @return string|string[] 165 */ 166 protected function filterWrapAsterisks($value) { 167 $map = function ($input) { 168 return "*$input*"; 169 }; 170 171 if(is_array($value)) { 172 $value = array_map($map, $value); 173 } else { 174 $value = $map($value); 175 } 176 return $value; 177 } 178 179 /** 180 * Change given string to use % instead of * 181 * 182 * @param string|string[] $value 183 * @return string|string[] 184 */ 185 protected function filterChangeToLike($value) { 186 $map = function ($input) { 187 return str_replace('*','%',$input); 188 }; 189 190 if(is_array($value)) { 191 $value = array_map($map, $value); 192 } else { 193 $value = $map($value); 194 } 195 return $value; 196 } 197 198 199 /** 200 * Set offset for the results 201 * 202 * @param int $offset 203 */ 204 public function setOffset($offset) { 205 $limit = 0; 206 if($this->range_end) { 207 // if there was a limit set previously, the range_end needs to be recalculated 208 $limit = $this->range_end - $this->range_begin; 209 } 210 $this->range_begin = $offset; 211 if($limit) $this->setLimit($limit); 212 } 213 214 /** 215 * Limit results to this number 216 * 217 * @param int $limit Set to 0 to disable limit again 218 */ 219 public function setLimit($limit) { 220 if($limit) { 221 $this->range_end = $this->range_begin + $limit; 222 } else { 223 $this->range_end = 0; 224 } 225 } 226 227 /** 228 * Return the number of results (regardless of limit and offset settings) 229 * 230 * Use this to implement paging. Important: this may only be called after running @see execute() 231 * 232 * @return int 233 */ 234 public function getCount() { 235 if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 236 return $this->count; 237 } 238 239 public function getPids() { 240 if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 241 return $this->result_pids; 242 } 243 244 /** 245 * Execute this search and return the result 246 * 247 * The result is a two dimensional array of Value()s. 248 * 249 * This will always query for the full result (not using offset and limit) and then 250 * return the wanted range, setting the count (@see getCount) to the whole result number 251 * 252 * @return Value[][] 253 */ 254 public function execute() { 255 list($sql, $opts) = $this->getSQL(); 256 257 /** @var \PDOStatement $res */ 258 $res = $this->sqlite->query($sql, $opts); 259 if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 260 261 $this->result_pids = array(); 262 $result = array(); 263 $cursor = -1; 264 while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 265 if ($this->isRowEmpty($row)) { 266 continue; 267 } 268 $cursor++; 269 if($cursor < $this->range_begin) continue; 270 if($this->range_end && $cursor >= $this->range_end) continue; 271 272 $this->result_pids[] = $row['PID']; 273 274 $C = 0; 275 $resrow = array(); 276 foreach($this->columns as $col) { 277 $val = $row["C$C"]; 278 if($col->isMulti()) { 279 $val = explode(self::CONCAT_SEPARATOR, $val); 280 } 281 $resrow[] = new Value($col, $val); 282 $C++; 283 } 284 $result[] = $resrow; 285 } 286 287 $this->sqlite->res_close($res); 288 $this->count = $cursor + 1; 289 return $result; 290 } 291 292 /** 293 * Transform the set search parameters into a statement 294 * 295 * @return array ($sql, $opts) The SQL and parameters to execute 296 */ 297 public function getSQL() { 298 if(!$this->columns) throw new StructException('nocolname'); 299 300 $QB = new QueryBuilder(); 301 302 // basic tables 303 $first_table = ''; 304 foreach($this->schemas as $schema) { 305 $datatable = 'data_'.$schema->getTable(); 306 if($first_table) { 307 // follow up tables 308 $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 309 } else { 310 // first table 311 $QB->addTable('schema_assignments'); 312 $QB->addTable($datatable); 313 $QB->addSelectColumn($datatable, 'pid', 'PID'); 314 $QB->addGroupByColumn($datatable, 'pid'); 315 316 $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 317 $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 318 $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 319 $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 320 $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 321 322 $first_table = $datatable; 323 } 324 $QB->filters()->whereAnd("$datatable.latest = 1"); 325 } 326 327 // columns to select, handling multis 328 $sep = self::CONCAT_SEPARATOR; 329 $n = 0; 330 foreach($this->columns as $col) { 331 $CN = 'C' . $n++; 332 333 if($col->isMulti()) { 334 $datatable = "data_{$col->getTable()}"; 335 $multitable = "multi_{$col->getTable()}"; 336 $MN = 'M' . $col->getColref(); 337 338 $QB->addLeftJoin( 339 $datatable, 340 $multitable, 341 $MN, 342 "$datatable.pid = $MN.pid AND 343 $datatable.rev = $MN.rev AND 344 $MN.colref = {$col->getColref()}" 345 ); 346 347 $col->getType()->select($QB, $MN, 'value' , $CN); 348 $sel = $QB->getSelectStatement($CN); 349 $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 350 } else { 351 $col->getType()->select($QB, 'data_'.$col->getTable(), $col->getColName() , $CN); 352 $QB->addGroupByStatement($CN); 353 } 354 } 355 356 // where clauses 357 foreach($this->filter as $filter) { 358 list($col, $value, $comp, $op) = $filter; 359 360 $datatable = "data_{$col->getTable()}"; 361 $multitable = "multi_{$col->getTable()}"; 362 363 /** @var $col Column */ 364 if($col->isMulti()) { 365 $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 366 367 $QB->addLeftJoin( 368 $datatable, 369 $multitable, 370 $MN, 371 "$datatable.pid = $MN.pid AND 372 $datatable.rev = $MN.rev AND 373 $MN.colref = {$col->getColref()}" 374 ); 375 $coltbl = $MN; 376 $colnam = 'value'; 377 } else { 378 $coltbl = $datatable; 379 $colnam = $col->getColName(); 380 } 381 382 $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 383 } 384 385 // sorting - we always sort by the single val column 386 foreach($this->sortby as $sort) { 387 list($col, $asc) = $sort; 388 /** @var $col Column */ 389 $QB->addOrderBy($col->getFullColName(false) . ' '.(($asc) ? 'ASC' : 'DESC')); 390 } 391 392 return $QB->getSQL(); 393 } 394 395 /** 396 * Returns all the columns that where added to the search 397 * 398 * @return Column[] 399 */ 400 public function getColumns() { 401 return $this->columns; 402 } 403 404 /** 405 * Checks if the given column is a * wildcard 406 * 407 * If it's a wildcard all matching columns are added to the column list, otherwise 408 * nothing happens 409 * 410 * @param string $colname 411 * @return bool was wildcard? 412 */ 413 protected function processWildcard($colname) { 414 list($colname, $table) = $this->resolveColumn($colname); 415 if($colname !== '*') return false; 416 417 // no table given? assume the first is meant 418 if($table === null) { 419 $schema_list = array_keys($this->schemas); 420 $table = $schema_list[0]; 421 } 422 423 $schema = $this->schemas[$table]; 424 if(!$schema) return false; 425 $this->columns = array_merge($this->columns, $schema->getColumns(false)); 426 return true; 427 } 428 429 /** 430 * Split a given column name into table and column 431 * 432 * Handles Aliases. Table might be null if none given. 433 * 434 * @param $colname 435 * @return array (colname, table) 436 */ 437 protected function resolveColumn($colname) { 438 if(!$this->schemas) throw new StructException('noschemas'); 439 440 // resolve the alias or table name 441 list($table, $colname) = explode('.', $colname, 2); 442 if(!$colname) { 443 $colname = $table; 444 $table = null; 445 } 446 if($table && isset($this->aliases[$table])) { 447 $table = $this->aliases[$table]; 448 } 449 450 if(!$colname) throw new StructException('nocolname'); 451 452 return array($colname, $table); 453 } 454 455 /** 456 * Find a column to be used in the search 457 * 458 * @param string $colname may contain an alias 459 * @return bool|Column 460 */ 461 public function findColumn($colname) { 462 if(!$this->schemas) throw new StructException('noschemas'); 463 464 // handling of page and title column is special - we add a "fake" column 465 $schema_list = array_keys($this->schemas); 466 if($colname == '%pageid%') { 467 return new PageColumn(0, new Page(), $schema_list[0]); 468 } 469 if($colname == '%title%') { 470 return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 471 } 472 if($colname == '%lastupdate%') { 473 return new RevisionColumn(0, new DateTime(), $schema_list[0]); 474 } 475 476 list($colname, $table) = $this->resolveColumn($colname); 477 478 // if table name given search only that, otherwise try all for matching column name 479 if($table !== null) { 480 $schemas = array($table => $this->schemas[$table]); 481 } else { 482 $schemas = $this->schemas; 483 } 484 485 // find it 486 $col = false; 487 foreach($schemas as $schema) { 488 if(empty($schema)) { 489 continue; 490 } 491 $col = $schema->findColumn($colname); 492 if($col) break; 493 } 494 495 return $col; 496 } 497 498 /** 499 * Check if a row is empty / only contains a reference to itself 500 * 501 * @param array $rowColumns an array as returned from the database 502 * @return bool 503 */ 504 private function isRowEmpty($rowColumns) { 505 $C = 0; 506 foreach($this->columns as $col) { 507 $val = $rowColumns["C$C"]; 508 $C += 1; 509 if (blank($val) || is_a($col->getType(),'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) { 510 continue; 511 } 512 return false; 513 } 514 return true; 515 } 516 517} 518 519 520