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; 8*2e12ac22SMichael Grosseuse dokuwiki\plugin\struct\types\User; 90561158fSAndreas Gohr 1015929be2SAndreas Gohrclass Search { 119d7a36f9SAndreas Gohr /** 129d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 139d7a36f9SAndreas Gohr */ 149d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 159d7a36f9SAndreas Gohr 165511bd5bSAndreas Gohr /** 175511bd5bSAndreas Gohr * The list of known and allowed comparators 182e7595e7SAndreas Gohr * (order matters) 195511bd5bSAndreas Gohr */ 2074461852SAndreas Gohr static public $COMPARATORS = array( 212e7595e7SAndreas Gohr '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', 225511bd5bSAndreas Gohr ); 235511bd5bSAndreas Gohr 249d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 259d7a36f9SAndreas Gohr protected $sqlite; 2615929be2SAndreas Gohr 2715929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 2815929be2SAndreas Gohr protected $schemas = array(); 2915929be2SAndreas Gohr 3015929be2SAndreas Gohr /** @var Column[] list of columns to select */ 3115929be2SAndreas Gohr protected $columns = array(); 3215929be2SAndreas Gohr 3315929be2SAndreas Gohr /** @var array the sorting of the result */ 3415929be2SAndreas Gohr protected $sortby = array(); 3515929be2SAndreas Gohr 369d7a36f9SAndreas Gohr /** @var array the filters */ 379d7a36f9SAndreas Gohr protected $filter = array(); 3815929be2SAndreas Gohr 3915929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 4015929be2SAndreas Gohr protected $aliases = array(); 4115929be2SAndreas Gohr 427f9cb794SAndreas Gohr /** @var int begin results from here */ 437f9cb794SAndreas Gohr protected $range_begin = 0; 447f9cb794SAndreas Gohr 457f9cb794SAndreas Gohr /** @var int end results here */ 467f9cb794SAndreas Gohr protected $range_end = 0; 477f9cb794SAndreas Gohr 487f9cb794SAndreas Gohr /** @var int the number of results */ 497f9cb794SAndreas Gohr protected $count = -1; 50d4b5a17cSAndreas Gohr /** @var string[] the PIDs of the result rows */ 51d4b5a17cSAndreas Gohr protected $result_pids = null; 527f9cb794SAndreas Gohr 5315929be2SAndreas Gohr /** 549d7a36f9SAndreas Gohr * Search constructor. 559d7a36f9SAndreas Gohr */ 569d7a36f9SAndreas Gohr public function __construct() { 579d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 589d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 599d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 609d7a36f9SAndreas Gohr } 619d7a36f9SAndreas Gohr 629d7a36f9SAndreas Gohr /** 6315929be2SAndreas Gohr * Add a schema to be searched 6415929be2SAndreas Gohr * 6515929be2SAndreas Gohr * Call multiple times for multiple schemas. 6615929be2SAndreas Gohr * 6715929be2SAndreas Gohr * @param string $table 6815929be2SAndreas Gohr * @param string $alias 6915929be2SAndreas Gohr */ 7015929be2SAndreas Gohr public function addSchema($table, $alias = '') { 712be187cdSAndreas Gohr $schema = new Schema($table); 722be187cdSAndreas Gohr if(!$schema->getId()) { 732be187cdSAndreas Gohr throw new StructException('schema missing', $table); 742be187cdSAndreas Gohr } 752be187cdSAndreas Gohr 762be187cdSAndreas Gohr if($this->schemas && 772be187cdSAndreas Gohr ( 782be187cdSAndreas Gohr $schema->isLookup() || 792be187cdSAndreas Gohr reset($this->schemas)->isLookup() 802be187cdSAndreas Gohr ) 812be187cdSAndreas Gohr ) { 822be187cdSAndreas Gohr throw new StructException('nolookupmix'); 832be187cdSAndreas Gohr } 842be187cdSAndreas Gohr 85712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 86712c650dSAndreas Gohr if($alias) $this->aliases[$alias] = $schema->getTable(); 8715929be2SAndreas Gohr } 8815929be2SAndreas Gohr 8915929be2SAndreas Gohr /** 9015929be2SAndreas Gohr * Add a column to be returned by the search 9115929be2SAndreas Gohr * 9215929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9315929be2SAndreas Gohr * added before 9415929be2SAndreas Gohr * 9515929be2SAndreas Gohr * @param string $colname may contain an alias 9615929be2SAndreas Gohr */ 9715929be2SAndreas Gohr public function addColumn($colname) { 98577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 9915929be2SAndreas Gohr $col = $this->findColumn($colname); 10015929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10115929be2SAndreas Gohr $this->columns[] = $col; 10215929be2SAndreas Gohr } 10315929be2SAndreas Gohr 10415929be2SAndreas Gohr /** 10515929be2SAndreas Gohr * Add sorting options 10615929be2SAndreas Gohr * 10715929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 10815929be2SAndreas Gohr * added before 10915929be2SAndreas Gohr * 11015929be2SAndreas Gohr * @param string $colname may contain an alias 11115929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 112eb55dc17SAndreas Gohr * @param bool $nc set true for caseinsensitivity 11315929be2SAndreas Gohr */ 114eb55dc17SAndreas Gohr public function addSort($colname, $asc = true, $nc = true) { 11515929be2SAndreas Gohr $col = $this->findColumn($colname); 11615929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 11715929be2SAndreas Gohr 118eb55dc17SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc, $nc); 11907993756SAndreas Gohr } 12007993756SAndreas Gohr 12107993756SAndreas Gohr /** 12207993756SAndreas Gohr * Returns all set sort columns 12307993756SAndreas Gohr * 12407993756SAndreas Gohr * @return array 12507993756SAndreas Gohr */ 12607993756SAndreas Gohr public function getSorts() { 12707993756SAndreas Gohr return $this->sortby; 12815929be2SAndreas Gohr } 12915929be2SAndreas Gohr 13015929be2SAndreas Gohr /** 1319d7a36f9SAndreas Gohr * Adds a filter 13215929be2SAndreas Gohr * 13315929be2SAndreas Gohr * @param string $colname may contain an alias 13453528ecfSAndreas Gohr * @param string|string[] $value 1355511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1364c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 13715929be2SAndreas Gohr */ 1384c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1399dbc32aeSAndreas Gohr /* Convert certain filters into others 1409dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1419dbc32aeSAndreas Gohr if($comp == '*~') { 14253528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1439dbc32aeSAndreas Gohr $comp = '~'; 1449dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1459dbc32aeSAndreas Gohr $comp = '!='; 1469dbc32aeSAndreas Gohr } 1479dbc32aeSAndreas Gohr 14874461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1494c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1509d7a36f9SAndreas Gohr 15115929be2SAndreas Gohr $col = $this->findColumn($colname); 1527da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1537da1a0fdSAndreas Gohr 1547da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1557da1a0fdSAndreas Gohr switch($comp) { 1567da1a0fdSAndreas Gohr case '~': 1577da1a0fdSAndreas Gohr $comp = 'LIKE'; 1587da1a0fdSAndreas Gohr break; 1597da1a0fdSAndreas Gohr case '!~': 1607da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1617da1a0fdSAndreas Gohr break; 1627da1a0fdSAndreas Gohr case '=*': 1637da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1647da1a0fdSAndreas Gohr break; 1657da1a0fdSAndreas Gohr } 1667da1a0fdSAndreas Gohr 1677da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1687da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 16953528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1707da1a0fdSAndreas Gohr } 1717da1a0fdSAndreas Gohr 1727da1a0fdSAndreas Gohr // add the filter 1734c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 17415929be2SAndreas Gohr } 17515929be2SAndreas Gohr 17615929be2SAndreas Gohr /** 17753528ecfSAndreas Gohr * Wrap given value in asterisks 17853528ecfSAndreas Gohr * 17953528ecfSAndreas Gohr * @param string|string[] $value 18053528ecfSAndreas Gohr * @return string|string[] 18153528ecfSAndreas Gohr */ 18253528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 18353528ecfSAndreas Gohr $map = function ($input) { 18453528ecfSAndreas Gohr return "*$input*"; 18553528ecfSAndreas Gohr }; 18653528ecfSAndreas Gohr 18753528ecfSAndreas Gohr if(is_array($value)) { 18853528ecfSAndreas Gohr $value = array_map($map, $value); 18953528ecfSAndreas Gohr } else { 19053528ecfSAndreas Gohr $value = $map($value); 19153528ecfSAndreas Gohr } 19253528ecfSAndreas Gohr return $value; 19353528ecfSAndreas Gohr } 19453528ecfSAndreas Gohr 19553528ecfSAndreas Gohr /** 19653528ecfSAndreas Gohr * Change given string to use % instead of * 19753528ecfSAndreas Gohr * 19853528ecfSAndreas Gohr * @param string|string[] $value 19953528ecfSAndreas Gohr * @return string|string[] 20053528ecfSAndreas Gohr */ 20153528ecfSAndreas Gohr protected function filterChangeToLike($value) { 20253528ecfSAndreas Gohr $map = function ($input) { 20353528ecfSAndreas Gohr return str_replace('*', '%', $input); 20453528ecfSAndreas Gohr }; 20553528ecfSAndreas Gohr 20653528ecfSAndreas Gohr if(is_array($value)) { 20753528ecfSAndreas Gohr $value = array_map($map, $value); 20853528ecfSAndreas Gohr } else { 20953528ecfSAndreas Gohr $value = $map($value); 21053528ecfSAndreas Gohr } 21153528ecfSAndreas Gohr return $value; 21253528ecfSAndreas Gohr } 21353528ecfSAndreas Gohr 21453528ecfSAndreas Gohr /** 2157f9cb794SAndreas Gohr * Set offset for the results 2167f9cb794SAndreas Gohr * 2177f9cb794SAndreas Gohr * @param int $offset 2187f9cb794SAndreas Gohr */ 2197f9cb794SAndreas Gohr public function setOffset($offset) { 2207f9cb794SAndreas Gohr $limit = 0; 2217f9cb794SAndreas Gohr if($this->range_end) { 2227f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2237f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2247f9cb794SAndreas Gohr } 2257f9cb794SAndreas Gohr $this->range_begin = $offset; 2267f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2277f9cb794SAndreas Gohr } 2287f9cb794SAndreas Gohr 2297f9cb794SAndreas Gohr /** 2307f9cb794SAndreas Gohr * Limit results to this number 2317f9cb794SAndreas Gohr * 2327f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2337f9cb794SAndreas Gohr */ 2347f9cb794SAndreas Gohr public function setLimit($limit) { 2357f9cb794SAndreas Gohr if($limit) { 2367f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2377f9cb794SAndreas Gohr } else { 2387f9cb794SAndreas Gohr $this->range_end = 0; 2397f9cb794SAndreas Gohr } 2407f9cb794SAndreas Gohr } 2417f9cb794SAndreas Gohr 2427f9cb794SAndreas Gohr /** 2437f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2447f9cb794SAndreas Gohr * 2457f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2467f9cb794SAndreas Gohr * 2477f9cb794SAndreas Gohr * @return int 2487f9cb794SAndreas Gohr */ 2497f9cb794SAndreas Gohr public function getCount() { 2507f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 2517f9cb794SAndreas Gohr return $this->count; 2527f9cb794SAndreas Gohr } 2537f9cb794SAndreas Gohr 254c46f5fb1SAndreas Gohr /** 255c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 256c46f5fb1SAndreas Gohr * 257c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 258c46f5fb1SAndreas Gohr * 259c46f5fb1SAndreas Gohr * @return \string[] 260c46f5fb1SAndreas Gohr */ 261d4b5a17cSAndreas Gohr public function getPids() { 262d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 263d4b5a17cSAndreas Gohr return $this->result_pids; 264d4b5a17cSAndreas Gohr } 265d4b5a17cSAndreas Gohr 2667f9cb794SAndreas Gohr /** 267b2ed727aSAndreas Gohr * Execute this search and return the result 268b2ed727aSAndreas Gohr * 26938fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2707f9cb794SAndreas Gohr * 2717f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2727f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 27338fa36fbSAndreas Gohr * 27438fa36fbSAndreas Gohr * @return Value[][] 275b2ed727aSAndreas Gohr */ 276b2ed727aSAndreas Gohr public function execute() { 277b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 278b2ed727aSAndreas Gohr 2797f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 280b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 28159b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 282b2ed727aSAndreas Gohr 283d4b5a17cSAndreas Gohr $this->result_pids = array(); 284b2ed727aSAndreas Gohr $result = array(); 2857f9cb794SAndreas Gohr $cursor = -1; 2865e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 2875e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 2885e4bfdb0SMichael Grosse }, true); 2897f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2907f9cb794SAndreas Gohr $cursor++; 2917f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2927f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2937f9cb794SAndreas Gohr 294b2ed727aSAndreas Gohr $C = 0; 295b2ed727aSAndreas Gohr $resrow = array(); 29699a70f28SAndreas Gohr $isempty = true; 297b2ed727aSAndreas Gohr foreach($this->columns as $col) { 29838fa36fbSAndreas Gohr $val = $row["C$C"]; 299b2ed727aSAndreas Gohr if($col->isMulti()) { 30038fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 301b2ed727aSAndreas Gohr } 30299a70f28SAndreas Gohr $value = new Value($col, $val); 3038cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 30499a70f28SAndreas Gohr $resrow[] = $value; 305b2ed727aSAndreas Gohr $C++; 306b2ed727aSAndreas Gohr } 30799a70f28SAndreas Gohr 30899a70f28SAndreas Gohr // skip empty rows 3098cba7aa0SMichael Grosse if($isempty && !$pageidAndRevOnly) { 31099a70f28SAndreas Gohr $cursor--; 31199a70f28SAndreas Gohr continue; 31299a70f28SAndreas Gohr } 31399a70f28SAndreas Gohr 31499a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 315b2ed727aSAndreas Gohr $result[] = $resrow; 316b2ed727aSAndreas Gohr } 3177f9cb794SAndreas Gohr 3187f9cb794SAndreas Gohr $this->sqlite->res_close($res); 3197f9cb794SAndreas Gohr $this->count = $cursor + 1; 320b2ed727aSAndreas Gohr return $result; 321b2ed727aSAndreas Gohr } 322b2ed727aSAndreas Gohr 323b2ed727aSAndreas Gohr /** 32415929be2SAndreas Gohr * Transform the set search parameters into a statement 32515929be2SAndreas Gohr * 326b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 32715929be2SAndreas Gohr */ 32815929be2SAndreas Gohr public function getSQL() { 3295511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 33015929be2SAndreas Gohr 3312f68434dSAndreas Gohr $QB = new QueryBuilder(); 33215929be2SAndreas Gohr 3339d7a36f9SAndreas Gohr // basic tables 334b2d67e7dSAndreas Gohr $first_table = ''; 3359d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 3362f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 337b2d67e7dSAndreas Gohr if($first_table) { 3389d7a36f9SAndreas Gohr // follow up tables 3392f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 3409d7a36f9SAndreas Gohr } else { 3419d7a36f9SAndreas Gohr // first table 342b75e0a08SAndreas Gohr 34313c321dbSAndreas Gohr if(!$schema->isLookup()) { 34413c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 3452f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3462f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3472f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3482f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3492f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 35013c321dbSAndreas Gohr } 35113c321dbSAndreas Gohr 35213c321dbSAndreas Gohr $QB->addTable($datatable); 35313c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 35413c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 355b2d67e7dSAndreas Gohr 3562f68434dSAndreas Gohr $first_table = $datatable; 35715929be2SAndreas Gohr } 3582f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 3599d7a36f9SAndreas Gohr } 36015929be2SAndreas Gohr 36115929be2SAndreas Gohr // columns to select, handling multis 3629d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 36315929be2SAndreas Gohr $n = 0; 36415929be2SAndreas Gohr foreach($this->columns as $col) { 36515929be2SAndreas Gohr $CN = 'C' . $n++; 36615929be2SAndreas Gohr 367d1b04e89SAndreas Gohr if($col->isMulti()) { 3682f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3692f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3702f68434dSAndreas Gohr $MN = 'M' . $col->getColref(); 3712f68434dSAndreas Gohr 3722f68434dSAndreas Gohr $QB->addLeftJoin( 3732f68434dSAndreas Gohr $datatable, 3742f68434dSAndreas Gohr $multitable, 3752f68434dSAndreas Gohr $MN, 3762f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3772f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3782f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3792f68434dSAndreas Gohr ); 380578407b2SAndreas Gohr 381578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 382578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 383578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 38415929be2SAndreas Gohr } else { 385578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 3860dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 38715929be2SAndreas Gohr } 38815929be2SAndreas Gohr } 38915929be2SAndreas Gohr 3909d7a36f9SAndreas Gohr // where clauses 391b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 3924c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 393b8fe6730SAndreas Gohr 3942f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3952f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3969d7a36f9SAndreas Gohr 3979d7a36f9SAndreas Gohr /** @var $col Column */ 3989d7a36f9SAndreas Gohr if($col->isMulti()) { 3992f68434dSAndreas Gohr $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 40015929be2SAndreas Gohr 4012f68434dSAndreas Gohr $QB->addLeftJoin( 4022f68434dSAndreas Gohr $datatable, 4032f68434dSAndreas Gohr $multitable, 4042f68434dSAndreas Gohr $MN, 4052f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 4062f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4072f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4082f68434dSAndreas Gohr ); 409690e4ebaSAndreas Gohr $coltbl = $MN; 410690e4ebaSAndreas Gohr $colnam = 'value'; 4119d7a36f9SAndreas Gohr } else { 412690e4ebaSAndreas Gohr $coltbl = $datatable; 413690e4ebaSAndreas Gohr $colnam = $col->getColName(); 4149d7a36f9SAndreas Gohr } 41553528ecfSAndreas Gohr 416690e4ebaSAndreas Gohr $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 4179d7a36f9SAndreas Gohr } 4189d7a36f9SAndreas Gohr 41929394e6cSAndreas Gohr // sorting - we always sort by the single val column 420b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 421eb55dc17SAndreas Gohr list($col, $asc, $nc) = $sort; 4229d7a36f9SAndreas Gohr /** @var $col Column */ 423eb55dc17SAndreas Gohr $colname = $col->getColName(false); 424eb55dc17SAndreas Gohr if($nc) $colname .= ' COLLATE NOCASE'; 425eb55dc17SAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $colname, $asc ? 'ASC' : 'DESC'); 4269e07bdbfSAndreas Gohr } 4279e07bdbfSAndreas Gohr 4282f68434dSAndreas Gohr return $QB->getSQL(); 42915929be2SAndreas Gohr } 43015929be2SAndreas Gohr 43115929be2SAndreas Gohr /** 43201dd90deSAndreas Gohr * Returns all the columns that where added to the search 43301dd90deSAndreas Gohr * 43401dd90deSAndreas Gohr * @return Column[] 43501dd90deSAndreas Gohr */ 43601dd90deSAndreas Gohr public function getColumns() { 43701dd90deSAndreas Gohr return $this->columns; 43801dd90deSAndreas Gohr } 43901dd90deSAndreas Gohr 440577b462bSAndreas Gohr /** 441577b462bSAndreas Gohr * Checks if the given column is a * wildcard 442577b462bSAndreas Gohr * 443577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 444577b462bSAndreas Gohr * nothing happens 445577b462bSAndreas Gohr * 446577b462bSAndreas Gohr * @param string $colname 447577b462bSAndreas Gohr * @return bool was wildcard? 448577b462bSAndreas Gohr */ 449577b462bSAndreas Gohr protected function processWildcard($colname) { 450636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 451577b462bSAndreas Gohr if($colname !== '*') return false; 452577b462bSAndreas Gohr 453577b462bSAndreas Gohr // no table given? assume the first is meant 454577b462bSAndreas Gohr if($table === null) { 455577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 456577b462bSAndreas Gohr $table = $schema_list[0]; 457577b462bSAndreas Gohr } 458577b462bSAndreas Gohr 459577b462bSAndreas Gohr $schema = $this->schemas[$table]; 460577b462bSAndreas Gohr if(!$schema) return false; 461577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 462577b462bSAndreas Gohr return true; 463577b462bSAndreas Gohr } 464577b462bSAndreas Gohr 465577b462bSAndreas Gohr /** 466577b462bSAndreas Gohr * Split a given column name into table and column 467577b462bSAndreas Gohr * 468577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 469577b462bSAndreas Gohr * 470577b462bSAndreas Gohr * @param $colname 471636a5173SAndreas Gohr * @return array (colname, table) 472577b462bSAndreas Gohr */ 473577b462bSAndreas Gohr protected function resolveColumn($colname) { 474577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 475577b462bSAndreas Gohr 476577b462bSAndreas Gohr // resolve the alias or table name 477577b462bSAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 478577b462bSAndreas Gohr if(!$colname) { 479577b462bSAndreas Gohr $colname = $table; 480577b462bSAndreas Gohr $table = null; 481577b462bSAndreas Gohr } 482577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 483577b462bSAndreas Gohr $table = $this->aliases[$table]; 484577b462bSAndreas Gohr } 485577b462bSAndreas Gohr 486577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 487577b462bSAndreas Gohr 488577b462bSAndreas Gohr return array($colname, $table); 489577b462bSAndreas Gohr } 49001dd90deSAndreas Gohr 49101dd90deSAndreas Gohr /** 49215929be2SAndreas Gohr * Find a column to be used in the search 49315929be2SAndreas Gohr * 49415929be2SAndreas Gohr * @param string $colname may contain an alias 49515929be2SAndreas Gohr * @return bool|Column 49615929be2SAndreas Gohr */ 49700f6af48SAndreas Gohr public function findColumn($colname) { 4985511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 499df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 500f107f479SAndreas Gohr 501f107f479SAndreas Gohr // add "fake" column for special col 502f107f479SAndreas Gohr if(!(reset($this->schemas)->isLookup())) { 503128d4e83SAndreas Gohr if($colname == '%pageid%') { 504128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 505d1b04e89SAndreas Gohr } 506128d4e83SAndreas Gohr if($colname == '%title%') { 507128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 508128d4e83SAndreas Gohr } 509cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 510cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 511cadfc3ccSAndreas Gohr } 512*2e12ac22SMichael Grosse if ($colname == '%lasteditor%') { 513*2e12ac22SMichael Grosse return new UserColumn(0, new User(), $schema_list[0]); 514*2e12ac22SMichael Grosse } 515f107f479SAndreas Gohr } else { 516f107f479SAndreas Gohr if($colname == '%rowid%') { 517f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 518f107f479SAndreas Gohr } 519f747a930SAndreas Gohr } 520d1b04e89SAndreas Gohr 521636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 52215929be2SAndreas Gohr 523bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 524577b462bSAndreas Gohr if($table !== null) { 52515929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 52615929be2SAndreas Gohr } else { 52715929be2SAndreas Gohr $schemas = $this->schemas; 52815929be2SAndreas Gohr } 52915929be2SAndreas Gohr 53015929be2SAndreas Gohr // find it 53115929be2SAndreas Gohr $col = false; 53215929be2SAndreas Gohr foreach($schemas as $schema) { 5334ac44d1cSMichael Grosse if(empty($schema)) { 5344ac44d1cSMichael Grosse continue; 5354ac44d1cSMichael Grosse } 53615929be2SAndreas Gohr $col = $schema->findColumn($colname); 53715929be2SAndreas Gohr if($col) break; 53815929be2SAndreas Gohr } 53915929be2SAndreas Gohr 54015929be2SAndreas Gohr return $col; 54115929be2SAndreas Gohr } 54215929be2SAndreas Gohr 5439dbc8ec0SMichael Grosse /** 54499a70f28SAndreas Gohr * Check if the given row is empty or references our own row 5459dbc8ec0SMichael Grosse * 54699a70f28SAndreas Gohr * @param Value $value 5479dbc8ec0SMichael Grosse * @return bool 5489dbc8ec0SMichael Grosse */ 5498cba7aa0SMichael Grosse protected function isEmptyValue(Value $value) { 55099a70f28SAndreas Gohr if ($value->isEmpty()) return true; 5518cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 5529dbc8ec0SMichael Grosse return false; 5539dbc8ec0SMichael Grosse } 55415929be2SAndreas Gohr} 55515929be2SAndreas Gohr 5565511bd5bSAndreas Gohr 557