115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 415929be2SAndreas Gohr 5cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\DateTime; 6ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Page; 70561158fSAndreas Gohr 815929be2SAndreas Gohrclass Search { 99d7a36f9SAndreas Gohr /** 109d7a36f9SAndreas Gohr * This separator will be used to concat multi values to flatten them in the result set 119d7a36f9SAndreas Gohr */ 129d7a36f9SAndreas Gohr const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n"; 139d7a36f9SAndreas Gohr 145511bd5bSAndreas Gohr /** 155511bd5bSAndreas Gohr * The list of known and allowed comparators 162e7595e7SAndreas Gohr * (order matters) 175511bd5bSAndreas Gohr */ 1874461852SAndreas Gohr static public $COMPARATORS = array( 192e7595e7SAndreas Gohr '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~', 205511bd5bSAndreas Gohr ); 215511bd5bSAndreas Gohr 229d7a36f9SAndreas Gohr /** @var \helper_plugin_sqlite */ 239d7a36f9SAndreas Gohr protected $sqlite; 2415929be2SAndreas Gohr 2515929be2SAndreas Gohr /** @var Schema[] list of schemas to query */ 2615929be2SAndreas Gohr protected $schemas = array(); 2715929be2SAndreas Gohr 2815929be2SAndreas Gohr /** @var Column[] list of columns to select */ 2915929be2SAndreas Gohr protected $columns = array(); 3015929be2SAndreas Gohr 3115929be2SAndreas Gohr /** @var array the sorting of the result */ 3215929be2SAndreas Gohr protected $sortby = array(); 3315929be2SAndreas Gohr 349d7a36f9SAndreas Gohr /** @var array the filters */ 359d7a36f9SAndreas Gohr protected $filter = array(); 3615929be2SAndreas Gohr 3715929be2SAndreas Gohr /** @var array list of aliases tables can be referenced by */ 3815929be2SAndreas Gohr protected $aliases = array(); 3915929be2SAndreas Gohr 407f9cb794SAndreas Gohr /** @var int begin results from here */ 417f9cb794SAndreas Gohr protected $range_begin = 0; 427f9cb794SAndreas Gohr 437f9cb794SAndreas Gohr /** @var int end results here */ 447f9cb794SAndreas Gohr protected $range_end = 0; 457f9cb794SAndreas Gohr 467f9cb794SAndreas Gohr /** @var int the number of results */ 477f9cb794SAndreas Gohr protected $count = -1; 48d4b5a17cSAndreas Gohr /** @var string[] the PIDs of the result rows */ 49d4b5a17cSAndreas Gohr protected $result_pids = null; 507f9cb794SAndreas Gohr 5115929be2SAndreas Gohr /** 529d7a36f9SAndreas Gohr * Search constructor. 539d7a36f9SAndreas Gohr */ 549d7a36f9SAndreas Gohr public function __construct() { 559d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 569d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 579d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 589d7a36f9SAndreas Gohr } 599d7a36f9SAndreas Gohr 609d7a36f9SAndreas Gohr /** 6115929be2SAndreas Gohr * Add a schema to be searched 6215929be2SAndreas Gohr * 6315929be2SAndreas Gohr * Call multiple times for multiple schemas. 6415929be2SAndreas Gohr * 6515929be2SAndreas Gohr * @param string $table 6615929be2SAndreas Gohr * @param string $alias 6715929be2SAndreas Gohr */ 6815929be2SAndreas Gohr public function addSchema($table, $alias = '') { 692be187cdSAndreas Gohr $schema = new Schema($table); 702be187cdSAndreas Gohr if(!$schema->getId()) { 712be187cdSAndreas Gohr throw new StructException('schema missing', $table); 722be187cdSAndreas Gohr } 732be187cdSAndreas Gohr 742be187cdSAndreas Gohr if($this->schemas && 752be187cdSAndreas Gohr ( 762be187cdSAndreas Gohr $schema->isLookup() || 772be187cdSAndreas Gohr reset($this->schemas)->isLookup() 782be187cdSAndreas Gohr ) 792be187cdSAndreas Gohr ) { 802be187cdSAndreas Gohr throw new StructException('nolookupmix'); 812be187cdSAndreas Gohr } 822be187cdSAndreas Gohr 832be187cdSAndreas Gohr $this->schemas[$table] = $schema; 8415929be2SAndreas Gohr if($alias) $this->aliases[$alias] = $table; 8515929be2SAndreas Gohr } 8615929be2SAndreas Gohr 8715929be2SAndreas Gohr /** 8815929be2SAndreas Gohr * Add a column to be returned by the search 8915929be2SAndreas Gohr * 9015929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9115929be2SAndreas Gohr * added before 9215929be2SAndreas Gohr * 9315929be2SAndreas Gohr * @param string $colname may contain an alias 9415929be2SAndreas Gohr */ 9515929be2SAndreas Gohr public function addColumn($colname) { 96577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 9715929be2SAndreas Gohr $col = $this->findColumn($colname); 9815929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 9915929be2SAndreas Gohr $this->columns[] = $col; 10015929be2SAndreas Gohr } 10115929be2SAndreas Gohr 10215929be2SAndreas Gohr /** 10315929be2SAndreas Gohr * Add sorting options 10415929be2SAndreas Gohr * 10515929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 10615929be2SAndreas Gohr * added before 10715929be2SAndreas Gohr * 10815929be2SAndreas Gohr * @param string $colname may contain an alias 10915929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 11015929be2SAndreas Gohr */ 11115929be2SAndreas Gohr public function addSort($colname, $asc = true) { 11215929be2SAndreas Gohr $col = $this->findColumn($colname); 11315929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 11415929be2SAndreas Gohr 11507993756SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc); 11607993756SAndreas Gohr } 11707993756SAndreas Gohr 11807993756SAndreas Gohr /** 11907993756SAndreas Gohr * Returns all set sort columns 12007993756SAndreas Gohr * 12107993756SAndreas Gohr * @return array 12207993756SAndreas Gohr */ 12307993756SAndreas Gohr public function getSorts() { 12407993756SAndreas Gohr return $this->sortby; 12515929be2SAndreas Gohr } 12615929be2SAndreas Gohr 12715929be2SAndreas Gohr /** 1289d7a36f9SAndreas Gohr * Adds a filter 12915929be2SAndreas Gohr * 13015929be2SAndreas Gohr * @param string $colname may contain an alias 13153528ecfSAndreas Gohr * @param string|string[] $value 1325511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1334c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 13415929be2SAndreas Gohr */ 1354c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1369dbc32aeSAndreas Gohr /* Convert certain filters into others 1379dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1389dbc32aeSAndreas Gohr if($comp == '*~') { 13953528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1409dbc32aeSAndreas Gohr $comp = '~'; 1419dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1429dbc32aeSAndreas Gohr $comp = '!='; 1439dbc32aeSAndreas Gohr } 1449dbc32aeSAndreas Gohr 14574461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1464c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1479d7a36f9SAndreas Gohr 14815929be2SAndreas Gohr $col = $this->findColumn($colname); 1497da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1507da1a0fdSAndreas Gohr 1517da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1527da1a0fdSAndreas Gohr switch($comp) { 1537da1a0fdSAndreas Gohr case '~': 1547da1a0fdSAndreas Gohr $comp = 'LIKE'; 1557da1a0fdSAndreas Gohr break; 1567da1a0fdSAndreas Gohr case '!~': 1577da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1587da1a0fdSAndreas Gohr break; 1597da1a0fdSAndreas Gohr case '=*': 1607da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1617da1a0fdSAndreas Gohr break; 1627da1a0fdSAndreas Gohr } 1637da1a0fdSAndreas Gohr 1647da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1657da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 16653528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1677da1a0fdSAndreas Gohr } 1687da1a0fdSAndreas Gohr 1697da1a0fdSAndreas Gohr // add the filter 1704c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 17115929be2SAndreas Gohr } 17215929be2SAndreas Gohr 17315929be2SAndreas Gohr /** 17453528ecfSAndreas Gohr * Wrap given value in asterisks 17553528ecfSAndreas Gohr * 17653528ecfSAndreas Gohr * @param string|string[] $value 17753528ecfSAndreas Gohr * @return string|string[] 17853528ecfSAndreas Gohr */ 17953528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 18053528ecfSAndreas Gohr $map = function ($input) { 18153528ecfSAndreas Gohr return "*$input*"; 18253528ecfSAndreas Gohr }; 18353528ecfSAndreas Gohr 18453528ecfSAndreas Gohr if(is_array($value)) { 18553528ecfSAndreas Gohr $value = array_map($map, $value); 18653528ecfSAndreas Gohr } else { 18753528ecfSAndreas Gohr $value = $map($value); 18853528ecfSAndreas Gohr } 18953528ecfSAndreas Gohr return $value; 19053528ecfSAndreas Gohr } 19153528ecfSAndreas Gohr 19253528ecfSAndreas Gohr /** 19353528ecfSAndreas Gohr * Change given string to use % instead of * 19453528ecfSAndreas Gohr * 19553528ecfSAndreas Gohr * @param string|string[] $value 19653528ecfSAndreas Gohr * @return string|string[] 19753528ecfSAndreas Gohr */ 19853528ecfSAndreas Gohr protected function filterChangeToLike($value) { 19953528ecfSAndreas Gohr $map = function ($input) { 20053528ecfSAndreas Gohr return str_replace('*', '%', $input); 20153528ecfSAndreas Gohr }; 20253528ecfSAndreas Gohr 20353528ecfSAndreas Gohr if(is_array($value)) { 20453528ecfSAndreas Gohr $value = array_map($map, $value); 20553528ecfSAndreas Gohr } else { 20653528ecfSAndreas Gohr $value = $map($value); 20753528ecfSAndreas Gohr } 20853528ecfSAndreas Gohr return $value; 20953528ecfSAndreas Gohr } 21053528ecfSAndreas Gohr 21153528ecfSAndreas Gohr /** 2127f9cb794SAndreas Gohr * Set offset for the results 2137f9cb794SAndreas Gohr * 2147f9cb794SAndreas Gohr * @param int $offset 2157f9cb794SAndreas Gohr */ 2167f9cb794SAndreas Gohr public function setOffset($offset) { 2177f9cb794SAndreas Gohr $limit = 0; 2187f9cb794SAndreas Gohr if($this->range_end) { 2197f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2207f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2217f9cb794SAndreas Gohr } 2227f9cb794SAndreas Gohr $this->range_begin = $offset; 2237f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2247f9cb794SAndreas Gohr } 2257f9cb794SAndreas Gohr 2267f9cb794SAndreas Gohr /** 2277f9cb794SAndreas Gohr * Limit results to this number 2287f9cb794SAndreas Gohr * 2297f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2307f9cb794SAndreas Gohr */ 2317f9cb794SAndreas Gohr public function setLimit($limit) { 2327f9cb794SAndreas Gohr if($limit) { 2337f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2347f9cb794SAndreas Gohr } else { 2357f9cb794SAndreas Gohr $this->range_end = 0; 2367f9cb794SAndreas Gohr } 2377f9cb794SAndreas Gohr } 2387f9cb794SAndreas Gohr 2397f9cb794SAndreas Gohr /** 2407f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2417f9cb794SAndreas Gohr * 2427f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2437f9cb794SAndreas Gohr * 2447f9cb794SAndreas Gohr * @return int 2457f9cb794SAndreas Gohr */ 2467f9cb794SAndreas Gohr public function getCount() { 2477f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 2487f9cb794SAndreas Gohr return $this->count; 2497f9cb794SAndreas Gohr } 2507f9cb794SAndreas Gohr 251c46f5fb1SAndreas Gohr /** 252c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 253c46f5fb1SAndreas Gohr * 254c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 255c46f5fb1SAndreas Gohr * 256c46f5fb1SAndreas Gohr * @return \string[] 257c46f5fb1SAndreas Gohr */ 258d4b5a17cSAndreas Gohr public function getPids() { 259d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 260d4b5a17cSAndreas Gohr return $this->result_pids; 261d4b5a17cSAndreas Gohr } 262d4b5a17cSAndreas Gohr 2637f9cb794SAndreas Gohr /** 264b2ed727aSAndreas Gohr * Execute this search and return the result 265b2ed727aSAndreas Gohr * 26638fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2677f9cb794SAndreas Gohr * 2687f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2697f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 27038fa36fbSAndreas Gohr * 27138fa36fbSAndreas Gohr * @return Value[][] 272b2ed727aSAndreas Gohr */ 273b2ed727aSAndreas Gohr public function execute() { 274b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 275b2ed727aSAndreas Gohr 2767f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 277b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 27859b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 279b2ed727aSAndreas Gohr 280d4b5a17cSAndreas Gohr $this->result_pids = array(); 281b2ed727aSAndreas Gohr $result = array(); 2827f9cb794SAndreas Gohr $cursor = -1; 2837f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2849dbc8ec0SMichael Grosse if($this->isRowEmpty($row)) { 2859dbc8ec0SMichael Grosse continue; 2869dbc8ec0SMichael Grosse } 2877f9cb794SAndreas Gohr $cursor++; 2887f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2897f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2907f9cb794SAndreas Gohr 291d4b5a17cSAndreas Gohr $this->result_pids[] = $row['PID']; 292d4b5a17cSAndreas Gohr 293b2ed727aSAndreas Gohr $C = 0; 294b2ed727aSAndreas Gohr $resrow = array(); 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 } 30038fa36fbSAndreas Gohr $resrow[] = new Value($col, $val); 301b2ed727aSAndreas Gohr $C++; 302b2ed727aSAndreas Gohr } 303b2ed727aSAndreas Gohr $result[] = $resrow; 304b2ed727aSAndreas Gohr } 3057f9cb794SAndreas Gohr 3067f9cb794SAndreas Gohr $this->sqlite->res_close($res); 3077f9cb794SAndreas Gohr $this->count = $cursor + 1; 308b2ed727aSAndreas Gohr return $result; 309b2ed727aSAndreas Gohr } 310b2ed727aSAndreas Gohr 311b2ed727aSAndreas Gohr /** 31215929be2SAndreas Gohr * Transform the set search parameters into a statement 31315929be2SAndreas Gohr * 314b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 31515929be2SAndreas Gohr */ 31615929be2SAndreas Gohr public function getSQL() { 3175511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 31815929be2SAndreas Gohr 3192f68434dSAndreas Gohr $QB = new QueryBuilder(); 32015929be2SAndreas Gohr 3219d7a36f9SAndreas Gohr // basic tables 322b2d67e7dSAndreas Gohr $first_table = ''; 3239d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 3242f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 325b2d67e7dSAndreas Gohr if($first_table) { 3269d7a36f9SAndreas Gohr // follow up tables 3272f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 3289d7a36f9SAndreas Gohr } else { 3299d7a36f9SAndreas Gohr // first table 330b75e0a08SAndreas Gohr 33113c321dbSAndreas Gohr if(!$schema->isLookup()) { 33213c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 3332f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3342f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3352f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3362f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3372f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 33813c321dbSAndreas Gohr } 33913c321dbSAndreas Gohr 34013c321dbSAndreas Gohr $QB->addTable($datatable); 34113c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 34213c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 343b2d67e7dSAndreas Gohr 3442f68434dSAndreas Gohr $first_table = $datatable; 34515929be2SAndreas Gohr } 3462f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 3479d7a36f9SAndreas Gohr } 34815929be2SAndreas Gohr 34915929be2SAndreas Gohr // columns to select, handling multis 3509d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 35115929be2SAndreas Gohr $n = 0; 35215929be2SAndreas Gohr foreach($this->columns as $col) { 35315929be2SAndreas Gohr $CN = 'C' . $n++; 35415929be2SAndreas Gohr 355d1b04e89SAndreas Gohr if($col->isMulti()) { 3562f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3572f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3582f68434dSAndreas Gohr $MN = 'M' . $col->getColref(); 3592f68434dSAndreas Gohr 3602f68434dSAndreas Gohr $QB->addLeftJoin( 3612f68434dSAndreas Gohr $datatable, 3622f68434dSAndreas Gohr $multitable, 3632f68434dSAndreas Gohr $MN, 3642f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3652f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3662f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3672f68434dSAndreas Gohr ); 368578407b2SAndreas Gohr 369578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 370578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 371578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 37215929be2SAndreas Gohr } else { 373578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 3740dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 37515929be2SAndreas Gohr } 37615929be2SAndreas Gohr } 37715929be2SAndreas Gohr 3789d7a36f9SAndreas Gohr // where clauses 379b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 3804c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 381b8fe6730SAndreas Gohr 3822f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3832f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3849d7a36f9SAndreas Gohr 3859d7a36f9SAndreas Gohr /** @var $col Column */ 3869d7a36f9SAndreas Gohr if($col->isMulti()) { 3872f68434dSAndreas Gohr $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 38815929be2SAndreas Gohr 3892f68434dSAndreas Gohr $QB->addLeftJoin( 3902f68434dSAndreas Gohr $datatable, 3912f68434dSAndreas Gohr $multitable, 3922f68434dSAndreas Gohr $MN, 3932f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3942f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3952f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3962f68434dSAndreas Gohr ); 397690e4ebaSAndreas Gohr $coltbl = $MN; 398690e4ebaSAndreas Gohr $colnam = 'value'; 3999d7a36f9SAndreas Gohr } else { 400690e4ebaSAndreas Gohr $coltbl = $datatable; 401690e4ebaSAndreas Gohr $colnam = $col->getColName(); 4029d7a36f9SAndreas Gohr } 40353528ecfSAndreas Gohr 404690e4ebaSAndreas Gohr $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 4059d7a36f9SAndreas Gohr } 4069d7a36f9SAndreas Gohr 40729394e6cSAndreas Gohr // sorting - we always sort by the single val column 408b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 409b8fe6730SAndreas Gohr list($col, $asc) = $sort; 4109d7a36f9SAndreas Gohr /** @var $col Column */ 4115a11127fSAndreas Gohr $col->getType()->sort($QB, 'data_' . $col->getTable(), $col->getColName(false), $asc ? 'ASC' : 'DESC'); 4129e07bdbfSAndreas Gohr } 4139e07bdbfSAndreas Gohr 4142f68434dSAndreas Gohr return $QB->getSQL(); 41515929be2SAndreas Gohr } 41615929be2SAndreas Gohr 41715929be2SAndreas Gohr /** 41801dd90deSAndreas Gohr * Returns all the columns that where added to the search 41901dd90deSAndreas Gohr * 42001dd90deSAndreas Gohr * @return Column[] 42101dd90deSAndreas Gohr */ 42201dd90deSAndreas Gohr public function getColumns() { 42301dd90deSAndreas Gohr return $this->columns; 42401dd90deSAndreas Gohr } 42501dd90deSAndreas Gohr 426577b462bSAndreas Gohr /** 427577b462bSAndreas Gohr * Checks if the given column is a * wildcard 428577b462bSAndreas Gohr * 429577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 430577b462bSAndreas Gohr * nothing happens 431577b462bSAndreas Gohr * 432577b462bSAndreas Gohr * @param string $colname 433577b462bSAndreas Gohr * @return bool was wildcard? 434577b462bSAndreas Gohr */ 435577b462bSAndreas Gohr protected function processWildcard($colname) { 436636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 437577b462bSAndreas Gohr if($colname !== '*') return false; 438577b462bSAndreas Gohr 439577b462bSAndreas Gohr // no table given? assume the first is meant 440577b462bSAndreas Gohr if($table === null) { 441577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 442577b462bSAndreas Gohr $table = $schema_list[0]; 443577b462bSAndreas Gohr } 444577b462bSAndreas Gohr 445577b462bSAndreas Gohr $schema = $this->schemas[$table]; 446577b462bSAndreas Gohr if(!$schema) return false; 447577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 448577b462bSAndreas Gohr return true; 449577b462bSAndreas Gohr } 450577b462bSAndreas Gohr 451577b462bSAndreas Gohr /** 452577b462bSAndreas Gohr * Split a given column name into table and column 453577b462bSAndreas Gohr * 454577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 455577b462bSAndreas Gohr * 456577b462bSAndreas Gohr * @param $colname 457636a5173SAndreas Gohr * @return array (colname, table) 458577b462bSAndreas Gohr */ 459577b462bSAndreas Gohr protected function resolveColumn($colname) { 460577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 461577b462bSAndreas Gohr 462577b462bSAndreas Gohr // resolve the alias or table name 463577b462bSAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 464577b462bSAndreas Gohr if(!$colname) { 465577b462bSAndreas Gohr $colname = $table; 466577b462bSAndreas Gohr $table = null; 467577b462bSAndreas Gohr } 468577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 469577b462bSAndreas Gohr $table = $this->aliases[$table]; 470577b462bSAndreas Gohr } 471577b462bSAndreas Gohr 472577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 473577b462bSAndreas Gohr 474577b462bSAndreas Gohr return array($colname, $table); 475577b462bSAndreas Gohr } 47601dd90deSAndreas Gohr 47701dd90deSAndreas Gohr /** 47815929be2SAndreas Gohr * Find a column to be used in the search 47915929be2SAndreas Gohr * 48015929be2SAndreas Gohr * @param string $colname may contain an alias 48115929be2SAndreas Gohr * @return bool|Column 48215929be2SAndreas Gohr */ 48300f6af48SAndreas Gohr public function findColumn($colname) { 4845511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 48515929be2SAndreas Gohr 486*f747a930SAndreas Gohr // add "fake" column for special col (unless it's a lookup) 487*f747a930SAndreas Gohr if(!(reset($this->schemas)->isLookup())) { 488df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 489128d4e83SAndreas Gohr if($colname == '%pageid%') { 490128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 491d1b04e89SAndreas Gohr } 492128d4e83SAndreas Gohr if($colname == '%title%') { 493128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 494128d4e83SAndreas Gohr } 495cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 496cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 497cadfc3ccSAndreas Gohr } 498*f747a930SAndreas Gohr } 499d1b04e89SAndreas Gohr 500636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 50115929be2SAndreas Gohr 502bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 503577b462bSAndreas Gohr if($table !== null) { 50415929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 50515929be2SAndreas Gohr } else { 50615929be2SAndreas Gohr $schemas = $this->schemas; 50715929be2SAndreas Gohr } 50815929be2SAndreas Gohr 50915929be2SAndreas Gohr // find it 51015929be2SAndreas Gohr $col = false; 51115929be2SAndreas Gohr foreach($schemas as $schema) { 5124ac44d1cSMichael Grosse if(empty($schema)) { 5134ac44d1cSMichael Grosse continue; 5144ac44d1cSMichael Grosse } 51515929be2SAndreas Gohr $col = $schema->findColumn($colname); 51615929be2SAndreas Gohr if($col) break; 51715929be2SAndreas Gohr } 51815929be2SAndreas Gohr 51915929be2SAndreas Gohr return $col; 52015929be2SAndreas Gohr } 52115929be2SAndreas Gohr 5229dbc8ec0SMichael Grosse /** 5239dbc8ec0SMichael Grosse * Check if a row is empty / only contains a reference to itself 5249dbc8ec0SMichael Grosse * 5259dbc8ec0SMichael Grosse * @param array $rowColumns an array as returned from the database 5269dbc8ec0SMichael Grosse * @return bool 5279dbc8ec0SMichael Grosse */ 5289dbc8ec0SMichael Grosse private function isRowEmpty($rowColumns) { 5299dbc8ec0SMichael Grosse $C = 0; 5309dbc8ec0SMichael Grosse foreach($this->columns as $col) { 5319dbc8ec0SMichael Grosse $val = $rowColumns["C$C"]; 5329dbc8ec0SMichael Grosse $C += 1; 533c9494930SMichael Grosse if(blank($val) || is_a($col->getType(), 'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) { 5349dbc8ec0SMichael Grosse continue; 5359dbc8ec0SMichael Grosse } 5369dbc8ec0SMichael Grosse return false; 5379dbc8ec0SMichael Grosse } 5389dbc8ec0SMichael Grosse return true; 5399dbc8ec0SMichael Grosse } 5409dbc8ec0SMichael Grosse 54115929be2SAndreas Gohr} 54215929be2SAndreas Gohr 5435511bd5bSAndreas Gohr 544