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 1261356325SAnna Dabrowskaclass Search 1361356325SAnna Dabrowska{ 149d7a36f9SAndreas Gohr /** 159d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 169d7a36f9SAndreas Gohr */ 179d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 189d7a36f9SAndreas Gohr 195511bd5bSAndreas Gohr /** 205511bd5bSAndreas Gohr * The list of known and allowed comparators 212e7595e7SAndreas Gohr * (order matters) 225511bd5bSAndreas Gohr */ 2361356325SAnna Dabrowska public static $COMPARATORS = array( 24aaa187abSSzymon Olewniczak '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', ' IN ' 255511bd5bSAndreas Gohr ); 265511bd5bSAndreas Gohr 279d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 289d7a36f9SAndreas Gohr protected $sqlite; 2915929be2SAndreas Gohr 3015929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 3115929be2SAndreas Gohr protected $schemas = array(); 3215929be2SAndreas Gohr 3315929be2SAndreas Gohr /** @var Column[] list of columns to select */ 3415929be2SAndreas Gohr protected $columns = array(); 3515929be2SAndreas Gohr 3615929be2SAndreas Gohr /** @var array the sorting of the result */ 3715929be2SAndreas Gohr protected $sortby = array(); 3815929be2SAndreas Gohr 399d7a36f9SAndreas Gohr /** @var array the filters */ 409d7a36f9SAndreas Gohr protected $filter = array(); 4115929be2SAndreas Gohr 4215929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 4315929be2SAndreas Gohr protected $aliases = array(); 4415929be2SAndreas Gohr 457f9cb794SAndreas Gohr /** @var int begin results from here */ 467f9cb794SAndreas Gohr protected $range_begin = 0; 477f9cb794SAndreas Gohr 487f9cb794SAndreas Gohr /** @var int end results here */ 497f9cb794SAndreas Gohr protected $range_end = 0; 507f9cb794SAndreas Gohr 517f9cb794SAndreas Gohr /** @var int the number of results */ 527f9cb794SAndreas Gohr protected $count = -1; 53d4b5a17cSAndreas Gohr /** @var string[] the PIDs of the result rows */ 54d4b5a17cSAndreas Gohr protected $result_pids = null; 550ceefd5cSAnna Dabrowska /** @var array the row ids of the result rows */ 560ceefd5cSAnna Dabrowska protected $result_rids = []; 576fd73b4bSAnna Dabrowska /** @var array the revisions of the result rows */ 586fd73b4bSAnna Dabrowska protected $result_revs = []; 597f9cb794SAndreas Gohr 6015929be2SAndreas Gohr /** 619d7a36f9SAndreas Gohr * Search constructor. 629d7a36f9SAndreas Gohr */ 6361356325SAnna Dabrowska public function __construct() 6461356325SAnna Dabrowska { 659d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 669d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 679d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 689d7a36f9SAndreas Gohr } 699d7a36f9SAndreas Gohr 709d7a36f9SAndreas Gohr /** 7115929be2SAndreas Gohr * Add a schema to be searched 7215929be2SAndreas Gohr * 7315929be2SAndreas Gohr * Call multiple times for multiple schemas. 7415929be2SAndreas Gohr * 7515929be2SAndreas Gohr * @param string $table 7615929be2SAndreas Gohr * @param string $alias 7715929be2SAndreas Gohr */ 7861356325SAnna Dabrowska public function addSchema($table, $alias = '') 7961356325SAnna Dabrowska { 802be187cdSAndreas Gohr $schema = new Schema($table); 812be187cdSAndreas Gohr if (!$schema->getId()) { 822be187cdSAndreas Gohr throw new StructException('schema missing', $table); 832be187cdSAndreas Gohr } 842be187cdSAndreas Gohr 85712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 86712c650dSAndreas Gohr if ($alias) $this->aliases[$alias] = $schema->getTable(); 8715929be2SAndreas Gohr } 8815929be2SAndreas Gohr 8915929be2SAndreas Gohr /** 9015929be2SAndreas Gohr * Add a column to be returned by the search 9115929be2SAndreas Gohr * 9215929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9315929be2SAndreas Gohr * added before 9415929be2SAndreas Gohr * 9515929be2SAndreas Gohr * @param string $colname may contain an alias 9615929be2SAndreas Gohr */ 9761356325SAnna Dabrowska public function addColumn($colname) 9861356325SAnna Dabrowska { 99577b462bSAndreas Gohr if ($this->processWildcard($colname)) return; // wildcard? 100*b26126edSAndreas Hubel if ($colname[0] == '-') { // remove column from previous wildcard lookup 101*b26126edSAndreas Hubel $colname = substr($colname, 1); 102*b26126edSAndreas Hubel foreach($this->columns as $key => $col){ 103*b26126edSAndreas Hubel if ($col->getLabel() == $colname) unset($this->columns[$key]); 104*b26126edSAndreas Hubel } 105*b26126edSAndreas Hubel return; 106*b26126edSAndreas Hubel } 107*b26126edSAndreas Hubel 10815929be2SAndreas Gohr $col = $this->findColumn($colname); 10915929be2SAndreas Gohr if (!$col) return; //FIXME do we really want to ignore missing columns? 11015929be2SAndreas Gohr $this->columns[] = $col; 11115929be2SAndreas Gohr } 11215929be2SAndreas Gohr 11315929be2SAndreas Gohr /** 11415929be2SAndreas Gohr * Add sorting options 11515929be2SAndreas Gohr * 11615929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 11715929be2SAndreas Gohr * added before 11815929be2SAndreas Gohr * 11915929be2SAndreas Gohr * @param string $colname may contain an alias 12015929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 121eb55dc17SAndreas Gohr * @param bool $nc set true for caseinsensitivity 12215929be2SAndreas Gohr */ 12361356325SAnna Dabrowska public function addSort($colname, $asc = true, $nc = true) 12461356325SAnna Dabrowska { 12515929be2SAndreas Gohr $col = $this->findColumn($colname); 12615929be2SAndreas Gohr if (!$col) return; //FIXME do we really want to ignore missing columns? 12715929be2SAndreas Gohr 128eb55dc17SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc, $nc); 12907993756SAndreas Gohr } 13007993756SAndreas Gohr 13107993756SAndreas Gohr /** 13207993756SAndreas Gohr * Returns all set sort columns 13307993756SAndreas Gohr * 13407993756SAndreas Gohr * @return array 13507993756SAndreas Gohr */ 13661356325SAnna Dabrowska public function getSorts() 13761356325SAnna Dabrowska { 13807993756SAndreas Gohr return $this->sortby; 13915929be2SAndreas Gohr } 14015929be2SAndreas Gohr 14115929be2SAndreas Gohr /** 1429d7a36f9SAndreas Gohr * Adds a filter 14315929be2SAndreas Gohr * 14415929be2SAndreas Gohr * @param string $colname may contain an alias 14553528ecfSAndreas Gohr * @param string|string[] $value 1465511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1474c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 14815929be2SAndreas Gohr */ 14961356325SAnna Dabrowska public function addFilter($colname, $value, $comp, $op = 'OR') 15061356325SAnna Dabrowska { 1519dbc32aeSAndreas Gohr /* Convert certain filters into others 1529dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1539dbc32aeSAndreas Gohr if ($comp == '*~') { 15453528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1559dbc32aeSAndreas Gohr $comp = '~'; 1569dbc32aeSAndreas Gohr } elseif ($comp == '<>') { 1579dbc32aeSAndreas Gohr $comp = '!='; 1589dbc32aeSAndreas Gohr } 1599dbc32aeSAndreas Gohr 16074461852SAndreas Gohr if (!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1614c13ff97SAndreas Gohr if ($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1629d7a36f9SAndreas Gohr 16315929be2SAndreas Gohr $col = $this->findColumn($colname); 1647da1a0fdSAndreas Gohr if (!$col) return; // ignore missing columns, filter might have been for different schema 1657da1a0fdSAndreas Gohr 1667da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1677da1a0fdSAndreas Gohr switch ($comp) { 1687da1a0fdSAndreas Gohr case '~': 1697da1a0fdSAndreas Gohr $comp = 'LIKE'; 1707da1a0fdSAndreas Gohr break; 1717da1a0fdSAndreas Gohr case '!~': 1727da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1737da1a0fdSAndreas Gohr break; 1747da1a0fdSAndreas Gohr case '=*': 1757da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1767da1a0fdSAndreas Gohr break; 1777da1a0fdSAndreas Gohr } 1787da1a0fdSAndreas Gohr 1797da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1807da1a0fdSAndreas Gohr if ($comp == 'LIKE' || $comp == 'NOT LIKE') { 18153528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1827da1a0fdSAndreas Gohr } 1837da1a0fdSAndreas Gohr 184aaa187abSSzymon Olewniczak if ($comp == ' IN ' && !is_array($value)) { 185efff5841SSzymon Olewniczak $value = $this->parseFilterValueList($value); 186aaa187abSSzymon Olewniczak //col IN ('a', 'b', 'c') is equal to col = 'a' OR 'col = 'b' OR col = 'c' 187aaa187abSSzymon Olewniczak $comp = '='; 188aaa187abSSzymon Olewniczak } 189aaa187abSSzymon Olewniczak 1907da1a0fdSAndreas Gohr // add the filter 1914c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 19215929be2SAndreas Gohr } 19315929be2SAndreas Gohr 19415929be2SAndreas Gohr /** 195aaa187abSSzymon Olewniczak * Parse SQLite row value into array 196aaa187abSSzymon Olewniczak * 197aaa187abSSzymon Olewniczak * @param string $value 198aaa187abSSzymon Olewniczak * @return string[] 199aaa187abSSzymon Olewniczak */ 20061356325SAnna Dabrowska protected function parseFilterValueList($value) 20161356325SAnna Dabrowska { 202efff5841SSzymon Olewniczak $Handler = new FilterValueListHandler(); 203670a6894SAnna Dabrowska $LexerClass = class_exists('\Doku_Lexer') ? '\Doku_Lexer' : '\dokuwiki\Parsing\Lexer\Lexer'; 204670a6894SAnna Dabrowska $isLegacy = $LexerClass === '\Doku_Lexer'; 205670a6894SAnna Dabrowska /** @var \Doku_Lexer|\dokuwiki\Parsing\Lexer\Lexer $Lexer */ 206670a6894SAnna Dabrowska $Lexer = new $LexerClass($Handler, 'base', true); 207670a6894SAnna Dabrowska 208aaa187abSSzymon Olewniczak 209aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('\(', 'base', 'row'); 210aaa187abSSzymon Olewniczak $Lexer->addPattern('\s*,\s*', 'row'); 211aaa187abSSzymon Olewniczak $Lexer->addExitPattern('\)', 'row'); 212aaa187abSSzymon Olewniczak 213aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('"', 'row', 'double_quote_string'); 214dfe3ae67SAnna Dabrowska $Lexer->addSpecialPattern('\\\\"', 'double_quote_string', 'escapeSequence'); 215aaa187abSSzymon Olewniczak $Lexer->addExitPattern('"', 'double_quote_string'); 216aaa187abSSzymon Olewniczak 217dfe3ae67SAnna Dabrowska $Lexer->addEntryPattern("'", 'row', 'singleQuoteString'); 218dfe3ae67SAnna Dabrowska $Lexer->addSpecialPattern("\\\\'", 'singleQuoteString', 'escapeSequence'); 219dfe3ae67SAnna Dabrowska $Lexer->addExitPattern("'", 'singleQuoteString'); 220aaa187abSSzymon Olewniczak 221dfe3ae67SAnna Dabrowska $Lexer->mapHandler('double_quote_string', 'singleQuoteString'); 222aaa187abSSzymon Olewniczak 223aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern('[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?', 'row', 'number'); 224aaa187abSSzymon Olewniczak 225aaa187abSSzymon Olewniczak $res = $Lexer->parse($value); 226aaa187abSSzymon Olewniczak 227670a6894SAnna Dabrowska $currentMode = $isLegacy ? $Lexer->_mode->getCurrent() : $Lexer->getModeStack()->getCurrent(); 228670a6894SAnna Dabrowska if (!$res || $currentMode != 'base') { 229aaa187abSSzymon Olewniczak throw new StructException('invalid row value syntax'); 230aaa187abSSzymon Olewniczak } 231aaa187abSSzymon Olewniczak 232dfe3ae67SAnna Dabrowska return $Handler->getRow(); 233aaa187abSSzymon Olewniczak } 234aaa187abSSzymon Olewniczak 235aaa187abSSzymon Olewniczak /** 23653528ecfSAndreas Gohr * Wrap given value in asterisks 23753528ecfSAndreas Gohr * 23853528ecfSAndreas Gohr * @param string|string[] $value 23953528ecfSAndreas Gohr * @return string|string[] 24053528ecfSAndreas Gohr */ 24161356325SAnna Dabrowska protected function filterWrapAsterisks($value) 24261356325SAnna Dabrowska { 24353528ecfSAndreas Gohr $map = function ($input) { 24453528ecfSAndreas Gohr return "*$input*"; 24553528ecfSAndreas Gohr }; 24653528ecfSAndreas Gohr 24753528ecfSAndreas Gohr if (is_array($value)) { 24853528ecfSAndreas Gohr $value = array_map($map, $value); 24953528ecfSAndreas Gohr } else { 25053528ecfSAndreas Gohr $value = $map($value); 25153528ecfSAndreas Gohr } 25253528ecfSAndreas Gohr return $value; 25353528ecfSAndreas Gohr } 25453528ecfSAndreas Gohr 25553528ecfSAndreas Gohr /** 25653528ecfSAndreas Gohr * Change given string to use % instead of * 25753528ecfSAndreas Gohr * 25853528ecfSAndreas Gohr * @param string|string[] $value 25953528ecfSAndreas Gohr * @return string|string[] 26053528ecfSAndreas Gohr */ 26161356325SAnna Dabrowska protected function filterChangeToLike($value) 26261356325SAnna Dabrowska { 26353528ecfSAndreas Gohr $map = function ($input) { 26453528ecfSAndreas Gohr return str_replace('*', '%', $input); 26553528ecfSAndreas Gohr }; 26653528ecfSAndreas Gohr 26753528ecfSAndreas Gohr if (is_array($value)) { 26853528ecfSAndreas Gohr $value = array_map($map, $value); 26953528ecfSAndreas Gohr } else { 27053528ecfSAndreas Gohr $value = $map($value); 27153528ecfSAndreas Gohr } 27253528ecfSAndreas Gohr return $value; 27353528ecfSAndreas Gohr } 27453528ecfSAndreas Gohr 27553528ecfSAndreas Gohr /** 2767f9cb794SAndreas Gohr * Set offset for the results 2777f9cb794SAndreas Gohr * 2787f9cb794SAndreas Gohr * @param int $offset 2797f9cb794SAndreas Gohr */ 28061356325SAnna Dabrowska public function setOffset($offset) 28161356325SAnna Dabrowska { 2827f9cb794SAndreas Gohr $limit = 0; 2837f9cb794SAndreas Gohr if ($this->range_end) { 2847f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2857f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2867f9cb794SAndreas Gohr } 2877f9cb794SAndreas Gohr $this->range_begin = $offset; 2887f9cb794SAndreas Gohr if ($limit) $this->setLimit($limit); 2897f9cb794SAndreas Gohr } 2907f9cb794SAndreas Gohr 2917f9cb794SAndreas Gohr /** 2927f9cb794SAndreas Gohr * Limit results to this number 2937f9cb794SAndreas Gohr * 2947f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2957f9cb794SAndreas Gohr */ 29661356325SAnna Dabrowska public function setLimit($limit) 29761356325SAnna Dabrowska { 2987f9cb794SAndreas Gohr if ($limit) { 2997f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 3007f9cb794SAndreas Gohr } else { 3017f9cb794SAndreas Gohr $this->range_end = 0; 3027f9cb794SAndreas Gohr } 3037f9cb794SAndreas Gohr } 3047f9cb794SAndreas Gohr 3057f9cb794SAndreas Gohr /** 3067f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 3077f9cb794SAndreas Gohr * 3087f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 3097f9cb794SAndreas Gohr * 3107f9cb794SAndreas Gohr * @return int 3117f9cb794SAndreas Gohr */ 31261356325SAnna Dabrowska public function getCount() 31361356325SAnna Dabrowska { 3147f9cb794SAndreas Gohr if ($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 3157f9cb794SAndreas Gohr return $this->count; 3167f9cb794SAndreas Gohr } 3177f9cb794SAndreas Gohr 318c46f5fb1SAndreas Gohr /** 319c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 320c46f5fb1SAndreas Gohr * 321c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 322c46f5fb1SAndreas Gohr * 323c46f5fb1SAndreas Gohr * @return \string[] 324c46f5fb1SAndreas Gohr */ 32561356325SAnna Dabrowska public function getPids() 32661356325SAnna Dabrowska { 327d4b5a17cSAndreas Gohr if ($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 328d4b5a17cSAndreas Gohr return $this->result_pids; 329d4b5a17cSAndreas Gohr } 330d4b5a17cSAndreas Gohr 3317f9cb794SAndreas Gohr /** 3320ceefd5cSAnna Dabrowska * Returns the rid associated with each result row 3330ceefd5cSAnna Dabrowska * 3340ceefd5cSAnna Dabrowska * Important: this may only be called after running @see execute() 3350ceefd5cSAnna Dabrowska * 3360ceefd5cSAnna Dabrowska * @return array 3370ceefd5cSAnna Dabrowska */ 33861356325SAnna Dabrowska public function getRids() 33961356325SAnna Dabrowska { 3400ceefd5cSAnna Dabrowska if ($this->result_rids === null) throw new StructException('rids are only accessible after executing the search'); 3410ceefd5cSAnna Dabrowska return $this->result_rids; 3420ceefd5cSAnna Dabrowska } 3430ceefd5cSAnna Dabrowska 3440ceefd5cSAnna Dabrowska /** 3456fd73b4bSAnna Dabrowska * Returns the rid associated with each result row 3466fd73b4bSAnna Dabrowska * 3476fd73b4bSAnna Dabrowska * Important: this may only be called after running @see execute() 3486fd73b4bSAnna Dabrowska * 3496fd73b4bSAnna Dabrowska * @return array 3506fd73b4bSAnna Dabrowska */ 35161356325SAnna Dabrowska public function getRevs() 35261356325SAnna Dabrowska { 3536fd73b4bSAnna Dabrowska if ($this->result_revs === null) throw new StructException('revs are only accessible after executing the search'); 3546fd73b4bSAnna Dabrowska return $this->result_revs; 3556fd73b4bSAnna Dabrowska } 3566fd73b4bSAnna Dabrowska 3576fd73b4bSAnna Dabrowska /** 358b2ed727aSAndreas Gohr * Execute this search and return the result 359b2ed727aSAndreas Gohr * 36038fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 3617f9cb794SAndreas Gohr * 3627f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 3637f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 36438fa36fbSAndreas Gohr * 36538fa36fbSAndreas Gohr * @return Value[][] 366b2ed727aSAndreas Gohr */ 3678ce43f5aSAnna Dabrowska public function execute() 36861356325SAnna Dabrowska { 3698ce43f5aSAnna Dabrowska list($sql, $opts) = $this->getSQL(); 370b2ed727aSAndreas Gohr 3717f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 372b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 37359b668aaSAndreas Gohr if ($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 374b2ed727aSAndreas Gohr 375d4b5a17cSAndreas Gohr $this->result_pids = array(); 376b2ed727aSAndreas Gohr $result = array(); 3777f9cb794SAndreas Gohr $cursor = -1; 3785e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 3795e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 3805e4bfdb0SMichael Grosse }, true); 3817f9cb794SAndreas Gohr while ($row = $res->fetch(\PDO::FETCH_ASSOC)) { 3827f9cb794SAndreas Gohr $cursor++; 3837f9cb794SAndreas Gohr if ($cursor < $this->range_begin) continue; 3847f9cb794SAndreas Gohr if ($this->range_end && $cursor >= $this->range_end) continue; 3857f9cb794SAndreas Gohr 386b2ed727aSAndreas Gohr $C = 0; 387b2ed727aSAndreas Gohr $resrow = array(); 38899a70f28SAndreas Gohr $isempty = true; 389b2ed727aSAndreas Gohr foreach ($this->columns as $col) { 39038fa36fbSAndreas Gohr $val = $row["C$C"]; 391b2ed727aSAndreas Gohr if ($col->isMulti()) { 39238fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 393b2ed727aSAndreas Gohr } 39499a70f28SAndreas Gohr $value = new Value($col, $val); 3958cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 39699a70f28SAndreas Gohr $resrow[] = $value; 397b2ed727aSAndreas Gohr $C++; 398b2ed727aSAndreas Gohr } 39999a70f28SAndreas Gohr 40099a70f28SAndreas Gohr // skip empty rows 4018cba7aa0SMichael Grosse if ($isempty && !$pageidAndRevOnly) { 40299a70f28SAndreas Gohr $cursor--; 40399a70f28SAndreas Gohr continue; 40499a70f28SAndreas Gohr } 40599a70f28SAndreas Gohr 40699a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 4070ceefd5cSAnna Dabrowska $this->result_rids[] = $row['rid']; 4086fd73b4bSAnna Dabrowska $this->result_revs[] = $row['rev']; 409b2ed727aSAndreas Gohr $result[] = $resrow; 410b2ed727aSAndreas Gohr } 4117f9cb794SAndreas Gohr 4127f9cb794SAndreas Gohr $this->sqlite->res_close($res); 4137f9cb794SAndreas Gohr $this->count = $cursor + 1; 414b2ed727aSAndreas Gohr return $result; 415b2ed727aSAndreas Gohr } 416b2ed727aSAndreas Gohr 417b2ed727aSAndreas Gohr /** 41815929be2SAndreas Gohr * Transform the set search parameters into a statement 41915929be2SAndreas Gohr * 420b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 42115929be2SAndreas Gohr */ 4228ce43f5aSAnna Dabrowska public function getSQL() 42361356325SAnna Dabrowska { 4245511bd5bSAndreas Gohr if (!$this->columns) throw new StructException('nocolname'); 42515929be2SAndreas Gohr 4262f68434dSAndreas Gohr $QB = new QueryBuilder(); 42715929be2SAndreas Gohr 4289d7a36f9SAndreas Gohr // basic tables 429b2d67e7dSAndreas Gohr $first_table = ''; 4309d7a36f9SAndreas Gohr foreach ($this->schemas as $schema) { 4312f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 432b2d67e7dSAndreas Gohr if ($first_table) { 4339d7a36f9SAndreas Gohr // follow up tables 4348ce43f5aSAnna Dabrowska $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 4359d7a36f9SAndreas Gohr } else { 4369d7a36f9SAndreas Gohr // first table 43713c321dbSAndreas Gohr $QB->addTable($datatable); 4388ce43f5aSAnna Dabrowska 4398ce43f5aSAnna Dabrowska // add conditional page clauses if pid has a value 4408ce43f5aSAnna Dabrowska $subAnd = $QB->filters()->whereSubAnd(); 4418ce43f5aSAnna Dabrowska $subAnd->whereAnd("$datatable.pid = ''"); 4428ce43f5aSAnna Dabrowska $subOr = $subAnd->whereSubOr(); 4438ce43f5aSAnna Dabrowska $subOr->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 4448ce43f5aSAnna Dabrowska $subOr->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 44554efd03eSAnna Dabrowska $subOr->whereAnd('(ASSIGNED = 1 OR ASSIGNED IS NULL)'); 4468ce43f5aSAnna Dabrowska 4478ce43f5aSAnna Dabrowska // add conditional schema assignment check 448f183bf6eSAnna Dabrowska $QB->addLeftJoin( 449f183bf6eSAnna Dabrowska $datatable, 450f183bf6eSAnna Dabrowska 'schema_assignments', 451f183bf6eSAnna Dabrowska '', 4528ce43f5aSAnna Dabrowska "$datatable.pid != '' 4538ce43f5aSAnna Dabrowska AND $datatable.pid = schema_assignments.pid 45438e29673SAnna Dabrowska AND schema_assignments.tbl = '{$schema->getTable()}'" 4558ce43f5aSAnna Dabrowska ); 4568ce43f5aSAnna Dabrowska 4570ceefd5cSAnna Dabrowska $QB->addSelectColumn($datatable, 'rid'); 45813c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 4596fd73b4bSAnna Dabrowska $QB->addSelectColumn($datatable, 'rev'); 46038e29673SAnna Dabrowska $QB->addSelectColumn('schema_assignments', 'assigned', 'ASSIGNED'); 46113c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 462e3104939SAnna Dabrowska $QB->addGroupByColumn($datatable, 'rid'); 463b2d67e7dSAndreas Gohr 4642f68434dSAndreas Gohr $first_table = $datatable; 46515929be2SAndreas Gohr } 4662f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 4679d7a36f9SAndreas Gohr } 46815929be2SAndreas Gohr 46915929be2SAndreas Gohr // columns to select, handling multis 4709d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 47115929be2SAndreas Gohr $n = 0; 47215929be2SAndreas Gohr foreach ($this->columns as $col) { 47315929be2SAndreas Gohr $CN = 'C' . $n++; 47415929be2SAndreas Gohr 475d1b04e89SAndreas Gohr if ($col->isMulti()) { 4762f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 4772f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 478db7970caSAndreas Gohr $MN = $QB->generateTableAlias('M'); 4792f68434dSAndreas Gohr 4802f68434dSAndreas Gohr $QB->addLeftJoin( 4812f68434dSAndreas Gohr $datatable, 4822f68434dSAndreas Gohr $multitable, 4832f68434dSAndreas Gohr $MN, 4848ce43f5aSAnna Dabrowska "$datatable.pid = $MN.pid AND $datatable.rid = $MN.rid AND 4852f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4862f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4872f68434dSAndreas Gohr ); 488578407b2SAndreas Gohr 489578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 490578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 491578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 49215929be2SAndreas Gohr } else { 493578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 4940dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 49515929be2SAndreas Gohr } 49615929be2SAndreas Gohr } 49715929be2SAndreas Gohr 4989d7a36f9SAndreas Gohr // where clauses 499af993d55SMichael Grosse if (!empty($this->filter)) { 500af993d55SMichael Grosse $userWHERE = $QB->filters()->where('AND'); 501af993d55SMichael Grosse } 502b8fe6730SAndreas Gohr foreach ($this->filter as $filter) { 5039ebd2ed6SAndreas Gohr /** @var Column $col */ 5044c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 505b8fe6730SAndreas Gohr 5062f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 5072f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 5089d7a36f9SAndreas Gohr 5099d7a36f9SAndreas Gohr /** @var $col Column */ 5109d7a36f9SAndreas Gohr if ($col->isMulti()) { 511db7970caSAndreas Gohr $MN = $QB->generateTableAlias('MN'); 51215929be2SAndreas Gohr 5132f68434dSAndreas Gohr $QB->addLeftJoin( 5142f68434dSAndreas Gohr $datatable, 5152f68434dSAndreas Gohr $multitable, 5162f68434dSAndreas Gohr $MN, 5178ce43f5aSAnna Dabrowska "$datatable.pid = $MN.pid AND $datatable.rid = $MN.rid AND 5182f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 5192f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 5202f68434dSAndreas Gohr ); 521690e4ebaSAndreas Gohr $coltbl = $MN; 522690e4ebaSAndreas Gohr $colnam = 'value'; 5239d7a36f9SAndreas Gohr } else { 524690e4ebaSAndreas Gohr $coltbl = $datatable; 525690e4ebaSAndreas Gohr $colnam = $col->getColName(); 5269d7a36f9SAndreas Gohr } 52753528ecfSAndreas Gohr 528af993d55SMichael Grosse $col->getType()->filter($userWHERE, $coltbl, $colnam, $comp, $value, $op); // type based filter 5299d7a36f9SAndreas Gohr } 5309d7a36f9SAndreas Gohr 53129394e6cSAndreas Gohr // sorting - we always sort by the single val column 532b8fe6730SAndreas Gohr foreach ($this->sortby as $sort) { 533eb55dc17SAndreas Gohr list($col, $asc, $nc) = $sort; 5349d7a36f9SAndreas Gohr /** @var $col Column */ 535eb55dc17SAndreas Gohr $colname = $col->getColName(false); 536eb55dc17SAndreas Gohr if ($nc) $colname .= ' COLLATE NOCASE'; 537eb55dc17SAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $colname, $asc ? 'ASC' : 'DESC'); 5389e07bdbfSAndreas Gohr } 5399e07bdbfSAndreas Gohr 5402f68434dSAndreas Gohr return $QB->getSQL(); 54115929be2SAndreas Gohr } 54215929be2SAndreas Gohr 54315929be2SAndreas Gohr /** 54401dd90deSAndreas Gohr * Returns all the columns that where added to the search 54501dd90deSAndreas Gohr * 54601dd90deSAndreas Gohr * @return Column[] 54701dd90deSAndreas Gohr */ 54861356325SAnna Dabrowska public function getColumns() 54961356325SAnna Dabrowska { 55001dd90deSAndreas Gohr return $this->columns; 55101dd90deSAndreas Gohr } 55201dd90deSAndreas Gohr 553577b462bSAndreas Gohr /** 5545a4df70dSAndreas Gohr * All the schemas currently added 5555a4df70dSAndreas Gohr * 5565a4df70dSAndreas Gohr * @return Schema[] 5575a4df70dSAndreas Gohr */ 55861356325SAnna Dabrowska public function getSchemas() 55961356325SAnna Dabrowska { 5605a4df70dSAndreas Gohr return array_values($this->schemas); 5615a4df70dSAndreas Gohr } 5625a4df70dSAndreas Gohr 5635a4df70dSAndreas Gohr /** 564577b462bSAndreas Gohr * Checks if the given column is a * wildcard 565577b462bSAndreas Gohr * 566577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 567577b462bSAndreas Gohr * nothing happens 568577b462bSAndreas Gohr * 569577b462bSAndreas Gohr * @param string $colname 570577b462bSAndreas Gohr * @return bool was wildcard? 571577b462bSAndreas Gohr */ 57261356325SAnna Dabrowska protected function processWildcard($colname) 57361356325SAnna Dabrowska { 574636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 575577b462bSAndreas Gohr if ($colname !== '*') return false; 576577b462bSAndreas Gohr 577577b462bSAndreas Gohr // no table given? assume the first is meant 578577b462bSAndreas Gohr if ($table === null) { 579577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 580577b462bSAndreas Gohr $table = $schema_list[0]; 581577b462bSAndreas Gohr } 582577b462bSAndreas Gohr 583577b462bSAndreas Gohr $schema = $this->schemas[$table]; 584577b462bSAndreas Gohr if (!$schema) return false; 585577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 586577b462bSAndreas Gohr return true; 587577b462bSAndreas Gohr } 588577b462bSAndreas Gohr 589577b462bSAndreas Gohr /** 590577b462bSAndreas Gohr * Split a given column name into table and column 591577b462bSAndreas Gohr * 592577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 593577b462bSAndreas Gohr * 594577b462bSAndreas Gohr * @param $colname 595636a5173SAndreas Gohr * @return array (colname, table) 596577b462bSAndreas Gohr */ 59761356325SAnna Dabrowska protected function resolveColumn($colname) 59861356325SAnna Dabrowska { 599577b462bSAndreas Gohr if (!$this->schemas) throw new StructException('noschemas'); 600577b462bSAndreas Gohr 601577b462bSAndreas Gohr // resolve the alias or table name 6029007da58SMichael Große @list($table, $colname) = explode('.', $colname, 2); 603577b462bSAndreas Gohr if (!$colname) { 604577b462bSAndreas Gohr $colname = $table; 605577b462bSAndreas Gohr $table = null; 606577b462bSAndreas Gohr } 607577b462bSAndreas Gohr if ($table && isset($this->aliases[$table])) { 608577b462bSAndreas Gohr $table = $this->aliases[$table]; 609577b462bSAndreas Gohr } 610577b462bSAndreas Gohr 611577b462bSAndreas Gohr if (!$colname) throw new StructException('nocolname'); 612577b462bSAndreas Gohr 613577b462bSAndreas Gohr return array($colname, $table); 614577b462bSAndreas Gohr } 61501dd90deSAndreas Gohr 61601dd90deSAndreas Gohr /** 61715929be2SAndreas Gohr * Find a column to be used in the search 61815929be2SAndreas Gohr * 61915929be2SAndreas Gohr * @param string $colname may contain an alias 62015929be2SAndreas Gohr * @return bool|Column 62115929be2SAndreas Gohr */ 6220280da9aSFrieder Schrempf public function findColumn($colname, $strict = false) 62361356325SAnna Dabrowska { 6245511bd5bSAndreas Gohr if (!$this->schemas) throw new StructException('noschemas'); 625df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 626f107f479SAndreas Gohr 627f107f479SAndreas Gohr // add "fake" column for special col 628128d4e83SAndreas Gohr if ($colname == '%pageid%') { 629128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 630d1b04e89SAndreas Gohr } 631128d4e83SAndreas Gohr if ($colname == '%title%') { 632128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 633128d4e83SAndreas Gohr } 634cadfc3ccSAndreas Gohr if ($colname == '%lastupdate%') { 635cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 636cadfc3ccSAndreas Gohr } 6372e12ac22SMichael Grosse if ($colname == '%lasteditor%') { 6382e12ac22SMichael Grosse return new UserColumn(0, new User(), $schema_list[0]); 6392e12ac22SMichael Grosse } 64088b58a21SSzymon Olewniczak if ($colname == '%lastsummary%') { 6416781c68dSSzymon Olewniczak return new SummaryColumn(0, new AutoSummary(), $schema_list[0]); 64288b58a21SSzymon Olewniczak } 643f107f479SAndreas Gohr if ($colname == '%rowid%') { 644f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 645f107f479SAndreas Gohr } 646d1b04e89SAndreas Gohr 647636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 64815929be2SAndreas Gohr 6490280da9aSFrieder Schrempf /* 6500280da9aSFrieder Schrempf * If table name is given search only that, otherwise if no strict behavior 6510280da9aSFrieder Schrempf * is requested by the caller, try all assigned schemas for matching the 6520280da9aSFrieder Schrempf * column name. 6530280da9aSFrieder Schrempf */ 65434ea6e10SAnna Dabrowska if ($table !== null && isset($this->schemas[$table])) { 65515929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 6560280da9aSFrieder Schrempf } else if ($table === null || !$strict) { 65715929be2SAndreas Gohr $schemas = $this->schemas; 6580280da9aSFrieder Schrempf } else { 6590280da9aSFrieder Schrempf return false; 66015929be2SAndreas Gohr } 66115929be2SAndreas Gohr 66215929be2SAndreas Gohr // find it 66315929be2SAndreas Gohr $col = false; 66415929be2SAndreas Gohr foreach ($schemas as $schema) { 6654ac44d1cSMichael Grosse if (empty($schema)) { 6664ac44d1cSMichael Grosse continue; 6674ac44d1cSMichael Grosse } 66815929be2SAndreas Gohr $col = $schema->findColumn($colname); 66915929be2SAndreas Gohr if ($col) break; 67015929be2SAndreas Gohr } 67115929be2SAndreas Gohr 67215929be2SAndreas Gohr return $col; 67315929be2SAndreas Gohr } 67415929be2SAndreas Gohr 6759dbc8ec0SMichael Grosse /** 67699a70f28SAndreas Gohr * Check if the given row is empty or references our own row 6779dbc8ec0SMichael Grosse * 67899a70f28SAndreas Gohr * @param Value $value 6799dbc8ec0SMichael Grosse * @return bool 6809dbc8ec0SMichael Grosse */ 68161356325SAnna Dabrowska protected function isEmptyValue(Value $value) 68261356325SAnna Dabrowska { 68399a70f28SAndreas Gohr if ($value->isEmpty()) return true; 6848cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 6859dbc8ec0SMichael Grosse return false; 6869dbc8ec0SMichael Grosse } 68715929be2SAndreas Gohr} 688