115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 415929be2SAndreas Gohr 5cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\DateTime; 6f107f479SAndreas Gohruse dokuwiki\plugin\struct\types\Decimal; 7ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Page; 86781c68dSSzymon Olewniczakuse dokuwiki\plugin\struct\types\AutoSummary; 988b58a21SSzymon Olewniczakuse dokuwiki\plugin\struct\types\Text; 102e12ac22SMichael Grosseuse dokuwiki\plugin\struct\types\User; 110561158fSAndreas Gohr 1215929be2SAndreas Gohrclass Search { 139d7a36f9SAndreas Gohr /** 149d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 159d7a36f9SAndreas Gohr */ 169d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 179d7a36f9SAndreas Gohr 185511bd5bSAndreas Gohr /** 195511bd5bSAndreas Gohr * The list of known and allowed comparators 202e7595e7SAndreas Gohr * (order matters) 215511bd5bSAndreas Gohr */ 2274461852SAndreas Gohr static public $COMPARATORS = array( 23aaa187abSSzymon Olewniczak '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', 'IN' 245511bd5bSAndreas Gohr ); 255511bd5bSAndreas Gohr 269d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 279d7a36f9SAndreas Gohr protected $sqlite; 2815929be2SAndreas Gohr 2915929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 3015929be2SAndreas Gohr protected $schemas = array(); 3115929be2SAndreas Gohr 3215929be2SAndreas Gohr /** @var Column[] list of columns to select */ 3315929be2SAndreas Gohr protected $columns = array(); 3415929be2SAndreas Gohr 3515929be2SAndreas Gohr /** @var array the sorting of the result */ 3615929be2SAndreas Gohr protected $sortby = array(); 3715929be2SAndreas Gohr 389d7a36f9SAndreas Gohr /** @var array the filters */ 399d7a36f9SAndreas Gohr protected $filter = array(); 4015929be2SAndreas Gohr 4115929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 4215929be2SAndreas Gohr protected $aliases = array(); 4315929be2SAndreas Gohr 447f9cb794SAndreas Gohr /** @var int begin results from here */ 457f9cb794SAndreas Gohr protected $range_begin = 0; 467f9cb794SAndreas Gohr 477f9cb794SAndreas Gohr /** @var int end results here */ 487f9cb794SAndreas Gohr protected $range_end = 0; 497f9cb794SAndreas Gohr 507f9cb794SAndreas Gohr /** @var int the number of results */ 517f9cb794SAndreas Gohr protected $count = -1; 52d4b5a17cSAndreas Gohr /** @var string[] the PIDs of the result rows */ 53d4b5a17cSAndreas Gohr protected $result_pids = null; 540ceefd5cSAnna Dabrowska /** @var array the row ids of the result rows */ 550ceefd5cSAnna Dabrowska protected $result_rids = []; 566fd73b4bSAnna Dabrowska /** @var array the revisions of the result rows */ 576fd73b4bSAnna Dabrowska protected $result_revs = []; 587f9cb794SAndreas Gohr 5915929be2SAndreas Gohr /** 609d7a36f9SAndreas Gohr * Search constructor. 619d7a36f9SAndreas Gohr */ 629d7a36f9SAndreas Gohr public function __construct() { 639d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 649d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 659d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 669d7a36f9SAndreas Gohr } 679d7a36f9SAndreas Gohr 689d7a36f9SAndreas Gohr /** 6915929be2SAndreas Gohr * Add a schema to be searched 7015929be2SAndreas Gohr * 7115929be2SAndreas Gohr * Call multiple times for multiple schemas. 7215929be2SAndreas Gohr * 7315929be2SAndreas Gohr * @param string $table 7415929be2SAndreas Gohr * @param string $alias 7515929be2SAndreas Gohr */ 7615929be2SAndreas Gohr public function addSchema($table, $alias = '') { 772be187cdSAndreas Gohr $schema = new Schema($table); 782be187cdSAndreas Gohr if(!$schema->getId()) { 792be187cdSAndreas Gohr throw new StructException('schema missing', $table); 802be187cdSAndreas Gohr } 812be187cdSAndreas Gohr 820ceefd5cSAnna Dabrowska // FIXME is the mixing still relevant? 830ceefd5cSAnna Dabrowska// if($this->schemas && 840ceefd5cSAnna Dabrowska// ( 850ceefd5cSAnna Dabrowska// $schema->isLookup() || 860ceefd5cSAnna Dabrowska// reset($this->schemas)->isLookup() 870ceefd5cSAnna Dabrowska// ) 880ceefd5cSAnna Dabrowska// ) { 890ceefd5cSAnna Dabrowska// throw new StructException('nolookupmix'); 900ceefd5cSAnna Dabrowska// } 912be187cdSAndreas Gohr 92712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 93712c650dSAndreas Gohr if($alias) $this->aliases[$alias] = $schema->getTable(); 9415929be2SAndreas Gohr } 9515929be2SAndreas Gohr 9615929be2SAndreas Gohr /** 9715929be2SAndreas Gohr * Add a column to be returned by the search 9815929be2SAndreas Gohr * 9915929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 10015929be2SAndreas Gohr * added before 10115929be2SAndreas Gohr * 10215929be2SAndreas Gohr * @param string $colname may contain an alias 10315929be2SAndreas Gohr */ 10415929be2SAndreas Gohr public function addColumn($colname) { 105577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 10615929be2SAndreas Gohr $col = $this->findColumn($colname); 10715929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10815929be2SAndreas Gohr $this->columns[] = $col; 10915929be2SAndreas Gohr } 11015929be2SAndreas Gohr 11115929be2SAndreas Gohr /** 11215929be2SAndreas Gohr * Add sorting options 11315929be2SAndreas Gohr * 11415929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 11515929be2SAndreas Gohr * added before 11615929be2SAndreas Gohr * 11715929be2SAndreas Gohr * @param string $colname may contain an alias 11815929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 119eb55dc17SAndreas Gohr * @param bool $nc set true for caseinsensitivity 12015929be2SAndreas Gohr */ 121eb55dc17SAndreas Gohr public function addSort($colname, $asc = true, $nc = true) { 12215929be2SAndreas Gohr $col = $this->findColumn($colname); 12315929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 12415929be2SAndreas Gohr 125eb55dc17SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc, $nc); 12607993756SAndreas Gohr } 12707993756SAndreas Gohr 12807993756SAndreas Gohr /** 12907993756SAndreas Gohr * Returns all set sort columns 13007993756SAndreas Gohr * 13107993756SAndreas Gohr * @return array 13207993756SAndreas Gohr */ 13307993756SAndreas Gohr public function getSorts() { 13407993756SAndreas Gohr return $this->sortby; 13515929be2SAndreas Gohr } 13615929be2SAndreas Gohr 13715929be2SAndreas Gohr /** 1389d7a36f9SAndreas Gohr * Adds a filter 13915929be2SAndreas Gohr * 14015929be2SAndreas Gohr * @param string $colname may contain an alias 14153528ecfSAndreas Gohr * @param string|string[] $value 1425511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1434c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 14415929be2SAndreas Gohr */ 1454c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1469dbc32aeSAndreas Gohr /* Convert certain filters into others 1479dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1489dbc32aeSAndreas Gohr if($comp == '*~') { 14953528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1509dbc32aeSAndreas Gohr $comp = '~'; 1519dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1529dbc32aeSAndreas Gohr $comp = '!='; 1539dbc32aeSAndreas Gohr } 1549dbc32aeSAndreas Gohr 15574461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1564c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1579d7a36f9SAndreas Gohr 15815929be2SAndreas Gohr $col = $this->findColumn($colname); 1597da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1607da1a0fdSAndreas Gohr 1617da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1627da1a0fdSAndreas Gohr switch($comp) { 1637da1a0fdSAndreas Gohr case '~': 1647da1a0fdSAndreas Gohr $comp = 'LIKE'; 1657da1a0fdSAndreas Gohr break; 1667da1a0fdSAndreas Gohr case '!~': 1677da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1687da1a0fdSAndreas Gohr break; 1697da1a0fdSAndreas Gohr case '=*': 1707da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1717da1a0fdSAndreas Gohr break; 1727da1a0fdSAndreas Gohr } 1737da1a0fdSAndreas Gohr 1747da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1757da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 17653528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1777da1a0fdSAndreas Gohr } 1787da1a0fdSAndreas Gohr 179aaa187abSSzymon Olewniczak if ($comp == 'IN' && !is_array($value)) { 180efff5841SSzymon Olewniczak $value = $this->parseFilterValueList($value); 181aaa187abSSzymon Olewniczak //col IN ('a', 'b', 'c') is equal to col = 'a' OR 'col = 'b' OR col = 'c' 182aaa187abSSzymon Olewniczak $comp = '='; 183aaa187abSSzymon Olewniczak } 184aaa187abSSzymon Olewniczak 1857da1a0fdSAndreas Gohr // add the filter 1864c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 18715929be2SAndreas Gohr } 18815929be2SAndreas Gohr 18915929be2SAndreas Gohr /** 190aaa187abSSzymon Olewniczak * Parse SQLite row value into array 191aaa187abSSzymon Olewniczak * 192aaa187abSSzymon Olewniczak * @param string $value 193aaa187abSSzymon Olewniczak * @return string[] 194aaa187abSSzymon Olewniczak */ 195efff5841SSzymon Olewniczak protected function parseFilterValueList($value) { 196efff5841SSzymon Olewniczak $Handler = new FilterValueListHandler(); 197*670a6894SAnna Dabrowska $LexerClass = class_exists('\Doku_Lexer') ? '\Doku_Lexer' : '\dokuwiki\Parsing\Lexer\Lexer'; 198*670a6894SAnna Dabrowska $isLegacy = $LexerClass === '\Doku_Lexer'; 199*670a6894SAnna Dabrowska /** @var \Doku_Lexer|\dokuwiki\Parsing\Lexer\Lexer $Lexer */ 200*670a6894SAnna Dabrowska $Lexer = new $LexerClass($Handler, 'base', true); 201*670a6894SAnna Dabrowska 202aaa187abSSzymon Olewniczak 203aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('\(','base','row'); 204aaa187abSSzymon Olewniczak $Lexer->addPattern('\s*,\s*','row'); 205aaa187abSSzymon Olewniczak $Lexer->addExitPattern('\)','row'); 206aaa187abSSzymon Olewniczak 207aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('"', 'row', 'double_quote_string'); 208aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern('\\\\"','double_quote_string','escape_sequence'); 209aaa187abSSzymon Olewniczak $Lexer->addExitPattern('"', 'double_quote_string'); 210aaa187abSSzymon Olewniczak 211aaa187abSSzymon Olewniczak $Lexer->addEntryPattern("'", 'row', 'single_quote_string'); 212aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern("\\\\'",'single_quote_string','escape_sequence'); 213aaa187abSSzymon Olewniczak $Lexer->addExitPattern("'", 'single_quote_string'); 214aaa187abSSzymon Olewniczak 215aaa187abSSzymon Olewniczak $Lexer->mapHandler('double_quote_string','single_quote_string'); 216aaa187abSSzymon Olewniczak 217aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern('[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?','row','number'); 218aaa187abSSzymon Olewniczak 219aaa187abSSzymon Olewniczak $res = $Lexer->parse($value); 220aaa187abSSzymon Olewniczak 221*670a6894SAnna Dabrowska $currentMode = $isLegacy ? $Lexer->_mode->getCurrent() : $Lexer->getModeStack()->getCurrent(); 222*670a6894SAnna Dabrowska if (!$res || $currentMode != 'base') { 223aaa187abSSzymon Olewniczak throw new StructException('invalid row value syntax'); 224aaa187abSSzymon Olewniczak } 225aaa187abSSzymon Olewniczak 226aaa187abSSzymon Olewniczak return $Handler->get_row(); 227aaa187abSSzymon Olewniczak } 228aaa187abSSzymon Olewniczak 229aaa187abSSzymon Olewniczak /** 23053528ecfSAndreas Gohr * Wrap given value in asterisks 23153528ecfSAndreas Gohr * 23253528ecfSAndreas Gohr * @param string|string[] $value 23353528ecfSAndreas Gohr * @return string|string[] 23453528ecfSAndreas Gohr */ 23553528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 23653528ecfSAndreas Gohr $map = function ($input) { 23753528ecfSAndreas Gohr return "*$input*"; 23853528ecfSAndreas Gohr }; 23953528ecfSAndreas Gohr 24053528ecfSAndreas Gohr if(is_array($value)) { 24153528ecfSAndreas Gohr $value = array_map($map, $value); 24253528ecfSAndreas Gohr } else { 24353528ecfSAndreas Gohr $value = $map($value); 24453528ecfSAndreas Gohr } 24553528ecfSAndreas Gohr return $value; 24653528ecfSAndreas Gohr } 24753528ecfSAndreas Gohr 24853528ecfSAndreas Gohr /** 24953528ecfSAndreas Gohr * Change given string to use % instead of * 25053528ecfSAndreas Gohr * 25153528ecfSAndreas Gohr * @param string|string[] $value 25253528ecfSAndreas Gohr * @return string|string[] 25353528ecfSAndreas Gohr */ 25453528ecfSAndreas Gohr protected function filterChangeToLike($value) { 25553528ecfSAndreas Gohr $map = function ($input) { 25653528ecfSAndreas Gohr return str_replace('*', '%', $input); 25753528ecfSAndreas Gohr }; 25853528ecfSAndreas Gohr 25953528ecfSAndreas Gohr if(is_array($value)) { 26053528ecfSAndreas Gohr $value = array_map($map, $value); 26153528ecfSAndreas Gohr } else { 26253528ecfSAndreas Gohr $value = $map($value); 26353528ecfSAndreas Gohr } 26453528ecfSAndreas Gohr return $value; 26553528ecfSAndreas Gohr } 26653528ecfSAndreas Gohr 26753528ecfSAndreas Gohr /** 2687f9cb794SAndreas Gohr * Set offset for the results 2697f9cb794SAndreas Gohr * 2707f9cb794SAndreas Gohr * @param int $offset 2717f9cb794SAndreas Gohr */ 2727f9cb794SAndreas Gohr public function setOffset($offset) { 2737f9cb794SAndreas Gohr $limit = 0; 2747f9cb794SAndreas Gohr if($this->range_end) { 2757f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2767f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2777f9cb794SAndreas Gohr } 2787f9cb794SAndreas Gohr $this->range_begin = $offset; 2797f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2807f9cb794SAndreas Gohr } 2817f9cb794SAndreas Gohr 2827f9cb794SAndreas Gohr /** 2837f9cb794SAndreas Gohr * Limit results to this number 2847f9cb794SAndreas Gohr * 2857f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2867f9cb794SAndreas Gohr */ 2877f9cb794SAndreas Gohr public function setLimit($limit) { 2887f9cb794SAndreas Gohr if($limit) { 2897f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2907f9cb794SAndreas Gohr } else { 2917f9cb794SAndreas Gohr $this->range_end = 0; 2927f9cb794SAndreas Gohr } 2937f9cb794SAndreas Gohr } 2947f9cb794SAndreas Gohr 2957f9cb794SAndreas Gohr /** 2967f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2977f9cb794SAndreas Gohr * 2987f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2997f9cb794SAndreas Gohr * 3007f9cb794SAndreas Gohr * @return int 3017f9cb794SAndreas Gohr */ 3027f9cb794SAndreas Gohr public function getCount() { 3037f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 3047f9cb794SAndreas Gohr return $this->count; 3057f9cb794SAndreas Gohr } 3067f9cb794SAndreas Gohr 307c46f5fb1SAndreas Gohr /** 308c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 309c46f5fb1SAndreas Gohr * 310c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 311c46f5fb1SAndreas Gohr * 312c46f5fb1SAndreas Gohr * @return \string[] 313c46f5fb1SAndreas Gohr */ 314d4b5a17cSAndreas Gohr public function getPids() { 315d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 316d4b5a17cSAndreas Gohr return $this->result_pids; 317d4b5a17cSAndreas Gohr } 318d4b5a17cSAndreas Gohr 3197f9cb794SAndreas Gohr /** 3200ceefd5cSAnna Dabrowska * Returns the rid associated with each result row 3210ceefd5cSAnna Dabrowska * 3220ceefd5cSAnna Dabrowska * Important: this may only be called after running @see execute() 3230ceefd5cSAnna Dabrowska * 3240ceefd5cSAnna Dabrowska * @return array 3250ceefd5cSAnna Dabrowska */ 3260ceefd5cSAnna Dabrowska public function getRids() { 3270ceefd5cSAnna Dabrowska if($this->result_rids === null) throw new StructException('rids are only accessible after executing the search'); 3280ceefd5cSAnna Dabrowska return $this->result_rids; 3290ceefd5cSAnna Dabrowska } 3300ceefd5cSAnna Dabrowska 3310ceefd5cSAnna Dabrowska /** 3326fd73b4bSAnna Dabrowska * Returns the rid associated with each result row 3336fd73b4bSAnna Dabrowska * 3346fd73b4bSAnna Dabrowska * Important: this may only be called after running @see execute() 3356fd73b4bSAnna Dabrowska * 3366fd73b4bSAnna Dabrowska * @return array 3376fd73b4bSAnna Dabrowska */ 3386fd73b4bSAnna Dabrowska public function getRevs() { 3396fd73b4bSAnna Dabrowska if($this->result_revs === null) throw new StructException('revs are only accessible after executing the search'); 3406fd73b4bSAnna Dabrowska return $this->result_revs; 3416fd73b4bSAnna Dabrowska } 3426fd73b4bSAnna Dabrowska 3436fd73b4bSAnna Dabrowska /** 344b2ed727aSAndreas Gohr * Execute this search and return the result 345b2ed727aSAndreas Gohr * 34638fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 3477f9cb794SAndreas Gohr * 3487f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 3497f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 35038fa36fbSAndreas Gohr * 351efe74305SAnna Dabrowska * @param string $idColumn Column on which to join tables 35238fa36fbSAndreas Gohr * @return Value[][] 353b2ed727aSAndreas Gohr */ 3547f803aa8SAnna Dabrowska public function execute($idColumn = 'rid') { 355efe74305SAnna Dabrowska list($sql, $opts) = $this->getSQL($idColumn); 356b2ed727aSAndreas Gohr 3577f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 358b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 35959b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 360b2ed727aSAndreas Gohr 361d4b5a17cSAndreas Gohr $this->result_pids = array(); 362b2ed727aSAndreas Gohr $result = array(); 3637f9cb794SAndreas Gohr $cursor = -1; 3645e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 3655e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 3665e4bfdb0SMichael Grosse }, true); 3677f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 3687f9cb794SAndreas Gohr $cursor++; 3697f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 3707f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 3717f9cb794SAndreas Gohr 372b2ed727aSAndreas Gohr $C = 0; 373b2ed727aSAndreas Gohr $resrow = array(); 37499a70f28SAndreas Gohr $isempty = true; 375b2ed727aSAndreas Gohr foreach($this->columns as $col) { 37638fa36fbSAndreas Gohr $val = $row["C$C"]; 377b2ed727aSAndreas Gohr if($col->isMulti()) { 37838fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 379b2ed727aSAndreas Gohr } 38099a70f28SAndreas Gohr $value = new Value($col, $val); 3818cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 38299a70f28SAndreas Gohr $resrow[] = $value; 383b2ed727aSAndreas Gohr $C++; 384b2ed727aSAndreas Gohr } 38599a70f28SAndreas Gohr 38699a70f28SAndreas Gohr // skip empty rows 3878cba7aa0SMichael Grosse if($isempty && !$pageidAndRevOnly) { 38899a70f28SAndreas Gohr $cursor--; 38999a70f28SAndreas Gohr continue; 39099a70f28SAndreas Gohr } 39199a70f28SAndreas Gohr 39299a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 3930ceefd5cSAnna Dabrowska $this->result_rids[] = $row['rid']; 3946fd73b4bSAnna Dabrowska $this->result_revs[] = $row['rev']; 395b2ed727aSAndreas Gohr $result[] = $resrow; 396b2ed727aSAndreas Gohr } 3977f9cb794SAndreas Gohr 3987f9cb794SAndreas Gohr $this->sqlite->res_close($res); 3997f9cb794SAndreas Gohr $this->count = $cursor + 1; 400b2ed727aSAndreas Gohr return $result; 401b2ed727aSAndreas Gohr } 402b2ed727aSAndreas Gohr 403b2ed727aSAndreas Gohr /** 40415929be2SAndreas Gohr * Transform the set search parameters into a statement 40515929be2SAndreas Gohr * 406efe74305SAnna Dabrowska * @param string $idColumn Column on which to join tables 407b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 40815929be2SAndreas Gohr */ 409efe74305SAnna Dabrowska public function getSQL($idColumn) { 4105511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 41115929be2SAndreas Gohr 4122f68434dSAndreas Gohr $QB = new QueryBuilder(); 41315929be2SAndreas Gohr 4149d7a36f9SAndreas Gohr // basic tables 415b2d67e7dSAndreas Gohr $first_table = ''; 4169d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 4172f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 418b2d67e7dSAndreas Gohr if($first_table) { 4199d7a36f9SAndreas Gohr // follow up tables 420efe74305SAnna Dabrowska $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.$idColumn = $datatable.$idColumn"); 4219d7a36f9SAndreas Gohr } else { 4229d7a36f9SAndreas Gohr // first table 42386cfd13cSAnna Dabrowska if($idColumn === 'pid') { 42413c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 4252f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 4262f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 4272f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 4282f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 4292f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 43013c321dbSAndreas Gohr } 43113c321dbSAndreas Gohr 43213c321dbSAndreas Gohr $QB->addTable($datatable); 4330ceefd5cSAnna Dabrowska $QB->addSelectColumn($datatable, 'rid'); 43413c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 4356fd73b4bSAnna Dabrowska $QB->addSelectColumn($datatable, 'rev'); 43613c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 437b2d67e7dSAndreas Gohr 4382f68434dSAndreas Gohr $first_table = $datatable; 43915929be2SAndreas Gohr } 4402f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 4419d7a36f9SAndreas Gohr } 44215929be2SAndreas Gohr 44315929be2SAndreas Gohr // columns to select, handling multis 4449d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 44515929be2SAndreas Gohr $n = 0; 44615929be2SAndreas Gohr foreach($this->columns as $col) { 44715929be2SAndreas Gohr $CN = 'C' . $n++; 44815929be2SAndreas Gohr 449d1b04e89SAndreas Gohr if($col->isMulti()) { 4502f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 4512f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 452db7970caSAndreas Gohr $MN = $QB->generateTableAlias('M'); 4532f68434dSAndreas Gohr 4542f68434dSAndreas Gohr $QB->addLeftJoin( 4552f68434dSAndreas Gohr $datatable, 4562f68434dSAndreas Gohr $multitable, 4572f68434dSAndreas Gohr $MN, 458efe74305SAnna Dabrowska "$datatable.$idColumn = $MN.$idColumn AND 4592f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4602f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4612f68434dSAndreas Gohr ); 462578407b2SAndreas Gohr 463578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 464578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 465578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 46615929be2SAndreas Gohr } else { 467578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 4680dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 46915929be2SAndreas Gohr } 47015929be2SAndreas Gohr } 47115929be2SAndreas Gohr 4729d7a36f9SAndreas Gohr // where clauses 473af993d55SMichael Grosse if (!empty($this->filter)) { 474af993d55SMichael Grosse $userWHERE = $QB->filters()->where('AND'); 475af993d55SMichael Grosse } 476b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 4779ebd2ed6SAndreas Gohr /** @var Column $col */ 4784c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 479b8fe6730SAndreas Gohr 4802f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 4812f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 4829d7a36f9SAndreas Gohr 4839d7a36f9SAndreas Gohr /** @var $col Column */ 4849d7a36f9SAndreas Gohr if($col->isMulti()) { 485db7970caSAndreas Gohr $MN = $QB->generateTableAlias('MN'); 48615929be2SAndreas Gohr 4872f68434dSAndreas Gohr $QB->addLeftJoin( 4882f68434dSAndreas Gohr $datatable, 4892f68434dSAndreas Gohr $multitable, 4902f68434dSAndreas Gohr $MN, 491efe74305SAnna Dabrowska "$datatable.$idColumn = $MN.$idColumn AND 4922f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4932f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4942f68434dSAndreas Gohr ); 495690e4ebaSAndreas Gohr $coltbl = $MN; 496690e4ebaSAndreas Gohr $colnam = 'value'; 4979d7a36f9SAndreas Gohr } else { 498690e4ebaSAndreas Gohr $coltbl = $datatable; 499690e4ebaSAndreas Gohr $colnam = $col->getColName(); 5009d7a36f9SAndreas Gohr } 50153528ecfSAndreas Gohr 502af993d55SMichael Grosse $col->getType()->filter($userWHERE, $coltbl, $colnam, $comp, $value, $op); // type based filter 5039d7a36f9SAndreas Gohr } 5049d7a36f9SAndreas Gohr 50529394e6cSAndreas Gohr // sorting - we always sort by the single val column 506b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 507eb55dc17SAndreas Gohr list($col, $asc, $nc) = $sort; 5089d7a36f9SAndreas Gohr /** @var $col Column */ 509eb55dc17SAndreas Gohr $colname = $col->getColName(false); 510eb55dc17SAndreas Gohr if($nc) $colname .= ' COLLATE NOCASE'; 511eb55dc17SAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $colname, $asc ? 'ASC' : 'DESC'); 5129e07bdbfSAndreas Gohr } 5139e07bdbfSAndreas Gohr 5142f68434dSAndreas Gohr return $QB->getSQL(); 51515929be2SAndreas Gohr } 51615929be2SAndreas Gohr 51715929be2SAndreas Gohr /** 51801dd90deSAndreas Gohr * Returns all the columns that where added to the search 51901dd90deSAndreas Gohr * 52001dd90deSAndreas Gohr * @return Column[] 52101dd90deSAndreas Gohr */ 52201dd90deSAndreas Gohr public function getColumns() { 52301dd90deSAndreas Gohr return $this->columns; 52401dd90deSAndreas Gohr } 52501dd90deSAndreas Gohr 526577b462bSAndreas Gohr /** 5275a4df70dSAndreas Gohr * All the schemas currently added 5285a4df70dSAndreas Gohr * 5295a4df70dSAndreas Gohr * @return Schema[] 5305a4df70dSAndreas Gohr */ 5315a4df70dSAndreas Gohr public function getSchemas() { 5325a4df70dSAndreas Gohr return array_values($this->schemas); 5335a4df70dSAndreas Gohr } 5345a4df70dSAndreas Gohr 5355a4df70dSAndreas Gohr /** 536577b462bSAndreas Gohr * Checks if the given column is a * wildcard 537577b462bSAndreas Gohr * 538577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 539577b462bSAndreas Gohr * nothing happens 540577b462bSAndreas Gohr * 541577b462bSAndreas Gohr * @param string $colname 542577b462bSAndreas Gohr * @return bool was wildcard? 543577b462bSAndreas Gohr */ 544577b462bSAndreas Gohr protected function processWildcard($colname) { 545636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 546577b462bSAndreas Gohr if($colname !== '*') return false; 547577b462bSAndreas Gohr 548577b462bSAndreas Gohr // no table given? assume the first is meant 549577b462bSAndreas Gohr if($table === null) { 550577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 551577b462bSAndreas Gohr $table = $schema_list[0]; 552577b462bSAndreas Gohr } 553577b462bSAndreas Gohr 554577b462bSAndreas Gohr $schema = $this->schemas[$table]; 555577b462bSAndreas Gohr if(!$schema) return false; 556577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 557577b462bSAndreas Gohr return true; 558577b462bSAndreas Gohr } 559577b462bSAndreas Gohr 560577b462bSAndreas Gohr /** 561577b462bSAndreas Gohr * Split a given column name into table and column 562577b462bSAndreas Gohr * 563577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 564577b462bSAndreas Gohr * 565577b462bSAndreas Gohr * @param $colname 566636a5173SAndreas Gohr * @return array (colname, table) 567577b462bSAndreas Gohr */ 568577b462bSAndreas Gohr protected function resolveColumn($colname) { 569577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 570577b462bSAndreas Gohr 571577b462bSAndreas Gohr // resolve the alias or table name 5729007da58SMichael Große @list($table, $colname) = explode('.', $colname, 2); 573577b462bSAndreas Gohr if(!$colname) { 574577b462bSAndreas Gohr $colname = $table; 575577b462bSAndreas Gohr $table = null; 576577b462bSAndreas Gohr } 577577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 578577b462bSAndreas Gohr $table = $this->aliases[$table]; 579577b462bSAndreas Gohr } 580577b462bSAndreas Gohr 581577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 582577b462bSAndreas Gohr 583577b462bSAndreas Gohr return array($colname, $table); 584577b462bSAndreas Gohr } 58501dd90deSAndreas Gohr 58601dd90deSAndreas Gohr /** 58715929be2SAndreas Gohr * Find a column to be used in the search 58815929be2SAndreas Gohr * 58915929be2SAndreas Gohr * @param string $colname may contain an alias 59015929be2SAndreas Gohr * @return bool|Column 59115929be2SAndreas Gohr */ 59200f6af48SAndreas Gohr public function findColumn($colname) { 5935511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 594df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 595f107f479SAndreas Gohr 596f107f479SAndreas Gohr // add "fake" column for special col 597128d4e83SAndreas Gohr if($colname == '%pageid%') { 598128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 599d1b04e89SAndreas Gohr } 600128d4e83SAndreas Gohr if($colname == '%title%') { 601128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 602128d4e83SAndreas Gohr } 603cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 604cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 605cadfc3ccSAndreas Gohr } 6062e12ac22SMichael Grosse if ($colname == '%lasteditor%') { 6072e12ac22SMichael Grosse return new UserColumn(0, new User(), $schema_list[0]); 6082e12ac22SMichael Grosse } 60988b58a21SSzymon Olewniczak if ($colname == '%lastsummary%') { 6106781c68dSSzymon Olewniczak return new SummaryColumn(0, new AutoSummary(), $schema_list[0]); 61188b58a21SSzymon Olewniczak } 612f107f479SAndreas Gohr if($colname == '%rowid%') { 613f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 614f107f479SAndreas Gohr } 615d1b04e89SAndreas Gohr 616636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 61715929be2SAndreas Gohr 618bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 619577b462bSAndreas Gohr if($table !== null) { 62015929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 62115929be2SAndreas Gohr } else { 62215929be2SAndreas Gohr $schemas = $this->schemas; 62315929be2SAndreas Gohr } 62415929be2SAndreas Gohr 62515929be2SAndreas Gohr // find it 62615929be2SAndreas Gohr $col = false; 62715929be2SAndreas Gohr foreach($schemas as $schema) { 6284ac44d1cSMichael Grosse if(empty($schema)) { 6294ac44d1cSMichael Grosse continue; 6304ac44d1cSMichael Grosse } 63115929be2SAndreas Gohr $col = $schema->findColumn($colname); 63215929be2SAndreas Gohr if($col) break; 63315929be2SAndreas Gohr } 63415929be2SAndreas Gohr 63515929be2SAndreas Gohr return $col; 63615929be2SAndreas Gohr } 63715929be2SAndreas Gohr 6389dbc8ec0SMichael Grosse /** 63999a70f28SAndreas Gohr * Check if the given row is empty or references our own row 6409dbc8ec0SMichael Grosse * 64199a70f28SAndreas Gohr * @param Value $value 6429dbc8ec0SMichael Grosse * @return bool 6439dbc8ec0SMichael Grosse */ 6448cba7aa0SMichael Grosse protected function isEmptyValue(Value $value) { 64599a70f28SAndreas Gohr if ($value->isEmpty()) return true; 6468cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 6479dbc8ec0SMichael Grosse return false; 6489dbc8ec0SMichael Grosse } 64915929be2SAndreas Gohr} 65015929be2SAndreas Gohr 6515511bd5bSAndreas Gohr 652