xref: /plugin/struct/meta/Search.php (revision 128d4e83a2c30080c70f075a7b5901412e157822)
115929be2SAndreas Gohr<?php
215929be2SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
415929be2SAndreas Gohr
5ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\Page;
60561158fSAndreas Gohr
715929be2SAndreas Gohrclass Search {
89d7a36f9SAndreas Gohr    /**
99d7a36f9SAndreas Gohr     * This separator will be used to concat multi values to flatten them in the result set
109d7a36f9SAndreas Gohr     */
119d7a36f9SAndreas Gohr    const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n";
129d7a36f9SAndreas Gohr
135511bd5bSAndreas Gohr    /**
145511bd5bSAndreas Gohr     * The list of known and allowed comparators
152e7595e7SAndreas Gohr     * (order matters)
165511bd5bSAndreas Gohr     */
1774461852SAndreas Gohr    static public $COMPARATORS = array(
182e7595e7SAndreas Gohr        '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~',
195511bd5bSAndreas Gohr    );
205511bd5bSAndreas Gohr
219d7a36f9SAndreas Gohr    /** @var  \helper_plugin_sqlite */
229d7a36f9SAndreas Gohr    protected $sqlite;
2315929be2SAndreas Gohr
2415929be2SAndreas Gohr    /** @var Schema[] list of schemas to query */
2515929be2SAndreas Gohr    protected $schemas = array();
2615929be2SAndreas Gohr
2715929be2SAndreas Gohr    /** @var Column[] list of columns to select */
2815929be2SAndreas Gohr    protected $columns = array();
2915929be2SAndreas Gohr
3015929be2SAndreas Gohr    /** @var array the sorting of the result */
3115929be2SAndreas Gohr    protected $sortby = array();
3215929be2SAndreas Gohr
339d7a36f9SAndreas Gohr    /** @var array the filters */
349d7a36f9SAndreas Gohr    protected $filter = array();
3515929be2SAndreas Gohr
3615929be2SAndreas Gohr    /** @var array list of aliases tables can be referenced by */
3715929be2SAndreas Gohr    protected $aliases = array();
3815929be2SAndreas Gohr
397f9cb794SAndreas Gohr    /** @var  int begin results from here */
407f9cb794SAndreas Gohr    protected $range_begin = 0;
417f9cb794SAndreas Gohr
427f9cb794SAndreas Gohr    /** @var  int end results here */
437f9cb794SAndreas Gohr    protected $range_end = 0;
447f9cb794SAndreas Gohr
457f9cb794SAndreas Gohr    /** @var int the number of results */
467f9cb794SAndreas Gohr    protected $count = -1;
477f9cb794SAndreas Gohr
4815929be2SAndreas Gohr    /**
499d7a36f9SAndreas Gohr     * Search constructor.
509d7a36f9SAndreas Gohr     */
519d7a36f9SAndreas Gohr    public function __construct() {
529d7a36f9SAndreas Gohr        /** @var \helper_plugin_struct_db $plugin */
539d7a36f9SAndreas Gohr        $plugin = plugin_load('helper', 'struct_db');
549d7a36f9SAndreas Gohr        $this->sqlite = $plugin->getDB();
559d7a36f9SAndreas Gohr    }
569d7a36f9SAndreas Gohr
579d7a36f9SAndreas Gohr    /**
5815929be2SAndreas Gohr     * Add a schema to be searched
5915929be2SAndreas Gohr     *
6015929be2SAndreas Gohr     * Call multiple times for multiple schemas.
6115929be2SAndreas Gohr     *
6215929be2SAndreas Gohr     * @param string $table
6315929be2SAndreas Gohr     * @param string $alias
6415929be2SAndreas Gohr     */
6515929be2SAndreas Gohr    public function addSchema($table, $alias = '') {
6615929be2SAndreas Gohr        $this->schemas[$table] = new Schema($table);
6715929be2SAndreas Gohr        if($alias) $this->aliases[$alias] = $table;
6815929be2SAndreas Gohr    }
6915929be2SAndreas Gohr
7015929be2SAndreas Gohr    /**
7115929be2SAndreas Gohr     * Add a column to be returned by the search
7215929be2SAndreas Gohr     *
7315929be2SAndreas Gohr     * Call multiple times for multiple columns. Be sure the referenced tables have been
7415929be2SAndreas Gohr     * added before
7515929be2SAndreas Gohr     *
7615929be2SAndreas Gohr     * @param string $colname may contain an alias
7715929be2SAndreas Gohr     */
7815929be2SAndreas Gohr    public function addColumn($colname) {
7915929be2SAndreas Gohr        $col = $this->findColumn($colname);
8015929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
8115929be2SAndreas Gohr        $this->columns[] = $col;
8215929be2SAndreas Gohr    }
8315929be2SAndreas Gohr
8415929be2SAndreas Gohr    /**
8515929be2SAndreas Gohr     * Add sorting options
8615929be2SAndreas Gohr     *
8715929be2SAndreas Gohr     * Call multiple times for multiple columns. Be sure the referenced tables have been
8815929be2SAndreas Gohr     * added before
8915929be2SAndreas Gohr     *
9015929be2SAndreas Gohr     * @param string $colname may contain an alias
9115929be2SAndreas Gohr     * @param bool $asc sort direction (ASC = true, DESC = false)
9215929be2SAndreas Gohr     */
9315929be2SAndreas Gohr    public function addSort($colname, $asc = true) {
9415929be2SAndreas Gohr        $col = $this->findColumn($colname);
9515929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
9615929be2SAndreas Gohr
9707993756SAndreas Gohr        $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc);
9807993756SAndreas Gohr    }
9907993756SAndreas Gohr
10007993756SAndreas Gohr    /**
10107993756SAndreas Gohr     * Returns all set sort columns
10207993756SAndreas Gohr     *
10307993756SAndreas Gohr     * @return array
10407993756SAndreas Gohr     */
10507993756SAndreas Gohr    public function getSorts() {
10607993756SAndreas Gohr        return $this->sortby;
10715929be2SAndreas Gohr    }
10815929be2SAndreas Gohr
10915929be2SAndreas Gohr    /**
1109d7a36f9SAndreas Gohr     * Adds a filter
11115929be2SAndreas Gohr     *
11215929be2SAndreas Gohr     * @param string $colname may contain an alias
11315929be2SAndreas Gohr     * @param string $value
1145511bd5bSAndreas Gohr     * @param string $comp @see self::COMPARATORS
1154c13ff97SAndreas Gohr     * @param string $op either 'OR' or 'AND'
11615929be2SAndreas Gohr     */
1174c13ff97SAndreas Gohr    public function addFilter($colname, $value, $comp, $op = 'OR') {
1189dbc32aeSAndreas Gohr        /* Convert certain filters into others
1199dbc32aeSAndreas Gohr         * this reduces the number of supported filters to implement in types */
1209dbc32aeSAndreas Gohr        if ($comp == '*~') {
1219dbc32aeSAndreas Gohr            $value = '*' . $value . '*';
1229dbc32aeSAndreas Gohr            $comp = '~';
1239dbc32aeSAndreas Gohr        } elseif ($comp == '<>') {
1249dbc32aeSAndreas Gohr            $comp = '!=';
1259dbc32aeSAndreas Gohr        }
1269dbc32aeSAndreas Gohr
12774461852SAndreas Gohr        if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS));
1284c13ff97SAndreas Gohr        if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed');
1299d7a36f9SAndreas Gohr
13015929be2SAndreas Gohr        $col = $this->findColumn($colname);
1317da1a0fdSAndreas Gohr        if(!$col) return; // ignore missing columns, filter might have been for different schema
1327da1a0fdSAndreas Gohr
1337da1a0fdSAndreas Gohr        // map filter operators to SQL syntax
1347da1a0fdSAndreas Gohr        switch($comp) {
1357da1a0fdSAndreas Gohr            case '~':
1367da1a0fdSAndreas Gohr                $comp = 'LIKE';
1377da1a0fdSAndreas Gohr                break;
1387da1a0fdSAndreas Gohr            case '!~':
1397da1a0fdSAndreas Gohr                $comp = 'NOT LIKE';
1407da1a0fdSAndreas Gohr                break;
1417da1a0fdSAndreas Gohr            case '=*':
1427da1a0fdSAndreas Gohr                $comp = 'REGEXP';
1437da1a0fdSAndreas Gohr                break;
1447da1a0fdSAndreas Gohr        }
1457da1a0fdSAndreas Gohr
1467da1a0fdSAndreas Gohr        // we use asterisks, but SQL wants percents
1477da1a0fdSAndreas Gohr        if($comp == 'LIKE' || $comp == 'NOT LIKE') {
14899894f21SMichael Große            $value = str_replace('*','%',$value);
1497da1a0fdSAndreas Gohr        }
1507da1a0fdSAndreas Gohr
1517da1a0fdSAndreas Gohr        // add the filter
1524c13ff97SAndreas Gohr        $this->filter[] = array($col, $value, $comp, $op);
15315929be2SAndreas Gohr    }
15415929be2SAndreas Gohr
15515929be2SAndreas Gohr    /**
1567f9cb794SAndreas Gohr     * Set offset for the results
1577f9cb794SAndreas Gohr     *
1587f9cb794SAndreas Gohr     * @param int $offset
1597f9cb794SAndreas Gohr     */
1607f9cb794SAndreas Gohr    public function setOffset($offset) {
1617f9cb794SAndreas Gohr        $limit = 0;
1627f9cb794SAndreas Gohr        if($this->range_end) {
1637f9cb794SAndreas Gohr            // if there was a limit set previously, the range_end needs to be recalculated
1647f9cb794SAndreas Gohr            $limit = $this->range_end - $this->range_begin;
1657f9cb794SAndreas Gohr        }
1667f9cb794SAndreas Gohr        $this->range_begin = $offset;
1677f9cb794SAndreas Gohr        if($limit) $this->setLimit($limit);
1687f9cb794SAndreas Gohr    }
1697f9cb794SAndreas Gohr
1707f9cb794SAndreas Gohr    /**
1717f9cb794SAndreas Gohr     * Limit results to this number
1727f9cb794SAndreas Gohr     *
1737f9cb794SAndreas Gohr     * @param int $limit Set to 0 to disable limit again
1747f9cb794SAndreas Gohr     */
1757f9cb794SAndreas Gohr    public function setLimit($limit) {
1767f9cb794SAndreas Gohr        if($limit) {
1777f9cb794SAndreas Gohr            $this->range_end = $this->range_begin + $limit;
1787f9cb794SAndreas Gohr        } else {
1797f9cb794SAndreas Gohr            $this->range_end = 0;
1807f9cb794SAndreas Gohr        }
1817f9cb794SAndreas Gohr    }
1827f9cb794SAndreas Gohr
1837f9cb794SAndreas Gohr    /**
1847f9cb794SAndreas Gohr     * Return the number of results (regardless of limit and offset settings)
1857f9cb794SAndreas Gohr     *
1867f9cb794SAndreas Gohr     * Use this to implement paging. Important: this may only be called after running @see execute()
1877f9cb794SAndreas Gohr     *
1887f9cb794SAndreas Gohr     * @return int
1897f9cb794SAndreas Gohr     */
1907f9cb794SAndreas Gohr    public function getCount() {
1917f9cb794SAndreas Gohr        if($this->count < 0) throw new StructException('Count is only accessible after executing the search');
1927f9cb794SAndreas Gohr        return $this->count;
1937f9cb794SAndreas Gohr    }
1947f9cb794SAndreas Gohr
1957f9cb794SAndreas Gohr    /**
196b2ed727aSAndreas Gohr     * Execute this search and return the result
197b2ed727aSAndreas Gohr     *
19838fa36fbSAndreas Gohr     * The result is a two dimensional array of Value()s.
1997f9cb794SAndreas Gohr     *
2007f9cb794SAndreas Gohr     * This will always query for the full result (not using offset and limit) and then
2017f9cb794SAndreas Gohr     * return the wanted range, setting the count (@see getCount) to the whole result number
20238fa36fbSAndreas Gohr     *
20338fa36fbSAndreas Gohr     * @return Value[][]
204b2ed727aSAndreas Gohr     */
205b2ed727aSAndreas Gohr    public function execute() {
206b2ed727aSAndreas Gohr        list($sql, $opts) = $this->getSQL();
207b2ed727aSAndreas Gohr
2087f9cb794SAndreas Gohr        /** @var \PDOStatement $res */
209b2ed727aSAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
21059b668aaSAndreas Gohr        if($res === false) throw new StructException("SQL execution failed for\n\n$sql");
211b2ed727aSAndreas Gohr
212b2ed727aSAndreas Gohr        $result = array();
2137f9cb794SAndreas Gohr        $cursor = -1;
2147f9cb794SAndreas Gohr        while($row = $res->fetch(\PDO::FETCH_ASSOC)) {
2159dbc8ec0SMichael Grosse            if ($this->isRowEmpty($row)) {
2169dbc8ec0SMichael Grosse                continue;
2179dbc8ec0SMichael Grosse            }
2187f9cb794SAndreas Gohr            $cursor++;
2197f9cb794SAndreas Gohr            if($cursor < $this->range_begin) continue;
2207f9cb794SAndreas Gohr            if($this->range_end && $cursor >= $this->range_end) continue;
2217f9cb794SAndreas Gohr
222b2ed727aSAndreas Gohr            $C = 0;
223b2ed727aSAndreas Gohr            $resrow = array();
224b2ed727aSAndreas Gohr            foreach($this->columns as $col) {
22538fa36fbSAndreas Gohr                $val = $row["C$C"];
226b2ed727aSAndreas Gohr                if($col->isMulti()) {
22738fa36fbSAndreas Gohr                    $val = explode(self::CONCAT_SEPARATOR, $val);
228b2ed727aSAndreas Gohr                }
22938fa36fbSAndreas Gohr                $resrow[] = new Value($col, $val);
230b2ed727aSAndreas Gohr                $C++;
231b2ed727aSAndreas Gohr            }
232b2ed727aSAndreas Gohr            $result[] = $resrow;
233b2ed727aSAndreas Gohr        }
2347f9cb794SAndreas Gohr
2357f9cb794SAndreas Gohr        $this->sqlite->res_close($res);
2367f9cb794SAndreas Gohr        $this->count = $cursor + 1;
237b2ed727aSAndreas Gohr        return $result;
238b2ed727aSAndreas Gohr    }
239b2ed727aSAndreas Gohr
240b2ed727aSAndreas Gohr    /**
24115929be2SAndreas Gohr     * Transform the set search parameters into a statement
24215929be2SAndreas Gohr     *
243b2ed727aSAndreas Gohr     * @return array ($sql, $opts) The SQL and parameters to execute
24415929be2SAndreas Gohr     */
24515929be2SAndreas Gohr    public function getSQL() {
2465511bd5bSAndreas Gohr        if(!$this->columns) throw new StructException('nocolname');
24715929be2SAndreas Gohr
2482f68434dSAndreas Gohr        $QB = new QueryBuilder();
24915929be2SAndreas Gohr
2509d7a36f9SAndreas Gohr        // basic tables
251b2d67e7dSAndreas Gohr        $first_table = '';
2529d7a36f9SAndreas Gohr        foreach($this->schemas as $schema) {
2532f68434dSAndreas Gohr            $datatable = 'data_'.$schema->getTable();
254b2d67e7dSAndreas Gohr            if($first_table) {
2559d7a36f9SAndreas Gohr                // follow up tables
2562f68434dSAndreas Gohr                $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid");
2579d7a36f9SAndreas Gohr            } else {
2589d7a36f9SAndreas Gohr                // first table
2592f68434dSAndreas Gohr                $QB->addTable('schema_assignments');
2602f68434dSAndreas Gohr                $QB->addTable($datatable);
2612f68434dSAndreas Gohr                $QB->addSelectColumn($datatable, 'pid', 'PID');
2622f68434dSAndreas Gohr                $QB->addGroupByColumn($datatable, 'pid');
263b75e0a08SAndreas Gohr
2642f68434dSAndreas Gohr                $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid");
2652f68434dSAndreas Gohr                $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'");
2662f68434dSAndreas Gohr                $QB->filters()->whereAnd("schema_assignments.assigned = 1");
2672f68434dSAndreas Gohr                $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0");
2682f68434dSAndreas Gohr                $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1");
269b2d67e7dSAndreas Gohr
2702f68434dSAndreas Gohr                $first_table = $datatable;
27115929be2SAndreas Gohr            }
2722f68434dSAndreas Gohr            $QB->filters()->whereAnd("$datatable.latest = 1");
2739d7a36f9SAndreas Gohr        }
27415929be2SAndreas Gohr
27515929be2SAndreas Gohr        // columns to select, handling multis
2769d7a36f9SAndreas Gohr        $sep = self::CONCAT_SEPARATOR;
27715929be2SAndreas Gohr        $n = 0;
27815929be2SAndreas Gohr        foreach($this->columns as $col) {
27915929be2SAndreas Gohr            $CN = 'C' . $n++;
28015929be2SAndreas Gohr
281d1b04e89SAndreas Gohr            if($col->isMulti()) {
2822f68434dSAndreas Gohr                $datatable = "data_{$col->getTable()}";
2832f68434dSAndreas Gohr                $multitable = "multi_{$col->getTable()}";
2842f68434dSAndreas Gohr                $MN = 'M' . $col->getColref();
2852f68434dSAndreas Gohr
2862f68434dSAndreas Gohr                $QB->addLeftJoin(
2872f68434dSAndreas Gohr                    $datatable,
2882f68434dSAndreas Gohr                    $multitable,
2892f68434dSAndreas Gohr                    $MN,
2902f68434dSAndreas Gohr                    "$datatable.pid = $MN.pid AND
2912f68434dSAndreas Gohr                     $datatable.rev = $MN.rev AND
2922f68434dSAndreas Gohr                     $MN.colref = {$col->getColref()}"
2932f68434dSAndreas Gohr                );
294578407b2SAndreas Gohr
295578407b2SAndreas Gohr                $col->getType()->select($QB, $MN, 'value' , $CN);
296578407b2SAndreas Gohr                $sel = $QB->getSelectStatement($CN);
297578407b2SAndreas Gohr                $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN);
29815929be2SAndreas Gohr            } else {
299578407b2SAndreas Gohr                $col->getType()->select($QB, 'data_'.$col->getTable(), $col->getColName() , $CN);
3000dbe7bf6SAndreas Gohr                $QB->addGroupByStatement($CN);
30115929be2SAndreas Gohr            }
30215929be2SAndreas Gohr        }
30315929be2SAndreas Gohr
3049d7a36f9SAndreas Gohr        // where clauses
305b8fe6730SAndreas Gohr        foreach($this->filter as $filter) {
3064c13ff97SAndreas Gohr            list($col, $value, $comp, $op) = $filter;
307b8fe6730SAndreas Gohr
3082f68434dSAndreas Gohr            $datatable = "data_{$col->getTable()}";
3092f68434dSAndreas Gohr            $multitable = "multi_{$col->getTable()}";
3109d7a36f9SAndreas Gohr
3119d7a36f9SAndreas Gohr            /** @var $col Column */
3129d7a36f9SAndreas Gohr            if($col->isMulti()) {
3132f68434dSAndreas Gohr                $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before
31415929be2SAndreas Gohr
3152f68434dSAndreas Gohr                $QB->addLeftJoin(
3162f68434dSAndreas Gohr                    $datatable,
3172f68434dSAndreas Gohr                    $multitable,
3182f68434dSAndreas Gohr                    $MN,
3192f68434dSAndreas Gohr                    "$datatable.pid = $MN.pid AND
3202f68434dSAndreas Gohr                     $datatable.rev = $MN.rev AND
3212f68434dSAndreas Gohr                     $MN.colref = {$col->getColref()}"
3222f68434dSAndreas Gohr                );
323690e4ebaSAndreas Gohr                $coltbl = $MN;
324690e4ebaSAndreas Gohr                $colnam = 'value';
3259d7a36f9SAndreas Gohr            } else {
326690e4ebaSAndreas Gohr                $coltbl = $datatable;
327690e4ebaSAndreas Gohr                $colnam = $col->getColName();
3289d7a36f9SAndreas Gohr            }
329690e4ebaSAndreas Gohr            $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter
3309d7a36f9SAndreas Gohr        }
3319d7a36f9SAndreas Gohr
33229394e6cSAndreas Gohr        // sorting - we always sort by the single val column
333b8fe6730SAndreas Gohr        foreach($this->sortby as $sort) {
334b8fe6730SAndreas Gohr            list($col, $asc) = $sort;
3359d7a36f9SAndreas Gohr            /** @var $col Column */
336578407b2SAndreas Gohr            $QB->addOrderBy($col->getFullColName(false) . ' '.(($asc) ? 'ASC' : 'DESC'));
3379e07bdbfSAndreas Gohr        }
3389e07bdbfSAndreas Gohr
3392f68434dSAndreas Gohr        return $QB->getSQL();
34015929be2SAndreas Gohr    }
34115929be2SAndreas Gohr
34215929be2SAndreas Gohr    /**
34301dd90deSAndreas Gohr     * Returns all the columns that where added to the search
34401dd90deSAndreas Gohr     *
34501dd90deSAndreas Gohr     * @return Column[]
34601dd90deSAndreas Gohr     */
34701dd90deSAndreas Gohr    public function getColumns() {
34801dd90deSAndreas Gohr        return $this->columns;
34901dd90deSAndreas Gohr    }
35001dd90deSAndreas Gohr
35101dd90deSAndreas Gohr
35201dd90deSAndreas Gohr    /**
35315929be2SAndreas Gohr     * Find a column to be used in the search
35415929be2SAndreas Gohr     *
35515929be2SAndreas Gohr     * @param string $colname may contain an alias
35615929be2SAndreas Gohr     * @return bool|Column
35715929be2SAndreas Gohr     */
35800f6af48SAndreas Gohr    public function findColumn($colname) {
3595511bd5bSAndreas Gohr        if(!$this->schemas) throw new StructException('noschemas');
36015929be2SAndreas Gohr
361*128d4e83SAndreas Gohr        // handling of page and title column is special - we add a "fake" column
362df02ffe6SMichael Große        $schema_list = array_keys($this->schemas);
363*128d4e83SAndreas Gohr        if($colname == '%pageid%') {
364*128d4e83SAndreas Gohr            return new PageColumn(0, new Page(), $schema_list[0]);
365d1b04e89SAndreas Gohr        }
366*128d4e83SAndreas Gohr        if($colname == '%title%') {
367*128d4e83SAndreas Gohr            return new PageColumn(0, new Page(array('usetitles' => true)),  $schema_list[0]);
368*128d4e83SAndreas Gohr        }
369d1b04e89SAndreas Gohr
37015929be2SAndreas Gohr        // resolve the alias or table name
37115929be2SAndreas Gohr        list($table, $colname) = explode('.', $colname, 2);
37215929be2SAndreas Gohr        if(!$colname) {
37315929be2SAndreas Gohr            $colname = $table;
37415929be2SAndreas Gohr            $table = '';
37515929be2SAndreas Gohr        }
37615929be2SAndreas Gohr        if($table && isset($this->aliases[$table])) {
37715929be2SAndreas Gohr            $table = $this->aliases[$table];
37815929be2SAndreas Gohr        }
37915929be2SAndreas Gohr
3805511bd5bSAndreas Gohr        if(!$colname) throw new StructException('nocolname');
38115929be2SAndreas Gohr
382bd363da9SAndreas Gohr        // if table name given search only that, otherwise try all for matching column name
38315929be2SAndreas Gohr        if($table) {
38415929be2SAndreas Gohr            $schemas = array($table => $this->schemas[$table]);
38515929be2SAndreas Gohr        } else {
38615929be2SAndreas Gohr            $schemas = $this->schemas;
38715929be2SAndreas Gohr        }
38815929be2SAndreas Gohr
38915929be2SAndreas Gohr        // find it
39015929be2SAndreas Gohr        $col = false;
39115929be2SAndreas Gohr        foreach($schemas as $schema) {
3924ac44d1cSMichael Grosse            if(empty($schema)) {
3934ac44d1cSMichael Grosse                continue;
3944ac44d1cSMichael Grosse            }
39515929be2SAndreas Gohr            $col = $schema->findColumn($colname);
39615929be2SAndreas Gohr            if($col) break;
39715929be2SAndreas Gohr        }
39815929be2SAndreas Gohr
39915929be2SAndreas Gohr        return $col;
40015929be2SAndreas Gohr    }
40115929be2SAndreas Gohr
4029dbc8ec0SMichael Grosse    /**
4039dbc8ec0SMichael Grosse     * Check if a row is empty / only contains a reference to itself
4049dbc8ec0SMichael Grosse     *
4059dbc8ec0SMichael Grosse     * @param array $rowColumns an array as returned from the database
4069dbc8ec0SMichael Grosse     * @return bool
4079dbc8ec0SMichael Grosse     */
4089dbc8ec0SMichael Grosse    private function isRowEmpty($rowColumns) {
4099dbc8ec0SMichael Grosse        $C = 0;
4109dbc8ec0SMichael Grosse        foreach($this->columns as $col) {
4119dbc8ec0SMichael Grosse            $val = $rowColumns["C$C"];
4129dbc8ec0SMichael Grosse            $C += 1;
413c9494930SMichael Grosse            if (blank($val) || is_a($col->getType(),'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) {
4149dbc8ec0SMichael Grosse                continue;
4159dbc8ec0SMichael Grosse            }
4169dbc8ec0SMichael Grosse            return false;
4179dbc8ec0SMichael Grosse        }
4189dbc8ec0SMichael Grosse        return true;
4199dbc8ec0SMichael Grosse    }
4209dbc8ec0SMichael Grosse
42115929be2SAndreas Gohr}
42215929be2SAndreas Gohr
4235511bd5bSAndreas Gohr
424