115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 415929be2SAndreas Gohr 5*cadfc3ccSAndreas Gohruse dokuwiki\plugin\struct\types\Date; 6*cadfc3ccSAndreas 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) { 8115929be2SAndreas Gohr $col = $this->findColumn($colname); 8215929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 8315929be2SAndreas Gohr $this->columns[] = $col; 8415929be2SAndreas Gohr } 8515929be2SAndreas Gohr 8615929be2SAndreas Gohr /** 8715929be2SAndreas Gohr * Add sorting options 8815929be2SAndreas Gohr * 8915929be2SAndreas Gohr * Call multiple times for multiple columns. Be sure the referenced tables have been 9015929be2SAndreas Gohr * added before 9115929be2SAndreas Gohr * 9215929be2SAndreas Gohr * @param string $colname may contain an alias 9315929be2SAndreas Gohr * @param bool $asc sort direction (ASC = true, DESC = false) 9415929be2SAndreas Gohr */ 9515929be2SAndreas Gohr public function addSort($colname, $asc = true) { 9615929be2SAndreas Gohr $col = $this->findColumn($colname); 9715929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 9815929be2SAndreas Gohr 9907993756SAndreas Gohr $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc); 10007993756SAndreas Gohr } 10107993756SAndreas Gohr 10207993756SAndreas Gohr /** 10307993756SAndreas Gohr * Returns all set sort columns 10407993756SAndreas Gohr * 10507993756SAndreas Gohr * @return array 10607993756SAndreas Gohr */ 10707993756SAndreas Gohr public function getSorts() { 10807993756SAndreas Gohr return $this->sortby; 10915929be2SAndreas Gohr } 11015929be2SAndreas Gohr 11115929be2SAndreas Gohr /** 1129d7a36f9SAndreas Gohr * Adds a filter 11315929be2SAndreas Gohr * 11415929be2SAndreas Gohr * @param string $colname may contain an alias 11515929be2SAndreas Gohr * @param string $value 1165511bd5bSAndreas Gohr * @param string $comp @see self::COMPARATORS 1174c13ff97SAndreas Gohr * @param string $op either 'OR' or 'AND' 11815929be2SAndreas Gohr */ 1194c13ff97SAndreas Gohr public function addFilter($colname, $value, $comp, $op = 'OR') { 1209dbc32aeSAndreas Gohr /* Convert certain filters into others 1219dbc32aeSAndreas Gohr * this reduces the number of supported filters to implement in types */ 1229dbc32aeSAndreas Gohr if ($comp == '*~') { 1239dbc32aeSAndreas Gohr $value = '*' . $value . '*'; 1249dbc32aeSAndreas Gohr $comp = '~'; 1259dbc32aeSAndreas Gohr } elseif ($comp == '<>') { 1269dbc32aeSAndreas Gohr $comp = '!='; 1279dbc32aeSAndreas Gohr } 1289dbc32aeSAndreas Gohr 12974461852SAndreas Gohr if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS)); 1304c13ff97SAndreas Gohr if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1319d7a36f9SAndreas Gohr 13215929be2SAndreas Gohr $col = $this->findColumn($colname); 1337da1a0fdSAndreas Gohr if(!$col) return; // ignore missing columns, filter might have been for different schema 1347da1a0fdSAndreas Gohr 1357da1a0fdSAndreas Gohr // map filter operators to SQL syntax 1367da1a0fdSAndreas Gohr switch($comp) { 1377da1a0fdSAndreas Gohr case '~': 1387da1a0fdSAndreas Gohr $comp = 'LIKE'; 1397da1a0fdSAndreas Gohr break; 1407da1a0fdSAndreas Gohr case '!~': 1417da1a0fdSAndreas Gohr $comp = 'NOT LIKE'; 1427da1a0fdSAndreas Gohr break; 1437da1a0fdSAndreas Gohr case '=*': 1447da1a0fdSAndreas Gohr $comp = 'REGEXP'; 1457da1a0fdSAndreas Gohr break; 1467da1a0fdSAndreas Gohr } 1477da1a0fdSAndreas Gohr 1487da1a0fdSAndreas Gohr // we use asterisks, but SQL wants percents 1497da1a0fdSAndreas Gohr if($comp == 'LIKE' || $comp == 'NOT LIKE') { 15099894f21SMichael Große $value = str_replace('*','%',$value); 1517da1a0fdSAndreas Gohr } 1527da1a0fdSAndreas Gohr 1537da1a0fdSAndreas Gohr // add the filter 1544c13ff97SAndreas Gohr $this->filter[] = array($col, $value, $comp, $op); 15515929be2SAndreas Gohr } 15615929be2SAndreas Gohr 15715929be2SAndreas Gohr /** 1587f9cb794SAndreas Gohr * Set offset for the results 1597f9cb794SAndreas Gohr * 1607f9cb794SAndreas Gohr * @param int $offset 1617f9cb794SAndreas Gohr */ 1627f9cb794SAndreas Gohr public function setOffset($offset) { 1637f9cb794SAndreas Gohr $limit = 0; 1647f9cb794SAndreas Gohr if($this->range_end) { 1657f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 1667f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 1677f9cb794SAndreas Gohr } 1687f9cb794SAndreas Gohr $this->range_begin = $offset; 1697f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 1707f9cb794SAndreas Gohr } 1717f9cb794SAndreas Gohr 1727f9cb794SAndreas Gohr /** 1737f9cb794SAndreas Gohr * Limit results to this number 1747f9cb794SAndreas Gohr * 1757f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 1767f9cb794SAndreas Gohr */ 1777f9cb794SAndreas Gohr public function setLimit($limit) { 1787f9cb794SAndreas Gohr if($limit) { 1797f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 1807f9cb794SAndreas Gohr } else { 1817f9cb794SAndreas Gohr $this->range_end = 0; 1827f9cb794SAndreas Gohr } 1837f9cb794SAndreas Gohr } 1847f9cb794SAndreas Gohr 1857f9cb794SAndreas Gohr /** 1867f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 1877f9cb794SAndreas Gohr * 1887f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 1897f9cb794SAndreas Gohr * 1907f9cb794SAndreas Gohr * @return int 1917f9cb794SAndreas Gohr */ 1927f9cb794SAndreas Gohr public function getCount() { 1937f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 1947f9cb794SAndreas Gohr return $this->count; 1957f9cb794SAndreas Gohr } 1967f9cb794SAndreas Gohr 1977f9cb794SAndreas Gohr /** 198b2ed727aSAndreas Gohr * Execute this search and return the result 199b2ed727aSAndreas Gohr * 20038fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 2017f9cb794SAndreas Gohr * 2027f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 2037f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 20438fa36fbSAndreas Gohr * 20538fa36fbSAndreas Gohr * @return Value[][] 206b2ed727aSAndreas Gohr */ 207b2ed727aSAndreas Gohr public function execute() { 208b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 209b2ed727aSAndreas Gohr 2107f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 211b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 21259b668aaSAndreas Gohr if($res === false) throw new StructException("SQL execution failed for\n\n$sql"); 213b2ed727aSAndreas Gohr 214b2ed727aSAndreas Gohr $result = array(); 2157f9cb794SAndreas Gohr $cursor = -1; 2167f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2179dbc8ec0SMichael Grosse if ($this->isRowEmpty($row)) { 2189dbc8ec0SMichael Grosse continue; 2199dbc8ec0SMichael Grosse } 2207f9cb794SAndreas Gohr $cursor++; 2217f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2227f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2237f9cb794SAndreas Gohr 224b2ed727aSAndreas Gohr $C = 0; 225b2ed727aSAndreas Gohr $resrow = array(); 226b2ed727aSAndreas Gohr foreach($this->columns as $col) { 22738fa36fbSAndreas Gohr $val = $row["C$C"]; 228b2ed727aSAndreas Gohr if($col->isMulti()) { 22938fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 230b2ed727aSAndreas Gohr } 23138fa36fbSAndreas Gohr $resrow[] = new Value($col, $val); 232b2ed727aSAndreas Gohr $C++; 233b2ed727aSAndreas Gohr } 234b2ed727aSAndreas Gohr $result[] = $resrow; 235b2ed727aSAndreas Gohr } 2367f9cb794SAndreas Gohr 2377f9cb794SAndreas Gohr $this->sqlite->res_close($res); 2387f9cb794SAndreas Gohr $this->count = $cursor + 1; 239b2ed727aSAndreas Gohr return $result; 240b2ed727aSAndreas Gohr } 241b2ed727aSAndreas Gohr 242b2ed727aSAndreas Gohr /** 24315929be2SAndreas Gohr * Transform the set search parameters into a statement 24415929be2SAndreas Gohr * 245b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 24615929be2SAndreas Gohr */ 24715929be2SAndreas Gohr public function getSQL() { 2485511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 24915929be2SAndreas Gohr 2502f68434dSAndreas Gohr $QB = new QueryBuilder(); 25115929be2SAndreas Gohr 2529d7a36f9SAndreas Gohr // basic tables 253b2d67e7dSAndreas Gohr $first_table = ''; 2549d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 2552f68434dSAndreas Gohr $datatable = 'data_'.$schema->getTable(); 256b2d67e7dSAndreas Gohr if($first_table) { 2579d7a36f9SAndreas Gohr // follow up tables 2582f68434dSAndreas Gohr $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid"); 2599d7a36f9SAndreas Gohr } else { 2609d7a36f9SAndreas Gohr // first table 2612f68434dSAndreas Gohr $QB->addTable('schema_assignments'); 2622f68434dSAndreas Gohr $QB->addTable($datatable); 2632f68434dSAndreas Gohr $QB->addSelectColumn($datatable, 'pid', 'PID'); 2642f68434dSAndreas Gohr $QB->addGroupByColumn($datatable, 'pid'); 265b75e0a08SAndreas Gohr 2662f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid"); 2672f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'"); 2682f68434dSAndreas Gohr $QB->filters()->whereAnd("schema_assignments.assigned = 1"); 2692f68434dSAndreas Gohr $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0"); 2702f68434dSAndreas Gohr $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1"); 271b2d67e7dSAndreas Gohr 2722f68434dSAndreas Gohr $first_table = $datatable; 27315929be2SAndreas Gohr } 2742f68434dSAndreas Gohr $QB->filters()->whereAnd("$datatable.latest = 1"); 2759d7a36f9SAndreas Gohr } 27615929be2SAndreas Gohr 27715929be2SAndreas Gohr // columns to select, handling multis 2789d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 27915929be2SAndreas Gohr $n = 0; 28015929be2SAndreas Gohr foreach($this->columns as $col) { 28115929be2SAndreas Gohr $CN = 'C' . $n++; 28215929be2SAndreas Gohr 283d1b04e89SAndreas Gohr if($col->isMulti()) { 2842f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 2852f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 2862f68434dSAndreas Gohr $MN = 'M' . $col->getColref(); 2872f68434dSAndreas Gohr 2882f68434dSAndreas Gohr $QB->addLeftJoin( 2892f68434dSAndreas Gohr $datatable, 2902f68434dSAndreas Gohr $multitable, 2912f68434dSAndreas Gohr $MN, 2922f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 2932f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 2942f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 2952f68434dSAndreas Gohr ); 296578407b2SAndreas Gohr 297578407b2SAndreas Gohr $col->getType()->select($QB, $MN, 'value' , $CN); 298578407b2SAndreas Gohr $sel = $QB->getSelectStatement($CN); 299578407b2SAndreas Gohr $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN); 30015929be2SAndreas Gohr } else { 301578407b2SAndreas Gohr $col->getType()->select($QB, 'data_'.$col->getTable(), $col->getColName() , $CN); 302*cadfc3ccSAndreas Gohr 303*cadfc3ccSAndreas Gohr // the %lastupdate% column needs datetime mangling 304*cadfc3ccSAndreas Gohr if(is_a($col, 'dokuwiki\\plugin\\struct\\meta\\RevisionColumn')) { 305*cadfc3ccSAndreas Gohr $sel = $QB->getSelectStatement($CN); 306*cadfc3ccSAndreas Gohr $QB->addSelectStatement("DATETIME($sel, 'unixepoch')", $CN); 307*cadfc3ccSAndreas Gohr } 308*cadfc3ccSAndreas Gohr 3090dbe7bf6SAndreas Gohr $QB->addGroupByStatement($CN); 31015929be2SAndreas Gohr } 31115929be2SAndreas Gohr } 31215929be2SAndreas Gohr 3139d7a36f9SAndreas Gohr // where clauses 314b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 3154c13ff97SAndreas Gohr list($col, $value, $comp, $op) = $filter; 316b8fe6730SAndreas Gohr 3172f68434dSAndreas Gohr $datatable = "data_{$col->getTable()}"; 3182f68434dSAndreas Gohr $multitable = "multi_{$col->getTable()}"; 3199d7a36f9SAndreas Gohr 3209d7a36f9SAndreas Gohr /** @var $col Column */ 3219d7a36f9SAndreas Gohr if($col->isMulti()) { 3222f68434dSAndreas Gohr $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 32315929be2SAndreas Gohr 3242f68434dSAndreas Gohr $QB->addLeftJoin( 3252f68434dSAndreas Gohr $datatable, 3262f68434dSAndreas Gohr $multitable, 3272f68434dSAndreas Gohr $MN, 3282f68434dSAndreas Gohr "$datatable.pid = $MN.pid AND 3292f68434dSAndreas Gohr $datatable.rev = $MN.rev AND 3302f68434dSAndreas Gohr $MN.colref = {$col->getColref()}" 3312f68434dSAndreas Gohr ); 332690e4ebaSAndreas Gohr $coltbl = $MN; 333690e4ebaSAndreas Gohr $colnam = 'value'; 3349d7a36f9SAndreas Gohr } else { 335690e4ebaSAndreas Gohr $coltbl = $datatable; 336690e4ebaSAndreas Gohr $colnam = $col->getColName(); 3379d7a36f9SAndreas Gohr } 338690e4ebaSAndreas Gohr $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter 3399d7a36f9SAndreas Gohr } 3409d7a36f9SAndreas Gohr 34129394e6cSAndreas Gohr // sorting - we always sort by the single val column 342b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 343b8fe6730SAndreas Gohr list($col, $asc) = $sort; 3449d7a36f9SAndreas Gohr /** @var $col Column */ 345578407b2SAndreas Gohr $QB->addOrderBy($col->getFullColName(false) . ' '.(($asc) ? 'ASC' : 'DESC')); 3469e07bdbfSAndreas Gohr } 3479e07bdbfSAndreas Gohr 3482f68434dSAndreas Gohr return $QB->getSQL(); 34915929be2SAndreas Gohr } 35015929be2SAndreas Gohr 35115929be2SAndreas Gohr /** 35201dd90deSAndreas Gohr * Returns all the columns that where added to the search 35301dd90deSAndreas Gohr * 35401dd90deSAndreas Gohr * @return Column[] 35501dd90deSAndreas Gohr */ 35601dd90deSAndreas Gohr public function getColumns() { 35701dd90deSAndreas Gohr return $this->columns; 35801dd90deSAndreas Gohr } 35901dd90deSAndreas Gohr 36001dd90deSAndreas Gohr 36101dd90deSAndreas Gohr /** 36215929be2SAndreas Gohr * Find a column to be used in the search 36315929be2SAndreas Gohr * 36415929be2SAndreas Gohr * @param string $colname may contain an alias 36515929be2SAndreas Gohr * @return bool|Column 36615929be2SAndreas Gohr */ 36700f6af48SAndreas Gohr public function findColumn($colname) { 3685511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 36915929be2SAndreas Gohr 370128d4e83SAndreas Gohr // handling of page and title column is special - we add a "fake" column 371df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 372128d4e83SAndreas Gohr if($colname == '%pageid%') { 373128d4e83SAndreas Gohr return new PageColumn(0, new Page(), $schema_list[0]); 374d1b04e89SAndreas Gohr } 375128d4e83SAndreas Gohr if($colname == '%title%') { 376128d4e83SAndreas Gohr return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]); 377128d4e83SAndreas Gohr } 378*cadfc3ccSAndreas Gohr if($colname == '%lastupdate%') { 379*cadfc3ccSAndreas Gohr return new RevisionColumn(0, new DateTime(), $schema_list[0]); 380*cadfc3ccSAndreas Gohr } 381d1b04e89SAndreas Gohr 38215929be2SAndreas Gohr // resolve the alias or table name 38315929be2SAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 38415929be2SAndreas Gohr if(!$colname) { 38515929be2SAndreas Gohr $colname = $table; 38615929be2SAndreas Gohr $table = ''; 38715929be2SAndreas Gohr } 38815929be2SAndreas Gohr if($table && isset($this->aliases[$table])) { 38915929be2SAndreas Gohr $table = $this->aliases[$table]; 39015929be2SAndreas Gohr } 39115929be2SAndreas Gohr 3925511bd5bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 39315929be2SAndreas Gohr 394bd363da9SAndreas Gohr // if table name given search only that, otherwise try all for matching column name 39515929be2SAndreas Gohr if($table) { 39615929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 39715929be2SAndreas Gohr } else { 39815929be2SAndreas Gohr $schemas = $this->schemas; 39915929be2SAndreas Gohr } 40015929be2SAndreas Gohr 40115929be2SAndreas Gohr // find it 40215929be2SAndreas Gohr $col = false; 40315929be2SAndreas Gohr foreach($schemas as $schema) { 4044ac44d1cSMichael Grosse if(empty($schema)) { 4054ac44d1cSMichael Grosse continue; 4064ac44d1cSMichael Grosse } 40715929be2SAndreas Gohr $col = $schema->findColumn($colname); 40815929be2SAndreas Gohr if($col) break; 40915929be2SAndreas Gohr } 41015929be2SAndreas Gohr 41115929be2SAndreas Gohr return $col; 41215929be2SAndreas Gohr } 41315929be2SAndreas Gohr 4149dbc8ec0SMichael Grosse /** 4159dbc8ec0SMichael Grosse * Check if a row is empty / only contains a reference to itself 4169dbc8ec0SMichael Grosse * 4179dbc8ec0SMichael Grosse * @param array $rowColumns an array as returned from the database 4189dbc8ec0SMichael Grosse * @return bool 4199dbc8ec0SMichael Grosse */ 4209dbc8ec0SMichael Grosse private function isRowEmpty($rowColumns) { 4219dbc8ec0SMichael Grosse $C = 0; 4229dbc8ec0SMichael Grosse foreach($this->columns as $col) { 4239dbc8ec0SMichael Grosse $val = $rowColumns["C$C"]; 4249dbc8ec0SMichael Grosse $C += 1; 425c9494930SMichael Grosse if (blank($val) || is_a($col->getType(),'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) { 4269dbc8ec0SMichael Grosse continue; 4279dbc8ec0SMichael Grosse } 4289dbc8ec0SMichael Grosse return false; 4299dbc8ec0SMichael Grosse } 4309dbc8ec0SMichael Grosse return true; 4319dbc8ec0SMichael Grosse } 4329dbc8ec0SMichael Grosse 43315929be2SAndreas Gohr} 43415929be2SAndreas Gohr 4355511bd5bSAndreas Gohr 436