xref: /plugin/struct/meta/Search.php (revision 5b2d2a8c1107b0d5bdd9023d2adf03f018da1eae)
115929be2SAndreas Gohr<?php
215929be2SAndreas Gohr
315929be2SAndreas Gohrnamespace plugin\struct\meta;
415929be2SAndreas Gohr
50561158fSAndreas Gohruse plugin\struct\types\Text;
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(
175511bd5bSAndreas Gohr        '<', '>', '<=', '>=', '!=', '!~', '~'
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
3815929be2SAndreas Gohr    /**
399d7a36f9SAndreas Gohr     * Search constructor.
409d7a36f9SAndreas Gohr     */
419d7a36f9SAndreas Gohr    public function __construct() {
429d7a36f9SAndreas Gohr        /** @var \helper_plugin_struct_db $plugin */
439d7a36f9SAndreas Gohr        $plugin = plugin_load('helper', 'struct_db');
449d7a36f9SAndreas Gohr        $this->sqlite = $plugin->getDB();
459d7a36f9SAndreas Gohr    }
469d7a36f9SAndreas Gohr
479d7a36f9SAndreas Gohr    /**
4815929be2SAndreas Gohr     * Add a schema to be searched
4915929be2SAndreas Gohr     *
5015929be2SAndreas Gohr     * Call multiple times for multiple schemas.
5115929be2SAndreas Gohr     *
5215929be2SAndreas Gohr     * @param string $table
5315929be2SAndreas Gohr     * @param string $alias
5415929be2SAndreas Gohr     */
5515929be2SAndreas Gohr    public function addSchema($table, $alias = '') {
5615929be2SAndreas Gohr        $this->schemas[$table] = new Schema($table);
5715929be2SAndreas Gohr        if($alias) $this->aliases[$alias] = $table;
5815929be2SAndreas Gohr    }
5915929be2SAndreas Gohr
6015929be2SAndreas Gohr    /**
6115929be2SAndreas Gohr     * Add a column to be returned by the search
6215929be2SAndreas Gohr     *
6315929be2SAndreas Gohr     * Call multiple times for multiple columns. Be sure the referenced tables have been
6415929be2SAndreas Gohr     * added before
6515929be2SAndreas Gohr     *
6615929be2SAndreas Gohr     * @param string $colname may contain an alias
6715929be2SAndreas Gohr     */
6815929be2SAndreas Gohr    public function addColumn($colname) {
6915929be2SAndreas Gohr        $col = $this->findColumn($colname);
7015929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
7115929be2SAndreas Gohr        $this->columns[] = $col;
7215929be2SAndreas Gohr    }
7315929be2SAndreas Gohr
7415929be2SAndreas Gohr    /**
7515929be2SAndreas Gohr     * Add sorting options
7615929be2SAndreas Gohr     *
7715929be2SAndreas Gohr     * Call multiple times for multiple columns. Be sure the referenced tables have been
7815929be2SAndreas Gohr     * added before
7915929be2SAndreas Gohr     *
8015929be2SAndreas Gohr     * @param string $colname may contain an alias
8115929be2SAndreas Gohr     * @param bool $asc sort direction (ASC = true, DESC = false)
8215929be2SAndreas Gohr     */
8315929be2SAndreas Gohr    public function addSort($colname, $asc = true) {
8415929be2SAndreas Gohr        $col = $this->findColumn($colname);
8515929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
8615929be2SAndreas Gohr
8715929be2SAndreas Gohr        $this->sortby[] = array($col, $asc);
8815929be2SAndreas Gohr    }
8915929be2SAndreas Gohr
9015929be2SAndreas Gohr    /**
919d7a36f9SAndreas Gohr     * Adds a filter
9215929be2SAndreas Gohr     *
9315929be2SAndreas Gohr     * @param string $colname may contain an alias
9415929be2SAndreas Gohr     * @param string $value
955511bd5bSAndreas Gohr     * @param string $comp @see self::COMPARATORS
969d7a36f9SAndreas Gohr     * @param string $type either 'OR' or 'AND'
9715929be2SAndreas Gohr     */
989d7a36f9SAndreas Gohr    public function addFilter($colname, $value, $comp, $type = 'OR') {
9974461852SAndreas Gohr        if(!in_array($comp, self::$COMPARATORS)) throw new StructException("Bad comperator. Use " . join(',', self::$COMPARATORS));
1005511bd5bSAndreas Gohr        if($type != 'OR' && $type != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed');
1019d7a36f9SAndreas Gohr
10215929be2SAndreas Gohr        $col = $this->findColumn($colname);
10315929be2SAndreas Gohr        if(!$col) return; //FIXME do we really want to ignore missing columns?
10415929be2SAndreas Gohr
1059d7a36f9SAndreas Gohr        $this->filter[] = array($col, $value, $comp, $type);
10615929be2SAndreas Gohr    }
10715929be2SAndreas Gohr
10815929be2SAndreas Gohr    /**
109b2ed727aSAndreas Gohr     * Execute this search and return the result
110b2ed727aSAndreas Gohr     *
111b2ed727aSAndreas Gohr     * The result is a two dimensional array of array. Each cell contains an array with
112b2ed727aSAndreas Gohr     * the keys 'col' (containing a Column object) and 'val' containing the value(s)
113b2ed727aSAndreas Gohr     */
114b2ed727aSAndreas Gohr    public function execute() {
115b2ed727aSAndreas Gohr        list($sql, $opts) = $this->getSQL();
116b2ed727aSAndreas Gohr
117b2ed727aSAndreas Gohr        $res = $this->sqlite->query($sql, $opts);
118b2ed727aSAndreas Gohr        $data = $this->sqlite->res2arr($res);
119b2ed727aSAndreas Gohr        $this->sqlite->res_close($res);
120b2ed727aSAndreas Gohr
121b2ed727aSAndreas Gohr        $result = array();
122b2ed727aSAndreas Gohr        foreach($data as $row) {
123b2ed727aSAndreas Gohr            $C = 0;
124b2ed727aSAndreas Gohr            $resrow = array();
125b2ed727aSAndreas Gohr            foreach($this->columns as $col) {
126b2ed727aSAndreas Gohr                $rescol = array();
127b2ed727aSAndreas Gohr                $rescol['col'] = $col;
128b2ed727aSAndreas Gohr                $rescol['val'] = $row["C$C"];
129b2ed727aSAndreas Gohr                if($col->isMulti()) {
130b2ed727aSAndreas Gohr                    $rescol['val'] = explode(self::CONCAT_SEPARATOR,$rescol['val']);
131b2ed727aSAndreas Gohr                }
132b2ed727aSAndreas Gohr                $resrow[] = $rescol;
133b2ed727aSAndreas Gohr                $C++;
134b2ed727aSAndreas Gohr            }
135b2ed727aSAndreas Gohr            $result[] = $resrow;
136b2ed727aSAndreas Gohr        }
137b2ed727aSAndreas Gohr        return $result;
138b2ed727aSAndreas Gohr    }
139b2ed727aSAndreas Gohr
140b2ed727aSAndreas Gohr    /**
14115929be2SAndreas Gohr     * Transform the set search parameters into a statement
14215929be2SAndreas Gohr     *
143b2ed727aSAndreas Gohr     * @return array ($sql, $opts) The SQL and parameters to execute
14415929be2SAndreas Gohr     */
14515929be2SAndreas Gohr    public function getSQL() {
1465511bd5bSAndreas Gohr        if(!$this->columns) throw new StructException('nocolname');
14715929be2SAndreas Gohr
14815929be2SAndreas Gohr        $from = '';
1499d7a36f9SAndreas Gohr        $select = '';
1509d7a36f9SAndreas Gohr        $order = '';
1519d7a36f9SAndreas Gohr        $grouping = array();
1529d7a36f9SAndreas Gohr        $opts = array();
1539d7a36f9SAndreas Gohr        $where = '1 = 1';
15415929be2SAndreas Gohr
1559d7a36f9SAndreas Gohr        // basic tables
1569d7a36f9SAndreas Gohr        $first = '';
1579d7a36f9SAndreas Gohr        foreach($this->schemas as $schema) {
1589d7a36f9SAndreas Gohr            if($first) {
1599d7a36f9SAndreas Gohr                // follow up tables
1609d7a36f9SAndreas Gohr                $from .= "\nLEFT OUTER JOIN data_{$schema->getTable()} ON data_$first.pid = data_{$schema->getTable()}.pid";
1619d7a36f9SAndreas Gohr            } else {
1629d7a36f9SAndreas Gohr                // first table
1639d7a36f9SAndreas Gohr                $select .= "data_{$schema->getTable()}.pid as PID, ";
1649d7a36f9SAndreas Gohr                $from .= "data_{$schema->getTable()}";
1659d7a36f9SAndreas Gohr                $first = $schema->getTable();
16615929be2SAndreas Gohr            }
1677059e7e1SAndreas Gohr
1687059e7e1SAndreas Gohr            $where .= "\nAND data_{$schema->getTable()}.latest = 1";
1699d7a36f9SAndreas Gohr        }
17015929be2SAndreas Gohr
17115929be2SAndreas Gohr        // columns to select, handling multis
1729d7a36f9SAndreas Gohr        $sep = self::CONCAT_SEPARATOR;
17315929be2SAndreas Gohr        $n = 0;
17415929be2SAndreas Gohr        foreach($this->columns as $col) {
17515929be2SAndreas Gohr            $CN = 'C' . $n++;
17615929be2SAndreas Gohr
177d1b04e89SAndreas Gohr            if($col->isMulti()) {
17815929be2SAndreas Gohr                $tn = 'M' . $col->getColref();
1799d7a36f9SAndreas Gohr                $select .= "GROUP_CONCAT($tn.value, '$sep') AS $CN, ";
1800fe33e72SAndreas Gohr                $from .= "\nLEFT OUTER JOIN multi_{$col->getTable()} AS $tn";
1819d7a36f9SAndreas Gohr                $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev";
1820fe33e72SAndreas Gohr                $from .= " AND $tn.colref = {$col->getColref()}\n";
18315929be2SAndreas Gohr            } else {
184*5b2d2a8cSAndreas Gohr                $select .= "{$col->getColName()} AS $CN, ";
1859d7a36f9SAndreas Gohr                $grouping[] = $CN;
18615929be2SAndreas Gohr            }
18715929be2SAndreas Gohr        }
18815929be2SAndreas Gohr        $select = rtrim($select, ', ');
18915929be2SAndreas Gohr
1909d7a36f9SAndreas Gohr        // where clauses
191b8fe6730SAndreas Gohr        foreach($this->filter as $filter) {
192b8fe6730SAndreas Gohr            list($col, $value, $comp, $type) = $filter;
193b8fe6730SAndreas Gohr
1949d7a36f9SAndreas Gohr            /** @var $col Column */
1959d7a36f9SAndreas Gohr            if($col->isMulti()) {
1969d7a36f9SAndreas Gohr                $tn = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before
1970fe33e72SAndreas Gohr                $from .= "\nLEFT OUTER JOIN multi_{$col->getTable()} AS $tn";
1989d7a36f9SAndreas Gohr                $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev";
1990fe33e72SAndreas Gohr                $from .= " AND $tn.colref = {$col->getColref()}\n";
20015929be2SAndreas Gohr
2019d7a36f9SAndreas Gohr                $column = "$tn.value";
2029d7a36f9SAndreas Gohr            } else {
203d1b04e89SAndreas Gohr                $column = $col->getColName();
2049d7a36f9SAndreas Gohr            }
2059d7a36f9SAndreas Gohr
2069d7a36f9SAndreas Gohr            list($wsql, $wopt) = $col->getType()->compare($column, $comp, $value);
2079d7a36f9SAndreas Gohr            $opts = array_merge($opts, $wopt);
2089d7a36f9SAndreas Gohr
209d1b04e89SAndreas Gohr            $where .= "\n$type $wsql";
2109d7a36f9SAndreas Gohr        }
2119d7a36f9SAndreas Gohr
2129d7a36f9SAndreas Gohr        // sorting
213b8fe6730SAndreas Gohr        foreach($this->sortby as $sort) {
214b8fe6730SAndreas Gohr            list($col, $asc) = $sort;
215b8fe6730SAndreas Gohr
2169d7a36f9SAndreas Gohr            /** @var $col Column */
2179d7a36f9SAndreas Gohr            if($col->isMulti()) {
2189d7a36f9SAndreas Gohr                // FIXME how to sort by multival?
2199d7a36f9SAndreas Gohr                // FIXME what if sort by non merged multival?
2209d7a36f9SAndreas Gohr            } else {
221d1b04e89SAndreas Gohr                $order .= $col->getColName().' ';
2229d7a36f9SAndreas Gohr                $order .= ($asc) ? 'ASC' : 'DESC';
2239d7a36f9SAndreas Gohr                $order .= ', ';
2249d7a36f9SAndreas Gohr            }
2259d7a36f9SAndreas Gohr        }
2269d7a36f9SAndreas Gohr        $order = rtrim($order, ', ');
2279d7a36f9SAndreas Gohr
2289d7a36f9SAndreas Gohr        $sql = "SELECT $select\n  FROM $from\nWHERE $where\nGROUP BY " . join(', ', $grouping);
2299d7a36f9SAndreas Gohr        if($order) $sql .= "\nORDER BY $order";
2309d7a36f9SAndreas Gohr
231b2ed727aSAndreas Gohr        return array($sql, $opts);
23215929be2SAndreas Gohr    }
23315929be2SAndreas Gohr
23415929be2SAndreas Gohr    /**
23515929be2SAndreas Gohr     * Find a column to be used in the search
23615929be2SAndreas Gohr     *
23715929be2SAndreas Gohr     * @param string $colname may contain an alias
23815929be2SAndreas Gohr     * @return bool|Column
23915929be2SAndreas Gohr     */
24015929be2SAndreas Gohr    protected function findColumn($colname) {
2415511bd5bSAndreas Gohr        if(!$this->schemas) throw new StructException('noschemas');
24215929be2SAndreas Gohr
243d1b04e89SAndreas Gohr        // handling of page column is special
244d1b04e89SAndreas Gohr        if($colname == '%pid%') {
245d1b04e89SAndreas Gohr            return new PageColumn(0, new Text(), array_shift(array_keys($this->schemas))); //FIXME the type should be Page
246d1b04e89SAndreas Gohr        }
247d1b04e89SAndreas Gohr        // FIXME %title% needs to be handled here, too (later)
248d1b04e89SAndreas Gohr
249d1b04e89SAndreas Gohr
25015929be2SAndreas Gohr        // resolve the alias or table name
25115929be2SAndreas Gohr        list($table, $colname) = explode('.', $colname, 2);
25215929be2SAndreas Gohr        if(!$colname) {
25315929be2SAndreas Gohr            $colname = $table;
25415929be2SAndreas Gohr            $table = '';
25515929be2SAndreas Gohr        }
25615929be2SAndreas Gohr        if($table && isset($this->aliases[$table])) {
25715929be2SAndreas Gohr            $table = $this->aliases[$table];
25815929be2SAndreas Gohr        }
25915929be2SAndreas Gohr
2605511bd5bSAndreas Gohr        if(!$colname) throw new StructException('nocolname');
26115929be2SAndreas Gohr
26215929be2SAndreas Gohr        // if table name given search only that, otherwiese try all for matching column name
26315929be2SAndreas Gohr        if($table) {
26415929be2SAndreas Gohr            $schemas = array($table => $this->schemas[$table]);
26515929be2SAndreas Gohr        } else {
26615929be2SAndreas Gohr            $schemas = $this->schemas;
26715929be2SAndreas Gohr        }
26815929be2SAndreas Gohr
26915929be2SAndreas Gohr        // find it
27015929be2SAndreas Gohr        $col = false;
27115929be2SAndreas Gohr        foreach($schemas as $schema) {
27215929be2SAndreas Gohr            $col = $schema->findColumn($colname);
27315929be2SAndreas Gohr            if($col) break;
27415929be2SAndreas Gohr        }
27515929be2SAndreas Gohr
27615929be2SAndreas Gohr        return $col;
27715929be2SAndreas Gohr    }
27815929be2SAndreas Gohr
27915929be2SAndreas Gohr}
28015929be2SAndreas Gohr
2815511bd5bSAndreas Gohr
282