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 */ 12d6d97f60SAnna Dabrowskaclass SearchConfig extends Search 13d6d97f60SAnna Dabrowska{ 1416b7d914SAndreas Gohr /** @var int default aggregation caching (depends on last struct save) */ 15d6d97f60SAnna Dabrowska public static $CACHE_DEFAULT = 1; 1616b7d914SAndreas Gohr /** @var int caching depends on current user */ 17d6d97f60SAnna Dabrowska public static $CACHE_USER = 2; 1816b7d914SAndreas Gohr /** @var int caching depends on current date */ 19d6d97f60SAnna Dabrowska public static $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 */ 40d6d97f60SAnna Dabrowska public function __construct($config) 41d6d97f60SAnna Dabrowska { 425511bd5bSAndreas Gohr parent::__construct(); 435511bd5bSAndreas Gohr 443ad292a8SAndreas Gohr // setup schemas and columns 450215a637SAndreas Gohr if (!empty($config['schemas'])) foreach ($config['schemas'] as $schema) { 463ad292a8SAndreas Gohr $this->addSchema($schema[0], $schema[1]); 473ad292a8SAndreas Gohr } 480215a637SAndreas Gohr if (!empty($config['cols'])) foreach ($config['cols'] as $col) { 493ad292a8SAndreas Gohr $this->addColumn($col); 503ad292a8SAndreas Gohr } 513ad292a8SAndreas Gohr 5216b7d914SAndreas Gohr // cache flag setting 5316b7d914SAndreas Gohr $this->cacheFlag = self::$CACHE_DEFAULT; 5416b7d914SAndreas Gohr if (!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']); 5516b7d914SAndreas Gohr 5600f6af48SAndreas Gohr // apply dynamic paramters 5700f6af48SAndreas Gohr $this->dynamicParameters = new SearchConfigParameters($this); 58668e4f8eSAndreas Gohr $config = $this->dynamicParameters->updateConfig($config); 5900f6af48SAndreas Gohr 6000f6af48SAndreas Gohr // configure search from configuration 6100f6af48SAndreas Gohr if (!empty($config['filter'])) foreach ($config['filter'] as $filter) { 625625b985SAndreas Gohr $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]); 635511bd5bSAndreas Gohr } 6400f6af48SAndreas Gohr 6500f6af48SAndreas Gohr if (!empty($config['sort'])) foreach ($config['sort'] as $sort) { 66aa124708SAndreas Gohr $this->addSort($sort[0], $sort[1]); 671a07b696SMichael Große } 681a07b696SMichael Große 691a07b696SMichael Große if (!empty($config['limit'])) { 701a07b696SMichael Große $this->setLimit($config['limit']); 711a07b696SMichael Große } 7200f6af48SAndreas Gohr 7300f6af48SAndreas Gohr if (!empty($config['offset'])) { 74d2a8ce05SPaweł Czochański $this->setOffset($config['offset']); 7500f6af48SAndreas Gohr } 76668e4f8eSAndreas Gohr 77668e4f8eSAndreas Gohr $this->config = $config; 781a07b696SMichael Große } 795511bd5bSAndreas Gohr 8000f6af48SAndreas Gohr /** 8116b7d914SAndreas Gohr * Set the cache flag accordingly to the set filter placeholders 8216b7d914SAndreas Gohr * 8316b7d914SAndreas Gohr * @param array $filters 8416b7d914SAndreas Gohr * @return int 8516b7d914SAndreas Gohr */ 86d6d97f60SAnna Dabrowska protected function determineCacheFlag($filters) 87d6d97f60SAnna Dabrowska { 8816b7d914SAndreas Gohr $flags = self::$CACHE_DEFAULT; 8916b7d914SAndreas Gohr 9016b7d914SAndreas Gohr foreach ($filters as $filter) { 9116b7d914SAndreas Gohr if (is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser 9216b7d914SAndreas Gohr 9316b7d914SAndreas Gohr if (strpos($filter, '$USER$') !== false) { 9416b7d914SAndreas Gohr $flags |= self::$CACHE_USER; 9516b7d914SAndreas Gohr } elseif (strpos($filter, '$TODAY$') !== false) { 9616b7d914SAndreas Gohr $flags |= self::$CACHE_DATE; 9716b7d914SAndreas Gohr } 9816b7d914SAndreas Gohr } 9916b7d914SAndreas Gohr 10016b7d914SAndreas Gohr return $flags; 10116b7d914SAndreas Gohr } 10216b7d914SAndreas Gohr 10316b7d914SAndreas Gohr /** 1045625b985SAndreas Gohr * Replaces placeholders in the given filter value by the proper value 1055625b985SAndreas Gohr * 1065625b985SAndreas Gohr * @param string $filter 10753528ecfSAndreas Gohr * @return string|string[] Result may be an array when a multi column placeholder is used 1085625b985SAndreas Gohr */ 109d6d97f60SAnna Dabrowska protected function applyFilterVars($filter) 110d6d97f60SAnna Dabrowska { 111ecf2cba2SAndreas Gohr global $INPUT; 11206fee43aSMichael Grosse global $INFO; 1131ca21e17SAnna Dabrowska if (!isset($INFO['id'])) { 1141ca21e17SAnna Dabrowska $INFO['id'] = null; 11534ea6e10SAnna Dabrowska } 1165625b985SAndreas Gohr 1175625b985SAndreas Gohr // apply inexpensive filters first 1185625b985SAndreas Gohr $filter = str_replace( 1195625b985SAndreas Gohr array( 1205625b985SAndreas Gohr '$ID$', 1215625b985SAndreas Gohr '$NS$', 1225625b985SAndreas Gohr '$PAGE$', 1235625b985SAndreas Gohr '$USER$', 1245625b985SAndreas Gohr '$TODAY$' 1255625b985SAndreas Gohr ), 1265625b985SAndreas Gohr array( 12706fee43aSMichael Grosse $INFO['id'], 12806fee43aSMichael Grosse getNS($INFO['id']), 12906fee43aSMichael Grosse noNS($INFO['id']), 130ecf2cba2SAndreas Gohr $INPUT->server->str('REMOTE_USER'), 1315625b985SAndreas Gohr date('Y-m-d') 1325625b985SAndreas Gohr ), 1335625b985SAndreas Gohr $filter 1345625b985SAndreas Gohr ); 1355625b985SAndreas Gohr 13653528ecfSAndreas Gohr // apply struct column placeholder (we support only one!) 13753528ecfSAndreas Gohr if (preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) { 138e983bcdaSSzymon Olewniczak $filter = $this->applyFilterVarsStruct($match); 139e983bcdaSSzymon Olewniczak } elseif (preg_match('/^(.*?)(?:\$USER\.(.*?)\$)(.*?)$/', $filter, $match)) { 140e983bcdaSSzymon Olewniczak $filter = $this->applyFilterVarsUser($match); 141e983bcdaSSzymon Olewniczak } 142e983bcdaSSzymon Olewniczak 143*7f610bd5Ssaggi-dw // apply date formula, given as strtotime 144*7f610bd5Ssaggi-dw if(preg_match('/^(.*?)(?:\$DATE\((.*?)\)\$?)(.*?)$/', $filter, $match)) { 145*7f610bd5Ssaggi-dw $toparse = $match[2]; 146*7f610bd5Ssaggi-dw if ($toparse == '') $toparse = 'now'; 147*7f610bd5Ssaggi-dw if (($timestamp = strtotime($toparse)) === false) { 148*7f610bd5Ssaggi-dw throw new StructException('datefilter',hsc($toparse)); 149*7f610bd5Ssaggi-dw } else { 150*7f610bd5Ssaggi-dw $filter = str_replace($filter,date('Y-m-d',$timestamp),$filter); 151*7f610bd5Ssaggi-dw } 152*7f610bd5Ssaggi-dw } 153*7f610bd5Ssaggi-dw 154e983bcdaSSzymon Olewniczak return $filter; 155e983bcdaSSzymon Olewniczak } 156e983bcdaSSzymon Olewniczak 157e983bcdaSSzymon Olewniczak /** 158e983bcdaSSzymon Olewniczak * Replaces struct placeholders in the given filter value by the proper value 159e983bcdaSSzymon Olewniczak * 160e983bcdaSSzymon Olewniczak * @param string $match 161e983bcdaSSzymon Olewniczak * @return string|string[] Result may be an array when a multi column placeholder is used 162e983bcdaSSzymon Olewniczak */ 163d6d97f60SAnna Dabrowska protected function applyFilterVarsStruct($match) 164d6d97f60SAnna Dabrowska { 165e983bcdaSSzymon Olewniczak global $INFO; 166e983bcdaSSzymon Olewniczak 16753528ecfSAndreas Gohr $key = $match[2]; 168d19ba4b1SAndreas Gohr 1690280da9aSFrieder Schrempf // we try to resolve the key via the assigned schemas first, otherwise take it literally 1700280da9aSFrieder Schrempf $column = $this->findColumn($key, true); 1715625b985SAndreas Gohr if ($column) { 1725625b985SAndreas Gohr $label = $column->getLabel(); 1735625b985SAndreas Gohr $table = $column->getTable(); 174aec9051bSAndreas Gohr } else { 1751ca21e17SAnna Dabrowska list($table, $label) = array_pad(explode('.', $key), 2, ''); 176aec9051bSAndreas Gohr } 177aec9051bSAndreas Gohr 178aec9051bSAndreas Gohr // get the data from the current page 179aec9051bSAndreas Gohr if ($table && $label) { 1804cd5cc28SAnna Dabrowska $schemaData = AccessTable::getPageAccess($table, $INFO['id']); 1817717c082SMichael Große $data = $schemaData->getData(); 18234db2096SMichael Große if (!isset($data[$label])) { 183e87d1e74SMichael Große throw new StructException("column not in table", $label, $table); 18434db2096SMichael Große } 1857717c082SMichael Große $value = $data[$label]->getCompareValue(); 186c75f25cfSAndreas Gohr 187c75f25cfSAndreas Gohr if (is_array($value) && !count($value)) { 188c75f25cfSAndreas Gohr $value = ''; 189c75f25cfSAndreas Gohr } 1905625b985SAndreas Gohr } else { 1915625b985SAndreas Gohr $value = ''; 1925625b985SAndreas Gohr } 1938b8243b2SAndreas Gohr 19453528ecfSAndreas Gohr // apply any pre and postfixes, even when multi value 19553528ecfSAndreas Gohr if (is_array($value)) { 19653528ecfSAndreas Gohr $filter = array(); 19753528ecfSAndreas Gohr foreach ($value as $item) { 19853528ecfSAndreas Gohr $filter[] = $match[1] . $item . $match[3]; 19953528ecfSAndreas Gohr } 20053528ecfSAndreas Gohr } else { 20153528ecfSAndreas Gohr $filter = $match[1] . $value . $match[3]; 20253528ecfSAndreas Gohr } 203e983bcdaSSzymon Olewniczak 204e983bcdaSSzymon Olewniczak return $filter; 205e983bcdaSSzymon Olewniczak } 206e983bcdaSSzymon Olewniczak 207e983bcdaSSzymon Olewniczak /** 208e983bcdaSSzymon Olewniczak * Replaces user placeholders in the given filter value by the proper value 209e983bcdaSSzymon Olewniczak * 210e983bcdaSSzymon Olewniczak * @param string $match 211e983bcdaSSzymon Olewniczak * @return string|string[] String for name and mail, array for grps 212e983bcdaSSzymon Olewniczak */ 213d6d97f60SAnna Dabrowska protected function applyFilterVarsUser($match) 214d6d97f60SAnna Dabrowska { 215e983bcdaSSzymon Olewniczak global $INFO; 216e983bcdaSSzymon Olewniczak 217daa4b09dSSzymon Olewniczak $key = strtolower($match[2]); 218daa4b09dSSzymon Olewniczak 219daa4b09dSSzymon Olewniczak if (!in_array($key, array('name', 'mail', 'grps'))) { 220daa4b09dSSzymon Olewniczak throw new StructException('"%s" is not a valid USER key', $key); 221daa4b09dSSzymon Olewniczak } 222daa4b09dSSzymon Olewniczak 223daa4b09dSSzymon Olewniczak if (empty($INFO['userinfo'])) { 224daa4b09dSSzymon Olewniczak $filter = ''; 225daa4b09dSSzymon Olewniczak } else { 226daa4b09dSSzymon Olewniczak $filter = $INFO['userinfo'][$key]; 227daa4b09dSSzymon Olewniczak } 2285625b985SAndreas Gohr 2295625b985SAndreas Gohr return $filter; 2305625b985SAndreas Gohr } 2315625b985SAndreas Gohr 2325625b985SAndreas Gohr /** 23316b7d914SAndreas Gohr * @return int cacheflag for this search 23416b7d914SAndreas Gohr */ 235d6d97f60SAnna Dabrowska public function getCacheFlag() 236d6d97f60SAnna Dabrowska { 23716b7d914SAndreas Gohr return $this->cacheFlag; 23816b7d914SAndreas Gohr } 23916b7d914SAndreas Gohr 24016b7d914SAndreas Gohr /** 24100f6af48SAndreas Gohr * Access the dynamic paramters of this search 24200f6af48SAndreas Gohr * 243668e4f8eSAndreas Gohr * Note: This call returns a clone of the parameters as they were initialized 24400f6af48SAndreas Gohr * 24500f6af48SAndreas Gohr * @return SearchConfigParameters 24600f6af48SAndreas Gohr */ 247d6d97f60SAnna Dabrowska public function getDynamicParameters() 248d6d97f60SAnna Dabrowska { 24900f6af48SAndreas Gohr return clone $this->dynamicParameters; 2505511bd5bSAndreas Gohr } 2515511bd5bSAndreas Gohr 25200f6af48SAndreas Gohr /** 25307993756SAndreas Gohr * @return array the current config 25400f6af48SAndreas Gohr */ 255d6d97f60SAnna Dabrowska public function getConf() 256d6d97f60SAnna Dabrowska { 2571a07b696SMichael Große return $this->config; 2581a07b696SMichael Große } 2595511bd5bSAndreas Gohr} 260