xref: /plugin/struct/meta/Search.php (revision 578407b2ec997327ec4fa24b752fecf51d223fd1)
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
155511bd5bSAndreas Gohr     */
1674461852SAndreas Gohr    static public $COMPARATORS = array(
1701a8eccdSMichael Große        '<=', '>=', '=', '<', '>', '!=', '!~', '~'
185511bd5bSAndreas Gohr    );
195511bd5bSAndreas Gohr
209d7a36f9SAndreas Gohr    /** @var  \helper_plugin_sqlite */
219d7a36f9SAndreas Gohr    protected $sqlite;
2215929be2SAndreas Gohr
2315929be2SAndreas Gohr    /** @var Schema[] list of schemas to query */
2415929be2SAndreas Gohr    protected $schemas = array();
2515929be2SAndreas Gohr
2615929be2SAndreas Gohr    /** @var Column[] list of columns to select */
2715929be2SAndreas Gohr    protected $columns = array();
2815929be2SAndreas Gohr
2915929be2SAndreas Gohr    /** @var array the sorting of the result */
3015929be2SAndreas Gohr    protected $sortby = array();
3115929be2SAndreas Gohr
329d7a36f9SAndreas Gohr    /** @var array the filters */
339d7a36f9SAndreas Gohr    protected $filter = array();
3415929be2SAndreas Gohr
3515929be2SAndreas Gohr    /** @var array list of aliases tables can be referenced by */
3615929be2SAndreas Gohr    protected $aliases = array();
3715929be2SAndreas Gohr
387f9cb794SAndreas Gohr    /** @var  int begin results from here */
397f9cb794SAndreas Gohr    protected $range_begin = 0;
407f9cb794SAndreas Gohr
417f9cb794SAndreas Gohr    /** @var  int end results here */
427f9cb794SAndreas Gohr    protected $range_end = 0;
437f9cb794SAndreas Gohr
447f9cb794SAndreas Gohr    /** @var int the number of results */
457f9cb794SAndreas Gohr    protected $count = -1;
467f9cb794SAndreas Gohr
4715929be2SAndreas Gohr    /**
489d7a36f9SAndreas Gohr     * Search constructor.
499d7a36f9SAndreas Gohr     */
509d7a36f9SAndreas Gohr    public function __construct() {
519d7a36f9SAndreas Gohr        /** @var \helper_plugin_struct_db $plugin */
529d7a36f9SAndreas Gohr        $plugin = plugin_load('helper', 'struct_db');
539d7a36f9SAndreas Gohr        $this->sqlite = $plugin->getDB();
549d7a36f9SAndreas Gohr    }
559d7a36f9SAndreas Gohr
569d7a36f9SAndreas Gohr    /**
5715929be2SAndreas Gohr     * Add a schema to be searched
5815929be2SAndreas Gohr     *
5915929be2SAndreas Gohr     * Call multiple times for multiple schemas.
6015929be2SAndreas Gohr     *
6115929be2SAndreas Gohr     * @param string $table
6215929be2SAndreas Gohr     * @param string $alias
6315929be2SAndreas Gohr     */
6415929be2SAndreas Gohr    public function addSchema($table, $alias = '') {
6515929be2SAndreas Gohr        $this->schemas[$table] = new Schema($table);
6615929be2SAndreas Gohr        if($alias) $this->aliases[$alias] = $table;
6715929be2SAndreas Gohr    }
6815929be2SAndreas Gohr
6915929be2SAndreas Gohr    /**
7015929be2SAndreas Gohr     * Add a column to be returned by the search
7115929be2SAndreas Gohr     *
7215929be2SAndreas Gohr     * Call multiple times for multiple columns. Be sure the referenced tables have been
7315929be2SAndreas Gohr     * added before
7415929be2SAndreas Gohr     *
7515929be2SAndreas Gohr     * @param string $colname may contain an alias
7615929be2SAndreas Gohr     */
7715929be2SAndreas Gohr    public function addColumn($colname) {
7815929be2SAndreas Gohr        $col = $this->findColumn($colname);
7915929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
8015929be2SAndreas Gohr        $this->columns[] = $col;
8115929be2SAndreas Gohr    }
8215929be2SAndreas Gohr
8315929be2SAndreas Gohr    /**
8415929be2SAndreas Gohr     * Add sorting options
8515929be2SAndreas Gohr     *
8615929be2SAndreas Gohr     * Call multiple times for multiple columns. Be sure the referenced tables have been
8715929be2SAndreas Gohr     * added before
8815929be2SAndreas Gohr     *
8915929be2SAndreas Gohr     * @param string $colname may contain an alias
9015929be2SAndreas Gohr     * @param bool $asc sort direction (ASC = true, DESC = false)
9115929be2SAndreas Gohr     */
9215929be2SAndreas Gohr    public function addSort($colname, $asc = true) {
9315929be2SAndreas Gohr        $col = $this->findColumn($colname);
9415929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
9515929be2SAndreas Gohr
9607993756SAndreas Gohr        $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc);
9707993756SAndreas Gohr    }
9807993756SAndreas Gohr
9907993756SAndreas Gohr    /**
10007993756SAndreas Gohr     * Returns all set sort columns
10107993756SAndreas Gohr     *
10207993756SAndreas Gohr     * @return array
10307993756SAndreas Gohr     */
10407993756SAndreas Gohr    public function getSorts() {
10507993756SAndreas Gohr        return $this->sortby;
10615929be2SAndreas Gohr    }
10715929be2SAndreas Gohr
10815929be2SAndreas Gohr    /**
1099d7a36f9SAndreas Gohr     * Adds a filter
11015929be2SAndreas Gohr     *
11115929be2SAndreas Gohr     * @param string $colname may contain an alias
11215929be2SAndreas Gohr     * @param string $value
1135511bd5bSAndreas Gohr     * @param string $comp @see self::COMPARATORS
1144c13ff97SAndreas Gohr     * @param string $op either 'OR' or 'AND'
11515929be2SAndreas Gohr     */
1164c13ff97SAndreas Gohr    public function addFilter($colname, $value, $comp, $op = 'OR') {
1179dbc32aeSAndreas Gohr        /* Convert certain filters into others
1189dbc32aeSAndreas Gohr         * this reduces the number of supported filters to implement in types */
1199dbc32aeSAndreas Gohr        if ($comp == '*~') {
1209dbc32aeSAndreas Gohr            $value = '*' . $value . '*';
1219dbc32aeSAndreas Gohr            $comp = '~';
1229dbc32aeSAndreas Gohr        } elseif ($comp == '<>') {
1239dbc32aeSAndreas Gohr            $comp = '!=';
1249dbc32aeSAndreas Gohr        }
1259dbc32aeSAndreas Gohr
12674461852SAndreas Gohr        if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS));
1274c13ff97SAndreas Gohr        if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed');
1289d7a36f9SAndreas Gohr
12915929be2SAndreas Gohr        $col = $this->findColumn($colname);
13015929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
13199894f21SMichael Große        $value = str_replace('*','%',$value);
1324c13ff97SAndreas Gohr        $this->filter[] = array($col, $value, $comp, $op);
13315929be2SAndreas Gohr    }
13415929be2SAndreas Gohr
13515929be2SAndreas Gohr    /**
1367f9cb794SAndreas Gohr     * Set offset for the results
1377f9cb794SAndreas Gohr     *
1387f9cb794SAndreas Gohr     * @param int $offset
1397f9cb794SAndreas Gohr     */
1407f9cb794SAndreas Gohr    public function setOffset($offset) {
1417f9cb794SAndreas Gohr        $limit = 0;
1427f9cb794SAndreas Gohr        if($this->range_end) {
1437f9cb794SAndreas Gohr            // if there was a limit set previously, the range_end needs to be recalculated
1447f9cb794SAndreas Gohr            $limit = $this->range_end - $this->range_begin;
1457f9cb794SAndreas Gohr        }
1467f9cb794SAndreas Gohr        $this->range_begin = $offset;
1477f9cb794SAndreas Gohr        if($limit) $this->setLimit($limit);
1487f9cb794SAndreas Gohr    }
1497f9cb794SAndreas Gohr
1507f9cb794SAndreas Gohr    /**
1517f9cb794SAndreas Gohr     * Limit results to this number
1527f9cb794SAndreas Gohr     *
1537f9cb794SAndreas Gohr     * @param int $limit Set to 0 to disable limit again
1547f9cb794SAndreas Gohr     */
1557f9cb794SAndreas Gohr    public function setLimit($limit) {
1567f9cb794SAndreas Gohr        if($limit) {
1577f9cb794SAndreas Gohr            $this->range_end = $this->range_begin + $limit;
1587f9cb794SAndreas Gohr        } else {
1597f9cb794SAndreas Gohr            $this->range_end = 0;
1607f9cb794SAndreas Gohr        }
1617f9cb794SAndreas Gohr    }
1627f9cb794SAndreas Gohr
1637f9cb794SAndreas Gohr    /**
1647f9cb794SAndreas Gohr     * Return the number of results (regardless of limit and offset settings)
1657f9cb794SAndreas Gohr     *
1667f9cb794SAndreas Gohr     * Use this to implement paging. Important: this may only be called after running @see execute()
1677f9cb794SAndreas Gohr     *
1687f9cb794SAndreas Gohr     * @return int
1697f9cb794SAndreas Gohr     */
1707f9cb794SAndreas Gohr    public function getCount() {
1717f9cb794SAndreas Gohr        if($this->count < 0) throw new StructException('Count is only accessible after executing the search');
1727f9cb794SAndreas Gohr        return $this->count;
1737f9cb794SAndreas Gohr    }
1747f9cb794SAndreas Gohr
1757f9cb794SAndreas Gohr    /**
176b2ed727aSAndreas Gohr     * Execute this search and return the result
177b2ed727aSAndreas Gohr     *
17838fa36fbSAndreas Gohr     * The result is a two dimensional array of Value()s.
1797f9cb794SAndreas Gohr     *
1807f9cb794SAndreas Gohr     * This will always query for the full result (not using offset and limit) and then
1817f9cb794SAndreas Gohr     * return the wanted range, setting the count (@see getCount) to the whole result number
18238fa36fbSAndreas Gohr     *
18338fa36fbSAndreas Gohr     * @return Value[][]
184b2ed727aSAndreas Gohr     */
185b2ed727aSAndreas Gohr    public function execute() {
186b2ed727aSAndreas Gohr        list($sql, $opts) = $this->getSQL();
187b2ed727aSAndreas Gohr
1887f9cb794SAndreas Gohr        /** @var \PDOStatement $res */
189b2ed727aSAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
19059b668aaSAndreas Gohr        if($res === false) throw new StructException("SQL execution failed for\n\n$sql");
191b2ed727aSAndreas Gohr
192b2ed727aSAndreas Gohr        $result = array();
1937f9cb794SAndreas Gohr        $cursor = -1;
1947f9cb794SAndreas Gohr        while($row = $res->fetch(\PDO::FETCH_ASSOC)) {
1957f9cb794SAndreas Gohr            $cursor++;
1967f9cb794SAndreas Gohr            if($cursor < $this->range_begin) continue;
1977f9cb794SAndreas Gohr            if($this->range_end && $cursor >= $this->range_end) continue;
1987f9cb794SAndreas Gohr
199b2ed727aSAndreas Gohr            $C = 0;
200b2ed727aSAndreas Gohr            $resrow = array();
201b2ed727aSAndreas Gohr            foreach($this->columns as $col) {
20238fa36fbSAndreas Gohr                $val = $row["C$C"];
203b2ed727aSAndreas Gohr                if($col->isMulti()) {
20438fa36fbSAndreas Gohr                    $val = explode(self::CONCAT_SEPARATOR, $val);
205b2ed727aSAndreas Gohr                }
20638fa36fbSAndreas Gohr                $resrow[] = new Value($col, $val);
207b2ed727aSAndreas Gohr                $C++;
208b2ed727aSAndreas Gohr            }
209b2ed727aSAndreas Gohr            $result[] = $resrow;
210b2ed727aSAndreas Gohr        }
2117f9cb794SAndreas Gohr
2127f9cb794SAndreas Gohr        $this->sqlite->res_close($res);
2137f9cb794SAndreas Gohr        $this->count = $cursor + 1;
214b2ed727aSAndreas Gohr        return $result;
215b2ed727aSAndreas Gohr    }
216b2ed727aSAndreas Gohr
217b2ed727aSAndreas Gohr    /**
21815929be2SAndreas Gohr     * Transform the set search parameters into a statement
21915929be2SAndreas Gohr     *
220b2ed727aSAndreas Gohr     * @return array ($sql, $opts) The SQL and parameters to execute
22115929be2SAndreas Gohr     */
22215929be2SAndreas Gohr    public function getSQL() {
2235511bd5bSAndreas Gohr        if(!$this->columns) throw new StructException('nocolname');
22415929be2SAndreas Gohr
2252f68434dSAndreas Gohr        $QB = new QueryBuilder();
22615929be2SAndreas Gohr
2279d7a36f9SAndreas Gohr        // basic tables
228b2d67e7dSAndreas Gohr        $first_table = '';
2299d7a36f9SAndreas Gohr        foreach($this->schemas as $schema) {
2302f68434dSAndreas Gohr            $datatable = 'data_'.$schema->getTable();
231b2d67e7dSAndreas Gohr            if($first_table) {
2329d7a36f9SAndreas Gohr                // follow up tables
2332f68434dSAndreas Gohr                $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid");
2349d7a36f9SAndreas Gohr            } else {
2359d7a36f9SAndreas Gohr                // first table
2362f68434dSAndreas Gohr                $QB->addTable('schema_assignments');
2372f68434dSAndreas Gohr                $QB->addTable($datatable);
2382f68434dSAndreas Gohr                $QB->addSelectColumn($datatable, 'pid', 'PID');
2392f68434dSAndreas Gohr                $QB->addGroupByColumn($datatable, 'pid');
240b75e0a08SAndreas Gohr
2412f68434dSAndreas Gohr                $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid");
2422f68434dSAndreas Gohr                $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'");
2432f68434dSAndreas Gohr                $QB->filters()->whereAnd("schema_assignments.assigned = 1");
2442f68434dSAndreas Gohr                $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0");
2452f68434dSAndreas Gohr                $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1");
246b2d67e7dSAndreas Gohr
2472f68434dSAndreas Gohr                $first_table = $datatable;
24815929be2SAndreas Gohr            }
2492f68434dSAndreas Gohr            $QB->filters()->whereAnd("$datatable.latest = 1");
2509d7a36f9SAndreas Gohr        }
25115929be2SAndreas Gohr
25215929be2SAndreas Gohr        // columns to select, handling multis
2539d7a36f9SAndreas Gohr        $sep = self::CONCAT_SEPARATOR;
25415929be2SAndreas Gohr        $n = 0;
25515929be2SAndreas Gohr        foreach($this->columns as $col) {
25615929be2SAndreas Gohr            $CN = 'C' . $n++;
25715929be2SAndreas Gohr
258d1b04e89SAndreas Gohr            if($col->isMulti()) {
2592f68434dSAndreas Gohr                $datatable = "data_{$col->getTable()}";
2602f68434dSAndreas Gohr                $multitable = "multi_{$col->getTable()}";
2612f68434dSAndreas Gohr                $MN = 'M' . $col->getColref();
2622f68434dSAndreas Gohr
2632f68434dSAndreas Gohr                $QB->addLeftJoin(
2642f68434dSAndreas Gohr                    $datatable,
2652f68434dSAndreas Gohr                    $multitable,
2662f68434dSAndreas Gohr                    $MN,
2672f68434dSAndreas Gohr                    "$datatable.pid = $MN.pid AND
2682f68434dSAndreas Gohr                     $datatable.rev = $MN.rev AND
2692f68434dSAndreas Gohr                     $MN.colref = {$col->getColref()}"
2702f68434dSAndreas Gohr                );
271*578407b2SAndreas Gohr
272*578407b2SAndreas Gohr                $col->getType()->select($QB, $MN, 'value' , $CN);
273*578407b2SAndreas Gohr                $sel = $QB->getSelectStatement($CN);
274*578407b2SAndreas Gohr                $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN);
27515929be2SAndreas Gohr            } else {
276*578407b2SAndreas Gohr                $QB->addGroupByStatement($col->getFullColName());
277*578407b2SAndreas Gohr                $col->getType()->select($QB, 'data_'.$col->getTable(), $col->getColName() , $CN);
27815929be2SAndreas Gohr            }
27915929be2SAndreas Gohr        }
28015929be2SAndreas Gohr
2819d7a36f9SAndreas Gohr        // where clauses
282b8fe6730SAndreas Gohr        foreach($this->filter as $filter) {
2834c13ff97SAndreas Gohr            list($col, $value, $comp, $op) = $filter;
284b8fe6730SAndreas Gohr
2859d7a36f9SAndreas Gohr            /** @var $col Column */
2869d7a36f9SAndreas Gohr            if($col->isMulti()) {
2872f68434dSAndreas Gohr                $datatable = "data_{$col->getTable()}";
2882f68434dSAndreas Gohr                $multitable = "multi_{$col->getTable()}";
2892f68434dSAndreas Gohr                $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before
29015929be2SAndreas Gohr
2912f68434dSAndreas Gohr                $QB->addLeftJoin(
2922f68434dSAndreas Gohr                    $datatable,
2932f68434dSAndreas Gohr                    $multitable,
2942f68434dSAndreas Gohr                    $MN,
2952f68434dSAndreas Gohr                    "$datatable.pid = $MN.pid AND
2962f68434dSAndreas Gohr                     $datatable.rev = $MN.rev AND
2972f68434dSAndreas Gohr                     $MN.colref = {$col->getColref()}"
2982f68434dSAndreas Gohr                );
2992f68434dSAndreas Gohr                $column = "$MN.value";
3009d7a36f9SAndreas Gohr            } else {
301*578407b2SAndreas Gohr                $column = $col->getFullColName();
3029d7a36f9SAndreas Gohr            }
3039d7a36f9SAndreas Gohr
3049d7a36f9SAndreas Gohr            list($wsql, $wopt) = $col->getType()->compare($column, $comp, $value);
3059d7a36f9SAndreas Gohr
3062f68434dSAndreas Gohr            // FIXME temporary until compare() uses the query builder directly
3072f68434dSAndreas Gohr            foreach($wopt as $opt) {
3082f68434dSAndreas Gohr                $key = $QB->addValue($opt);
3092f68434dSAndreas Gohr                $wsql = preg_replace('/\?/', $key, $wsql, 1);
3102f68434dSAndreas Gohr            }
3112f68434dSAndreas Gohr
3124c13ff97SAndreas Gohr            $QB->filters()->where($op, $wsql);
3139d7a36f9SAndreas Gohr        }
3149d7a36f9SAndreas Gohr
31529394e6cSAndreas Gohr        // sorting - we always sort by the single val column
316b8fe6730SAndreas Gohr        foreach($this->sortby as $sort) {
317b8fe6730SAndreas Gohr            list($col, $asc) = $sort;
3189d7a36f9SAndreas Gohr            /** @var $col Column */
319*578407b2SAndreas Gohr            $QB->addOrderBy($col->getFullColName(false) . ' '.(($asc) ? 'ASC' : 'DESC'));
3209e07bdbfSAndreas Gohr        }
3219e07bdbfSAndreas Gohr
3222f68434dSAndreas Gohr        return $QB->getSQL();
32315929be2SAndreas Gohr    }
32415929be2SAndreas Gohr
32515929be2SAndreas Gohr    /**
32601dd90deSAndreas Gohr     * Returns all the columns that where added to the search
32701dd90deSAndreas Gohr     *
32801dd90deSAndreas Gohr     * @return Column[]
32901dd90deSAndreas Gohr     */
33001dd90deSAndreas Gohr    public function getColumns() {
33101dd90deSAndreas Gohr        return $this->columns;
33201dd90deSAndreas Gohr    }
33301dd90deSAndreas Gohr
33401dd90deSAndreas Gohr
33501dd90deSAndreas Gohr    /**
33615929be2SAndreas Gohr     * Find a column to be used in the search
33715929be2SAndreas Gohr     *
33815929be2SAndreas Gohr     * @param string $colname may contain an alias
33915929be2SAndreas Gohr     * @return bool|Column
34015929be2SAndreas Gohr     */
34100f6af48SAndreas Gohr    public function findColumn($colname) {
3425511bd5bSAndreas Gohr        if(!$this->schemas) throw new StructException('noschemas');
34315929be2SAndreas Gohr
344d1b04e89SAndreas Gohr        // handling of page column is special
345d3f94fb7SAndreas Gohr        if($colname == '%pageid%') {
346df02ffe6SMichael Große            $schema_list = array_keys($this->schemas);
347df02ffe6SMichael Große            return new PageColumn(0, new Page(), array_shift($schema_list));
348d1b04e89SAndreas Gohr        }
349d1b04e89SAndreas Gohr        // FIXME %title% needs to be handled here, too (later)
350d1b04e89SAndreas Gohr
35115929be2SAndreas Gohr        // resolve the alias or table name
35215929be2SAndreas Gohr        list($table, $colname) = explode('.', $colname, 2);
35315929be2SAndreas Gohr        if(!$colname) {
35415929be2SAndreas Gohr            $colname = $table;
35515929be2SAndreas Gohr            $table = '';
35615929be2SAndreas Gohr        }
35715929be2SAndreas Gohr        if($table && isset($this->aliases[$table])) {
35815929be2SAndreas Gohr            $table = $this->aliases[$table];
35915929be2SAndreas Gohr        }
36015929be2SAndreas Gohr
3615511bd5bSAndreas Gohr        if(!$colname) throw new StructException('nocolname');
36215929be2SAndreas Gohr
363bd363da9SAndreas Gohr        // if table name given search only that, otherwise try all for matching column name
36415929be2SAndreas Gohr        if($table) {
36515929be2SAndreas Gohr            $schemas = array($table => $this->schemas[$table]);
36615929be2SAndreas Gohr        } else {
36715929be2SAndreas Gohr            $schemas = $this->schemas;
36815929be2SAndreas Gohr        }
36915929be2SAndreas Gohr
37015929be2SAndreas Gohr        // find it
37115929be2SAndreas Gohr        $col = false;
37215929be2SAndreas Gohr        foreach($schemas as $schema) {
3734ac44d1cSMichael Grosse            if(empty($schema)) {
3744ac44d1cSMichael Grosse                continue;
3754ac44d1cSMichael Grosse            }
37615929be2SAndreas Gohr            $col = $schema->findColumn($colname);
37715929be2SAndreas Gohr            if($col) break;
37815929be2SAndreas Gohr        }
37915929be2SAndreas Gohr
38015929be2SAndreas Gohr        return $col;
38115929be2SAndreas Gohr    }
38215929be2SAndreas Gohr
38315929be2SAndreas Gohr}
38415929be2SAndreas Gohr
3855511bd5bSAndreas Gohr
386