xref: /plugin/struct/meta/Search.php (revision fc26989e4442674ded604c12f445cb8ca3510cf9)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5use dokuwiki\plugin\struct\types\DateTime;
6use dokuwiki\plugin\struct\types\Page;
7
8class Search {
9    /**
10     * This separator will be used to concat multi values to flatten them in the result set
11     */
12    const CONCAT_SEPARATOR = "\n!_-_-_-_-_!\n";
13
14    /**
15     * The list of known and allowed comparators
16     * (order matters)
17     */
18    static public $COMPARATORS = array(
19        '<=', '>=', '=*', '=', '<', '>', '!=', '!~', '~',
20    );
21
22    /** @var  \helper_plugin_sqlite */
23    protected $sqlite;
24
25    /** @var Schema[] list of schemas to query */
26    protected $schemas = array();
27
28    /** @var Column[] list of columns to select */
29    protected $columns = array();
30
31    /** @var array the sorting of the result */
32    protected $sortby = array();
33
34    /** @var array the filters */
35    protected $filter = array();
36
37    /** @var array list of aliases tables can be referenced by */
38    protected $aliases = array();
39
40    /** @var  int begin results from here */
41    protected $range_begin = 0;
42
43    /** @var  int end results here */
44    protected $range_end = 0;
45
46    /** @var int the number of results */
47    protected $count = -1;
48    /** @var  string[] the PIDs of the result rows */
49    protected $result_pids = null;
50
51    /**
52     * Search constructor.
53     */
54    public function __construct() {
55        /** @var \helper_plugin_struct_db $plugin */
56        $plugin = plugin_load('helper', 'struct_db');
57        $this->sqlite = $plugin->getDB();
58    }
59
60    /**
61     * Add a schema to be searched
62     *
63     * Call multiple times for multiple schemas.
64     *
65     * @param string $table
66     * @param string $alias
67     */
68    public function addSchema($table, $alias = '') {
69        $schema = new Schema($table);
70        if(!$schema->getId()) {
71            throw new StructException('schema missing', $table);
72        }
73
74        if($this->schemas &&
75            (
76                $schema->isLookup() ||
77                reset($this->schemas)->isLookup()
78            )
79        ) {
80            throw new StructException('nolookupmix');
81        }
82
83        $this->schemas[$table] = $schema;
84        if($alias) $this->aliases[$alias] = $table;
85    }
86
87    /**
88     * Add a column to be returned by the search
89     *
90     * Call multiple times for multiple columns. Be sure the referenced tables have been
91     * added before
92     *
93     * @param string $colname may contain an alias
94     */
95    public function addColumn($colname) {
96        if($this->processWildcard($colname)) return; // wildcard?
97        $col = $this->findColumn($colname);
98        if(!$col) return; //FIXME do we really want to ignore missing columns?
99        $this->columns[] = $col;
100    }
101
102    /**
103     * Add sorting options
104     *
105     * Call multiple times for multiple columns. Be sure the referenced tables have been
106     * added before
107     *
108     * @param string $colname may contain an alias
109     * @param bool $asc sort direction (ASC = true, DESC = false)
110     */
111    public function addSort($colname, $asc = true) {
112        $col = $this->findColumn($colname);
113        if(!$col) return; //FIXME do we really want to ignore missing columns?
114
115        $this->sortby[$col->getFullQualifiedLabel()] = array($col, $asc);
116    }
117
118    /**
119     * Returns all set sort columns
120     *
121     * @return array
122     */
123    public function getSorts() {
124        return $this->sortby;
125    }
126
127    /**
128     * Adds a filter
129     *
130     * @param string $colname may contain an alias
131     * @param string|string[] $value
132     * @param string $comp @see self::COMPARATORS
133     * @param string $op either 'OR' or 'AND'
134     */
135    public function addFilter($colname, $value, $comp, $op = 'OR') {
136        /* Convert certain filters into others
137         * this reduces the number of supported filters to implement in types */
138        if($comp == '*~') {
139            $value = $this->filterWrapAsterisks($value);
140            $comp = '~';
141        } elseif($comp == '<>') {
142            $comp = '!=';
143        }
144
145        if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS));
146        if($op != 'OR' && $op != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed');
147
148        $col = $this->findColumn($colname);
149        if(!$col) return; // ignore missing columns, filter might have been for different schema
150
151        // map filter operators to SQL syntax
152        switch($comp) {
153            case '~':
154                $comp = 'LIKE';
155                break;
156            case '!~':
157                $comp = 'NOT LIKE';
158                break;
159            case '=*':
160                $comp = 'REGEXP';
161                break;
162        }
163
164        // we use asterisks, but SQL wants percents
165        if($comp == 'LIKE' || $comp == 'NOT LIKE') {
166            $value = $this->filterChangeToLike($value);
167        }
168
169        // add the filter
170        $this->filter[] = array($col, $value, $comp, $op);
171    }
172
173    /**
174     * Wrap given value in asterisks
175     *
176     * @param string|string[] $value
177     * @return string|string[]
178     */
179    protected function filterWrapAsterisks($value) {
180        $map = function ($input) {
181            return "*$input*";
182        };
183
184        if(is_array($value)) {
185            $value = array_map($map, $value);
186        } else {
187            $value = $map($value);
188        }
189        return $value;
190    }
191
192    /**
193     * Change given string to use % instead of *
194     *
195     * @param string|string[] $value
196     * @return string|string[]
197     */
198    protected function filterChangeToLike($value) {
199        $map = function ($input) {
200            return str_replace('*', '%', $input);
201        };
202
203        if(is_array($value)) {
204            $value = array_map($map, $value);
205        } else {
206            $value = $map($value);
207        }
208        return $value;
209    }
210
211    /**
212     * Set offset for the results
213     *
214     * @param int $offset
215     */
216    public function setOffset($offset) {
217        $limit = 0;
218        if($this->range_end) {
219            // if there was a limit set previously, the range_end needs to be recalculated
220            $limit = $this->range_end - $this->range_begin;
221        }
222        $this->range_begin = $offset;
223        if($limit) $this->setLimit($limit);
224    }
225
226    /**
227     * Limit results to this number
228     *
229     * @param int $limit Set to 0 to disable limit again
230     */
231    public function setLimit($limit) {
232        if($limit) {
233            $this->range_end = $this->range_begin + $limit;
234        } else {
235            $this->range_end = 0;
236        }
237    }
238
239    /**
240     * Return the number of results (regardless of limit and offset settings)
241     *
242     * Use this to implement paging. Important: this may only be called after running @see execute()
243     *
244     * @return int
245     */
246    public function getCount() {
247        if($this->count < 0) throw new StructException('Count is only accessible after executing the search');
248        return $this->count;
249    }
250
251    /**
252     * Returns the PID associated with each result row
253     *
254     * Important: this may only be called after running @see execute()
255     *
256     * @return \string[]
257     */
258    public function getPids() {
259        if($this->result_pids === null) throw new StructException('PIDs are only accessible after executing the search');
260        return $this->result_pids;
261    }
262
263    /**
264     * Execute this search and return the result
265     *
266     * The result is a two dimensional array of Value()s.
267     *
268     * This will always query for the full result (not using offset and limit) and then
269     * return the wanted range, setting the count (@see getCount) to the whole result number
270     *
271     * @return Value[][]
272     */
273    public function execute() {
274        list($sql, $opts) = $this->getSQL();
275
276        /** @var \PDOStatement $res */
277        $res = $this->sqlite->query($sql, $opts);
278        if($res === false) throw new StructException("SQL execution failed for\n\n$sql");
279
280        $this->result_pids = array();
281        $result = array();
282        $cursor = -1;
283        while($row = $res->fetch(\PDO::FETCH_ASSOC)) {
284            if($this->isRowEmpty($row)) {
285                continue;
286            }
287            $cursor++;
288            if($cursor < $this->range_begin) continue;
289            if($this->range_end && $cursor >= $this->range_end) continue;
290
291            $this->result_pids[] = $row['PID'];
292
293            $C = 0;
294            $resrow = array();
295            foreach($this->columns as $col) {
296                $val = $row["C$C"];
297                if($col->isMulti()) {
298                    $val = explode(self::CONCAT_SEPARATOR, $val);
299                }
300                $resrow[] = new Value($col, $val);
301                $C++;
302            }
303            $result[] = $resrow;
304        }
305
306        $this->sqlite->res_close($res);
307        $this->count = $cursor + 1;
308        return $result;
309    }
310
311    /**
312     * Transform the set search parameters into a statement
313     *
314     * @return array ($sql, $opts) The SQL and parameters to execute
315     */
316    public function getSQL() {
317        if(!$this->columns) throw new StructException('nocolname');
318
319        $QB = new QueryBuilder();
320
321        // basic tables
322        $first_table = '';
323        foreach($this->schemas as $schema) {
324            $datatable = 'data_' . $schema->getTable();
325            if($first_table) {
326                // follow up tables
327                $QB->addLeftJoin($first_table, $datatable, $datatable, "$first_table.pid = $datatable.pid");
328            } else {
329                // first table
330
331                if(!$schema->isLookup()) {
332                    $QB->addTable('schema_assignments');
333                    $QB->filters()->whereAnd("$datatable.pid = schema_assignments.pid");
334                    $QB->filters()->whereAnd("schema_assignments.tbl = '{$schema->getTable()}'");
335                    $QB->filters()->whereAnd("schema_assignments.assigned = 1");
336                    $QB->filters()->whereAnd("GETACCESSLEVEL($datatable.pid) > 0");
337                    $QB->filters()->whereAnd("PAGEEXISTS($datatable.pid) = 1");
338                }
339
340                $QB->addTable($datatable);
341                $QB->addSelectColumn($datatable, 'pid', 'PID');
342                $QB->addGroupByColumn($datatable, 'pid');
343
344                $first_table = $datatable;
345            }
346            $QB->filters()->whereAnd("$datatable.latest = 1");
347        }
348
349        // columns to select, handling multis
350        $sep = self::CONCAT_SEPARATOR;
351        $n = 0;
352        foreach($this->columns as $col) {
353            $CN = 'C' . $n++;
354
355            if($col->isMulti()) {
356                $datatable = "data_{$col->getTable()}";
357                $multitable = "multi_{$col->getTable()}";
358                $MN = 'M' . $col->getColref();
359
360                $QB->addLeftJoin(
361                    $datatable,
362                    $multitable,
363                    $MN,
364                    "$datatable.pid = $MN.pid AND
365                     $datatable.rev = $MN.rev AND
366                     $MN.colref = {$col->getColref()}"
367                );
368
369                $col->getType()->select($QB, $MN, 'value', $CN);
370                $sel = $QB->getSelectStatement($CN);
371                $QB->addSelectStatement("GROUP_CONCAT($sel, '$sep')", $CN);
372            } else {
373                $col->getType()->select($QB, 'data_' . $col->getTable(), $col->getColName(), $CN);
374                $QB->addGroupByStatement($CN);
375            }
376        }
377
378        // where clauses
379        foreach($this->filter as $filter) {
380            list($col, $value, $comp, $op) = $filter;
381
382            $datatable = "data_{$col->getTable()}";
383            $multitable = "multi_{$col->getTable()}";
384
385            /** @var $col Column */
386            if($col->isMulti()) {
387                $MN = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before
388
389                $QB->addLeftJoin(
390                    $datatable,
391                    $multitable,
392                    $MN,
393                    "$datatable.pid = $MN.pid AND
394                     $datatable.rev = $MN.rev AND
395                     $MN.colref = {$col->getColref()}"
396                );
397                $coltbl = $MN;
398                $colnam = 'value';
399            } else {
400                $coltbl = $datatable;
401                $colnam = $col->getColName();
402            }
403
404            $col->getType()->filter($QB, $coltbl, $colnam, $comp, $value, $op); // type based filter
405        }
406
407        // sorting - we always sort by the single val column
408        foreach($this->sortby as $sort) {
409            list($col, $asc) = $sort;
410            /** @var $col Column */
411            $col->getType()->sort($QB, 'data_' . $col->getTable(), $col->getColName(false), $asc ? 'ASC' : 'DESC');
412        }
413
414        return $QB->getSQL();
415    }
416
417    /**
418     * Returns all the columns that where added to the search
419     *
420     * @return Column[]
421     */
422    public function getColumns() {
423        return $this->columns;
424    }
425
426    /**
427     * Checks if the given column is a * wildcard
428     *
429     * If it's a wildcard all matching columns are added to the column list, otherwise
430     * nothing happens
431     *
432     * @param string $colname
433     * @return bool was wildcard?
434     */
435    protected function processWildcard($colname) {
436        list($colname, $table) = $this->resolveColumn($colname);
437        if($colname !== '*') return false;
438
439        // no table given? assume the first is meant
440        if($table === null) {
441            $schema_list = array_keys($this->schemas);
442            $table = $schema_list[0];
443        }
444
445        $schema = $this->schemas[$table];
446        if(!$schema) return false;
447        $this->columns = array_merge($this->columns, $schema->getColumns(false));
448        return true;
449    }
450
451    /**
452     * Split a given column name into table and column
453     *
454     * Handles Aliases. Table might be null if none given.
455     *
456     * @param $colname
457     * @return array (colname, table)
458     */
459    protected function resolveColumn($colname) {
460        if(!$this->schemas) throw new StructException('noschemas');
461
462        // resolve the alias or table name
463        list($table, $colname) = explode('.', $colname, 2);
464        if(!$colname) {
465            $colname = $table;
466            $table = null;
467        }
468        if($table && isset($this->aliases[$table])) {
469            $table = $this->aliases[$table];
470        }
471
472        if(!$colname) throw new StructException('nocolname');
473
474        return array($colname, $table);
475    }
476
477    /**
478     * Find a column to be used in the search
479     *
480     * @param string $colname may contain an alias
481     * @return bool|Column
482     */
483    public function findColumn($colname) {
484        if(!$this->schemas) throw new StructException('noschemas');
485
486        // add "fake" column for special col (unless it's a lookup)
487        if(!(reset($this->schemas)->isLookup())) {
488            $schema_list = array_keys($this->schemas);
489            if($colname == '%pageid%') {
490                return new PageColumn(0, new Page(), $schema_list[0]);
491            }
492            if($colname == '%title%') {
493                return new PageColumn(0, new Page(array('usetitles' => true)), $schema_list[0]);
494            }
495            if($colname == '%lastupdate%') {
496                return new RevisionColumn(0, new DateTime(), $schema_list[0]);
497            }
498        }
499
500        list($colname, $table) = $this->resolveColumn($colname);
501
502        // if table name given search only that, otherwise try all for matching column name
503        if($table !== null) {
504            $schemas = array($table => $this->schemas[$table]);
505        } else {
506            $schemas = $this->schemas;
507        }
508
509        // find it
510        $col = false;
511        foreach($schemas as $schema) {
512            if(empty($schema)) {
513                continue;
514            }
515            $col = $schema->findColumn($colname);
516            if($col) break;
517        }
518
519        return $col;
520    }
521
522    /**
523     * Check if a row is empty / only contains a reference to itself
524     *
525     * @param array $rowColumns an array as returned from the database
526     * @return bool
527     */
528    private function isRowEmpty($rowColumns) {
529        $C = 0;
530        foreach($this->columns as $col) {
531            $val = $rowColumns["C$C"];
532            $C += 1;
533            if(blank($val) || is_a($col->getType(), 'dokuwiki\plugin\struct\types\Page') && $val == $rowColumns["PID"]) {
534                continue;
535            }
536            return false;
537        }
538        return true;
539    }
540
541}
542
543
544