115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 415929be2SAndreas Gohr 5*7234bfb1Ssplitbrainuse dokuwiki\Parsing\Lexer\Lexer; 60549dcc5SAndreas Gohruse dokuwiki\plugin\struct\types\AutoSummary; 7cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\DateTime; 8f107f479SAndreas Gohruse dokuwiki\plugin\struct\types\Decimal; 9ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Page; 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 */ 1717a3a578SAndreas Gohr public const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 189d7a36f9SAndreas Gohr 195511bd5bSAndreas Gohr /** 205511bd5bSAndreas Gohr * The list of known and allowed comparators 212e7595e7SAndreas Gohr * (order matters) 225511bd5bSAndreas Gohr */ 23*7234bfb1Ssplitbrain public static $COMPARATORS = ['<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', ' IN ']; 245511bd5bSAndreas Gohr 25bb8d98c4SAnna Dabrowska /** @var \helper_plugin_struct_db */ 26bb8d98c4SAnna Dabrowska protected $dbHelper; 27bb8d98c4SAnna Dabrowska 289d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 299d7a36f9SAndreas Gohr protected $sqlite; 3015929be2SAndreas Gohr 3115929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 32*7234bfb1Ssplitbrain protected $schemas = []; 3315929be2SAndreas Gohr 3415929be2SAndreas Gohr /** @var Column[] list of columns to select */ 35*7234bfb1Ssplitbrain protected $columns = []; 3615929be2SAndreas Gohr 3715929be2SAndreas Gohr /** @var array the sorting of the result */ 38*7234bfb1Ssplitbrain protected $sortby = []; 3915929be2SAndreas Gohr 409d7a36f9SAndreas Gohr /** @var array the filters */ 41*7234bfb1Ssplitbrain protected $filter = []; 4215929be2SAndreas Gohr 431057ed14SAndreas Gohr /** @var array the filters */ 44*7234bfb1Ssplitbrain protected $dynamicFilter = []; 451057ed14SAndreas Gohr 4615929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 47*7234bfb1Ssplitbrain protected $aliases = []; 4815929be2SAndreas Gohr 497f9cb794SAndreas Gohr /** @var int begin results from here */ 507f9cb794SAndreas Gohr protected $range_begin = 0; 517f9cb794SAndreas Gohr 527f9cb794SAndreas Gohr /** @var int end results here */ 537f9cb794SAndreas Gohr protected $range_end = 0; 547f9cb794SAndreas Gohr 557f9cb794SAndreas Gohr /** @var int the number of results */ 567f9cb794SAndreas Gohr protected $count = -1; 57d4b5a17cSAndreas Gohr /** @var string[] the PIDs of the result rows */ 58*7234bfb1Ssplitbrain protected $result_pids; 590ceefd5cSAnna Dabrowska /** @var array the row ids of the result rows */ 600ceefd5cSAnna Dabrowska protected $result_rids = []; 616fd73b4bSAnna Dabrowska /** @var array the revisions of the result rows */ 626fd73b4bSAnna Dabrowska protected $result_revs = []; 637f9cb794SAndreas Gohr 64eff6062eSAnna Dabrowska /** @var bool Include latest = 1 in select query */ 65eff6062eSAnna Dabrowska protected $selectLatest = true; 66eff6062eSAnna Dabrowska 6715929be2SAndreas Gohr /** 689d7a36f9SAndreas Gohr * Search constructor. 699d7a36f9SAndreas Gohr */ 7061356325SAnna Dabrowska public function __construct() 7161356325SAnna Dabrowska { 72837e87e2SAnna Dabrowska /** @var $dbHelper */ 73837e87e2SAnna Dabrowska $this->dbHelper = plugin_load('helper', 'struct_db'); 74837e87e2SAnna Dabrowska $this->sqlite = $this->dbHelper->getDB(); 75bb8d98c4SAnna Dabrowska } 76837e87e2SAnna Dabrowska 77837e87e2SAnna Dabrowska public function getDb() 78837e87e2SAnna Dabrowska { 79837e87e2SAnna Dabrowska return $this->sqlite; 809d7a36f9SAndreas Gohr } 819d7a36f9SAndreas Gohr 829d7a36f9SAndreas Gohr /** 8315929be2SAndreas Gohr * Add a schema to be searched 8415929be2SAndreas Gohr * 8515929be2SAndreas Gohr * Call multiple times for multiple schemas. 8615929be2SAndreas Gohr * 8715929be2SAndreas Gohr * @param string $table 8815929be2SAndreas Gohr * @param string $alias 8915929be2SAndreas Gohr */ 9061356325SAnna Dabrowska public function addSchema($table, $alias = '') 9161356325SAnna Dabrowska { 922be187cdSAndreas Gohr $schema = new Schema($table); 932be187cdSAndreas Gohr if (!$schema->getId()) { 942be187cdSAndreas Gohr throw new StructException('schema missing', $table); 952be187cdSAndreas Gohr } 962be187cdSAndreas Gohr 97712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 98712c650dSAndreas Gohr if ($alias) $this->aliases[$alias] = $schema->getTable(); 9915929be2SAndreas Gohr } 10015929be2SAndreas Gohr 10115929be2SAndreas Gohr /** 10215929be2SAndreas Gohr * Add a column to be returned by the search 10315929be2SAndreas Gohr * 10415929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 10515929be2SAndreas Gohr * added before 10615929be2SAndreas Gohr * 10715929be2SAndreas Gohr * @param string $colname may contain an alias 10815929be2SAndreas Gohr */ 10961356325SAnna Dabrowska public function addColumn($colname) 11061356325SAnna Dabrowska { 111577b462bSAndreas Gohr if ($this->processWildcard($colname)) return; // wildcard? 112b26126edSAndreas Hubel if ($colname[0] == '-') { // remove column from previous wildcard lookup 113b26126edSAndreas Hubel $colname = substr($colname, 1); 114b26126edSAndreas Hubel foreach ($this->columns as $key => $col) { 115*7234bfb1Ssplitbrain if ($col->getLabel() === $colname) unset($this->columns[$key]); 116b26126edSAndreas Hubel } 117b26126edSAndreas Hubel return; 118b26126edSAndreas Hubel } 119b26126edSAndreas Hubel 12015929be2SAndreas Gohr $col = $this->findColumn($colname); 12115929be2SAndreas Gohr if (!$col) return; //FIXME do we really want to ignore missing columns? 12215929be2SAndreas Gohr $this->columns[] = $col; 12315929be2SAndreas Gohr } 12415929be2SAndreas Gohr 12515929be2SAndreas Gohr /** 12615929be2SAndreas Gohr * Add sorting options 12715929be2SAndreas Gohr * 12815929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 12915929be2SAndreas Gohr * added before 13015929be2SAndreas Gohr * 13115929be2SAndreas Gohr * @param string $colname may contain an alias 13215929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 133eb55dc17SAndreas Gohr * @param bool $nc set true for caseinsensitivity 13415929be2SAndreas Gohr */ 13561356325SAnna Dabrowska public function addSort($colname, $asc = true, $nc = true) 13661356325SAnna Dabrowska { 13715929be2SAndreas Gohr $col = $this->findColumn($colname); 13815929be2SAndreas Gohr if (!$col) return; //FIXME do we really want to ignore missing columns? 13915929be2SAndreas Gohr 140*7234bfb1Ssplitbrain $this->sortby[$col->getFullQualifiedLabel()] = [$col, $asc, $nc]; 14107993756SAndreas Gohr } 14207993756SAndreas Gohr 14307993756SAndreas Gohr /** 144b3a9db22SAndreas Gohr * Clear all sorting options 145b3a9db22SAndreas Gohr * 146b3a9db22SAndreas Gohr * @return void 147b3a9db22SAndreas Gohr */ 148b3a9db22SAndreas Gohr public function clearSort() 149b3a9db22SAndreas Gohr { 150b3a9db22SAndreas Gohr $this->sortby = []; 151b3a9db22SAndreas Gohr } 152b3a9db22SAndreas Gohr 153b3a9db22SAndreas Gohr /** 15407993756SAndreas Gohr * Returns all set sort columns 15507993756SAndreas Gohr * 15607993756SAndreas Gohr * @return array 15707993756SAndreas Gohr */ 15861356325SAnna Dabrowska public function getSorts() 15961356325SAnna Dabrowska { 16007993756SAndreas Gohr return $this->sortby; 16115929be2SAndreas Gohr } 16215929be2SAndreas Gohr 16315929be2SAndreas Gohr /** 1649d7a36f9SAndreas Gohr * Adds a filter 16515929be2SAndreas Gohr * 16615929be2SAndreas Gohr * @param string $colname may contain an alias 16753528ecfSAndreas Gohr * @param string|string[] $value 1685511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1694c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 17015929be2SAndreas Gohr */ 17161356325SAnna Dabrowska public function addFilter($colname, $value, $comp, $op = 'OR') 17261356325SAnna Dabrowska { 1731057ed14SAndreas Gohr $filter = $this->createFilter($colname, $value, $comp, $op); 1741057ed14SAndreas Gohr if ($filter) $this->filter[] = $filter; 1751057ed14SAndreas Gohr } 1761057ed14SAndreas Gohr 1771057ed14SAndreas Gohr /** 1781057ed14SAndreas Gohr * Adds a dynamic filter 1791057ed14SAndreas Gohr * 1801057ed14SAndreas Gohr * @param string $colname may contain an alias 1811057ed14SAndreas Gohr * @param string|string[] $value 1821057ed14SAndreas Gohr * @param string $comp @see self::COMPARATORS 1831057ed14SAndreas Gohr * @param string $op either 'OR' or 'AND' 1841057ed14SAndreas Gohr */ 1851057ed14SAndreas Gohr public function addDynamicFilter($colname, $value, $comp, $op = 'OR') 1861057ed14SAndreas Gohr { 1871057ed14SAndreas Gohr $filter = $this->createFilter($colname, $value, $comp, $op); 1881057ed14SAndreas Gohr if ($filter) $this->dynamicFilter[] = $filter; 1891057ed14SAndreas Gohr } 1901057ed14SAndreas Gohr 1911057ed14SAndreas Gohr /** 1921057ed14SAndreas Gohr * Create a filter definition 1931057ed14SAndreas Gohr * 1941057ed14SAndreas Gohr * @param string $colname may contain an alias 1951057ed14SAndreas Gohr * @param string|string[] $value 1961057ed14SAndreas Gohr * @param string $comp @see self::COMPARATORS 1971057ed14SAndreas Gohr * @param string $op either 'OR' or 'AND' 1981057ed14SAndreas Gohr * @return array|null [Column col, string|string[] value, string comp, string op] 1991057ed14SAndreas Gohr */ 2001057ed14SAndreas Gohr protected function createFilter($colname, $value, $comp, $op = 'OR') 2011057ed14SAndreas Gohr { 2029dbc32aeSAndreas Gohr /* Convert certain filters into others 2039dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 2049dbc32aeSAndreas Gohr if ($comp == '*~') { 20553528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 2069dbc32aeSAndreas Gohr $comp = '~'; 2079dbc32aeSAndreas Gohr } elseif ($comp == '<>') { 2089dbc32aeSAndreas Gohr $comp = '!='; 2099dbc32aeSAndreas Gohr } 2109dbc32aeSAndreas Gohr 21117a3a578SAndreas Gohr if (!in_array($comp, self::$COMPARATORS)) 212*7234bfb1Ssplitbrain throw new StructException("Bad comperator. Use " . implode(',', self::$COMPARATORS)); 21317a3a578SAndreas Gohr if ($op != 'OR' && $op != 'AND') 21417a3a578SAndreas Gohr throw new StructException('Bad filter type . Only AND or OR allowed'); 2159d7a36f9SAndreas Gohr 21615929be2SAndreas Gohr $col = $this->findColumn($colname); 2171057ed14SAndreas Gohr if (!$col) return null; // ignore missing columns, filter might have been for different schema 2187da1a0fdSAndreas Gohr 2197da1a0fdSAndreas Gohr // map filter operators to SQL syntax 2207da1a0fdSAndreas Gohr switch ($comp) { 2217da1a0fdSAndreas Gohr case '~': 2227da1a0fdSAndreas Gohr $comp = 'LIKE'; 2237da1a0fdSAndreas Gohr break; 2247da1a0fdSAndreas Gohr case '!~': 2257da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 2267da1a0fdSAndreas Gohr break; 2277da1a0fdSAndreas Gohr case '=*': 2287da1a0fdSAndreas Gohr $comp = 'REGEXP'; 2297da1a0fdSAndreas Gohr break; 2307da1a0fdSAndreas Gohr } 2317da1a0fdSAndreas Gohr 2327da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 2337da1a0fdSAndreas Gohr if ($comp == 'LIKE' || $comp == 'NOT LIKE') { 23453528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 2357da1a0fdSAndreas Gohr } 2367da1a0fdSAndreas Gohr 237aaa187abSSzymon Olewniczak if ($comp == ' IN ' && !is_array($value)) { 238efff5841SSzymon Olewniczak $value = $this->parseFilterValueList($value); 239aaa187abSSzymon Olewniczak //col IN ('a', 'b', 'c') is equal to col = 'a' OR 'col = 'b' OR col = 'c' 240aaa187abSSzymon Olewniczak $comp = '='; 241aaa187abSSzymon Olewniczak } 242aaa187abSSzymon Olewniczak 2437da1a0fdSAndreas Gohr // add the filter 244*7234bfb1Ssplitbrain return [$col, $value, $comp, $op]; 24515929be2SAndreas Gohr } 24615929be2SAndreas Gohr 24715929be2SAndreas Gohr /** 248aaa187abSSzymon Olewniczak * Parse SQLite row value into array 249aaa187abSSzymon Olewniczak * 250aaa187abSSzymon Olewniczak * @param string $value 251aaa187abSSzymon Olewniczak * @return string[] 252aaa187abSSzymon Olewniczak */ 25361356325SAnna Dabrowska protected function parseFilterValueList($value) 25461356325SAnna Dabrowska { 255efff5841SSzymon Olewniczak $Handler = new FilterValueListHandler(); 256670a6894SAnna Dabrowska $LexerClass = class_exists('\Doku_Lexer') ? '\Doku_Lexer' : '\dokuwiki\Parsing\Lexer\Lexer'; 257670a6894SAnna Dabrowska $isLegacy = $LexerClass === '\Doku_Lexer'; 258*7234bfb1Ssplitbrain /** @var \Doku_Lexer|Lexer $Lexer */ 259670a6894SAnna Dabrowska $Lexer = new $LexerClass($Handler, 'base', true); 260670a6894SAnna Dabrowska 261aaa187abSSzymon Olewniczak 262aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('\(', 'base', 'row'); 263aaa187abSSzymon Olewniczak $Lexer->addPattern('\s*,\s*', 'row'); 264aaa187abSSzymon Olewniczak $Lexer->addExitPattern('\)', 'row'); 265aaa187abSSzymon Olewniczak 266aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('"', 'row', 'double_quote_string'); 267dfe3ae67SAnna Dabrowska $Lexer->addSpecialPattern('\\\\"', 'double_quote_string', 'escapeSequence'); 268aaa187abSSzymon Olewniczak $Lexer->addExitPattern('"', 'double_quote_string'); 269aaa187abSSzymon Olewniczak 270dfe3ae67SAnna Dabrowska $Lexer->addEntryPattern("'", 'row', 'singleQuoteString'); 271dfe3ae67SAnna Dabrowska $Lexer->addSpecialPattern("\\\\'", 'singleQuoteString', 'escapeSequence'); 272dfe3ae67SAnna Dabrowska $Lexer->addExitPattern("'", 'singleQuoteString'); 273aaa187abSSzymon Olewniczak 274dfe3ae67SAnna Dabrowska $Lexer->mapHandler('double_quote_string', 'singleQuoteString'); 275aaa187abSSzymon Olewniczak 276aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern('[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?', 'row', 'number'); 277aaa187abSSzymon Olewniczak 278aaa187abSSzymon Olewniczak $res = $Lexer->parse($value); 279aaa187abSSzymon Olewniczak 280670a6894SAnna Dabrowska $currentMode = $isLegacy ? $Lexer->_mode->getCurrent() : $Lexer->getModeStack()->getCurrent(); 281670a6894SAnna Dabrowska if (!$res || $currentMode != 'base') { 282aaa187abSSzymon Olewniczak throw new StructException('invalid row value syntax'); 283aaa187abSSzymon Olewniczak } 284aaa187abSSzymon Olewniczak 285dfe3ae67SAnna Dabrowska return $Handler->getRow(); 286aaa187abSSzymon Olewniczak } 287aaa187abSSzymon Olewniczak 288aaa187abSSzymon Olewniczak /** 28953528ecfSAndreas Gohr * Wrap given value in asterisks 29053528ecfSAndreas Gohr * 29153528ecfSAndreas Gohr * @param string|string[] $value 29253528ecfSAndreas Gohr * @return string|string[] 29353528ecfSAndreas Gohr */ 29461356325SAnna Dabrowska protected function filterWrapAsterisks($value) 29561356325SAnna Dabrowska { 29653528ecfSAndreas Gohr $map = function ($input) { 29753528ecfSAndreas Gohr return "*$input*"; 29853528ecfSAndreas Gohr }; 29953528ecfSAndreas Gohr 30053528ecfSAndreas Gohr if (is_array($value)) { 30153528ecfSAndreas Gohr $value = array_map($map, $value); 30253528ecfSAndreas Gohr } else { 30353528ecfSAndreas Gohr $value = $map($value); 30453528ecfSAndreas Gohr } 30553528ecfSAndreas Gohr return $value; 30653528ecfSAndreas Gohr } 30753528ecfSAndreas Gohr 30853528ecfSAndreas Gohr /** 30953528ecfSAndreas Gohr * Change given string to use % instead of * 31053528ecfSAndreas Gohr * 31153528ecfSAndreas Gohr * @param string|string[] $value 31253528ecfSAndreas Gohr * @return string|string[] 31353528ecfSAndreas Gohr */ 31461356325SAnna Dabrowska protected function filterChangeToLike($value) 31561356325SAnna Dabrowska { 31653528ecfSAndreas Gohr $map = function ($input) { 31753528ecfSAndreas Gohr return str_replace('*', '%', $input); 31853528ecfSAndreas Gohr }; 31953528ecfSAndreas Gohr 32053528ecfSAndreas Gohr if (is_array($value)) { 32153528ecfSAndreas Gohr $value = array_map($map, $value); 32253528ecfSAndreas Gohr } else { 32353528ecfSAndreas Gohr $value = $map($value); 32453528ecfSAndreas Gohr } 32553528ecfSAndreas Gohr return $value; 32653528ecfSAndreas Gohr } 32753528ecfSAndreas Gohr 32853528ecfSAndreas Gohr /** 3297f9cb794SAndreas Gohr * Set offset for the results 3307f9cb794SAndreas Gohr * 3317f9cb794SAndreas Gohr * @param int $offset 3327f9cb794SAndreas Gohr */ 33361356325SAnna Dabrowska public function setOffset($offset) 33461356325SAnna Dabrowska { 3357f9cb794SAndreas Gohr $limit = 0; 3367f9cb794SAndreas Gohr if ($this->range_end) { 3377f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 3387f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 3397f9cb794SAndreas Gohr } 3407f9cb794SAndreas Gohr $this->range_begin = $offset; 3417f9cb794SAndreas Gohr if ($limit) $this->setLimit($limit); 3427f9cb794SAndreas Gohr } 3437f9cb794SAndreas Gohr 3447f9cb794SAndreas Gohr /** 345fdf37115SAndreas Gohr * Get the current offset for the results 346fdf37115SAndreas Gohr * 347fdf37115SAndreas Gohr * @return int 348fdf37115SAndreas Gohr */ 349fdf37115SAndreas Gohr public function getOffset() 350fdf37115SAndreas Gohr { 351fdf37115SAndreas Gohr return $this->range_begin; 352fdf37115SAndreas Gohr } 353fdf37115SAndreas Gohr 354fdf37115SAndreas Gohr /** 3557f9cb794SAndreas Gohr * Limit results to this number 3567f9cb794SAndreas Gohr * 3577f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 3587f9cb794SAndreas Gohr */ 35961356325SAnna Dabrowska public function setLimit($limit) 36061356325SAnna Dabrowska { 3617f9cb794SAndreas Gohr if ($limit) { 3627f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 3637f9cb794SAndreas Gohr } else { 3647f9cb794SAndreas Gohr $this->range_end = 0; 3657f9cb794SAndreas Gohr } 3667f9cb794SAndreas Gohr } 3677f9cb794SAndreas Gohr 3687f9cb794SAndreas Gohr /** 369fdf37115SAndreas Gohr * Get the current limit for the results 370fdf37115SAndreas Gohr * 371fdf37115SAndreas Gohr * @return int 372fdf37115SAndreas Gohr */ 373fdf37115SAndreas Gohr public function getLimit() 374fdf37115SAndreas Gohr { 375fdf37115SAndreas Gohr if ($this->range_end) { 376fdf37115SAndreas Gohr return $this->range_end - $this->range_begin; 377fdf37115SAndreas Gohr } 378fdf37115SAndreas Gohr return 0; 379fdf37115SAndreas Gohr } 380fdf37115SAndreas Gohr 381fdf37115SAndreas Gohr /** 382eff6062eSAnna Dabrowska * Allows disabling default 'latest = 1' clause in select statement 383eff6062eSAnna Dabrowska * 384eff6062eSAnna Dabrowska * @param bool $selectLatest 385eff6062eSAnna Dabrowska */ 386eff6062eSAnna Dabrowska public function setSelectLatest($selectLatest): void 387eff6062eSAnna Dabrowska { 388eff6062eSAnna Dabrowska $this->selectLatest = $selectLatest; 389eff6062eSAnna Dabrowska } 390eff6062eSAnna Dabrowska 391eff6062eSAnna Dabrowska /** 3927f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 3937f9cb794SAndreas Gohr * 3940549dcc5SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @return int 3950549dcc5SAndreas Gohr * @see execute() 3967f9cb794SAndreas Gohr * 3977f9cb794SAndreas Gohr */ 39861356325SAnna Dabrowska public function getCount() 39961356325SAnna Dabrowska { 4007f9cb794SAndreas Gohr if ($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 4017f9cb794SAndreas Gohr return $this->count; 4027f9cb794SAndreas Gohr } 4037f9cb794SAndreas Gohr 404c46f5fb1SAndreas Gohr /** 405c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 406c46f5fb1SAndreas Gohr * 4070549dcc5SAndreas Gohr * Important: this may only be called after running @return \string[] 4080549dcc5SAndreas Gohr * @see execute() 409c46f5fb1SAndreas Gohr * 410c46f5fb1SAndreas Gohr */ 41161356325SAnna Dabrowska public function getPids() 41261356325SAnna Dabrowska { 41317a3a578SAndreas Gohr if ($this->result_pids === null) 41417a3a578SAndreas Gohr throw new StructException('PIDs are only accessible after executing the search'); 415d4b5a17cSAndreas Gohr return $this->result_pids; 416d4b5a17cSAndreas Gohr } 417d4b5a17cSAndreas Gohr 4187f9cb794SAndreas Gohr /** 4190ceefd5cSAnna Dabrowska * Returns the rid associated with each result row 4200ceefd5cSAnna Dabrowska * 4210549dcc5SAndreas Gohr * Important: this may only be called after running @return array 4220549dcc5SAndreas Gohr * @see execute() 4230ceefd5cSAnna Dabrowska * 4240ceefd5cSAnna Dabrowska */ 42561356325SAnna Dabrowska public function getRids() 42661356325SAnna Dabrowska { 42717a3a578SAndreas Gohr if ($this->result_rids === null) 42817a3a578SAndreas Gohr throw new StructException('rids are only accessible after executing the search'); 4290ceefd5cSAnna Dabrowska return $this->result_rids; 4300ceefd5cSAnna Dabrowska } 4310ceefd5cSAnna Dabrowska 4320ceefd5cSAnna Dabrowska /** 4336fd73b4bSAnna Dabrowska * Returns the rid associated with each result row 4346fd73b4bSAnna Dabrowska * 4350549dcc5SAndreas Gohr * Important: this may only be called after running @return array 4360549dcc5SAndreas Gohr * @see execute() 4376fd73b4bSAnna Dabrowska * 4386fd73b4bSAnna Dabrowska */ 43961356325SAnna Dabrowska public function getRevs() 44061356325SAnna Dabrowska { 44117a3a578SAndreas Gohr if ($this->result_revs === null) 44217a3a578SAndreas Gohr throw new StructException('revs are only accessible after executing the search'); 4436fd73b4bSAnna Dabrowska return $this->result_revs; 4446fd73b4bSAnna Dabrowska } 4456fd73b4bSAnna Dabrowska 4466fd73b4bSAnna Dabrowska /** 447b2ed727aSAndreas Gohr * Execute this search and return the result 448b2ed727aSAndreas Gohr * 44938fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 4507f9cb794SAndreas Gohr * 4517f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 4520549dcc5SAndreas Gohr * return the wanted range, setting the count (@return Value[][] 4530549dcc5SAndreas Gohr * @see getCount) to the whole result number 45438fa36fbSAndreas Gohr * 455b2ed727aSAndreas Gohr */ 4568ce43f5aSAnna Dabrowska public function execute() 45761356325SAnna Dabrowska { 458*7234bfb1Ssplitbrain [$sql, $opts] = $this->getSQL(); 459b2ed727aSAndreas Gohr 4607f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 461b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 46259b668aaSAndreas Gohr if ($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 463b2ed727aSAndreas Gohr 464*7234bfb1Ssplitbrain $this->result_pids = []; 465*7234bfb1Ssplitbrain $result = []; 4667f9cb794SAndreas Gohr $cursor = -1; 4675e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 4685e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 4695e4bfdb0SMichael Grosse }, true); 4707f9cb794SAndreas Gohr while ($row = $res->fetch(\PDO::FETCH_ASSOC)) { 4717f9cb794SAndreas Gohr $cursor++; 4727f9cb794SAndreas Gohr if ($cursor < $this->range_begin) continue; 4737f9cb794SAndreas Gohr if ($this->range_end && $cursor >= $this->range_end) continue; 4747f9cb794SAndreas Gohr 475b2ed727aSAndreas Gohr $C = 0; 476*7234bfb1Ssplitbrain $resrow = []; 47799a70f28SAndreas Gohr $isempty = true; 478b2ed727aSAndreas Gohr foreach ($this->columns as $col) { 47938fa36fbSAndreas Gohr $val = $row["C$C"]; 480b2ed727aSAndreas Gohr if ($col->isMulti()) { 48138fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 482b2ed727aSAndreas Gohr } 48399a70f28SAndreas Gohr $value = new Value($col, $val); 4848cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 48599a70f28SAndreas Gohr $resrow[] = $value; 486b2ed727aSAndreas Gohr $C++; 487b2ed727aSAndreas Gohr } 48899a70f28SAndreas Gohr 48999a70f28SAndreas Gohr // skip empty rows 4908cba7aa0SMichael Grosse if ($isempty && !$pageidAndRevOnly) { 49199a70f28SAndreas Gohr $cursor--; 49299a70f28SAndreas Gohr continue; 49399a70f28SAndreas Gohr } 49499a70f28SAndreas Gohr 49599a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 4960ceefd5cSAnna Dabrowska $this->result_rids[] = $row['rid']; 4976fd73b4bSAnna Dabrowska $this->result_revs[] = $row['rev']; 498b2ed727aSAndreas Gohr $result[] = $resrow; 499b2ed727aSAndreas Gohr } 5007f9cb794SAndreas Gohr 50179b29326SAnna Dabrowska $res->closeCursor(); 5027f9cb794SAndreas Gohr $this->count = $cursor + 1; 503b2ed727aSAndreas Gohr return $result; 504b2ed727aSAndreas Gohr } 505b2ed727aSAndreas Gohr 506b2ed727aSAndreas Gohr /** 50715929be2SAndreas Gohr * Transform the set search parameters into a statement 50815929be2SAndreas Gohr * 509299ca8ccSAndreas Gohr * Calls runSQLBuilder() 510299ca8ccSAndreas Gohr * 511b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 51215929be2SAndreas Gohr */ 5138ce43f5aSAnna Dabrowska public function getSQL() 51461356325SAnna Dabrowska { 5155511bd5bSAndreas Gohr if (!$this->columns) throw new StructException('nocolname'); 516299ca8ccSAndreas Gohr return $this->runSQLBuilder()->getSQL(); 517299ca8ccSAndreas Gohr } 51815929be2SAndreas Gohr 519299ca8ccSAndreas Gohr /** 520299ca8ccSAndreas Gohr * Initialize and execute the SQLBuilder 521299ca8ccSAndreas Gohr * 522299ca8ccSAndreas Gohr * Called from getSQL(). Can be overwritten to extend the query using the query builder 523299ca8ccSAndreas Gohr * 524299ca8ccSAndreas Gohr * @return SearchSQLBuilder 525299ca8ccSAndreas Gohr */ 526299ca8ccSAndreas Gohr protected function runSQLBuilder() 527299ca8ccSAndreas Gohr { 528fd9c77d3SAndreas Gohr $sqlBuilder = new SearchSQLBuilder(); 529b37d1c8bSAnna Dabrowska $sqlBuilder->setSelectLatest($this->selectLatest); 530fd9c77d3SAndreas Gohr $sqlBuilder->addSchemas($this->schemas); 531fd9c77d3SAndreas Gohr $sqlBuilder->addColumns($this->columns); 532fd9c77d3SAndreas Gohr $sqlBuilder->addFilters($this->filter); 533fd9c77d3SAndreas Gohr $sqlBuilder->addFilters($this->dynamicFilter); 534fd9c77d3SAndreas Gohr $sqlBuilder->addSorts($this->sortby); 535299ca8ccSAndreas Gohr return $sqlBuilder; 53615929be2SAndreas Gohr } 53715929be2SAndreas Gohr 53815929be2SAndreas Gohr /** 53901dd90deSAndreas Gohr * Returns all the columns that where added to the search 54001dd90deSAndreas Gohr * 54101dd90deSAndreas Gohr * @return Column[] 54201dd90deSAndreas Gohr */ 54361356325SAnna Dabrowska public function getColumns() 54461356325SAnna Dabrowska { 54501dd90deSAndreas Gohr return $this->columns; 54601dd90deSAndreas Gohr } 54701dd90deSAndreas Gohr 548577b462bSAndreas Gohr /** 5495a4df70dSAndreas Gohr * All the schemas currently added 5505a4df70dSAndreas Gohr * 5515a4df70dSAndreas Gohr * @return Schema[] 5525a4df70dSAndreas Gohr */ 55361356325SAnna Dabrowska public function getSchemas() 55461356325SAnna Dabrowska { 5555a4df70dSAndreas Gohr return array_values($this->schemas); 5565a4df70dSAndreas Gohr } 5575a4df70dSAndreas Gohr 5585a4df70dSAndreas Gohr /** 559577b462bSAndreas Gohr * Checks if the given column is a * wildcard 560577b462bSAndreas Gohr * 561577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 562577b462bSAndreas Gohr * nothing happens 563577b462bSAndreas Gohr * 564577b462bSAndreas Gohr * @param string $colname 565577b462bSAndreas Gohr * @return bool was wildcard? 566577b462bSAndreas Gohr */ 56761356325SAnna Dabrowska protected function processWildcard($colname) 56861356325SAnna Dabrowska { 569*7234bfb1Ssplitbrain [$colname, $table] = $this->resolveColumn($colname); 570577b462bSAndreas Gohr if ($colname !== '*') return false; 571577b462bSAndreas Gohr 572577b462bSAndreas Gohr // no table given? assume the first is meant 573577b462bSAndreas Gohr if ($table === null) { 574577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 575577b462bSAndreas Gohr $table = $schema_list[0]; 576577b462bSAndreas Gohr } 577577b462bSAndreas Gohr 5781ca21e17SAnna Dabrowska $schema = $this->schemas[$table] ?? null; 579*7234bfb1Ssplitbrain if (!$schema instanceof Schema) return false; 580577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 581577b462bSAndreas Gohr return true; 582577b462bSAndreas Gohr } 583577b462bSAndreas Gohr 584577b462bSAndreas Gohr /** 585577b462bSAndreas Gohr * Split a given column name into table and column 586577b462bSAndreas Gohr * 587577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 588577b462bSAndreas Gohr * 589577b462bSAndreas Gohr * @param $colname 590636a5173SAndreas Gohr * @return array (colname, table) 591577b462bSAndreas Gohr */ 59261356325SAnna Dabrowska protected function resolveColumn($colname) 59361356325SAnna Dabrowska { 594577b462bSAndreas Gohr if (!$this->schemas) throw new StructException('noschemas'); 595577b462bSAndreas Gohr 596577b462bSAndreas Gohr // resolve the alias or table name 597*7234bfb1Ssplitbrain @[$table, $colname] = array_pad(explode('.', $colname, 2), 2, ''); 598577b462bSAndreas Gohr if (!$colname) { 599577b462bSAndreas Gohr $colname = $table; 600577b462bSAndreas Gohr $table = null; 601577b462bSAndreas Gohr } 602577b462bSAndreas Gohr if ($table && isset($this->aliases[$table])) { 603577b462bSAndreas Gohr $table = $this->aliases[$table]; 604577b462bSAndreas Gohr } 605577b462bSAndreas Gohr 606577b462bSAndreas Gohr if (!$colname) throw new StructException('nocolname'); 607577b462bSAndreas Gohr 608*7234bfb1Ssplitbrain return [$colname, $table]; 609577b462bSAndreas Gohr } 61001dd90deSAndreas Gohr 61101dd90deSAndreas Gohr /** 61215929be2SAndreas Gohr * Find a column to be used in the search 61315929be2SAndreas Gohr * 61415929be2SAndreas Gohr * @param string $colname may contain an alias 61515929be2SAndreas Gohr * @return bool|Column 61615929be2SAndreas Gohr */ 6170280da9aSFrieder Schrempf public function findColumn($colname, $strict = false) 61861356325SAnna Dabrowska { 6195511bd5bSAndreas Gohr if (!$this->schemas) throw new StructException('noschemas'); 620df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 621f107f479SAndreas Gohr 622f107f479SAndreas Gohr // add "fake" column for special col 623128d4e83SAndreas Gohr if ($colname == '%pageid%') { 624128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 625d1b04e89SAndreas Gohr } 626128d4e83SAndreas Gohr if ($colname == '%title%') { 627*7234bfb1Ssplitbrain return new PageColumn(0, new Page(['usetitles' => true]), $schema_list[0]); 628128d4e83SAndreas Gohr } 629cadfc3ccSAndreas Gohr if ($colname == '%lastupdate%') { 630cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 631cadfc3ccSAndreas Gohr } 6322e12ac22SMichael Grosse if ($colname == '%lasteditor%') { 6332e12ac22SMichael Grosse return new UserColumn(0, new User(), $schema_list[0]); 6342e12ac22SMichael Grosse } 63588b58a21SSzymon Olewniczak if ($colname == '%lastsummary%') { 6366781c68dSSzymon Olewniczak return new SummaryColumn(0, new AutoSummary(), $schema_list[0]); 63788b58a21SSzymon Olewniczak } 638f107f479SAndreas Gohr if ($colname == '%rowid%') { 639f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 640f107f479SAndreas Gohr } 641da62ec9cSAnna Dabrowska if ($colname == '%published%') { 642da62ec9cSAnna Dabrowska return new PublishedColumn(0, new Decimal(), $schema_list[0]); 643da62ec9cSAnna Dabrowska } 644d1b04e89SAndreas Gohr 645*7234bfb1Ssplitbrain [$colname, $table] = $this->resolveColumn($colname); 64615929be2SAndreas Gohr 6470280da9aSFrieder Schrempf /* 6480280da9aSFrieder Schrempf * If table name is given search only that, otherwise if no strict behavior 6490280da9aSFrieder Schrempf * is requested by the caller, try all assigned schemas for matching the 6500280da9aSFrieder Schrempf * column name. 6510280da9aSFrieder Schrempf */ 65234ea6e10SAnna Dabrowska if ($table !== null && isset($this->schemas[$table])) { 653*7234bfb1Ssplitbrain $schemas = [$table => $this->schemas[$table]]; 6540280da9aSFrieder Schrempf } elseif ($table === null || !$strict) { 65515929be2SAndreas Gohr $schemas = $this->schemas; 6560280da9aSFrieder Schrempf } else { 6570280da9aSFrieder Schrempf return false; 65815929be2SAndreas Gohr } 65915929be2SAndreas Gohr 66015929be2SAndreas Gohr // find it 66115929be2SAndreas Gohr $col = false; 66215929be2SAndreas Gohr foreach ($schemas as $schema) { 6634ac44d1cSMichael Grosse if (empty($schema)) { 6644ac44d1cSMichael Grosse continue; 6654ac44d1cSMichael Grosse } 66615929be2SAndreas Gohr $col = $schema->findColumn($colname); 66715929be2SAndreas Gohr if ($col) break; 66815929be2SAndreas Gohr } 66915929be2SAndreas Gohr 67015929be2SAndreas Gohr return $col; 67115929be2SAndreas Gohr } 67215929be2SAndreas Gohr 6739dbc8ec0SMichael Grosse /** 67499a70f28SAndreas Gohr * Check if the given row is empty or references our own row 6759dbc8ec0SMichael Grosse * 67699a70f28SAndreas Gohr * @param Value $value 6779dbc8ec0SMichael Grosse * @return bool 6789dbc8ec0SMichael Grosse */ 67961356325SAnna Dabrowska protected function isEmptyValue(Value $value) 68061356325SAnna Dabrowska { 68199a70f28SAndreas Gohr if ($value->isEmpty()) return true; 6828cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 6839dbc8ec0SMichael Grosse return false; 6849dbc8ec0SMichael Grosse } 68515929be2SAndreas Gohr} 686