15511bd5bSAndreas Gohr<?php 25511bd5bSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 45511bd5bSAndreas Gohr 55511bd5bSAndreas Gohr/** 65511bd5bSAndreas Gohr * Class SearchConfig 75511bd5bSAndreas Gohr * 85511bd5bSAndreas Gohr * The same as @see Search but can be initialized by a configuration array 95511bd5bSAndreas Gohr * 10ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 115511bd5bSAndreas Gohr */ 125511bd5bSAndreas Gohrclass SearchConfig extends Search { 135511bd5bSAndreas Gohr 1416b7d914SAndreas Gohr /** @var int default aggregation caching (depends on last struct save) */ 1516b7d914SAndreas Gohr static public $CACHE_DEFAULT = 1; 1616b7d914SAndreas Gohr /** @var int caching depends on current user */ 1716b7d914SAndreas Gohr static public $CACHE_USER = 2; 1816b7d914SAndreas Gohr /** @var int caching depends on current date */ 1916b7d914SAndreas Gohr static public $CACHE_DATE = 4; 2016b7d914SAndreas Gohr 21668e4f8eSAndreas Gohr /** 22668e4f8eSAndreas Gohr * @var array hold the configuration as parsed and extended by dynamic params 23668e4f8eSAndreas Gohr */ 241a07b696SMichael Große protected $config; 251a07b696SMichael Große 26668e4f8eSAndreas Gohr /** 27668e4f8eSAndreas Gohr * @var SearchConfigParameters manages dynamic parameters 28668e4f8eSAndreas Gohr */ 2900f6af48SAndreas Gohr protected $dynamicParameters; 3000f6af48SAndreas Gohr 315511bd5bSAndreas Gohr /** 3216b7d914SAndreas Gohr * @var int the cache flag to use (binary flags) 3316b7d914SAndreas Gohr */ 3416b7d914SAndreas Gohr protected $cacheFlag; 3516b7d914SAndreas Gohr 3616b7d914SAndreas Gohr /** 375511bd5bSAndreas Gohr * SearchConfig constructor. 3800f6af48SAndreas Gohr * @param array $config The parsed configuration for this search 395511bd5bSAndreas Gohr */ 405511bd5bSAndreas Gohr public function __construct($config) { 415511bd5bSAndreas Gohr parent::__construct(); 425511bd5bSAndreas Gohr 433ad292a8SAndreas Gohr // setup schemas and columns 440215a637SAndreas Gohr if(!empty($config['schemas'])) foreach($config['schemas'] as $schema) { 453ad292a8SAndreas Gohr $this->addSchema($schema[0], $schema[1]); 463ad292a8SAndreas Gohr } 470215a637SAndreas Gohr if(!empty($config['cols'])) foreach($config['cols'] as $col) { 483ad292a8SAndreas Gohr $this->addColumn($col); 493ad292a8SAndreas Gohr } 503ad292a8SAndreas Gohr 5116b7d914SAndreas Gohr // cache flag setting 5216b7d914SAndreas Gohr $this->cacheFlag = self::$CACHE_DEFAULT; 5316b7d914SAndreas Gohr if(!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']); 5416b7d914SAndreas Gohr 5500f6af48SAndreas Gohr // apply dynamic paramters 5600f6af48SAndreas Gohr $this->dynamicParameters = new SearchConfigParameters($this); 57668e4f8eSAndreas Gohr $config = $this->dynamicParameters->updateConfig($config); 5800f6af48SAndreas Gohr 5900f6af48SAndreas Gohr // configure search from configuration 6000f6af48SAndreas Gohr if(!empty($config['filter'])) foreach($config['filter'] as $filter) { 615625b985SAndreas Gohr $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]); 625511bd5bSAndreas Gohr } 6300f6af48SAndreas Gohr 6400f6af48SAndreas Gohr if(!empty($config['sort'])) foreach($config['sort'] as $sort) { 65aa124708SAndreas Gohr $this->addSort($sort[0], $sort[1]); 661a07b696SMichael Große } 671a07b696SMichael Große 681a07b696SMichael Große if(!empty($config['limit'])) { 691a07b696SMichael Große $this->setLimit($config['limit']); 701a07b696SMichael Große } 7100f6af48SAndreas Gohr 7200f6af48SAndreas Gohr if(!empty($config['offset'])) { 7300f6af48SAndreas Gohr $this->setLimit($config['offset']); 7400f6af48SAndreas Gohr } 75668e4f8eSAndreas Gohr 76668e4f8eSAndreas Gohr $this->config = $config; 771a07b696SMichael Große } 785511bd5bSAndreas Gohr 7900f6af48SAndreas Gohr /** 8016b7d914SAndreas Gohr * Set the cache flag accordingly to the set filter placeholders 8116b7d914SAndreas Gohr * 8216b7d914SAndreas Gohr * @param array $filters 8316b7d914SAndreas Gohr * @return int 8416b7d914SAndreas Gohr */ 8516b7d914SAndreas Gohr protected function determineCacheFlag($filters) { 8616b7d914SAndreas Gohr $flags = self::$CACHE_DEFAULT; 8716b7d914SAndreas Gohr 8816b7d914SAndreas Gohr foreach($filters as $filter) { 8916b7d914SAndreas Gohr if(is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser 9016b7d914SAndreas Gohr 9116b7d914SAndreas Gohr if(strpos($filter, '$USER$') !== false) { 9216b7d914SAndreas Gohr $flags |= self::$CACHE_USER; 9316b7d914SAndreas Gohr } else if(strpos($filter, '$TODAY$') !== false) { 9416b7d914SAndreas Gohr $flags |= self::$CACHE_DATE; 9516b7d914SAndreas Gohr } 9616b7d914SAndreas Gohr } 9716b7d914SAndreas Gohr 9816b7d914SAndreas Gohr return $flags; 9916b7d914SAndreas Gohr } 10016b7d914SAndreas Gohr 10116b7d914SAndreas Gohr /** 1025625b985SAndreas Gohr * Replaces placeholders in the given filter value by the proper value 1035625b985SAndreas Gohr * 1045625b985SAndreas Gohr * @param string $filter 1055625b985SAndreas Gohr * @return string 1065625b985SAndreas Gohr */ 1075625b985SAndreas Gohr protected function applyFilterVars($filter) { 1085625b985SAndreas Gohr global $ID; 1095625b985SAndreas Gohr 1105625b985SAndreas Gohr // apply inexpensive filters first 1115625b985SAndreas Gohr $filter = str_replace( 1125625b985SAndreas Gohr array( 1135625b985SAndreas Gohr '$ID$', 1145625b985SAndreas Gohr '$NS$', 1155625b985SAndreas Gohr '$PAGE$', 1167249dd99SMichael Grosse '$TITLE$', 1175625b985SAndreas Gohr '$USER$', 1185625b985SAndreas Gohr '$TODAY$' 1195625b985SAndreas Gohr ), 1205625b985SAndreas Gohr array( 1215625b985SAndreas Gohr $ID, 1225625b985SAndreas Gohr getNS($ID), 1235625b985SAndreas Gohr noNS($ID), 1247249dd99SMichael Grosse p_get_first_heading($ID) ? p_get_first_heading($ID) : $ID, 1255625b985SAndreas Gohr isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '', 1265625b985SAndreas Gohr date('Y-m-d') 1275625b985SAndreas Gohr ), 1285625b985SAndreas Gohr $filter 1295625b985SAndreas Gohr ); 1305625b985SAndreas Gohr 1315625b985SAndreas Gohr // apply struct filter 132d19ba4b1SAndreas Gohr while(preg_match('/\$STRUCT\.(.*?)\$/', $filter, $match)) { 1335625b985SAndreas Gohr $key = $match[1]; 1345625b985SAndreas Gohr $column = $this->findColumn($key); 135d19ba4b1SAndreas Gohr 1365625b985SAndreas Gohr if($column) { 1375625b985SAndreas Gohr $label = $column->getLabel(); 1385625b985SAndreas Gohr $table = $column->getTable(); 1395625b985SAndreas Gohr $schemaData = new SchemaData($table, $ID, 0); 140*bf4e3880SAndreas Gohr $schemaData->optionRawValue(true); 1415625b985SAndreas Gohr $data = $schemaData->getDataArray(); 1425625b985SAndreas Gohr $value = $data[$label]; 1435625b985SAndreas Gohr if(is_array($value)) $value = array_shift($value); 1445625b985SAndreas Gohr } else { 1455625b985SAndreas Gohr $value = ''; 1465625b985SAndreas Gohr } 1478b8243b2SAndreas Gohr 1485625b985SAndreas Gohr $key = preg_quote_cb($key); 149d19ba4b1SAndreas Gohr $filter = preg_replace('/\$STRUCT\.' . $key . '\$/', $value, $filter, 1); 150d19ba4b1SAndreas Gohr 1515625b985SAndreas Gohr } 1525625b985SAndreas Gohr 1535625b985SAndreas Gohr return $filter; 1545625b985SAndreas Gohr } 1555625b985SAndreas Gohr 1565625b985SAndreas Gohr /** 15716b7d914SAndreas Gohr * @return int cacheflag for this search 15816b7d914SAndreas Gohr */ 15916b7d914SAndreas Gohr public function getCacheFlag() { 16016b7d914SAndreas Gohr return $this->cacheFlag; 16116b7d914SAndreas Gohr } 16216b7d914SAndreas Gohr 16316b7d914SAndreas Gohr /** 16400f6af48SAndreas Gohr * Access the dynamic paramters of this search 16500f6af48SAndreas Gohr * 166668e4f8eSAndreas Gohr * Note: This call returns a clone of the parameters as they were initialized 16700f6af48SAndreas Gohr * 16800f6af48SAndreas Gohr * @return SearchConfigParameters 16900f6af48SAndreas Gohr */ 17000f6af48SAndreas Gohr public function getDynamicParameters() { 17100f6af48SAndreas Gohr return clone $this->dynamicParameters; 1725511bd5bSAndreas Gohr } 1735511bd5bSAndreas Gohr 17400f6af48SAndreas Gohr /** 17507993756SAndreas Gohr * @return array the current config 17600f6af48SAndreas Gohr */ 17707993756SAndreas Gohr public function getConf() { 1781a07b696SMichael Große return $this->config; 1791a07b696SMichael Große } 1801a07b696SMichael Große 1815511bd5bSAndreas Gohr} 182