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; 888b58a21SSzymon Olewniczakuse dokuwiki\plugin\struct\types\Summary; 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( 23*aaa187abSSzymon 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; 547f9cb794SAndreas Gohr 5515929be2SAndreas Gohr /** 569d7a36f9SAndreas Gohr * Search constructor. 579d7a36f9SAndreas Gohr */ 589d7a36f9SAndreas Gohr public function __construct() { 599d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 609d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 619d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 629d7a36f9SAndreas Gohr } 639d7a36f9SAndreas Gohr 649d7a36f9SAndreas Gohr /** 6515929be2SAndreas Gohr * Add a schema to be searched 6615929be2SAndreas Gohr * 6715929be2SAndreas Gohr * Call multiple times for multiple schemas. 6815929be2SAndreas Gohr * 6915929be2SAndreas Gohr * @param string $table 7015929be2SAndreas Gohr * @param string $alias 7115929be2SAndreas Gohr */ 7215929be2SAndreas Gohr public function addSchema($table, $alias = '') { 732be187cdSAndreas Gohr $schema = new Schema($table); 742be187cdSAndreas Gohr if(!$schema->getId()) { 752be187cdSAndreas Gohr throw new StructException('schema missing', $table); 762be187cdSAndreas Gohr } 772be187cdSAndreas Gohr 782be187cdSAndreas Gohr if($this->schemas && 792be187cdSAndreas Gohr ( 802be187cdSAndreas Gohr $schema->isLookup() || 812be187cdSAndreas Gohr reset($this->schemas)->isLookup() 822be187cdSAndreas Gohr ) 832be187cdSAndreas Gohr ) { 842be187cdSAndreas Gohr throw new StructException('nolookupmix'); 852be187cdSAndreas Gohr } 862be187cdSAndreas Gohr 87712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 88712c650dSAndreas Gohr if($alias) $this->aliases[$alias] = $schema->getTable(); 8915929be2SAndreas Gohr } 9015929be2SAndreas Gohr 9115929be2SAndreas Gohr /** 9215929be2SAndreas Gohr * Add a column to be returned by the search 9315929be2SAndreas Gohr * 9415929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9515929be2SAndreas Gohr * added before 9615929be2SAndreas Gohr * 9715929be2SAndreas Gohr * @param string $colname may contain an alias 9815929be2SAndreas Gohr */ 9915929be2SAndreas Gohr public function addColumn($colname) { 100577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 10115929be2SAndreas Gohr $col = $this->findColumn($colname); 10215929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10315929be2SAndreas Gohr $this->columns[] = $col; 10415929be2SAndreas Gohr } 10515929be2SAndreas Gohr 10615929be2SAndreas Gohr /** 10715929be2SAndreas Gohr * Add sorting options 10815929be2SAndreas Gohr * 10915929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 11015929be2SAndreas Gohr * added before 11115929be2SAndreas Gohr * 11215929be2SAndreas Gohr * @param string $colname may contain an alias 11315929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 114eb55dc17SAndreas Gohr * @param bool $nc set true for caseinsensitivity 11515929be2SAndreas Gohr */ 116eb55dc17SAndreas Gohr public function addSort($colname, $asc = true, $nc = true) { 11715929be2SAndreas Gohr $col = $this->findColumn($colname); 11815929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 11915929be2SAndreas Gohr 120eb55dc17SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc, $nc); 12107993756SAndreas Gohr } 12207993756SAndreas Gohr 12307993756SAndreas Gohr /** 12407993756SAndreas Gohr * Returns all set sort columns 12507993756SAndreas Gohr * 12607993756SAndreas Gohr * @return array 12707993756SAndreas Gohr */ 12807993756SAndreas Gohr public function getSorts() { 12907993756SAndreas Gohr return $this->sortby; 13015929be2SAndreas Gohr } 13115929be2SAndreas Gohr 13215929be2SAndreas Gohr /** 1339d7a36f9SAndreas Gohr * Adds a filter 13415929be2SAndreas Gohr * 13515929be2SAndreas Gohr * @param string $colname may contain an alias 13653528ecfSAndreas Gohr * @param string|string[] $value 1375511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1384c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 13915929be2SAndreas Gohr */ 1404c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1419dbc32aeSAndreas Gohr /* Convert certain filters into others 1429dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1439dbc32aeSAndreas Gohr if($comp == '*~') { 14453528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1459dbc32aeSAndreas Gohr $comp = '~'; 1469dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1479dbc32aeSAndreas Gohr $comp = '!='; 1489dbc32aeSAndreas Gohr } 1499dbc32aeSAndreas Gohr 15074461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1514c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1529d7a36f9SAndreas Gohr 15315929be2SAndreas Gohr $col = $this->findColumn($colname); 1547da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1557da1a0fdSAndreas Gohr 1567da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1577da1a0fdSAndreas Gohr switch($comp) { 1587da1a0fdSAndreas Gohr case '~': 1597da1a0fdSAndreas Gohr $comp = 'LIKE'; 1607da1a0fdSAndreas Gohr break; 1617da1a0fdSAndreas Gohr case '!~': 1627da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1637da1a0fdSAndreas Gohr break; 1647da1a0fdSAndreas Gohr case '=*': 1657da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1667da1a0fdSAndreas Gohr break; 1677da1a0fdSAndreas Gohr } 1687da1a0fdSAndreas Gohr 1697da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1707da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 17153528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1727da1a0fdSAndreas Gohr } 1737da1a0fdSAndreas Gohr 174*aaa187abSSzymon Olewniczak if ($comp == 'IN' && !is_array($value)) { 175*aaa187abSSzymon Olewniczak $value = $this->parseRowValue($value); 176*aaa187abSSzymon Olewniczak //col IN ('a', 'b', 'c') is equal to col = 'a' OR 'col = 'b' OR col = 'c' 177*aaa187abSSzymon Olewniczak $comp = '='; 178*aaa187abSSzymon Olewniczak } 179*aaa187abSSzymon Olewniczak 1807da1a0fdSAndreas Gohr // add the filter 1814c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 18215929be2SAndreas Gohr } 18315929be2SAndreas Gohr 18415929be2SAndreas Gohr /** 185*aaa187abSSzymon Olewniczak * Parse SQLite row value into array 186*aaa187abSSzymon Olewniczak * 187*aaa187abSSzymon Olewniczak * @param string $value 188*aaa187abSSzymon Olewniczak * @return string[] 189*aaa187abSSzymon Olewniczak */ 190*aaa187abSSzymon Olewniczak protected function parseRowValue($value) { 191*aaa187abSSzymon Olewniczak $Handler = new RowValueHandler(); 192*aaa187abSSzymon Olewniczak $Lexer = new \Doku_Lexer($Handler, 'base', true); 193*aaa187abSSzymon Olewniczak 194*aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('\(','base','row'); 195*aaa187abSSzymon Olewniczak $Lexer->addPattern('\s*,\s*','row'); 196*aaa187abSSzymon Olewniczak $Lexer->addExitPattern('\)','row'); 197*aaa187abSSzymon Olewniczak 198*aaa187abSSzymon Olewniczak $Lexer->addEntryPattern('"', 'row', 'double_quote_string'); 199*aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern('\\\\"','double_quote_string','escape_sequence'); 200*aaa187abSSzymon Olewniczak $Lexer->addExitPattern('"', 'double_quote_string'); 201*aaa187abSSzymon Olewniczak 202*aaa187abSSzymon Olewniczak $Lexer->addEntryPattern("'", 'row', 'single_quote_string'); 203*aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern("\\\\'",'single_quote_string','escape_sequence'); 204*aaa187abSSzymon Olewniczak $Lexer->addExitPattern("'", 'single_quote_string'); 205*aaa187abSSzymon Olewniczak 206*aaa187abSSzymon Olewniczak $Lexer->mapHandler('double_quote_string','single_quote_string'); 207*aaa187abSSzymon Olewniczak 208*aaa187abSSzymon Olewniczak $Lexer->addSpecialPattern('[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?','row','number'); 209*aaa187abSSzymon Olewniczak 210*aaa187abSSzymon Olewniczak $res = $Lexer->parse($value); 211*aaa187abSSzymon Olewniczak 212*aaa187abSSzymon Olewniczak if (!$res || $Lexer->_mode->getCurrent() != 'base') { 213*aaa187abSSzymon Olewniczak throw new StructException('invalid row value syntax'); 214*aaa187abSSzymon Olewniczak } 215*aaa187abSSzymon Olewniczak 216*aaa187abSSzymon Olewniczak return $Handler->get_row(); 217*aaa187abSSzymon Olewniczak } 218*aaa187abSSzymon Olewniczak 219*aaa187abSSzymon Olewniczak /** 22053528ecfSAndreas Gohr * Wrap given value in asterisks 22153528ecfSAndreas Gohr * 22253528ecfSAndreas Gohr * @param string|string[] $value 22353528ecfSAndreas Gohr * @return string|string[] 22453528ecfSAndreas Gohr */ 22553528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 22653528ecfSAndreas Gohr $map = function ($input) { 22753528ecfSAndreas Gohr return "*$input*"; 22853528ecfSAndreas Gohr }; 22953528ecfSAndreas Gohr 23053528ecfSAndreas Gohr if(is_array($value)) { 23153528ecfSAndreas Gohr $value = array_map($map, $value); 23253528ecfSAndreas Gohr } else { 23353528ecfSAndreas Gohr $value = $map($value); 23453528ecfSAndreas Gohr } 23553528ecfSAndreas Gohr return $value; 23653528ecfSAndreas Gohr } 23753528ecfSAndreas Gohr 23853528ecfSAndreas Gohr /** 23953528ecfSAndreas Gohr * Change given string to use % instead of * 24053528ecfSAndreas Gohr * 24153528ecfSAndreas Gohr * @param string|string[] $value 24253528ecfSAndreas Gohr * @return string|string[] 24353528ecfSAndreas Gohr */ 24453528ecfSAndreas Gohr protected function filterChangeToLike($value) { 24553528ecfSAndreas Gohr $map = function ($input) { 24653528ecfSAndreas Gohr return str_replace('*', '%', $input); 24753528ecfSAndreas Gohr }; 24853528ecfSAndreas Gohr 24953528ecfSAndreas Gohr if(is_array($value)) { 25053528ecfSAndreas Gohr $value = array_map($map, $value); 25153528ecfSAndreas Gohr } else { 25253528ecfSAndreas Gohr $value = $map($value); 25353528ecfSAndreas Gohr } 25453528ecfSAndreas Gohr return $value; 25553528ecfSAndreas Gohr } 25653528ecfSAndreas Gohr 25753528ecfSAndreas Gohr /** 2587f9cb794SAndreas Gohr * Set offset for the results 2597f9cb794SAndreas Gohr * 2607f9cb794SAndreas Gohr * @param int $offset 2617f9cb794SAndreas Gohr */ 2627f9cb794SAndreas Gohr public function setOffset($offset) { 2637f9cb794SAndreas Gohr $limit = 0; 2647f9cb794SAndreas Gohr if($this->range_end) { 2657f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2667f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2677f9cb794SAndreas Gohr } 2687f9cb794SAndreas Gohr $this->range_begin = $offset; 2697f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2707f9cb794SAndreas Gohr } 2717f9cb794SAndreas Gohr 2727f9cb794SAndreas Gohr /** 2737f9cb794SAndreas Gohr * Limit results to this number 2747f9cb794SAndreas Gohr * 2757f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2767f9cb794SAndreas Gohr */ 2777f9cb794SAndreas Gohr public function setLimit($limit) { 2787f9cb794SAndreas Gohr if($limit) { 2797f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2807f9cb794SAndreas Gohr } else { 2817f9cb794SAndreas Gohr $this->range_end = 0; 2827f9cb794SAndreas Gohr } 2837f9cb794SAndreas Gohr } 2847f9cb794SAndreas Gohr 2857f9cb794SAndreas Gohr /** 2867f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2877f9cb794SAndreas Gohr * 2887f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2897f9cb794SAndreas Gohr * 2907f9cb794SAndreas Gohr * @return int 2917f9cb794SAndreas Gohr */ 2927f9cb794SAndreas Gohr public function getCount() { 2937f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 2947f9cb794SAndreas Gohr return $this->count; 2957f9cb794SAndreas Gohr } 2967f9cb794SAndreas Gohr 297c46f5fb1SAndreas Gohr /** 298c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 299c46f5fb1SAndreas Gohr * 300c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 301c46f5fb1SAndreas Gohr * 302c46f5fb1SAndreas Gohr * @return \string[] 303c46f5fb1SAndreas Gohr */ 304d4b5a17cSAndreas Gohr public function getPids() { 305d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 306d4b5a17cSAndreas Gohr return $this->result_pids; 307d4b5a17cSAndreas Gohr } 308d4b5a17cSAndreas Gohr 3097f9cb794SAndreas Gohr /** 310b2ed727aSAndreas Gohr * Execute this search and return the result 311b2ed727aSAndreas Gohr * 31238fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 3137f9cb794SAndreas Gohr * 3147f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 3157f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 31638fa36fbSAndreas Gohr * 31738fa36fbSAndreas Gohr * @return Value[][] 318b2ed727aSAndreas Gohr */ 319b2ed727aSAndreas Gohr public function execute() { 320b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 321b2ed727aSAndreas Gohr 3227f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 323b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 32459b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 325b2ed727aSAndreas Gohr 326d4b5a17cSAndreas Gohr $this->result_pids = array(); 327b2ed727aSAndreas Gohr $result = array(); 3287f9cb794SAndreas Gohr $cursor = -1; 3295e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 3305e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 3315e4bfdb0SMichael Grosse }, true); 3327f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 3337f9cb794SAndreas Gohr $cursor++; 3347f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 3357f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 3367f9cb794SAndreas Gohr 337b2ed727aSAndreas Gohr $C = 0; 338b2ed727aSAndreas Gohr $resrow = array(); 33999a70f28SAndreas Gohr $isempty = true; 340b2ed727aSAndreas Gohr foreach($this->columns as $col) { 34138fa36fbSAndreas Gohr $val = $row["C$C"]; 342b2ed727aSAndreas Gohr if($col->isMulti()) { 34338fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 344b2ed727aSAndreas Gohr } 34599a70f28SAndreas Gohr $value = new Value($col, $val); 3468cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 34799a70f28SAndreas Gohr $resrow[] = $value; 348b2ed727aSAndreas Gohr $C++; 349b2ed727aSAndreas Gohr } 35099a70f28SAndreas Gohr 35199a70f28SAndreas Gohr // skip empty rows 3528cba7aa0SMichael Grosse if($isempty && !$pageidAndRevOnly) { 35399a70f28SAndreas Gohr $cursor--; 35499a70f28SAndreas Gohr continue; 35599a70f28SAndreas Gohr } 35699a70f28SAndreas Gohr 35799a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 358b2ed727aSAndreas Gohr $result[] = $resrow; 359b2ed727aSAndreas Gohr } 3607f9cb794SAndreas Gohr 3617f9cb794SAndreas Gohr $this->sqlite->res_close($res); 3627f9cb794SAndreas Gohr $this->count = $cursor + 1; 363b2ed727aSAndreas Gohr return $result; 364b2ed727aSAndreas Gohr } 365b2ed727aSAndreas Gohr 366b2ed727aSAndreas Gohr /** 36715929be2SAndreas Gohr * Transform the set search parameters into a statement 36815929be2SAndreas Gohr * 369b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 37015929be2SAndreas Gohr */ 37115929be2SAndreas Gohr public function getSQL() { 3725511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 37315929be2SAndreas Gohr 3742f68434dSAndreas Gohr $QB = new QueryBuilder(); 37515929be2SAndreas Gohr 3769d7a36f9SAndreas Gohr // basic tables 377b2d67e7dSAndreas Gohr $first_table = ''; 3789d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 3792f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 380b2d67e7dSAndreas Gohr if($first_table) { 3819d7a36f9SAndreas Gohr // follow up tables 3822f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 3839d7a36f9SAndreas Gohr } else { 3849d7a36f9SAndreas Gohr // first table 385b75e0a08SAndreas Gohr 38613c321dbSAndreas Gohr if(!$schema->isLookup()) { 38713c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 3882f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3892f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3902f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3912f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3922f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 39313c321dbSAndreas Gohr } 39413c321dbSAndreas Gohr 39513c321dbSAndreas Gohr $QB->addTable($datatable); 39613c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 39713c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 398b2d67e7dSAndreas Gohr 3992f68434dSAndreas Gohr $first_table = $datatable; 40015929be2SAndreas Gohr } 4012f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 4029d7a36f9SAndreas Gohr } 40315929be2SAndreas Gohr 40415929be2SAndreas Gohr // columns to select, handling multis 4059d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 40615929be2SAndreas Gohr $n = 0; 40715929be2SAndreas Gohr foreach($this->columns as $col) { 40815929be2SAndreas Gohr $CN = 'C' . $n++; 40915929be2SAndreas Gohr 410d1b04e89SAndreas Gohr if($col->isMulti()) { 4112f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 4122f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 413db7970caSAndreas Gohr $MN = $QB->generateTableAlias('M'); 4142f68434dSAndreas Gohr 4152f68434dSAndreas Gohr $QB->addLeftJoin( 4162f68434dSAndreas Gohr $datatable, 4172f68434dSAndreas Gohr $multitable, 4182f68434dSAndreas Gohr $MN, 4192f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 4202f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4212f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4222f68434dSAndreas Gohr ); 423578407b2SAndreas Gohr 424578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 425578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 426578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 42715929be2SAndreas Gohr } else { 428578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 4290dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 43015929be2SAndreas Gohr } 43115929be2SAndreas Gohr } 43215929be2SAndreas Gohr 4339d7a36f9SAndreas Gohr // where clauses 434af993d55SMichael Grosse if (!empty($this->filter)) { 435af993d55SMichael Grosse $userWHERE = $QB->filters()->where('AND'); 436af993d55SMichael Grosse } 437b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 4389ebd2ed6SAndreas Gohr /** @var Column $col */ 4394c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 440b8fe6730SAndreas Gohr 4412f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 4422f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 4439d7a36f9SAndreas Gohr 4449d7a36f9SAndreas Gohr /** @var $col Column */ 4459d7a36f9SAndreas Gohr if($col->isMulti()) { 446db7970caSAndreas Gohr $MN = $QB->generateTableAlias('MN'); 44715929be2SAndreas Gohr 4482f68434dSAndreas Gohr $QB->addLeftJoin( 4492f68434dSAndreas Gohr $datatable, 4502f68434dSAndreas Gohr $multitable, 4512f68434dSAndreas Gohr $MN, 4522f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 4532f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4542f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4552f68434dSAndreas Gohr ); 456690e4ebaSAndreas Gohr $coltbl = $MN; 457690e4ebaSAndreas Gohr $colnam = 'value'; 4589d7a36f9SAndreas Gohr } else { 459690e4ebaSAndreas Gohr $coltbl = $datatable; 460690e4ebaSAndreas Gohr $colnam = $col->getColName(); 4619d7a36f9SAndreas Gohr } 46253528ecfSAndreas Gohr 463af993d55SMichael Grosse $col->getType()->filter($userWHERE, $coltbl, $colnam, $comp, $value, $op); // type based filter 4649d7a36f9SAndreas Gohr } 4659d7a36f9SAndreas Gohr 46629394e6cSAndreas Gohr // sorting - we always sort by the single val column 467b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 468eb55dc17SAndreas Gohr list($col, $asc, $nc) = $sort; 4699d7a36f9SAndreas Gohr /** @var $col Column */ 470eb55dc17SAndreas Gohr $colname = $col->getColName(false); 471eb55dc17SAndreas Gohr if($nc) $colname .= ' COLLATE NOCASE'; 472eb55dc17SAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $colname, $asc ? 'ASC' : 'DESC'); 4739e07bdbfSAndreas Gohr } 4749e07bdbfSAndreas Gohr 4752f68434dSAndreas Gohr return $QB->getSQL(); 47615929be2SAndreas Gohr } 47715929be2SAndreas Gohr 47815929be2SAndreas Gohr /** 47901dd90deSAndreas Gohr * Returns all the columns that where added to the search 48001dd90deSAndreas Gohr * 48101dd90deSAndreas Gohr * @return Column[] 48201dd90deSAndreas Gohr */ 48301dd90deSAndreas Gohr public function getColumns() { 48401dd90deSAndreas Gohr return $this->columns; 48501dd90deSAndreas Gohr } 48601dd90deSAndreas Gohr 487577b462bSAndreas Gohr /** 4885a4df70dSAndreas Gohr * All the schemas currently added 4895a4df70dSAndreas Gohr * 4905a4df70dSAndreas Gohr * @return Schema[] 4915a4df70dSAndreas Gohr */ 4925a4df70dSAndreas Gohr public function getSchemas() { 4935a4df70dSAndreas Gohr return array_values($this->schemas); 4945a4df70dSAndreas Gohr } 4955a4df70dSAndreas Gohr 4965a4df70dSAndreas Gohr /** 497577b462bSAndreas Gohr * Checks if the given column is a * wildcard 498577b462bSAndreas Gohr * 499577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 500577b462bSAndreas Gohr * nothing happens 501577b462bSAndreas Gohr * 502577b462bSAndreas Gohr * @param string $colname 503577b462bSAndreas Gohr * @return bool was wildcard? 504577b462bSAndreas Gohr */ 505577b462bSAndreas Gohr protected function processWildcard($colname) { 506636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 507577b462bSAndreas Gohr if($colname !== '*') return false; 508577b462bSAndreas Gohr 509577b462bSAndreas Gohr // no table given? assume the first is meant 510577b462bSAndreas Gohr if($table === null) { 511577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 512577b462bSAndreas Gohr $table = $schema_list[0]; 513577b462bSAndreas Gohr } 514577b462bSAndreas Gohr 515577b462bSAndreas Gohr $schema = $this->schemas[$table]; 516577b462bSAndreas Gohr if(!$schema) return false; 517577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 518577b462bSAndreas Gohr return true; 519577b462bSAndreas Gohr } 520577b462bSAndreas Gohr 521577b462bSAndreas Gohr /** 522577b462bSAndreas Gohr * Split a given column name into table and column 523577b462bSAndreas Gohr * 524577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 525577b462bSAndreas Gohr * 526577b462bSAndreas Gohr * @param $colname 527636a5173SAndreas Gohr * @return array (colname, table) 528577b462bSAndreas Gohr */ 529577b462bSAndreas Gohr protected function resolveColumn($colname) { 530577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 531577b462bSAndreas Gohr 532577b462bSAndreas Gohr // resolve the alias or table name 5339007da58SMichael Große @list($table, $colname) = explode('.', $colname, 2); 534577b462bSAndreas Gohr if(!$colname) { 535577b462bSAndreas Gohr $colname = $table; 536577b462bSAndreas Gohr $table = null; 537577b462bSAndreas Gohr } 538577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 539577b462bSAndreas Gohr $table = $this->aliases[$table]; 540577b462bSAndreas Gohr } 541577b462bSAndreas Gohr 542577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 543577b462bSAndreas Gohr 544577b462bSAndreas Gohr return array($colname, $table); 545577b462bSAndreas Gohr } 54601dd90deSAndreas Gohr 54701dd90deSAndreas Gohr /** 54815929be2SAndreas Gohr * Find a column to be used in the search 54915929be2SAndreas Gohr * 55015929be2SAndreas Gohr * @param string $colname may contain an alias 55115929be2SAndreas Gohr * @return bool|Column 55215929be2SAndreas Gohr */ 55300f6af48SAndreas Gohr public function findColumn($colname) { 5545511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 555df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 556f107f479SAndreas Gohr 557f107f479SAndreas Gohr // add "fake" column for special col 558f107f479SAndreas Gohr if(!(reset($this->schemas)->isLookup())) { 559128d4e83SAndreas Gohr if($colname == '%pageid%') { 560128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 561d1b04e89SAndreas Gohr } 562128d4e83SAndreas Gohr if($colname == '%title%') { 563128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 564128d4e83SAndreas Gohr } 565cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 566cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 567cadfc3ccSAndreas Gohr } 5682e12ac22SMichael Grosse if ($colname == '%lasteditor%') { 5692e12ac22SMichael Grosse return new UserColumn(0, new User(), $schema_list[0]); 5702e12ac22SMichael Grosse } 57188b58a21SSzymon Olewniczak if ($colname == '%lastsummary%') { 57288b58a21SSzymon Olewniczak return new SummaryColumn(0, new Summary(), $schema_list[0]); 57388b58a21SSzymon Olewniczak } 574f107f479SAndreas Gohr } else { 575f107f479SAndreas Gohr if($colname == '%rowid%') { 576f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 577f107f479SAndreas Gohr } 578f747a930SAndreas Gohr } 579d1b04e89SAndreas Gohr 580636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 58115929be2SAndreas Gohr 582bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 583577b462bSAndreas Gohr if($table !== null) { 58415929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 58515929be2SAndreas Gohr } else { 58615929be2SAndreas Gohr $schemas = $this->schemas; 58715929be2SAndreas Gohr } 58815929be2SAndreas Gohr 58915929be2SAndreas Gohr // find it 59015929be2SAndreas Gohr $col = false; 59115929be2SAndreas Gohr foreach($schemas as $schema) { 5924ac44d1cSMichael Grosse if(empty($schema)) { 5934ac44d1cSMichael Grosse continue; 5944ac44d1cSMichael Grosse } 59515929be2SAndreas Gohr $col = $schema->findColumn($colname); 59615929be2SAndreas Gohr if($col) break; 59715929be2SAndreas Gohr } 59815929be2SAndreas Gohr 59915929be2SAndreas Gohr return $col; 60015929be2SAndreas Gohr } 60115929be2SAndreas Gohr 6029dbc8ec0SMichael Grosse /** 60399a70f28SAndreas Gohr * Check if the given row is empty or references our own row 6049dbc8ec0SMichael Grosse * 60599a70f28SAndreas Gohr * @param Value $value 6069dbc8ec0SMichael Grosse * @return bool 6079dbc8ec0SMichael Grosse */ 6088cba7aa0SMichael Grosse protected function isEmptyValue(Value $value) { 60999a70f28SAndreas Gohr if ($value->isEmpty()) return true; 6108cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 6119dbc8ec0SMichael Grosse return false; 6129dbc8ec0SMichael Grosse } 61315929be2SAndreas Gohr} 61415929be2SAndreas Gohr 6155511bd5bSAndreas Gohr 616