115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 415929be2SAndreas Gohr 5cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\DateTime; 6f107f479SAndreas Gohruse dokuwiki\plugin\struct\types\Decimal; 7ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Page; 86781c68dSSzymon Olewniczakuse dokuwiki\plugin\struct\types\AutoSummary; 988b58a21SSzymon Olewniczakuse dokuwiki\plugin\struct\types\Text; 102e12ac22SMichael Grosseuse dokuwiki\plugin\struct\types\User; 110561158fSAndreas Gohr 1215929be2SAndreas Gohrclass Search { 139d7a36f9SAndreas Gohr /** 149d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 159d7a36f9SAndreas Gohr */ 169d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 179d7a36f9SAndreas Gohr 185511bd5bSAndreas Gohr /** 195511bd5bSAndreas Gohr * The list of known and allowed comparators 202e7595e7SAndreas Gohr * (order matters) 215511bd5bSAndreas Gohr */ 2274461852SAndreas Gohr static public $COMPARATORS = array( 232e7595e7SAndreas Gohr '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', 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; 54*0ceefd5cSAnna Dabrowska /** @var array the row ids of the result rows */ 55*0ceefd5cSAnna Dabrowska protected $result_rids = []; 567f9cb794SAndreas Gohr 5715929be2SAndreas Gohr /** 589d7a36f9SAndreas Gohr * Search constructor. 599d7a36f9SAndreas Gohr */ 609d7a36f9SAndreas Gohr public function __construct() { 619d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 629d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 639d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 649d7a36f9SAndreas Gohr } 659d7a36f9SAndreas Gohr 669d7a36f9SAndreas Gohr /** 6715929be2SAndreas Gohr * Add a schema to be searched 6815929be2SAndreas Gohr * 6915929be2SAndreas Gohr * Call multiple times for multiple schemas. 7015929be2SAndreas Gohr * 7115929be2SAndreas Gohr * @param string $table 7215929be2SAndreas Gohr * @param string $alias 7315929be2SAndreas Gohr */ 7415929be2SAndreas Gohr public function addSchema($table, $alias = '') { 752be187cdSAndreas Gohr $schema = new Schema($table); 762be187cdSAndreas Gohr if(!$schema->getId()) { 772be187cdSAndreas Gohr throw new StructException('schema missing', $table); 782be187cdSAndreas Gohr } 792be187cdSAndreas Gohr 80*0ceefd5cSAnna Dabrowska // FIXME is the mixing still relevant? 81*0ceefd5cSAnna Dabrowska// if($this->schemas && 82*0ceefd5cSAnna Dabrowska// ( 83*0ceefd5cSAnna Dabrowska// $schema->isLookup() || 84*0ceefd5cSAnna Dabrowska// reset($this->schemas)->isLookup() 85*0ceefd5cSAnna Dabrowska// ) 86*0ceefd5cSAnna Dabrowska// ) { 87*0ceefd5cSAnna Dabrowska// throw new StructException('nolookupmix'); 88*0ceefd5cSAnna Dabrowska// } 892be187cdSAndreas Gohr 90712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 91712c650dSAndreas Gohr if($alias) $this->aliases[$alias] = $schema->getTable(); 9215929be2SAndreas Gohr } 9315929be2SAndreas Gohr 9415929be2SAndreas Gohr /** 9515929be2SAndreas Gohr * Add a column to be returned by the search 9615929be2SAndreas Gohr * 9715929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9815929be2SAndreas Gohr * added before 9915929be2SAndreas Gohr * 10015929be2SAndreas Gohr * @param string $colname may contain an alias 10115929be2SAndreas Gohr */ 10215929be2SAndreas Gohr public function addColumn($colname) { 103577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 10415929be2SAndreas Gohr $col = $this->findColumn($colname); 10515929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10615929be2SAndreas Gohr $this->columns[] = $col; 10715929be2SAndreas Gohr } 10815929be2SAndreas Gohr 10915929be2SAndreas Gohr /** 11015929be2SAndreas Gohr * Add sorting options 11115929be2SAndreas Gohr * 11215929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 11315929be2SAndreas Gohr * added before 11415929be2SAndreas Gohr * 11515929be2SAndreas Gohr * @param string $colname may contain an alias 11615929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 117eb55dc17SAndreas Gohr * @param bool $nc set true for caseinsensitivity 11815929be2SAndreas Gohr */ 119eb55dc17SAndreas Gohr public function addSort($colname, $asc = true, $nc = true) { 12015929be2SAndreas Gohr $col = $this->findColumn($colname); 12115929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 12215929be2SAndreas Gohr 123eb55dc17SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc, $nc); 12407993756SAndreas Gohr } 12507993756SAndreas Gohr 12607993756SAndreas Gohr /** 12707993756SAndreas Gohr * Returns all set sort columns 12807993756SAndreas Gohr * 12907993756SAndreas Gohr * @return array 13007993756SAndreas Gohr */ 13107993756SAndreas Gohr public function getSorts() { 13207993756SAndreas Gohr return $this->sortby; 13315929be2SAndreas Gohr } 13415929be2SAndreas Gohr 13515929be2SAndreas Gohr /** 1369d7a36f9SAndreas Gohr * Adds a filter 13715929be2SAndreas Gohr * 13815929be2SAndreas Gohr * @param string $colname may contain an alias 13953528ecfSAndreas Gohr * @param string|string[] $value 1405511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1414c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 14215929be2SAndreas Gohr */ 1434c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1449dbc32aeSAndreas Gohr /* Convert certain filters into others 1459dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1469dbc32aeSAndreas Gohr if($comp == '*~') { 14753528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1489dbc32aeSAndreas Gohr $comp = '~'; 1499dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1509dbc32aeSAndreas Gohr $comp = '!='; 1519dbc32aeSAndreas Gohr } 1529dbc32aeSAndreas Gohr 15374461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1544c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1559d7a36f9SAndreas Gohr 15615929be2SAndreas Gohr $col = $this->findColumn($colname); 1577da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1587da1a0fdSAndreas Gohr 1597da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1607da1a0fdSAndreas Gohr switch($comp) { 1617da1a0fdSAndreas Gohr case '~': 1627da1a0fdSAndreas Gohr $comp = 'LIKE'; 1637da1a0fdSAndreas Gohr break; 1647da1a0fdSAndreas Gohr case '!~': 1657da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1667da1a0fdSAndreas Gohr break; 1677da1a0fdSAndreas Gohr case '=*': 1687da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1697da1a0fdSAndreas Gohr break; 1707da1a0fdSAndreas Gohr } 1717da1a0fdSAndreas Gohr 1727da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1737da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 17453528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1757da1a0fdSAndreas Gohr } 1767da1a0fdSAndreas Gohr 1777da1a0fdSAndreas Gohr // add the filter 1784c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 17915929be2SAndreas Gohr } 18015929be2SAndreas Gohr 18115929be2SAndreas Gohr /** 18253528ecfSAndreas Gohr * Wrap given value in asterisks 18353528ecfSAndreas Gohr * 18453528ecfSAndreas Gohr * @param string|string[] $value 18553528ecfSAndreas Gohr * @return string|string[] 18653528ecfSAndreas Gohr */ 18753528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 18853528ecfSAndreas Gohr $map = function ($input) { 18953528ecfSAndreas Gohr return "*$input*"; 19053528ecfSAndreas Gohr }; 19153528ecfSAndreas Gohr 19253528ecfSAndreas Gohr if(is_array($value)) { 19353528ecfSAndreas Gohr $value = array_map($map, $value); 19453528ecfSAndreas Gohr } else { 19553528ecfSAndreas Gohr $value = $map($value); 19653528ecfSAndreas Gohr } 19753528ecfSAndreas Gohr return $value; 19853528ecfSAndreas Gohr } 19953528ecfSAndreas Gohr 20053528ecfSAndreas Gohr /** 20153528ecfSAndreas Gohr * Change given string to use % instead of * 20253528ecfSAndreas Gohr * 20353528ecfSAndreas Gohr * @param string|string[] $value 20453528ecfSAndreas Gohr * @return string|string[] 20553528ecfSAndreas Gohr */ 20653528ecfSAndreas Gohr protected function filterChangeToLike($value) { 20753528ecfSAndreas Gohr $map = function ($input) { 20853528ecfSAndreas Gohr return str_replace('*', '%', $input); 20953528ecfSAndreas Gohr }; 21053528ecfSAndreas Gohr 21153528ecfSAndreas Gohr if(is_array($value)) { 21253528ecfSAndreas Gohr $value = array_map($map, $value); 21353528ecfSAndreas Gohr } else { 21453528ecfSAndreas Gohr $value = $map($value); 21553528ecfSAndreas Gohr } 21653528ecfSAndreas Gohr return $value; 21753528ecfSAndreas Gohr } 21853528ecfSAndreas Gohr 21953528ecfSAndreas Gohr /** 2207f9cb794SAndreas Gohr * Set offset for the results 2217f9cb794SAndreas Gohr * 2227f9cb794SAndreas Gohr * @param int $offset 2237f9cb794SAndreas Gohr */ 2247f9cb794SAndreas Gohr public function setOffset($offset) { 2257f9cb794SAndreas Gohr $limit = 0; 2267f9cb794SAndreas Gohr if($this->range_end) { 2277f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2287f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2297f9cb794SAndreas Gohr } 2307f9cb794SAndreas Gohr $this->range_begin = $offset; 2317f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2327f9cb794SAndreas Gohr } 2337f9cb794SAndreas Gohr 2347f9cb794SAndreas Gohr /** 2357f9cb794SAndreas Gohr * Limit results to this number 2367f9cb794SAndreas Gohr * 2377f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2387f9cb794SAndreas Gohr */ 2397f9cb794SAndreas Gohr public function setLimit($limit) { 2407f9cb794SAndreas Gohr if($limit) { 2417f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2427f9cb794SAndreas Gohr } else { 2437f9cb794SAndreas Gohr $this->range_end = 0; 2447f9cb794SAndreas Gohr } 2457f9cb794SAndreas Gohr } 2467f9cb794SAndreas Gohr 2477f9cb794SAndreas Gohr /** 2487f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2497f9cb794SAndreas Gohr * 2507f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2517f9cb794SAndreas Gohr * 2527f9cb794SAndreas Gohr * @return int 2537f9cb794SAndreas Gohr */ 2547f9cb794SAndreas Gohr public function getCount() { 2557f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 2567f9cb794SAndreas Gohr return $this->count; 2577f9cb794SAndreas Gohr } 2587f9cb794SAndreas Gohr 259c46f5fb1SAndreas Gohr /** 260c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 261c46f5fb1SAndreas Gohr * 262c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 263c46f5fb1SAndreas Gohr * 264c46f5fb1SAndreas Gohr * @return \string[] 265c46f5fb1SAndreas Gohr */ 266d4b5a17cSAndreas Gohr public function getPids() { 267d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 268d4b5a17cSAndreas Gohr return $this->result_pids; 269d4b5a17cSAndreas Gohr } 270d4b5a17cSAndreas Gohr 2717f9cb794SAndreas Gohr /** 272*0ceefd5cSAnna Dabrowska * Returns the rid associated with each result row 273*0ceefd5cSAnna Dabrowska * 274*0ceefd5cSAnna Dabrowska * Important: this may only be called after running @see execute() 275*0ceefd5cSAnna Dabrowska * 276*0ceefd5cSAnna Dabrowska * @return array 277*0ceefd5cSAnna Dabrowska */ 278*0ceefd5cSAnna Dabrowska public function getRids() { 279*0ceefd5cSAnna Dabrowska if($this->result_rids === null) throw new StructException('rids are only accessible after executing the search'); 280*0ceefd5cSAnna Dabrowska return $this->result_rids; 281*0ceefd5cSAnna Dabrowska } 282*0ceefd5cSAnna Dabrowska 283*0ceefd5cSAnna Dabrowska /** 284b2ed727aSAndreas Gohr * Execute this search and return the result 285b2ed727aSAndreas Gohr * 28638fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2877f9cb794SAndreas Gohr * 2887f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2897f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 29038fa36fbSAndreas Gohr * 29138fa36fbSAndreas Gohr * @return Value[][] 292b2ed727aSAndreas Gohr */ 293b2ed727aSAndreas Gohr public function execute() { 294b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 295b2ed727aSAndreas Gohr 2967f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 297b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 29859b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 299b2ed727aSAndreas Gohr 300d4b5a17cSAndreas Gohr $this->result_pids = array(); 301b2ed727aSAndreas Gohr $result = array(); 3027f9cb794SAndreas Gohr $cursor = -1; 3035e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 3045e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 3055e4bfdb0SMichael Grosse }, true); 3067f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 3077f9cb794SAndreas Gohr $cursor++; 3087f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 3097f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 3107f9cb794SAndreas Gohr 311b2ed727aSAndreas Gohr $C = 0; 312b2ed727aSAndreas Gohr $resrow = array(); 31399a70f28SAndreas Gohr $isempty = true; 314b2ed727aSAndreas Gohr foreach($this->columns as $col) { 31538fa36fbSAndreas Gohr $val = $row["C$C"]; 316b2ed727aSAndreas Gohr if($col->isMulti()) { 31738fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 318b2ed727aSAndreas Gohr } 31999a70f28SAndreas Gohr $value = new Value($col, $val); 3208cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 32199a70f28SAndreas Gohr $resrow[] = $value; 322b2ed727aSAndreas Gohr $C++; 323b2ed727aSAndreas Gohr } 32499a70f28SAndreas Gohr 32599a70f28SAndreas Gohr // skip empty rows 3268cba7aa0SMichael Grosse if($isempty && !$pageidAndRevOnly) { 32799a70f28SAndreas Gohr $cursor--; 32899a70f28SAndreas Gohr continue; 32999a70f28SAndreas Gohr } 33099a70f28SAndreas Gohr 33199a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 332*0ceefd5cSAnna Dabrowska $this->result_rids[] = $row['rid']; 333b2ed727aSAndreas Gohr $result[] = $resrow; 334b2ed727aSAndreas Gohr } 3357f9cb794SAndreas Gohr 3367f9cb794SAndreas Gohr $this->sqlite->res_close($res); 3377f9cb794SAndreas Gohr $this->count = $cursor + 1; 338b2ed727aSAndreas Gohr return $result; 339b2ed727aSAndreas Gohr } 340b2ed727aSAndreas Gohr 341b2ed727aSAndreas Gohr /** 34215929be2SAndreas Gohr * Transform the set search parameters into a statement 34315929be2SAndreas Gohr * 344b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 34515929be2SAndreas Gohr */ 34615929be2SAndreas Gohr public function getSQL() { 3475511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 34815929be2SAndreas Gohr 3492f68434dSAndreas Gohr $QB = new QueryBuilder(); 35015929be2SAndreas Gohr 3519d7a36f9SAndreas Gohr // basic tables 352b2d67e7dSAndreas Gohr $first_table = ''; 3539d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 3542f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 355b2d67e7dSAndreas Gohr if($first_table) { 3569d7a36f9SAndreas Gohr // follow up tables 3572f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 3589d7a36f9SAndreas Gohr } else { 3599d7a36f9SAndreas Gohr // first table 360b75e0a08SAndreas Gohr 361*0ceefd5cSAnna Dabrowska // FIXME this breaks page search, a different check is needed 362*0ceefd5cSAnna Dabrowska if(false) { 36313c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 3642f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3652f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3662f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3672f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3682f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 36913c321dbSAndreas Gohr } 37013c321dbSAndreas Gohr 37113c321dbSAndreas Gohr $QB->addTable($datatable); 372*0ceefd5cSAnna Dabrowska $QB->addSelectColumn($datatable, 'rid'); 37313c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 37413c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 375b2d67e7dSAndreas Gohr 3762f68434dSAndreas Gohr $first_table = $datatable; 37715929be2SAndreas Gohr } 3782f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 3799d7a36f9SAndreas Gohr } 38015929be2SAndreas Gohr 38115929be2SAndreas Gohr // columns to select, handling multis 3829d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 38315929be2SAndreas Gohr $n = 0; 38415929be2SAndreas Gohr foreach($this->columns as $col) { 38515929be2SAndreas Gohr $CN = 'C' . $n++; 38615929be2SAndreas Gohr 387d1b04e89SAndreas Gohr if($col->isMulti()) { 3882f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3892f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 390db7970caSAndreas Gohr $MN = $QB->generateTableAlias('M'); 3912f68434dSAndreas Gohr 3922f68434dSAndreas Gohr $QB->addLeftJoin( 3932f68434dSAndreas Gohr $datatable, 3942f68434dSAndreas Gohr $multitable, 3952f68434dSAndreas Gohr $MN, 3962f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3972f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3982f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3992f68434dSAndreas Gohr ); 400578407b2SAndreas Gohr 401578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 402578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 403578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 40415929be2SAndreas Gohr } else { 405578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 4060dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 40715929be2SAndreas Gohr } 40815929be2SAndreas Gohr } 40915929be2SAndreas Gohr 4109d7a36f9SAndreas Gohr // where clauses 411af993d55SMichael Grosse if (!empty($this->filter)) { 412af993d55SMichael Grosse $userWHERE = $QB->filters()->where('AND'); 413af993d55SMichael Grosse } 414b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 4159ebd2ed6SAndreas Gohr /** @var Column $col */ 4164c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 417b8fe6730SAndreas Gohr 4182f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 4192f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 4209d7a36f9SAndreas Gohr 4219d7a36f9SAndreas Gohr /** @var $col Column */ 4229d7a36f9SAndreas Gohr if($col->isMulti()) { 423db7970caSAndreas Gohr $MN = $QB->generateTableAlias('MN'); 42415929be2SAndreas Gohr 4252f68434dSAndreas Gohr $QB->addLeftJoin( 4262f68434dSAndreas Gohr $datatable, 4272f68434dSAndreas Gohr $multitable, 4282f68434dSAndreas Gohr $MN, 4292f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 4302f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4312f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4322f68434dSAndreas Gohr ); 433690e4ebaSAndreas Gohr $coltbl = $MN; 434690e4ebaSAndreas Gohr $colnam = 'value'; 4359d7a36f9SAndreas Gohr } else { 436690e4ebaSAndreas Gohr $coltbl = $datatable; 437690e4ebaSAndreas Gohr $colnam = $col->getColName(); 4389d7a36f9SAndreas Gohr } 43953528ecfSAndreas Gohr 440af993d55SMichael Grosse $col->getType()->filter($userWHERE, $coltbl, $colnam, $comp, $value, $op); // type based filter 4419d7a36f9SAndreas Gohr } 4429d7a36f9SAndreas Gohr 44329394e6cSAndreas Gohr // sorting - we always sort by the single val column 444b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 445eb55dc17SAndreas Gohr list($col, $asc, $nc) = $sort; 4469d7a36f9SAndreas Gohr /** @var $col Column */ 447eb55dc17SAndreas Gohr $colname = $col->getColName(false); 448eb55dc17SAndreas Gohr if($nc) $colname .= ' COLLATE NOCASE'; 449eb55dc17SAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $colname, $asc ? 'ASC' : 'DESC'); 4509e07bdbfSAndreas Gohr } 4519e07bdbfSAndreas Gohr 4522f68434dSAndreas Gohr return $QB->getSQL(); 45315929be2SAndreas Gohr } 45415929be2SAndreas Gohr 45515929be2SAndreas Gohr /** 45601dd90deSAndreas Gohr * Returns all the columns that where added to the search 45701dd90deSAndreas Gohr * 45801dd90deSAndreas Gohr * @return Column[] 45901dd90deSAndreas Gohr */ 46001dd90deSAndreas Gohr public function getColumns() { 46101dd90deSAndreas Gohr return $this->columns; 46201dd90deSAndreas Gohr } 46301dd90deSAndreas Gohr 464577b462bSAndreas Gohr /** 4655a4df70dSAndreas Gohr * All the schemas currently added 4665a4df70dSAndreas Gohr * 4675a4df70dSAndreas Gohr * @return Schema[] 4685a4df70dSAndreas Gohr */ 4695a4df70dSAndreas Gohr public function getSchemas() { 4705a4df70dSAndreas Gohr return array_values($this->schemas); 4715a4df70dSAndreas Gohr } 4725a4df70dSAndreas Gohr 4735a4df70dSAndreas Gohr /** 474577b462bSAndreas Gohr * Checks if the given column is a * wildcard 475577b462bSAndreas Gohr * 476577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 477577b462bSAndreas Gohr * nothing happens 478577b462bSAndreas Gohr * 479577b462bSAndreas Gohr * @param string $colname 480577b462bSAndreas Gohr * @return bool was wildcard? 481577b462bSAndreas Gohr */ 482577b462bSAndreas Gohr protected function processWildcard($colname) { 483636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 484577b462bSAndreas Gohr if($colname !== '*') return false; 485577b462bSAndreas Gohr 486577b462bSAndreas Gohr // no table given? assume the first is meant 487577b462bSAndreas Gohr if($table === null) { 488577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 489577b462bSAndreas Gohr $table = $schema_list[0]; 490577b462bSAndreas Gohr } 491577b462bSAndreas Gohr 492577b462bSAndreas Gohr $schema = $this->schemas[$table]; 493577b462bSAndreas Gohr if(!$schema) return false; 494577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 495577b462bSAndreas Gohr return true; 496577b462bSAndreas Gohr } 497577b462bSAndreas Gohr 498577b462bSAndreas Gohr /** 499577b462bSAndreas Gohr * Split a given column name into table and column 500577b462bSAndreas Gohr * 501577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 502577b462bSAndreas Gohr * 503577b462bSAndreas Gohr * @param $colname 504636a5173SAndreas Gohr * @return array (colname, table) 505577b462bSAndreas Gohr */ 506577b462bSAndreas Gohr protected function resolveColumn($colname) { 507577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 508577b462bSAndreas Gohr 509577b462bSAndreas Gohr // resolve the alias or table name 5109007da58SMichael Große @list($table, $colname) = explode('.', $colname, 2); 511577b462bSAndreas Gohr if(!$colname) { 512577b462bSAndreas Gohr $colname = $table; 513577b462bSAndreas Gohr $table = null; 514577b462bSAndreas Gohr } 515577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 516577b462bSAndreas Gohr $table = $this->aliases[$table]; 517577b462bSAndreas Gohr } 518577b462bSAndreas Gohr 519577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 520577b462bSAndreas Gohr 521577b462bSAndreas Gohr return array($colname, $table); 522577b462bSAndreas Gohr } 52301dd90deSAndreas Gohr 52401dd90deSAndreas Gohr /** 52515929be2SAndreas Gohr * Find a column to be used in the search 52615929be2SAndreas Gohr * 52715929be2SAndreas Gohr * @param string $colname may contain an alias 52815929be2SAndreas Gohr * @return bool|Column 52915929be2SAndreas Gohr */ 53000f6af48SAndreas Gohr public function findColumn($colname) { 5315511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 532df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 533f107f479SAndreas Gohr 534*0ceefd5cSAnna Dabrowska // FIXME check for page schema? 535f107f479SAndreas Gohr // add "fake" column for special col 536*0ceefd5cSAnna Dabrowska// if(!(reset($this->schemas)->isLookup())) { 537128d4e83SAndreas Gohr if($colname == '%pageid%') { 538128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 539d1b04e89SAndreas Gohr } 540128d4e83SAndreas Gohr if($colname == '%title%') { 541128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 542128d4e83SAndreas Gohr } 543cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 544cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 545cadfc3ccSAndreas Gohr } 5462e12ac22SMichael Grosse if ($colname == '%lasteditor%') { 5472e12ac22SMichael Grosse return new UserColumn(0, new User(), $schema_list[0]); 5482e12ac22SMichael Grosse } 54988b58a21SSzymon Olewniczak if ($colname == '%lastsummary%') { 5506781c68dSSzymon Olewniczak return new SummaryColumn(0, new AutoSummary(), $schema_list[0]); 55188b58a21SSzymon Olewniczak } 552*0ceefd5cSAnna Dabrowska// } else { 553f107f479SAndreas Gohr if($colname == '%rowid%') { 554f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 555f107f479SAndreas Gohr } 556*0ceefd5cSAnna Dabrowska// } 557d1b04e89SAndreas Gohr 558636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 55915929be2SAndreas Gohr 560bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 561577b462bSAndreas Gohr if($table !== null) { 56215929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 56315929be2SAndreas Gohr } else { 56415929be2SAndreas Gohr $schemas = $this->schemas; 56515929be2SAndreas Gohr } 56615929be2SAndreas Gohr 56715929be2SAndreas Gohr // find it 56815929be2SAndreas Gohr $col = false; 56915929be2SAndreas Gohr foreach($schemas as $schema) { 5704ac44d1cSMichael Grosse if(empty($schema)) { 5714ac44d1cSMichael Grosse continue; 5724ac44d1cSMichael Grosse } 57315929be2SAndreas Gohr $col = $schema->findColumn($colname); 57415929be2SAndreas Gohr if($col) break; 57515929be2SAndreas Gohr } 57615929be2SAndreas Gohr 57715929be2SAndreas Gohr return $col; 57815929be2SAndreas Gohr } 57915929be2SAndreas Gohr 5809dbc8ec0SMichael Grosse /** 58199a70f28SAndreas Gohr * Check if the given row is empty or references our own row 5829dbc8ec0SMichael Grosse * 58399a70f28SAndreas Gohr * @param Value $value 5849dbc8ec0SMichael Grosse * @return bool 5859dbc8ec0SMichael Grosse */ 5868cba7aa0SMichael Grosse protected function isEmptyValue(Value $value) { 58799a70f28SAndreas Gohr if ($value->isEmpty()) return true; 5888cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 5899dbc8ec0SMichael Grosse return false; 5909dbc8ec0SMichael Grosse } 59115929be2SAndreas Gohr} 59215929be2SAndreas Gohr 5935511bd5bSAndreas Gohr 594