115929be2SAndreas Gohr<?php 215929be2SAndreas Gohr 315929be2SAndreas Gohrnamespace plugin\struct\meta; 415929be2SAndreas Gohr 50234787dSAndreas Gohruse 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 479cc86dfcSAndreas Gohr /** @var bool only distinct data? */ 489cc86dfcSAndreas Gohr protected $distinct = false; 499cc86dfcSAndreas 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 1179d7a36f9SAndreas Gohr * @param string $type either 'OR' or 'AND' 11815929be2SAndreas Gohr */ 1199d7a36f9SAndreas Gohr public function addFilter($colname, $value, $comp, $type = '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)); 1305511bd5bSAndreas Gohr if($type != 'OR' && $type != 'AND') throw new StructException('Bad filter type . Only AND or OR allowed'); 1319d7a36f9SAndreas Gohr 13215929be2SAndreas Gohr $col = $this->findColumn($colname); 13315929be2SAndreas Gohr if(!$col) return; //FIXME do we really want to ignore missing columns? 13499894f21SMichael Große $value = str_replace('*','%',$value); 1359d7a36f9SAndreas Gohr $this->filter[] = array($col, $value, $comp, $type); 13615929be2SAndreas Gohr } 13715929be2SAndreas Gohr 13815929be2SAndreas Gohr /** 1397f9cb794SAndreas Gohr * Set offset for the results 1407f9cb794SAndreas Gohr * 1417f9cb794SAndreas Gohr * @param int $offset 1427f9cb794SAndreas Gohr */ 1437f9cb794SAndreas Gohr public function setOffset($offset) { 1447f9cb794SAndreas Gohr $limit = 0; 1457f9cb794SAndreas Gohr if($this->range_end) { 1467f9cb794SAndreas Gohr // if there was a limit set previously, the range_end needs to be recalculated 1477f9cb794SAndreas Gohr $limit = $this->range_end - $this->range_begin; 1487f9cb794SAndreas Gohr } 1497f9cb794SAndreas Gohr $this->range_begin = $offset; 1507f9cb794SAndreas Gohr if($limit) $this->setLimit($limit); 1517f9cb794SAndreas Gohr } 1527f9cb794SAndreas Gohr 1537f9cb794SAndreas Gohr /** 1547f9cb794SAndreas Gohr * Limit results to this number 1557f9cb794SAndreas Gohr * 1567f9cb794SAndreas Gohr * @param int $limit Set to 0 to disable limit again 1577f9cb794SAndreas Gohr */ 1587f9cb794SAndreas Gohr public function setLimit($limit) { 1597f9cb794SAndreas Gohr if($limit) { 1607f9cb794SAndreas Gohr $this->range_end = $this->range_begin + $limit; 1617f9cb794SAndreas Gohr } else { 1627f9cb794SAndreas Gohr $this->range_end = 0; 1637f9cb794SAndreas Gohr } 1647f9cb794SAndreas Gohr } 1657f9cb794SAndreas Gohr 1667f9cb794SAndreas Gohr /** 1679cc86dfcSAndreas Gohr * Request distinct data only, no PID column 1689cc86dfcSAndreas Gohr * 1699cc86dfcSAndreas Gohr * @param boolean $distinct 1709cc86dfcSAndreas Gohr */ 1719cc86dfcSAndreas Gohr public function setDistinct($distinct) { 1729cc86dfcSAndreas Gohr $this->distinct = $distinct; 1739cc86dfcSAndreas Gohr } 1749cc86dfcSAndreas Gohr 1759cc86dfcSAndreas Gohr /** 1767f9cb794SAndreas Gohr * Return the number of results (regardless of limit and offset settings) 1777f9cb794SAndreas Gohr * 1787f9cb794SAndreas Gohr * Use this to implement paging. Important: this may only be called after running @see execute() 1797f9cb794SAndreas Gohr * 1807f9cb794SAndreas Gohr * @return int 1817f9cb794SAndreas Gohr */ 1827f9cb794SAndreas Gohr public function getCount() { 1837f9cb794SAndreas Gohr if($this->count < 0) throw new StructException('Count is only accessible after executing the search'); 1847f9cb794SAndreas Gohr return $this->count; 1857f9cb794SAndreas Gohr } 1867f9cb794SAndreas Gohr 1877f9cb794SAndreas Gohr /** 188b2ed727aSAndreas Gohr * Execute this search and return the result 189b2ed727aSAndreas Gohr * 19038fa36fbSAndreas Gohr * The result is a two dimensional array of Value()s. 1917f9cb794SAndreas Gohr * 1927f9cb794SAndreas Gohr * This will always query for the full result (not using offset and limit) and then 1937f9cb794SAndreas Gohr * return the wanted range, setting the count (@see getCount) to the whole result number 19438fa36fbSAndreas Gohr * 19538fa36fbSAndreas Gohr * @return Value[][] 196b2ed727aSAndreas Gohr */ 197b2ed727aSAndreas Gohr public function execute() { 198b2ed727aSAndreas Gohr list($sql, $opts) = $this->getSQL(); 199b2ed727aSAndreas Gohr 2007f9cb794SAndreas Gohr /** @var \PDOStatement $res */ 201b2ed727aSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 202b2ed727aSAndreas Gohr 203b2ed727aSAndreas Gohr $result = array(); 2047f9cb794SAndreas Gohr $cursor = -1; 2057f9cb794SAndreas Gohr while($row = $res->fetch(\PDO::FETCH_ASSOC)) { 2067f9cb794SAndreas Gohr $cursor++; 2077f9cb794SAndreas Gohr if($cursor < $this->range_begin) continue; 2087f9cb794SAndreas Gohr if($this->range_end && $cursor >= $this->range_end) continue; 2097f9cb794SAndreas Gohr 210b2ed727aSAndreas Gohr $C = 0; 211b2ed727aSAndreas Gohr $resrow = array(); 212b2ed727aSAndreas Gohr foreach($this->columns as $col) { 21338fa36fbSAndreas Gohr $val = $row["C$C"]; 214b2ed727aSAndreas Gohr if($col->isMulti()) { 21538fa36fbSAndreas Gohr $val = explode(self::CONCAT_SEPARATOR, $val); 216b2ed727aSAndreas Gohr } 21738fa36fbSAndreas Gohr $resrow[] = new Value($col, $val); 218b2ed727aSAndreas Gohr $C++; 219b2ed727aSAndreas Gohr } 220b2ed727aSAndreas Gohr $result[] = $resrow; 221b2ed727aSAndreas Gohr } 2227f9cb794SAndreas Gohr 2237f9cb794SAndreas Gohr $this->sqlite->res_close($res); 2247f9cb794SAndreas Gohr $this->count = $cursor + 1; 225b2ed727aSAndreas Gohr return $result; 226b2ed727aSAndreas Gohr } 227b2ed727aSAndreas Gohr 228b2ed727aSAndreas Gohr /** 22915929be2SAndreas Gohr * Transform the set search parameters into a statement 23015929be2SAndreas Gohr * 231b2ed727aSAndreas Gohr * @return array ($sql, $opts) The SQL and parameters to execute 23215929be2SAndreas Gohr */ 23315929be2SAndreas Gohr public function getSQL() { 2345511bd5bSAndreas Gohr if(!$this->columns) throw new StructException('nocolname'); 23515929be2SAndreas Gohr 23615929be2SAndreas Gohr $from = ''; 2379d7a36f9SAndreas Gohr $select = ''; 2389d7a36f9SAndreas Gohr $order = ''; 2399d7a36f9SAndreas Gohr $grouping = array(); 2409d7a36f9SAndreas Gohr $opts = array(); 2419d7a36f9SAndreas Gohr $where = '1 = 1'; 2429e07bdbfSAndreas Gohr $fwhere = ''; 24315929be2SAndreas Gohr 2449d7a36f9SAndreas Gohr // basic tables 2459d7a36f9SAndreas Gohr $first = ''; 2469d7a36f9SAndreas Gohr foreach($this->schemas as $schema) { 2479d7a36f9SAndreas Gohr if($first) { 2489d7a36f9SAndreas Gohr // follow up tables 2499d7a36f9SAndreas Gohr $from .= "\nLEFT OUTER JOIN data_{$schema->getTable()} ON data_$first.pid = data_{$schema->getTable()}.pid"; 2509d7a36f9SAndreas Gohr } else { 2519d7a36f9SAndreas Gohr // first table 2529cc86dfcSAndreas Gohr if($this->distinct) { 2539cc86dfcSAndreas Gohr $select .= 'DISTINCT '; 2549cc86dfcSAndreas Gohr } else { 2559d7a36f9SAndreas Gohr $select .= "data_{$schema->getTable()}.pid as PID, "; 2569cc86dfcSAndreas Gohr } 257b75e0a08SAndreas Gohr 258b75e0a08SAndreas Gohr $from .= 'schema_assignments, '; 259b75e0a08SAndreas Gohr $where .= "\nAND data_{$schema->getTable()}.pid = schema_assignments.pid"; 260b75e0a08SAndreas Gohr $where .= "\nAND schema_assignments.assigned = 1"; 261b75e0a08SAndreas Gohr 2629d7a36f9SAndreas Gohr $from .= "data_{$schema->getTable()}"; 263401d0067SAndreas Gohr $where .= "\nAND GETACCESSLEVEL(data_{$schema->getTable()}.pid) > 0"; 2649d7a36f9SAndreas Gohr $first = $schema->getTable(); 26515929be2SAndreas Gohr } 2667059e7e1SAndreas Gohr 2677059e7e1SAndreas Gohr $where .= "\nAND data_{$schema->getTable()}.latest = 1"; 2689d7a36f9SAndreas Gohr } 26915929be2SAndreas Gohr 27015929be2SAndreas Gohr // columns to select, handling multis 2719d7a36f9SAndreas Gohr $sep = self::CONCAT_SEPARATOR; 27215929be2SAndreas Gohr $n = 0; 27315929be2SAndreas Gohr foreach($this->columns as $col) { 27415929be2SAndreas Gohr $CN = 'C' . $n++; 27515929be2SAndreas Gohr 276d1b04e89SAndreas Gohr if($col->isMulti()) { 27715929be2SAndreas Gohr $tn = 'M' . $col->getColref(); 2789d7a36f9SAndreas Gohr $select .= "GROUP_CONCAT($tn.value, '$sep') AS $CN, "; 2790fe33e72SAndreas Gohr $from .= "\nLEFT OUTER JOIN multi_{$col->getTable()} AS $tn"; 2809d7a36f9SAndreas Gohr $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev"; 2810fe33e72SAndreas Gohr $from .= " AND $tn.colref = {$col->getColref()}\n"; 28215929be2SAndreas Gohr } else { 2835b2d2a8cSAndreas Gohr $select .= "{$col->getColName()} AS $CN, "; 2849d7a36f9SAndreas Gohr $grouping[] = $CN; 28515929be2SAndreas Gohr } 28615929be2SAndreas Gohr } 28715929be2SAndreas Gohr $select = rtrim($select, ', '); 28815929be2SAndreas Gohr 2899d7a36f9SAndreas Gohr // where clauses 290b8fe6730SAndreas Gohr foreach($this->filter as $filter) { 291b8fe6730SAndreas Gohr list($col, $value, $comp, $type) = $filter; 292b8fe6730SAndreas Gohr 2939d7a36f9SAndreas Gohr /** @var $col Column */ 2949d7a36f9SAndreas Gohr if($col->isMulti()) { 2959d7a36f9SAndreas Gohr $tn = 'MN' . $col->getColref(); // FIXME this joins a second time if the column was selected before 2960fe33e72SAndreas Gohr $from .= "\nLEFT OUTER JOIN multi_{$col->getTable()} AS $tn"; 2979d7a36f9SAndreas Gohr $from .= " ON data_{$col->getTable()}.pid = $tn.pid AND data_{$col->getTable()}.rev = $tn.rev"; 2980fe33e72SAndreas Gohr $from .= " AND $tn.colref = {$col->getColref()}\n"; 29915929be2SAndreas Gohr 3009d7a36f9SAndreas Gohr $column = "$tn.value"; 3019d7a36f9SAndreas Gohr } else { 302d1b04e89SAndreas Gohr $column = $col->getColName(); 3039d7a36f9SAndreas Gohr } 3049d7a36f9SAndreas Gohr 3059d7a36f9SAndreas Gohr list($wsql, $wopt) = $col->getType()->compare($column, $comp, $value); 3069d7a36f9SAndreas Gohr $opts = array_merge($opts, $wopt); 3079d7a36f9SAndreas Gohr 3089e07bdbfSAndreas Gohr if(!$fwhere) $type = ''; // no type for first filter 3099e07bdbfSAndreas Gohr $fwhere .= "\n$type $wsql"; 3109d7a36f9SAndreas Gohr } 3119d7a36f9SAndreas Gohr 312*29394e6cSAndreas Gohr // sorting - we always sort by the single val column 313b8fe6730SAndreas Gohr foreach($this->sortby as $sort) { 314b8fe6730SAndreas Gohr list($col, $asc) = $sort; 3159d7a36f9SAndreas Gohr /** @var $col Column */ 316d1b04e89SAndreas Gohr $order .= $col->getColName() . ' '; 3179d7a36f9SAndreas Gohr $order .= ($asc) ? 'ASC' : 'DESC'; 3189d7a36f9SAndreas Gohr $order .= ', '; 3199d7a36f9SAndreas Gohr } 3209d7a36f9SAndreas Gohr $order = rtrim($order, ', '); 3219d7a36f9SAndreas Gohr 3229e07bdbfSAndreas Gohr $fwhere = trim($fwhere); 3239e07bdbfSAndreas Gohr if($fwhere) { 3249e07bdbfSAndreas Gohr $fwhere = "AND ($fwhere\n)"; 3259e07bdbfSAndreas Gohr } 3269e07bdbfSAndreas Gohr 3279e07bdbfSAndreas Gohr $sql = "SELECT $select\n FROM $from\nWHERE $where\n$fwhere\nGROUP BY " . join(', ', $grouping); 3289d7a36f9SAndreas Gohr if($order) $sql .= "\nORDER BY $order"; 3299d7a36f9SAndreas Gohr 330b2ed727aSAndreas Gohr return array($sql, $opts); 33115929be2SAndreas Gohr } 33215929be2SAndreas Gohr 33315929be2SAndreas Gohr /** 33401dd90deSAndreas Gohr * Returns all the columns that where added to the search 33501dd90deSAndreas Gohr * 33601dd90deSAndreas Gohr * @return Column[] 33701dd90deSAndreas Gohr */ 33801dd90deSAndreas Gohr public function getColumns() { 33901dd90deSAndreas Gohr return $this->columns; 34001dd90deSAndreas Gohr } 34101dd90deSAndreas Gohr 34201dd90deSAndreas Gohr 34301dd90deSAndreas Gohr /** 34415929be2SAndreas Gohr * Find a column to be used in the search 34515929be2SAndreas Gohr * 34615929be2SAndreas Gohr * @param string $colname may contain an alias 34715929be2SAndreas Gohr * @return bool|Column 34815929be2SAndreas Gohr */ 34900f6af48SAndreas Gohr public function findColumn($colname) { 3505511bd5bSAndreas Gohr if(!$this->schemas) throw new StructException('noschemas'); 35115929be2SAndreas Gohr 352d1b04e89SAndreas Gohr // handling of page column is special 353d3f94fb7SAndreas Gohr if($colname == '%pageid%') { 354df02ffe6SMichael Große $schema_list = array_keys($this->schemas); 355df02ffe6SMichael Große return new PageColumn(0, new Page(), array_shift($schema_list)); 356d1b04e89SAndreas Gohr } 357d1b04e89SAndreas Gohr // FIXME %title% needs to be handled here, too (later) 358d1b04e89SAndreas Gohr 35915929be2SAndreas Gohr // resolve the alias or table name 36015929be2SAndreas Gohr list($table, $colname) = explode('.', $colname, 2); 36115929be2SAndreas Gohr if(!$colname) { 36215929be2SAndreas Gohr $colname = $table; 36315929be2SAndreas Gohr $table = ''; 36415929be2SAndreas Gohr } 36515929be2SAndreas Gohr if($table && isset($this->aliases[$table])) { 36615929be2SAndreas Gohr $table = $this->aliases[$table]; 36715929be2SAndreas Gohr } 36815929be2SAndreas Gohr 3695511bd5bSAndreas Gohr if(!$colname) throw new StructException('nocolname'); 37015929be2SAndreas Gohr 37115929be2SAndreas Gohr // if table name given search only that, otherwiese try all for matching column name 37215929be2SAndreas Gohr if($table) { 37315929be2SAndreas Gohr $schemas = array($table => $this->schemas[$table]); 37415929be2SAndreas Gohr } else { 37515929be2SAndreas Gohr $schemas = $this->schemas; 37615929be2SAndreas Gohr } 37715929be2SAndreas Gohr 37815929be2SAndreas Gohr // find it 37915929be2SAndreas Gohr $col = false; 38015929be2SAndreas Gohr foreach($schemas as $schema) { 38115929be2SAndreas Gohr $col = $schema->findColumn($colname); 38215929be2SAndreas Gohr if($col) break; 38315929be2SAndreas Gohr } 38415929be2SAndreas Gohr 38515929be2SAndreas Gohr return $col; 38615929be2SAndreas Gohr } 38715929be2SAndreas Gohr 38815929be2SAndreas Gohr} 38915929be2SAndreas Gohr 3905511bd5bSAndreas Gohr 391