115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 415929be2SAndreas Gohr 5cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\Date; 6cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\DateTime; 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; 497f9cb794SAndreas Gohr 5015929be2SAndreas Gohr /** 519d7a36f9SAndreas Gohr * Search constructor. 529d7a36f9SAndreas Gohr */ 539d7a36f9SAndreas Gohr public function __construct() { 549d7a36f9SAndreas Gohr /** @var \helper_plugin_struct_db $plugin */ 559d7a36f9SAndreas Gohr $plugin = plugin_load('helper', 'struct_db'); 569d7a36f9SAndreas Gohr $this->sqlite = $plugin->getDB(); 579d7a36f9SAndreas Gohr } 589d7a36f9SAndreas Gohr 599d7a36f9SAndreas Gohr /** 6015929be2SAndreas Gohr * Add a schema to be searched 6115929be2SAndreas Gohr * 6215929be2SAndreas Gohr * Call multiple times for multiple schemas. 6315929be2SAndreas Gohr * 6415929be2SAndreas Gohr * @param string $table 6515929be2SAndreas Gohr * @param string $alias 6615929be2SAndreas Gohr */ 6715929be2SAndreas Gohr public function addSchema($table, $alias = '') { 6815929be2SAndreas Gohr $this->schemas[$table] = new Schema($table); 6915929be2SAndreas Gohr if($alias) $this->aliases[$alias] = $table; 7015929be2SAndreas Gohr } 7115929be2SAndreas Gohr 7215929be2SAndreas Gohr /** 7315929be2SAndreas Gohr * Add a column to be returned by the search 7415929be2SAndreas Gohr * 7515929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 7615929be2SAndreas Gohr * added before 7715929be2SAndreas Gohr * 7815929be2SAndreas Gohr * @param string $colname may contain an alias 7915929be2SAndreas Gohr */ 8015929be2SAndreas Gohr public function addColumn($colname) { 81577b462bSAndreas Gohr if($this->processWildcard($colname)) return; // wildcard? 8215929be2SAndreas Gohr $col = $this->findColumn($colname); 8315929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 8415929be2SAndreas Gohr $this->columns[] = $col; 8515929be2SAndreas Gohr } 8615929be2SAndreas Gohr 8715929be2SAndreas Gohr /** 8815929be2SAndreas Gohr * Add sorting options 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 * @param bool $asc sort direction (ASC = true, DESC = false) 9515929be2SAndreas Gohr */ 9615929be2SAndreas Gohr public function addSort($colname, $asc = true) { 9715929be2SAndreas Gohr $col = $this->findColumn($colname); 9815929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 9915929be2SAndreas Gohr 10007993756SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc); 10107993756SAndreas Gohr } 10207993756SAndreas Gohr 10307993756SAndreas Gohr /** 10407993756SAndreas Gohr * Returns all set sort columns 10507993756SAndreas Gohr * 10607993756SAndreas Gohr * @return array 10707993756SAndreas Gohr */ 10807993756SAndreas Gohr public function getSorts() { 10907993756SAndreas Gohr return $this->sortby; 11015929be2SAndreas Gohr } 11115929be2SAndreas Gohr 11215929be2SAndreas Gohr /** 1139d7a36f9SAndreas Gohr * Adds a filter 11415929be2SAndreas Gohr * 11515929be2SAndreas Gohr * @param string $colname may contain an alias 11653528ecfSAndreas Gohr * @param string|string[] $value 1175511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1184c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 11915929be2SAndreas Gohr */ 1204c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1219dbc32aeSAndreas Gohr /* Convert certain filters into others 1229dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1239dbc32aeSAndreas Gohr if ($comp == '*~') { 12453528ecfSAndreas Gohr $value = $this->filterWrapAsterisks($value); 1259dbc32aeSAndreas Gohr $comp = '~'; 1269dbc32aeSAndreas Gohr } elseif ($comp == '<>') { 1279dbc32aeSAndreas Gohr $comp = '!='; 1289dbc32aeSAndreas Gohr } 1299dbc32aeSAndreas Gohr 13074461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1314c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1329d7a36f9SAndreas Gohr 13315929be2SAndreas Gohr $col = $this->findColumn($colname); 1347da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1357da1a0fdSAndreas Gohr 1367da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1377da1a0fdSAndreas Gohr switch($comp) { 1387da1a0fdSAndreas Gohr case '~': 1397da1a0fdSAndreas Gohr $comp = 'LIKE'; 1407da1a0fdSAndreas Gohr break; 1417da1a0fdSAndreas Gohr case '!~': 1427da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1437da1a0fdSAndreas Gohr break; 1447da1a0fdSAndreas Gohr case '=*': 1457da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1467da1a0fdSAndreas Gohr break; 1477da1a0fdSAndreas Gohr } 1487da1a0fdSAndreas Gohr 1497da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1507da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 15153528ecfSAndreas Gohr $value = $this->filterChangeToLike($value); 1527da1a0fdSAndreas Gohr } 1537da1a0fdSAndreas Gohr 1547da1a0fdSAndreas Gohr // add the filter 1554c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 15615929be2SAndreas Gohr } 15715929be2SAndreas Gohr 15815929be2SAndreas Gohr /** 15953528ecfSAndreas Gohr * Wrap given value in asterisks 16053528ecfSAndreas Gohr * 16153528ecfSAndreas Gohr * @param string|string[] $value 16253528ecfSAndreas Gohr * @return string|string[] 16353528ecfSAndreas Gohr */ 16453528ecfSAndreas Gohr protected function filterWrapAsterisks($value) { 16553528ecfSAndreas Gohr $map = function ($input) { 16653528ecfSAndreas Gohr return "*$input*"; 16753528ecfSAndreas Gohr }; 16853528ecfSAndreas Gohr 16953528ecfSAndreas Gohr if(is_array($value)) { 17053528ecfSAndreas Gohr $value = array_map($map, $value); 17153528ecfSAndreas Gohr } else { 17253528ecfSAndreas Gohr $value = $map($value); 17353528ecfSAndreas Gohr } 17453528ecfSAndreas Gohr return $value; 17553528ecfSAndreas Gohr } 17653528ecfSAndreas Gohr 17753528ecfSAndreas Gohr /** 17853528ecfSAndreas Gohr * Change given string to use % instead of * 17953528ecfSAndreas Gohr * 18053528ecfSAndreas Gohr * @param string|string[] $value 18153528ecfSAndreas Gohr * @return string|string[] 18253528ecfSAndreas Gohr */ 18353528ecfSAndreas Gohr protected function filterChangeToLike($value) { 18453528ecfSAndreas Gohr $map = function ($input) { 18553528ecfSAndreas Gohr return str_replace('*','%',$input); 18653528ecfSAndreas Gohr }; 18753528ecfSAndreas Gohr 18853528ecfSAndreas Gohr if(is_array($value)) { 18953528ecfSAndreas Gohr $value = array_map($map, $value); 19053528ecfSAndreas Gohr } else { 19153528ecfSAndreas Gohr $value = $map($value); 19253528ecfSAndreas Gohr } 19353528ecfSAndreas Gohr return $value; 19453528ecfSAndreas Gohr } 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 2377f9cb794SAndreas Gohr /** 238b2ed727aSAndreas Gohr * Execute this search and return the result 239b2ed727aSAndreas Gohr * 24038fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2417f9cb794SAndreas Gohr * 2427f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2437f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 24438fa36fbSAndreas Gohr * 24538fa36fbSAndreas Gohr * @return Value[][] 246b2ed727aSAndreas Gohr */ 247b2ed727aSAndreas Gohr public function execute() { 248b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 249b2ed727aSAndreas Gohr 2507f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 251b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 25259b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 253b2ed727aSAndreas Gohr 254b2ed727aSAndreas Gohr $result = array(); 2557f9cb794SAndreas Gohr $cursor = -1; 2567f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2579dbc8ec0SMichael Grosse if ($this->isRowEmpty($row)) { 2589dbc8ec0SMichael Grosse continue; 2599dbc8ec0SMichael Grosse } 2607f9cb794SAndreas Gohr $cursor++; 2617f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2627f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2637f9cb794SAndreas Gohr 264b2ed727aSAndreas Gohr $C = 0; 265b2ed727aSAndreas Gohr $resrow = array(); 266b2ed727aSAndreas Gohr foreach($this->columns as $col) { 26738fa36fbSAndreas Gohr $val = $row["C$C"]; 268b2ed727aSAndreas Gohr if($col->isMulti()) { 26938fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 270b2ed727aSAndreas Gohr } 27138fa36fbSAndreas Gohr $resrow[] = new Value($col, $val); 272b2ed727aSAndreas Gohr $C++; 273b2ed727aSAndreas Gohr } 274b2ed727aSAndreas Gohr $result[] = $resrow; 275b2ed727aSAndreas Gohr } 2767f9cb794SAndreas Gohr 2777f9cb794SAndreas Gohr $this->sqlite->res_close($res); 2787f9cb794SAndreas Gohr $this->count = $cursor + 1; 279b2ed727aSAndreas Gohr return $result; 280b2ed727aSAndreas Gohr } 281b2ed727aSAndreas Gohr 282b2ed727aSAndreas Gohr /** 28315929be2SAndreas Gohr * Transform the set search parameters into a statement 28415929be2SAndreas Gohr * 285b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 28615929be2SAndreas Gohr */ 28715929be2SAndreas Gohr public function getSQL() { 2885511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 28915929be2SAndreas Gohr 2902f68434dSAndreas Gohr $QB = new QueryBuilder(); 29115929be2SAndreas Gohr 2929d7a36f9SAndreas Gohr // basic tables 293b2d67e7dSAndreas Gohr $first_table = ''; 2949d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 2952f68434dSAndreas Gohr $datatable = 'data_'.$schema->getTable(); 296b2d67e7dSAndreas Gohr if($first_table) { 2979d7a36f9SAndreas Gohr // follow up tables 2982f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 2999d7a36f9SAndreas Gohr } else { 3009d7a36f9SAndreas Gohr // first table 3012f68434dSAndreas Gohr $QB->addTable('schema_assignments'); 3022f68434dSAndreas Gohr $QB->addTable($datatable); 3032f68434dSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 3042f68434dSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 305b75e0a08SAndreas Gohr 3062f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 3072f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 3082f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 3092f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 3102f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 311b2d67e7dSAndreas Gohr 3122f68434dSAndreas Gohr $first_table = $datatable; 31315929be2SAndreas Gohr } 3142f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 3159d7a36f9SAndreas Gohr } 31615929be2SAndreas Gohr 31715929be2SAndreas Gohr // columns to select, handling multis 3189d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 31915929be2SAndreas Gohr $n = 0; 32015929be2SAndreas Gohr foreach($this->columns as $col) { 32115929be2SAndreas Gohr $CN = 'C' . $n++; 32215929be2SAndreas Gohr 323d1b04e89SAndreas Gohr if($col->isMulti()) { 3242f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3252f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3262f68434dSAndreas Gohr $MN = 'M' . $col->getColref(); 3272f68434dSAndreas Gohr 3282f68434dSAndreas Gohr $QB->addLeftJoin( 3292f68434dSAndreas Gohr $datatable, 3302f68434dSAndreas Gohr $multitable, 3312f68434dSAndreas Gohr $MN, 3322f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3332f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3342f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3352f68434dSAndreas Gohr ); 336578407b2SAndreas Gohr 337578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value' , $CN); 338578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 339578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 34015929be2SAndreas Gohr } else { 341578407b2SAndreas Gohr $col->getType()->select($QB, 'data_'.$col->getTable(), $col->getColName() , $CN); 3420dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 34315929be2SAndreas Gohr } 34415929be2SAndreas Gohr } 34515929be2SAndreas Gohr 3469d7a36f9SAndreas Gohr // where clauses 347b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 3484c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 349b8fe6730SAndreas Gohr 3502f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3512f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3529d7a36f9SAndreas Gohr 3539d7a36f9SAndreas Gohr /** @var $col Column */ 3549d7a36f9SAndreas Gohr if($col->isMulti()) { 3552f68434dSAndreas Gohr $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 35615929be2SAndreas Gohr 3572f68434dSAndreas Gohr $QB->addLeftJoin( 3582f68434dSAndreas Gohr $datatable, 3592f68434dSAndreas Gohr $multitable, 3602f68434dSAndreas Gohr $MN, 3612f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3622f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3632f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3642f68434dSAndreas Gohr ); 365690e4ebaSAndreas Gohr $coltbl = $MN; 366690e4ebaSAndreas Gohr $colnam = 'value'; 3679d7a36f9SAndreas Gohr } else { 368690e4ebaSAndreas Gohr $coltbl = $datatable; 369690e4ebaSAndreas Gohr $colnam = $col->getColName(); 3709d7a36f9SAndreas Gohr } 37153528ecfSAndreas Gohr 372690e4ebaSAndreas Gohr $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 3739d7a36f9SAndreas Gohr } 3749d7a36f9SAndreas Gohr 37529394e6cSAndreas Gohr // sorting - we always sort by the single val column 376b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 377b8fe6730SAndreas Gohr list($col, $asc) = $sort; 3789d7a36f9SAndreas Gohr /** @var $col Column */ 379*5a11127fSAndreas Gohr $col->getType()->sort($QB, 'data_'.$col->getTable(), $col->getColName(false), $asc ? 'ASC' : 'DESC'); 3809e07bdbfSAndreas Gohr } 3819e07bdbfSAndreas Gohr 3822f68434dSAndreas Gohr return $QB->getSQL(); 38315929be2SAndreas Gohr } 38415929be2SAndreas Gohr 38515929be2SAndreas Gohr /** 38601dd90deSAndreas Gohr * Returns all the columns that where added to the search 38701dd90deSAndreas Gohr * 38801dd90deSAndreas Gohr * @return Column[] 38901dd90deSAndreas Gohr */ 39001dd90deSAndreas Gohr public function getColumns() { 39101dd90deSAndreas Gohr return $this->columns; 39201dd90deSAndreas Gohr } 39301dd90deSAndreas Gohr 394577b462bSAndreas Gohr /** 395577b462bSAndreas Gohr * Checks if the given column is a * wildcard 396577b462bSAndreas Gohr * 397577b462bSAndreas Gohr * If it's a wildcard all matching columns are added to the column list, otherwise 398577b462bSAndreas Gohr * nothing happens 399577b462bSAndreas Gohr * 400577b462bSAndreas Gohr * @param string $colname 401577b462bSAndreas Gohr * @return bool was wildcard? 402577b462bSAndreas Gohr */ 403577b462bSAndreas Gohr protected function processWildcard($colname) { 404636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 405577b462bSAndreas Gohr if($colname !== '*') return false; 406577b462bSAndreas Gohr 407577b462bSAndreas Gohr // no table given? assume the first is meant 408577b462bSAndreas Gohr if($table === null) { 409577b462bSAndreas Gohr $schema_list = array_keys($this->schemas); 410577b462bSAndreas Gohr $table = $schema_list[0]; 411577b462bSAndreas Gohr } 412577b462bSAndreas Gohr 413577b462bSAndreas Gohr $schema = $this->schemas[$table]; 414577b462bSAndreas Gohr if(!$schema) return false; 415577b462bSAndreas Gohr $this->columns = array_merge($this->columns, $schema->getColumns(false)); 416577b462bSAndreas Gohr return true; 417577b462bSAndreas Gohr } 418577b462bSAndreas Gohr 419577b462bSAndreas Gohr /** 420577b462bSAndreas Gohr * Split a given column name into table and column 421577b462bSAndreas Gohr * 422577b462bSAndreas Gohr * Handles Aliases. Table might be null if none given. 423577b462bSAndreas Gohr * 424577b462bSAndreas Gohr * @param $colname 425636a5173SAndreas Gohr * @return array (colname, table) 426577b462bSAndreas Gohr */ 427577b462bSAndreas Gohr protected function resolveColumn($colname) { 428577b462bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 429577b462bSAndreas Gohr 430577b462bSAndreas Gohr // resolve the alias or table name 431577b462bSAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 432577b462bSAndreas Gohr if(!$colname) { 433577b462bSAndreas Gohr $colname = $table; 434577b462bSAndreas Gohr $table = null; 435577b462bSAndreas Gohr } 436577b462bSAndreas Gohr if($table && isset($this->aliases[$table])) { 437577b462bSAndreas Gohr $table = $this->aliases[$table]; 438577b462bSAndreas Gohr } 439577b462bSAndreas Gohr 440577b462bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 441577b462bSAndreas Gohr 442577b462bSAndreas Gohr return array($colname, $table); 443577b462bSAndreas Gohr } 44401dd90deSAndreas Gohr 44501dd90deSAndreas Gohr /** 44615929be2SAndreas Gohr * Find a column to be used in the search 44715929be2SAndreas Gohr * 44815929be2SAndreas Gohr * @param string $colname may contain an alias 44915929be2SAndreas Gohr * @return bool|Column 45015929be2SAndreas Gohr */ 45100f6af48SAndreas Gohr public function findColumn($colname) { 4525511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 45315929be2SAndreas Gohr 454128d4e83SAndreas Gohr // handling of page and title column is special - we add a "fake" column 455df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 456128d4e83SAndreas Gohr if($colname == '%pageid%') { 457128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 458d1b04e89SAndreas Gohr } 459128d4e83SAndreas Gohr if($colname == '%title%') { 460128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 461128d4e83SAndreas Gohr } 462cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 463cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 464cadfc3ccSAndreas Gohr } 465d1b04e89SAndreas Gohr 466636a5173SAndreas Gohr list($colname, $table) = $this->resolveColumn($colname); 46715929be2SAndreas Gohr 468bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 469577b462bSAndreas Gohr if($table !== null) { 47015929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 47115929be2SAndreas Gohr } else { 47215929be2SAndreas Gohr $schemas = $this->schemas; 47315929be2SAndreas Gohr } 47415929be2SAndreas Gohr 47515929be2SAndreas Gohr // find it 47615929be2SAndreas Gohr $col = false; 47715929be2SAndreas Gohr foreach($schemas as $schema) { 4784ac44d1cSMichael Grosse if(empty($schema)) { 4794ac44d1cSMichael Grosse continue; 4804ac44d1cSMichael Grosse } 48115929be2SAndreas Gohr $col = $schema->findColumn($colname); 48215929be2SAndreas Gohr if($col) break; 48315929be2SAndreas Gohr } 48415929be2SAndreas Gohr 48515929be2SAndreas Gohr return $col; 48615929be2SAndreas Gohr } 48715929be2SAndreas Gohr 4889dbc8ec0SMichael Grosse /** 4899dbc8ec0SMichael Grosse * Check if a row is empty / only contains a reference to itself 4909dbc8ec0SMichael Grosse * 4919dbc8ec0SMichael Grosse * @param array $rowColumns an array as returned from the database 4929dbc8ec0SMichael Grosse * @return bool 4939dbc8ec0SMichael Grosse */ 4949dbc8ec0SMichael Grosse private function isRowEmpty($rowColumns) { 4959dbc8ec0SMichael Grosse $C = 0; 4969dbc8ec0SMichael Grosse foreach($this->columns as $col) { 4979dbc8ec0SMichael Grosse $val = $rowColumns["C$C"]; 4989dbc8ec0SMichael Grosse $C += 1; 499c9494930SMichael Grosse if (blank($val) || is_a($col->getType(),'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) { 5009dbc8ec0SMichael Grosse continue; 5019dbc8ec0SMichael Grosse } 5029dbc8ec0SMichael Grosse return false; 5039dbc8ec0SMichael Grosse } 5049dbc8ec0SMichael Grosse return true; 5059dbc8ec0SMichael Grosse } 5069dbc8ec0SMichael Grosse 50715929be2SAndreas Gohr} 50815929be2SAndreas Gohr 5095511bd5bSAndreas Gohr 510