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 10553528ecfSAndreas Gohr * @return string|string[] Result may be an array when a multi column placeholder is used 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$', 1165625b985SAndreas Gohr '$USER$', 1175625b985SAndreas Gohr '$TODAY$' 1185625b985SAndreas Gohr ), 1195625b985SAndreas Gohr array( 1205625b985SAndreas Gohr $ID, 1215625b985SAndreas Gohr getNS($ID), 1225625b985SAndreas Gohr noNS($ID), 1235625b985SAndreas Gohr isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '', 1245625b985SAndreas Gohr date('Y-m-d') 1255625b985SAndreas Gohr ), 1265625b985SAndreas Gohr $filter 1275625b985SAndreas Gohr ); 1285625b985SAndreas Gohr 12953528ecfSAndreas Gohr // apply struct column placeholder (we support only one!) 13053528ecfSAndreas Gohr if(preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) { 13153528ecfSAndreas Gohr $key = $match[2]; 132d19ba4b1SAndreas Gohr 133*aec9051bSAndreas Gohr // we try to resolve the key via current schema aliases first, otherwise take it literally 134*aec9051bSAndreas Gohr $column = $this->findColumn($key); 1355625b985SAndreas Gohr if($column) { 1365625b985SAndreas Gohr $label = $column->getLabel(); 1375625b985SAndreas Gohr $table = $column->getTable(); 138*aec9051bSAndreas Gohr } else { 139*aec9051bSAndreas Gohr list($table, $label) = explode('.', $key); 140*aec9051bSAndreas Gohr } 141*aec9051bSAndreas Gohr 142*aec9051bSAndreas Gohr // get the data from the current page 143*aec9051bSAndreas Gohr if($table && $label) { 1445625b985SAndreas Gohr $schemaData = new SchemaData($table, $ID, 0); 145bf4e3880SAndreas Gohr $schemaData->optionRawValue(true); 1465625b985SAndreas Gohr $data = $schemaData->getDataArray(); 1475625b985SAndreas Gohr $value = $data[$label]; 1485625b985SAndreas Gohr } else { 1495625b985SAndreas Gohr $value = ''; 1505625b985SAndreas Gohr } 1518b8243b2SAndreas Gohr 15253528ecfSAndreas Gohr // apply any pre and postfixes, even when multi value 15353528ecfSAndreas Gohr if(is_array($value)) { 15453528ecfSAndreas Gohr $filter = array(); 15553528ecfSAndreas Gohr foreach($value as $item) { 15653528ecfSAndreas Gohr $filter[] = $match[1] . $item . $match[3]; 15753528ecfSAndreas Gohr } 15853528ecfSAndreas Gohr } else { 15953528ecfSAndreas Gohr $filter = $match[1] . $value . $match[3]; 16053528ecfSAndreas Gohr } 1615625b985SAndreas Gohr } 1625625b985SAndreas Gohr 1635625b985SAndreas Gohr return $filter; 1645625b985SAndreas Gohr } 1655625b985SAndreas Gohr 1665625b985SAndreas Gohr /** 16716b7d914SAndreas Gohr * @return int cacheflag for this search 16816b7d914SAndreas Gohr */ 16916b7d914SAndreas Gohr public function getCacheFlag() { 17016b7d914SAndreas Gohr return $this->cacheFlag; 17116b7d914SAndreas Gohr } 17216b7d914SAndreas Gohr 17316b7d914SAndreas Gohr /** 17400f6af48SAndreas Gohr * Access the dynamic paramters of this search 17500f6af48SAndreas Gohr * 176668e4f8eSAndreas Gohr * Note: This call returns a clone of the parameters as they were initialized 17700f6af48SAndreas Gohr * 17800f6af48SAndreas Gohr * @return SearchConfigParameters 17900f6af48SAndreas Gohr */ 18000f6af48SAndreas Gohr public function getDynamicParameters() { 18100f6af48SAndreas Gohr return clone $this->dynamicParameters; 1825511bd5bSAndreas Gohr } 1835511bd5bSAndreas Gohr 18400f6af48SAndreas Gohr /** 18507993756SAndreas Gohr * @return array the current config 18600f6af48SAndreas Gohr */ 18707993756SAndreas Gohr public function getConf() { 1881a07b696SMichael Große return $this->config; 1891a07b696SMichael Große } 1901a07b696SMichael Große 1915511bd5bSAndreas Gohr} 192