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 = '') { 6915929be2SAndreas Gohr $this->schemas[$table] = new Schema($table); 7015929be2SAndreas Gohr if($alias) $this->aliases[$alias] = $table; 7115929be2SAndreas Gohr } 7215929be2SAndreas Gohr 7315929be2SAndreas Gohr /** 7415929be2SAndreas Gohr * Add a column to be returned by the search 7515929be2SAndreas Gohr * 7615929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 7715929be2SAndreas Gohr * added before 7815929be2SAndreas Gohr * 7915929be2SAndreas Gohr * @param string $colname may contain an alias 8015929be2SAndreas Gohr */ 8115929be2SAndreas Gohr public function addColumn($colname) { 82577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 8315929be2SAndreas Gohr $col = $this->findColumn($colname); 8415929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 8515929be2SAndreas Gohr $this->columns[] = $col; 8615929be2SAndreas Gohr } 8715929be2SAndreas Gohr 8815929be2SAndreas Gohr /** 8915929be2SAndreas Gohr * Add sorting options 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 * @param bool $asc sort direction (ASC = true, DESC = false) 9615929be2SAndreas Gohr */ 9715929be2SAndreas Gohr public function addSort($colname, $asc = true) { 9815929be2SAndreas Gohr $col = $this->findColumn($colname); 9915929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 10015929be2SAndreas Gohr 10107993756SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc); 10207993756SAndreas Gohr } 10307993756SAndreas Gohr 10407993756SAndreas Gohr /** 10507993756SAndreas Gohr * Returns all set sort columns 10607993756SAndreas Gohr * 10707993756SAndreas Gohr * @return array 10807993756SAndreas Gohr */ 10907993756SAndreas Gohr public function getSorts() { 11007993756SAndreas Gohr return $this->sortby; 11115929be2SAndreas Gohr } 11215929be2SAndreas Gohr 11315929be2SAndreas Gohr /** 1149d7a36f9SAndreas Gohr * Adds a filter 11515929be2SAndreas Gohr * 11615929be2SAndreas Gohr * @param string $colname may contain an alias 11753528ecfSAndreas Gohr * @param string|string[] $value 1185511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1194c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 12015929be2SAndreas Gohr */ 1214c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1229dbc32aeSAndreas Gohr /* Convert certain filters into others 1239dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1249dbc32aeSAndreas Gohr if($comp == '*~') { 12553528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1269dbc32aeSAndreas Gohr $comp = '~'; 1279dbc32aeSAndreas Gohr } elseif($comp == '<>') { 1289dbc32aeSAndreas Gohr $comp = '!='; 1299dbc32aeSAndreas Gohr } 1309dbc32aeSAndreas Gohr 13174461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1324c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1339d7a36f9SAndreas Gohr 13415929be2SAndreas Gohr $col = $this->findColumn($colname); 1357da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1367da1a0fdSAndreas Gohr 1377da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1387da1a0fdSAndreas Gohr switch($comp) { 1397da1a0fdSAndreas Gohr case '~': 1407da1a0fdSAndreas Gohr $comp = 'LIKE'; 1417da1a0fdSAndreas Gohr break; 1427da1a0fdSAndreas Gohr case '!~': 1437da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1447da1a0fdSAndreas Gohr break; 1457da1a0fdSAndreas Gohr case '=*': 1467da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1477da1a0fdSAndreas Gohr break; 1487da1a0fdSAndreas Gohr } 1497da1a0fdSAndreas Gohr 1507da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1517da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 15253528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1537da1a0fdSAndreas Gohr } 1547da1a0fdSAndreas Gohr 1557da1a0fdSAndreas Gohr // add the filter 1564c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 15715929be2SAndreas Gohr } 15815929be2SAndreas Gohr 15915929be2SAndreas Gohr /** 16053528ecfSAndreas Gohr * Wrap given value in asterisks 16153528ecfSAndreas Gohr * 16253528ecfSAndreas Gohr * @param string|string[] $value 16353528ecfSAndreas Gohr * @return string|string[] 16453528ecfSAndreas Gohr */ 16553528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 16653528ecfSAndreas Gohr $map = function ($input) { 16753528ecfSAndreas Gohr return "*$input*"; 16853528ecfSAndreas Gohr }; 16953528ecfSAndreas Gohr 17053528ecfSAndreas Gohr if(is_array($value)) { 17153528ecfSAndreas Gohr $value = array_map($map, $value); 17253528ecfSAndreas Gohr } else { 17353528ecfSAndreas Gohr $value = $map($value); 17453528ecfSAndreas Gohr } 17553528ecfSAndreas Gohr return $value; 17653528ecfSAndreas Gohr } 17753528ecfSAndreas Gohr 17853528ecfSAndreas Gohr /** 17953528ecfSAndreas Gohr * Change given string to use % instead of * 18053528ecfSAndreas Gohr * 18153528ecfSAndreas Gohr * @param string|string[] $value 18253528ecfSAndreas Gohr * @return string|string[] 18353528ecfSAndreas Gohr */ 18453528ecfSAndreas Gohr protected function filterChangeToLike($value) { 18553528ecfSAndreas Gohr $map = function ($input) { 18653528ecfSAndreas Gohr return str_replace('*', '%', $input); 18753528ecfSAndreas Gohr }; 18853528ecfSAndreas Gohr 18953528ecfSAndreas Gohr if(is_array($value)) { 19053528ecfSAndreas Gohr $value = array_map($map, $value); 19153528ecfSAndreas Gohr } else { 19253528ecfSAndreas Gohr $value = $map($value); 19353528ecfSAndreas Gohr } 19453528ecfSAndreas Gohr return $value; 19553528ecfSAndreas Gohr } 19653528ecfSAndreas Gohr 19753528ecfSAndreas Gohr /** 1987f9cb794SAndreas Gohr * Set offset for the results 1997f9cb794SAndreas Gohr * 2007f9cb794SAndreas Gohr * @param int $offset 2017f9cb794SAndreas Gohr */ 2027f9cb794SAndreas Gohr public function setOffset($offset) { 2037f9cb794SAndreas Gohr $limit = 0; 2047f9cb794SAndreas Gohr if($this->range_end) { 2057f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 2067f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 2077f9cb794SAndreas Gohr } 2087f9cb794SAndreas Gohr $this->range_begin = $offset; 2097f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 2107f9cb794SAndreas Gohr } 2117f9cb794SAndreas Gohr 2127f9cb794SAndreas Gohr /** 2137f9cb794SAndreas Gohr * Limit results to this number 2147f9cb794SAndreas Gohr * 2157f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 2167f9cb794SAndreas Gohr */ 2177f9cb794SAndreas Gohr public function setLimit($limit) { 2187f9cb794SAndreas Gohr if($limit) { 2197f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 2207f9cb794SAndreas Gohr } else { 2217f9cb794SAndreas Gohr $this->range_end = 0; 2227f9cb794SAndreas Gohr } 2237f9cb794SAndreas Gohr } 2247f9cb794SAndreas Gohr 2257f9cb794SAndreas Gohr /** 2267f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 2277f9cb794SAndreas Gohr * 2287f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 2297f9cb794SAndreas Gohr * 2307f9cb794SAndreas Gohr * @return int 2317f9cb794SAndreas Gohr */ 2327f9cb794SAndreas Gohr public function getCount() { 2337f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 2347f9cb794SAndreas Gohr return $this->count; 2357f9cb794SAndreas Gohr } 2367f9cb794SAndreas Gohr 237c46f5fb1SAndreas Gohr /** 238c46f5fb1SAndreas Gohr * Returns the PID associated with each result row 239c46f5fb1SAndreas Gohr * 240c46f5fb1SAndreas Gohr * Important: this may only be called after running @see execute() 241c46f5fb1SAndreas Gohr * 242c46f5fb1SAndreas Gohr * @return \string[] 243c46f5fb1SAndreas Gohr */ 244d4b5a17cSAndreas Gohr public function getPids() { 245d4b5a17cSAndreas Gohr if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search'); 246d4b5a17cSAndreas Gohr return $this->result_pids; 247d4b5a17cSAndreas Gohr } 248d4b5a17cSAndreas Gohr 2497f9cb794SAndreas Gohr /** 250b2ed727aSAndreas Gohr * Execute this search and return the result 251b2ed727aSAndreas Gohr * 25238fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2537f9cb794SAndreas Gohr * 2547f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2557f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 25638fa36fbSAndreas Gohr * 25738fa36fbSAndreas Gohr * @return Value[][] 258b2ed727aSAndreas Gohr */ 259b2ed727aSAndreas Gohr public function execute() { 260b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 261b2ed727aSAndreas Gohr 2627f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 263b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 26459b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 265b2ed727aSAndreas Gohr 266d4b5a17cSAndreas Gohr $this->result_pids = array(); 267b2ed727aSAndreas Gohr $result = array(); 2687f9cb794SAndreas Gohr $cursor = -1; 2697f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2709dbc8ec0SMichael Grosse if($this->isRowEmpty($row)) { 2719dbc8ec0SMichael Grosse continue; 2729dbc8ec0SMichael Grosse } 2737f9cb794SAndreas Gohr $cursor++; 2747f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2757f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2767f9cb794SAndreas Gohr 277d4b5a17cSAndreas Gohr $this->result_pids[] = $row['PID']; 278d4b5a17cSAndreas Gohr 279b2ed727aSAndreas Gohr $C = 0; 280b2ed727aSAndreas Gohr $resrow = array(); 281b2ed727aSAndreas Gohr foreach($this->columns as $col) { 28238fa36fbSAndreas Gohr $val = $row["C$C"]; 283b2ed727aSAndreas Gohr if($col->isMulti()) { 28438fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 285b2ed727aSAndreas Gohr } 28638fa36fbSAndreas Gohr $resrow[] = new Value($col, $val); 287b2ed727aSAndreas Gohr $C++; 288b2ed727aSAndreas Gohr } 289b2ed727aSAndreas Gohr $result[] = $resrow; 290b2ed727aSAndreas Gohr } 2917f9cb794SAndreas Gohr 2927f9cb794SAndreas Gohr $this->sqlite->res_close($res); 2937f9cb794SAndreas Gohr $this->count = $cursor + 1; 294b2ed727aSAndreas Gohr return $result; 295b2ed727aSAndreas Gohr } 296b2ed727aSAndreas Gohr 297b2ed727aSAndreas Gohr /** 29815929be2SAndreas Gohr * Transform the set search parameters into a statement 29915929be2SAndreas Gohr * 300b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 30115929be2SAndreas Gohr */ 30215929be2SAndreas Gohr public function getSQL() { 3035511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 30415929be2SAndreas Gohr 3052f68434dSAndreas Gohr $QB = new QueryBuilder(); 30615929be2SAndreas Gohr 3079d7a36f9SAndreas Gohr // basic tables 308b2d67e7dSAndreas Gohr $first_table = ''; 3099d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 3102f68434dSAndreas Gohr $datatable = 'data_' . $schema->getTable(); 311b2d67e7dSAndreas Gohr if($first_table) { 3129d7a36f9SAndreas Gohr // follow up tables 3132f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 3149d7a36f9SAndreas Gohr } else { 3159d7a36f9SAndreas Gohr // first table 316b75e0a08SAndreas Gohr 317*13c321dbSAndreas Gohr if(!$schema->isLookup()) { 318*13c321dbSAndreas Gohr $QB->addTable('schema_assignments'); 3192f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3202f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3212f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3222f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3232f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 324*13c321dbSAndreas Gohr } 325*13c321dbSAndreas Gohr 326*13c321dbSAndreas Gohr $QB->addTable($datatable); 327*13c321dbSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 328*13c321dbSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 329b2d67e7dSAndreas Gohr 3302f68434dSAndreas Gohr $first_table = $datatable; 33115929be2SAndreas Gohr } 3322f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 3339d7a36f9SAndreas Gohr } 33415929be2SAndreas Gohr 33515929be2SAndreas Gohr // columns to select, handling multis 3369d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 33715929be2SAndreas Gohr $n = 0; 33815929be2SAndreas Gohr foreach($this->columns as $col) { 33915929be2SAndreas Gohr $CN = 'C' . $n++; 34015929be2SAndreas Gohr 341d1b04e89SAndreas Gohr if($col->isMulti()) { 3422f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3432f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3442f68434dSAndreas Gohr $MN = 'M' . $col->getColref(); 3452f68434dSAndreas Gohr 3462f68434dSAndreas Gohr $QB->addLeftJoin( 3472f68434dSAndreas Gohr $datatable, 3482f68434dSAndreas Gohr $multitable, 3492f68434dSAndreas Gohr $MN, 3502f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3512f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3522f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3532f68434dSAndreas Gohr ); 354578407b2SAndreas Gohr 355578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value', $CN); 356578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 357578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 35815929be2SAndreas Gohr } else { 359578407b2SAndreas Gohr $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN); 3600dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 36115929be2SAndreas Gohr } 36215929be2SAndreas Gohr } 36315929be2SAndreas Gohr 3649d7a36f9SAndreas Gohr // where clauses 365b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 3664c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 367b8fe6730SAndreas Gohr 3682f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3692f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3709d7a36f9SAndreas Gohr 3719d7a36f9SAndreas Gohr /** @var $col Column */ 3729d7a36f9SAndreas Gohr if($col->isMulti()) { 3732f68434dSAndreas Gohr $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 37415929be2SAndreas Gohr 3752f68434dSAndreas Gohr $QB->addLeftJoin( 3762f68434dSAndreas Gohr $datatable, 3772f68434dSAndreas Gohr $multitable, 3782f68434dSAndreas Gohr $MN, 3792f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3802f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3812f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3822f68434dSAndreas Gohr ); 383690e4ebaSAndreas Gohr $coltbl = $MN; 384690e4ebaSAndreas Gohr $colnam = 'value'; 3859d7a36f9SAndreas Gohr } else { 386690e4ebaSAndreas Gohr $coltbl = $datatable; 387690e4ebaSAndreas Gohr $colnam = $col->getColName(); 3889d7a36f9SAndreas Gohr } 38953528ecfSAndreas Gohr 390690e4ebaSAndreas Gohr $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 3919d7a36f9SAndreas Gohr } 3929d7a36f9SAndreas Gohr 39329394e6cSAndreas Gohr // sorting - we always sort by the single val column 394b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 395b8fe6730SAndreas Gohr list($col, $asc) = $sort; 3969d7a36f9SAndreas Gohr /** @var $col Column */ 397578407b2SAndreas Gohr $QB->addOrderBy($col->getFullColName(false) . ' ' . (($asc) ? 'ASC' : 'DESC')); 3989e07bdbfSAndreas Gohr } 3999e07bdbfSAndreas Gohr 4002f68434dSAndreas Gohr return $QB->getSQL(); 40115929be2SAndreas Gohr } 40215929be2SAndreas Gohr 40315929be2SAndreas Gohr /** 40401dd90deSAndreas Gohr * Returns all the columns that where added to the search 40501dd90deSAndreas Gohr * 40601dd90deSAndreas Gohr * @return Column[] 40701dd90deSAndreas Gohr */ 40801dd90deSAndreas Gohr public function getColumns() { 40901dd90deSAndreas Gohr return $this->columns; 41001dd90deSAndreas Gohr } 41101dd90deSAndreas Gohr 412577b462bSAndreas Gohr /** 413577b462bSAndreas Gohr * Checks if the given column is a * wildcard 414577b462bSAndreas Gohr * 415577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 416577b462bSAndreas Gohr * nothing happens 417577b462bSAndreas Gohr * 418577b462bSAndreas Gohr * @param string $colname 419577b462bSAndreas Gohr * @return bool was wildcard? 420577b462bSAndreas Gohr */ 421577b462bSAndreas Gohr protected function processWildcard($colname) { 422636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 423577b462bSAndreas Gohr if($colname !== '*') return false; 424577b462bSAndreas Gohr 425577b462bSAndreas Gohr // no table given? assume the first is meant 426577b462bSAndreas Gohr if($table === null) { 427577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 428577b462bSAndreas Gohr $table = $schema_list[0]; 429577b462bSAndreas Gohr } 430577b462bSAndreas Gohr 431577b462bSAndreas Gohr $schema = $this->schemas[$table]; 432577b462bSAndreas Gohr if(!$schema) return false; 433577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 434577b462bSAndreas Gohr return true; 435577b462bSAndreas Gohr } 436577b462bSAndreas Gohr 437577b462bSAndreas Gohr /** 438577b462bSAndreas Gohr * Split a given column name into table and column 439577b462bSAndreas Gohr * 440577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 441577b462bSAndreas Gohr * 442577b462bSAndreas Gohr * @param $colname 443636a5173SAndreas Gohr * @return array (colname, table) 444577b462bSAndreas Gohr */ 445577b462bSAndreas Gohr protected function resolveColumn($colname) { 446577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 447577b462bSAndreas Gohr 448577b462bSAndreas Gohr // resolve the alias or table name 449577b462bSAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 450577b462bSAndreas Gohr if(!$colname) { 451577b462bSAndreas Gohr $colname = $table; 452577b462bSAndreas Gohr $table = null; 453577b462bSAndreas Gohr } 454577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 455577b462bSAndreas Gohr $table = $this->aliases[$table]; 456577b462bSAndreas Gohr } 457577b462bSAndreas Gohr 458577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 459577b462bSAndreas Gohr 460577b462bSAndreas Gohr return array($colname, $table); 461577b462bSAndreas Gohr } 46201dd90deSAndreas Gohr 46301dd90deSAndreas Gohr /** 46415929be2SAndreas Gohr * Find a column to be used in the search 46515929be2SAndreas Gohr * 46615929be2SAndreas Gohr * @param string $colname may contain an alias 46715929be2SAndreas Gohr * @return bool|Column 46815929be2SAndreas Gohr */ 46900f6af48SAndreas Gohr public function findColumn($colname) { 4705511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 47115929be2SAndreas Gohr 472128d4e83SAndreas Gohr // handling of page and title column is special - we add a "fake" column 473df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 474128d4e83SAndreas Gohr if($colname == '%pageid%') { 475128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 476d1b04e89SAndreas Gohr } 477128d4e83SAndreas Gohr if($colname == '%title%') { 478128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 479128d4e83SAndreas Gohr } 480cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 481cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 482cadfc3ccSAndreas Gohr } 483d1b04e89SAndreas Gohr 484636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 48515929be2SAndreas Gohr 486bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 487577b462bSAndreas Gohr if($table !== null) { 48815929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 48915929be2SAndreas Gohr } else { 49015929be2SAndreas Gohr $schemas = $this->schemas; 49115929be2SAndreas Gohr } 49215929be2SAndreas Gohr 49315929be2SAndreas Gohr // find it 49415929be2SAndreas Gohr $col = false; 49515929be2SAndreas Gohr foreach($schemas as $schema) { 4964ac44d1cSMichael Grosse if(empty($schema)) { 4974ac44d1cSMichael Grosse continue; 4984ac44d1cSMichael Grosse } 49915929be2SAndreas Gohr $col = $schema->findColumn($colname); 50015929be2SAndreas Gohr if($col) break; 50115929be2SAndreas Gohr } 50215929be2SAndreas Gohr 50315929be2SAndreas Gohr return $col; 50415929be2SAndreas Gohr } 50515929be2SAndreas Gohr 5069dbc8ec0SMichael Grosse /** 5079dbc8ec0SMichael Grosse * Check if a row is empty / only contains a reference to itself 5089dbc8ec0SMichael Grosse * 5099dbc8ec0SMichael Grosse * @param array $rowColumns an array as returned from the database 5109dbc8ec0SMichael Grosse * @return bool 5119dbc8ec0SMichael Grosse */ 5129dbc8ec0SMichael Grosse private function isRowEmpty($rowColumns) { 5139dbc8ec0SMichael Grosse $C = 0; 5149dbc8ec0SMichael Grosse foreach($this->columns as $col) { 5159dbc8ec0SMichael Grosse $val = $rowColumns["C$C"]; 5169dbc8ec0SMichael Grosse $C += 1; 517c9494930SMichael Grosse if(blank($val) || is_a($col->getType(), 'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) { 5189dbc8ec0SMichael Grosse continue; 5199dbc8ec0SMichael Grosse } 5209dbc8ec0SMichael Grosse return false; 5219dbc8ec0SMichael Grosse } 5229dbc8ec0SMichael Grosse return true; 5239dbc8ec0SMichael Grosse } 5249dbc8ec0SMichael Grosse 52515929be2SAndreas Gohr} 52615929be2SAndreas Gohr 5275511bd5bSAndreas Gohr 528