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; 80561158fSAndreas Gohr 915929be2SAndreas Gohrclass Search { 109d7a36f9SAndreas Gohr /** 119d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 129d7a36f9SAndreas Gohr */ 139d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 149d7a36f9SAndreas Gohr 155511bd5bSAndreas Gohr /** 165511bd5bSAndreas Gohr * The list of known and allowed comparators 172e7595e7SAndreas Gohr * (order matters) 185511bd5bSAndreas Gohr */ 1974461852SAndreas Gohr static public $COMPARATORS = array( 202e7595e7SAndreas Gohr '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', 215511bd5bSAndreas Gohr ); 225511bd5bSAndreas Gohr 239d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 249d7a36f9SAndreas Gohr protected $sqlite; 2515929be2SAndreas Gohr 2615929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 2715929be2SAndreas Gohr protected $schemas = array(); 2815929be2SAndreas Gohr 2915929be2SAndreas Gohr /** @var Column[] list of columns to select */ 3015929be2SAndreas Gohr protected $columns = array(); 3115929be2SAndreas Gohr 3215929be2SAndreas Gohr /** @var array the sorting of the result */ 3315929be2SAndreas Gohr protected $sortby = array(); 3415929be2SAndreas Gohr 359d7a36f9SAndreas Gohr /** @var array the filters */ 369d7a36f9SAndreas Gohr protected $filter = array(); 3715929be2SAndreas Gohr 3815929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 3915929be2SAndreas Gohr protected $aliases = array(); 4015929be2SAndreas Gohr 417f9cb794SAndreas Gohr /** @var int begin results from here */ 427f9cb794SAndreas Gohr protected $range_begin = 0; 437f9cb794SAndreas Gohr 447f9cb794SAndreas Gohr /** @var int end results here */ 457f9cb794SAndreas Gohr protected $range_end = 0; 467f9cb794SAndreas Gohr 477f9cb794SAndreas Gohr /** @var int the number of results */ 487f9cb794SAndreas Gohr protected $count = -1; 49d4b5a17cSAndreas Gohr /** @var string[] the PIDs of the result rows */ 50d4b5a17cSAndreas Gohr protected $result_pids = null; 517f9cb794SAndreas Gohr 5215929be2SAndreas Gohr /** 539d7a36f9SAndreas Gohr * Search constructor. 549d7a36f9SAndreas Gohr */ 559d7a36f9SAndreas Gohr public function __construct() { 569d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 579d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 589d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 599d7a36f9SAndreas Gohr } 609d7a36f9SAndreas Gohr 619d7a36f9SAndreas Gohr /** 6215929be2SAndreas Gohr * Add a schema to be searched 6315929be2SAndreas Gohr * 6415929be2SAndreas Gohr * Call multiple times for multiple schemas. 6515929be2SAndreas Gohr * 6615929be2SAndreas Gohr * @param string $table 6715929be2SAndreas Gohr * @param string $alias 6815929be2SAndreas Gohr */ 6915929be2SAndreas Gohr public function addSchema($table, $alias = '') { 702be187cdSAndreas Gohr $schema = new Schema($table); 712be187cdSAndreas Gohr if(!$schema->getId()) { 722be187cdSAndreas Gohr throw new StructException('schema missing', $table); 732be187cdSAndreas Gohr } 742be187cdSAndreas Gohr 752be187cdSAndreas Gohr if($this->schemas && 762be187cdSAndreas Gohr ( 772be187cdSAndreas Gohr $schema->isLookup() || 782be187cdSAndreas Gohr reset($this->schemas)->isLookup() 792be187cdSAndreas Gohr ) 802be187cdSAndreas Gohr ) { 812be187cdSAndreas Gohr throw new StructException('nolookupmix'); 822be187cdSAndreas Gohr } 832be187cdSAndreas Gohr 84*712c650dSAndreas Gohr $this->schemas[$schema->getTable()] = $schema; 85*712c650dSAndreas Gohr if($alias) $this->aliases[$alias] = $schema->getTable(); 8615929be2SAndreas Gohr } 8715929be2SAndreas Gohr 8815929be2SAndreas Gohr /** 8915929be2SAndreas Gohr * Add a column to be returned by the search 9015929be2SAndreas Gohr * 9115929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9215929be2SAndreas Gohr * added before 9315929be2SAndreas Gohr * 9415929be2SAndreas Gohr * @param string $colname may contain an alias 9515929be2SAndreas Gohr */ 9615929be2SAndreas Gohr public function addColumn($colname) { 97577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 9815929be2SAndreas Gohr $col = $this->findColumn($colname); 9915929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10015929be2SAndreas Gohr $this->columns[] = $col; 10115929be2SAndreas Gohr } 10215929be2SAndreas Gohr 10315929be2SAndreas Gohr /** 10415929be2SAndreas Gohr * Add sorting options 10515929be2SAndreas Gohr * 10615929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 10715929be2SAndreas Gohr * added before 10815929be2SAndreas Gohr * 10915929be2SAndreas Gohr * @param string $colname may contain an alias 11015929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 11115929be2SAndreas Gohr */ 11215929be2SAndreas Gohr public function addSort($colname, $asc = true) { 11315929be2SAndreas Gohr $col = $this->findColumn($colname); 11415929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 11515929be2SAndreas Gohr 11607993756SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc); 11707993756SAndreas Gohr } 11807993756SAndreas Gohr 11907993756SAndreas Gohr /** 12007993756SAndreas Gohr * Returns all set sort columns 12107993756SAndreas Gohr * 12207993756SAndreas Gohr * @return array 12307993756SAndreas Gohr */ 12407993756SAndreas Gohr public function getSorts() { 12507993756SAndreas Gohr return $this->sortby; 12615929be2SAndreas Gohr } 12715929be2SAndreas Gohr 12815929be2SAndreas Gohr /** 1299d7a36f9SAndreas Gohr * Adds a filter 13015929be2SAndreas Gohr * 13115929be2SAndreas Gohr * @param string $colname may contain an alias 13253528ecfSAndreas Gohr * @param string|string[] $value 1335511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1344c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 13515929be2SAndreas Gohr */ 1364c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1379dbc32aeSAndreas Gohr /* Convert certain filters into others 1389dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1399dbc32aeSAndreas Gohr if($comp == '*~') { 14053528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1419dbc32aeSAndreas Gohr $comp = '~'; 1429dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1439dbc32aeSAndreas Gohr $comp = '!='; 1449dbc32aeSAndreas Gohr } 1459dbc32aeSAndreas Gohr 14674461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1474c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1489d7a36f9SAndreas Gohr 14915929be2SAndreas Gohr $col = $this->findColumn($colname); 1507da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1517da1a0fdSAndreas Gohr 1527da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1537da1a0fdSAndreas Gohr switch($comp) { 1547da1a0fdSAndreas Gohr case '~': 1557da1a0fdSAndreas Gohr $comp = 'LIKE'; 1567da1a0fdSAndreas Gohr break; 1577da1a0fdSAndreas Gohr case '!~': 1587da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1597da1a0fdSAndreas Gohr break; 1607da1a0fdSAndreas Gohr case '=*': 1617da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1627da1a0fdSAndreas Gohr break; 1637da1a0fdSAndreas Gohr } 1647da1a0fdSAndreas Gohr 1657da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1667da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 16753528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1687da1a0fdSAndreas Gohr } 1697da1a0fdSAndreas Gohr 1707da1a0fdSAndreas Gohr // add the filter 1714c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 17215929be2SAndreas Gohr } 17315929be2SAndreas Gohr 17415929be2SAndreas Gohr /** 17553528ecfSAndreas Gohr * Wrap given value in asterisks 17653528ecfSAndreas Gohr * 17753528ecfSAndreas Gohr * @param string|string[] $value 17853528ecfSAndreas Gohr * @return string|string[] 17953528ecfSAndreas Gohr */ 18053528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 18153528ecfSAndreas Gohr $map = function ($input) { 18253528ecfSAndreas Gohr return "*$input*"; 18353528ecfSAndreas Gohr }; 18453528ecfSAndreas Gohr 18553528ecfSAndreas Gohr if(is_array($value)) { 18653528ecfSAndreas Gohr $value = array_map($map, $value); 18753528ecfSAndreas Gohr } else { 18853528ecfSAndreas Gohr $value = $map($value); 18953528ecfSAndreas Gohr } 19053528ecfSAndreas Gohr return $value; 19153528ecfSAndreas Gohr } 19253528ecfSAndreas Gohr 19353528ecfSAndreas Gohr /** 19453528ecfSAndreas Gohr * Change given string to use % instead of * 19553528ecfSAndreas Gohr * 19653528ecfSAndreas Gohr * @param string|string[] $value 19753528ecfSAndreas Gohr * @return string|string[] 19853528ecfSAndreas Gohr */ 19953528ecfSAndreas Gohr protected function filterChangeToLike($value) { 20053528ecfSAndreas Gohr $map = function ($input) { 20153528ecfSAndreas Gohr return str_replace('*', '%', $input); 20253528ecfSAndreas Gohr }; 20353528ecfSAndreas Gohr 20453528ecfSAndreas Gohr if(is_array($value)) { 20553528ecfSAndreas Gohr $value = array_map($map, $value); 20653528ecfSAndreas Gohr } else { 20753528ecfSAndreas Gohr $value = $map($value); 20853528ecfSAndreas Gohr } 20953528ecfSAndreas Gohr return $value; 21053528ecfSAndreas Gohr } 21153528ecfSAndreas Gohr 21253528ecfSAndreas Gohr /** 2137f9cb794SAndreas Gohr * Set offset for the results 2147f9cb794SAndreas Gohr * 2157f9cb794SAndreas Gohr * @param int $offset 2167f9cb794SAndreas Gohr */ 2177f9cb794SAndreas Gohr public function setOffset($offset) { 2187f9cb794SAndreas Gohr $limit = 0; 2197f9cb794SAndreas Gohr if($this->range_end) { 2207f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2217f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2227f9cb794SAndreas Gohr } 2237f9cb794SAndreas Gohr $this->range_begin = $offset; 2247f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2257f9cb794SAndreas Gohr } 2267f9cb794SAndreas Gohr 2277f9cb794SAndreas Gohr /** 2287f9cb794SAndreas Gohr * Limit results to this number 2297f9cb794SAndreas Gohr * 2307f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2317f9cb794SAndreas Gohr */ 2327f9cb794SAndreas Gohr public function setLimit($limit) { 2337f9cb794SAndreas Gohr if($limit) { 2347f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2357f9cb794SAndreas Gohr } else { 2367f9cb794SAndreas Gohr $this->range_end = 0; 2377f9cb794SAndreas Gohr } 2387f9cb794SAndreas Gohr } 2397f9cb794SAndreas Gohr 2407f9cb794SAndreas Gohr /** 2417f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2427f9cb794SAndreas Gohr * 2437f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2447f9cb794SAndreas Gohr * 2457f9cb794SAndreas Gohr * @return int 2467f9cb794SAndreas Gohr */ 2477f9cb794SAndreas Gohr public function getCount() { 2487f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 2497f9cb794SAndreas Gohr return $this->count; 2507f9cb794SAndreas Gohr } 2517f9cb794SAndreas Gohr 252c46f5fb1SAndreas Gohr /** 253c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 254c46f5fb1SAndreas Gohr * 255c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 256c46f5fb1SAndreas Gohr * 257c46f5fb1SAndreas Gohr * @return \string[] 258c46f5fb1SAndreas Gohr */ 259d4b5a17cSAndreas Gohr public function getPids() { 260d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 261d4b5a17cSAndreas Gohr return $this->result_pids; 262d4b5a17cSAndreas Gohr } 263d4b5a17cSAndreas Gohr 2647f9cb794SAndreas Gohr /** 265b2ed727aSAndreas Gohr * Execute this search and return the result 266b2ed727aSAndreas Gohr * 26738fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2687f9cb794SAndreas Gohr * 2697f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2707f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 27138fa36fbSAndreas Gohr * 27238fa36fbSAndreas Gohr * @return Value[][] 273b2ed727aSAndreas Gohr */ 274b2ed727aSAndreas Gohr public function execute() { 275b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 276b2ed727aSAndreas Gohr 2777f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 278b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 27959b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 280b2ed727aSAndreas Gohr 281d4b5a17cSAndreas Gohr $this->result_pids = array(); 282b2ed727aSAndreas Gohr $result = array(); 2837f9cb794SAndreas Gohr $cursor = -1; 2845e4bfdb0SMichael Grosse $pageidAndRevOnly = array_reduce($this->columns, function ($pageidAndRevOnly, Column $col) { 2855e4bfdb0SMichael Grosse return $pageidAndRevOnly && ($col->getTid() == 0); 2865e4bfdb0SMichael Grosse }, true); 2877f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2887f9cb794SAndreas Gohr $cursor++; 2897f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2907f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2917f9cb794SAndreas Gohr 292b2ed727aSAndreas Gohr $C = 0; 293b2ed727aSAndreas Gohr $resrow = array(); 29499a70f28SAndreas Gohr $isempty = true; 295b2ed727aSAndreas Gohr foreach($this->columns as $col) { 29638fa36fbSAndreas Gohr $val = $row["C$C"]; 297b2ed727aSAndreas Gohr if($col->isMulti()) { 29838fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 299b2ed727aSAndreas Gohr } 30099a70f28SAndreas Gohr $value = new Value($col, $val); 3018cba7aa0SMichael Grosse $isempty &= $this->isEmptyValue($value); 30299a70f28SAndreas Gohr $resrow[] = $value; 303b2ed727aSAndreas Gohr $C++; 304b2ed727aSAndreas Gohr } 30599a70f28SAndreas Gohr 30699a70f28SAndreas Gohr // skip empty rows 3078cba7aa0SMichael Grosse if($isempty && !$pageidAndRevOnly) { 30899a70f28SAndreas Gohr $cursor--; 30999a70f28SAndreas Gohr continue; 31099a70f28SAndreas Gohr } 31199a70f28SAndreas Gohr 31299a70f28SAndreas Gohr $this->result_pids[] = $row['PID']; 313b2ed727aSAndreas Gohr $result[] = $resrow; 314b2ed727aSAndreas Gohr } 3157f9cb794SAndreas Gohr 3167f9cb794SAndreas Gohr $this->sqlite->res_close($res); 3177f9cb794SAndreas Gohr $this->count = $cursor + 1; 318b2ed727aSAndreas Gohr return $result; 319b2ed727aSAndreas Gohr } 320b2ed727aSAndreas Gohr 321b2ed727aSAndreas Gohr /** 32215929be2SAndreas Gohr * Transform the set search parameters into a statement 32315929be2SAndreas Gohr * 324b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 32515929be2SAndreas Gohr */ 32615929be2SAndreas Gohr public function getSQL() { 3275511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 32815929be2SAndreas Gohr 3292f68434dSAndreas Gohr $QB = new QueryBuilder(); 33015929be2SAndreas Gohr 3319d7a36f9SAndreas Gohr // basic tables 332b2d67e7dSAndreas Gohr $first_table = ''; 3339d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 3342f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 335b2d67e7dSAndreas Gohr if($first_table) { 3369d7a36f9SAndreas Gohr // follow up tables 3372f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 3389d7a36f9SAndreas Gohr } else { 3399d7a36f9SAndreas Gohr // first table 340b75e0a08SAndreas Gohr 34113c321dbSAndreas Gohr if(!$schema->isLookup()) { 34213c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 3432f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3442f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3452f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3462f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3472f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 34813c321dbSAndreas Gohr } 34913c321dbSAndreas Gohr 35013c321dbSAndreas Gohr $QB->addTable($datatable); 35113c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 35213c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 353b2d67e7dSAndreas Gohr 3542f68434dSAndreas Gohr $first_table = $datatable; 35515929be2SAndreas Gohr } 3562f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 3579d7a36f9SAndreas Gohr } 35815929be2SAndreas Gohr 35915929be2SAndreas Gohr // columns to select, handling multis 3609d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 36115929be2SAndreas Gohr $n = 0; 36215929be2SAndreas Gohr foreach($this->columns as $col) { 36315929be2SAndreas Gohr $CN = 'C' . $n++; 36415929be2SAndreas Gohr 365d1b04e89SAndreas Gohr if($col->isMulti()) { 3662f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3672f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3682f68434dSAndreas Gohr $MN = 'M' . $col->getColref(); 3692f68434dSAndreas Gohr 3702f68434dSAndreas Gohr $QB->addLeftJoin( 3712f68434dSAndreas Gohr $datatable, 3722f68434dSAndreas Gohr $multitable, 3732f68434dSAndreas Gohr $MN, 3742f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3752f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3762f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3772f68434dSAndreas Gohr ); 378578407b2SAndreas Gohr 379578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 380578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 381578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 38215929be2SAndreas Gohr } else { 383578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 3840dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 38515929be2SAndreas Gohr } 38615929be2SAndreas Gohr } 38715929be2SAndreas Gohr 3889d7a36f9SAndreas Gohr // where clauses 389b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 3904c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 391b8fe6730SAndreas Gohr 3922f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3932f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3949d7a36f9SAndreas Gohr 3959d7a36f9SAndreas Gohr /** @var $col Column */ 3969d7a36f9SAndreas Gohr if($col->isMulti()) { 3972f68434dSAndreas Gohr $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 39815929be2SAndreas Gohr 3992f68434dSAndreas Gohr $QB->addLeftJoin( 4002f68434dSAndreas Gohr $datatable, 4012f68434dSAndreas Gohr $multitable, 4022f68434dSAndreas Gohr $MN, 4032f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 4042f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 4052f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 4062f68434dSAndreas Gohr ); 407690e4ebaSAndreas Gohr $coltbl = $MN; 408690e4ebaSAndreas Gohr $colnam = 'value'; 4099d7a36f9SAndreas Gohr } else { 410690e4ebaSAndreas Gohr $coltbl = $datatable; 411690e4ebaSAndreas Gohr $colnam = $col->getColName(); 4129d7a36f9SAndreas Gohr } 41353528ecfSAndreas Gohr 414690e4ebaSAndreas Gohr $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 4159d7a36f9SAndreas Gohr } 4169d7a36f9SAndreas Gohr 41729394e6cSAndreas Gohr // sorting - we always sort by the single val column 418b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 419b8fe6730SAndreas Gohr list($col, $asc) = $sort; 4209d7a36f9SAndreas Gohr /** @var $col Column */ 4215a11127fSAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $col->getColName(false), $asc ? 'ASC' : 'DESC'); 4229e07bdbfSAndreas Gohr } 4239e07bdbfSAndreas Gohr 4242f68434dSAndreas Gohr return $QB->getSQL(); 42515929be2SAndreas Gohr } 42615929be2SAndreas Gohr 42715929be2SAndreas Gohr /** 42801dd90deSAndreas Gohr * Returns all the columns that where added to the search 42901dd90deSAndreas Gohr * 43001dd90deSAndreas Gohr * @return Column[] 43101dd90deSAndreas Gohr */ 43201dd90deSAndreas Gohr public function getColumns() { 43301dd90deSAndreas Gohr return $this->columns; 43401dd90deSAndreas Gohr } 43501dd90deSAndreas Gohr 436577b462bSAndreas Gohr /** 437577b462bSAndreas Gohr * Checks if the given column is a * wildcard 438577b462bSAndreas Gohr * 439577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 440577b462bSAndreas Gohr * nothing happens 441577b462bSAndreas Gohr * 442577b462bSAndreas Gohr * @param string $colname 443577b462bSAndreas Gohr * @return bool was wildcard? 444577b462bSAndreas Gohr */ 445577b462bSAndreas Gohr protected function processWildcard($colname) { 446636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 447577b462bSAndreas Gohr if($colname !== '*') return false; 448577b462bSAndreas Gohr 449577b462bSAndreas Gohr // no table given? assume the first is meant 450577b462bSAndreas Gohr if($table === null) { 451577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 452577b462bSAndreas Gohr $table = $schema_list[0]; 453577b462bSAndreas Gohr } 454577b462bSAndreas Gohr 455577b462bSAndreas Gohr $schema = $this->schemas[$table]; 456577b462bSAndreas Gohr if(!$schema) return false; 457577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 458577b462bSAndreas Gohr return true; 459577b462bSAndreas Gohr } 460577b462bSAndreas Gohr 461577b462bSAndreas Gohr /** 462577b462bSAndreas Gohr * Split a given column name into table and column 463577b462bSAndreas Gohr * 464577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 465577b462bSAndreas Gohr * 466577b462bSAndreas Gohr * @param $colname 467636a5173SAndreas Gohr * @return array (colname, table) 468577b462bSAndreas Gohr */ 469577b462bSAndreas Gohr protected function resolveColumn($colname) { 470577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 471577b462bSAndreas Gohr 472577b462bSAndreas Gohr // resolve the alias or table name 473577b462bSAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 474577b462bSAndreas Gohr if(!$colname) { 475577b462bSAndreas Gohr $colname = $table; 476577b462bSAndreas Gohr $table = null; 477577b462bSAndreas Gohr } 478577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 479577b462bSAndreas Gohr $table = $this->aliases[$table]; 480577b462bSAndreas Gohr } 481577b462bSAndreas Gohr 482577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 483577b462bSAndreas Gohr 484577b462bSAndreas Gohr return array($colname, $table); 485577b462bSAndreas Gohr } 48601dd90deSAndreas Gohr 48701dd90deSAndreas Gohr /** 48815929be2SAndreas Gohr * Find a column to be used in the search 48915929be2SAndreas Gohr * 49015929be2SAndreas Gohr * @param string $colname may contain an alias 49115929be2SAndreas Gohr * @return bool|Column 49215929be2SAndreas Gohr */ 49300f6af48SAndreas Gohr public function findColumn($colname) { 4945511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 495df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 496f107f479SAndreas Gohr 497f107f479SAndreas Gohr // add "fake" column for special col 498f107f479SAndreas Gohr if(!(reset($this->schemas)->isLookup())) { 499128d4e83SAndreas Gohr if($colname == '%pageid%') { 500128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 501d1b04e89SAndreas Gohr } 502128d4e83SAndreas Gohr if($colname == '%title%') { 503128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 504128d4e83SAndreas Gohr } 505cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 506cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 507cadfc3ccSAndreas Gohr } 508f107f479SAndreas Gohr } else { 509f107f479SAndreas Gohr if($colname == '%rowid%') { 510f107f479SAndreas Gohr return new RowColumn(0, new Decimal(), $schema_list[0]); 511f107f479SAndreas Gohr } 512f747a930SAndreas Gohr } 513d1b04e89SAndreas Gohr 514636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 51515929be2SAndreas Gohr 516bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 517577b462bSAndreas Gohr if($table !== null) { 51815929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 51915929be2SAndreas Gohr } else { 52015929be2SAndreas Gohr $schemas = $this->schemas; 52115929be2SAndreas Gohr } 52215929be2SAndreas Gohr 52315929be2SAndreas Gohr // find it 52415929be2SAndreas Gohr $col = false; 52515929be2SAndreas Gohr foreach($schemas as $schema) { 5264ac44d1cSMichael Grosse if(empty($schema)) { 5274ac44d1cSMichael Grosse continue; 5284ac44d1cSMichael Grosse } 52915929be2SAndreas Gohr $col = $schema->findColumn($colname); 53015929be2SAndreas Gohr if($col) break; 53115929be2SAndreas Gohr } 53215929be2SAndreas Gohr 53315929be2SAndreas Gohr return $col; 53415929be2SAndreas Gohr } 53515929be2SAndreas Gohr 5369dbc8ec0SMichael Grosse /** 53799a70f28SAndreas Gohr * Check if the given row is empty or references our own row 5389dbc8ec0SMichael Grosse * 53999a70f28SAndreas Gohr * @param Value $value 5409dbc8ec0SMichael Grosse * @return bool 5419dbc8ec0SMichael Grosse */ 5428cba7aa0SMichael Grosse protected function isEmptyValue(Value $value) { 54399a70f28SAndreas Gohr if ($value->isEmpty()) return true; 5448cba7aa0SMichael Grosse if ($value->getColumn()->getTid() == 0) return true; 5459dbc8ec0SMichael Grosse return false; 5469dbc8ec0SMichael Grosse } 54715929be2SAndreas Gohr} 54815929be2SAndreas Gohr 5495511bd5bSAndreas Gohr 550