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 39b3a9db22SAndreas Gohr * @param bool $dynamic Should dynamic parameters be applied? 405511bd5bSAndreas Gohr */ 41b3a9db22SAndreas Gohr public function __construct($config, $dynamic = true) 42d6d97f60SAnna Dabrowska { 435511bd5bSAndreas Gohr parent::__construct(); 445511bd5bSAndreas Gohr 453ad292a8SAndreas Gohr // setup schemas and columns 460215a637SAndreas Gohr if (!empty($config['schemas'])) foreach ($config['schemas'] as $schema) { 473ad292a8SAndreas Gohr $this->addSchema($schema[0], $schema[1]); 483ad292a8SAndreas Gohr } 490215a637SAndreas Gohr if (!empty($config['cols'])) foreach ($config['cols'] as $col) { 503ad292a8SAndreas Gohr $this->addColumn($col); 513ad292a8SAndreas Gohr } 523ad292a8SAndreas Gohr 5316b7d914SAndreas Gohr // cache flag setting 5416b7d914SAndreas Gohr $this->cacheFlag = self::$CACHE_DEFAULT; 5516b7d914SAndreas Gohr if (!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']); 5616b7d914SAndreas Gohr 5700f6af48SAndreas Gohr // configure search from configuration 5800f6af48SAndreas Gohr if (!empty($config['filter'])) foreach ($config['filter'] as $filter) { 595625b985SAndreas Gohr $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]); 605511bd5bSAndreas Gohr } 6100f6af48SAndreas Gohr 6200f6af48SAndreas Gohr if (!empty($config['sort'])) foreach ($config['sort'] as $sort) { 63aa124708SAndreas Gohr $this->addSort($sort[0], $sort[1]); 641a07b696SMichael Große } 651a07b696SMichael Große 661a07b696SMichael Große if (!empty($config['limit'])) { 671a07b696SMichael Große $this->setLimit($config['limit']); 681a07b696SMichael Große } 6900f6af48SAndreas Gohr 7000f6af48SAndreas Gohr if (!empty($config['offset'])) { 71d2a8ce05SPaweł Czochański $this->setOffset($config['offset']); 7200f6af48SAndreas Gohr } 73668e4f8eSAndreas Gohr 74b3a9db22SAndreas Gohr // prepare dynamic parameters 75b3a9db22SAndreas Gohr $this->dynamicParameters = new SearchConfigParameters($this); 76b3a9db22SAndreas Gohr if($dynamic) { 77b3a9db22SAndreas Gohr $this->dynamicParameters->apply(); 78b3a9db22SAndreas Gohr } 79b3a9db22SAndreas Gohr 80668e4f8eSAndreas Gohr $this->config = $config; 811a07b696SMichael Große } 825511bd5bSAndreas Gohr 8300f6af48SAndreas Gohr /** 8416b7d914SAndreas Gohr * Set the cache flag accordingly to the set filter placeholders 8516b7d914SAndreas Gohr * 8616b7d914SAndreas Gohr * @param array $filters 8716b7d914SAndreas Gohr * @return int 8816b7d914SAndreas Gohr */ 89d6d97f60SAnna Dabrowska protected function determineCacheFlag($filters) 90d6d97f60SAnna Dabrowska { 9116b7d914SAndreas Gohr $flags = self::$CACHE_DEFAULT; 9216b7d914SAndreas Gohr 9316b7d914SAndreas Gohr foreach ($filters as $filter) { 9416b7d914SAndreas Gohr if (is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser 9516b7d914SAndreas Gohr 9616b7d914SAndreas Gohr if (strpos($filter, '$USER$') !== false) { 9716b7d914SAndreas Gohr $flags |= self::$CACHE_USER; 9816b7d914SAndreas Gohr } elseif (strpos($filter, '$TODAY$') !== false) { 9916b7d914SAndreas Gohr $flags |= self::$CACHE_DATE; 10016b7d914SAndreas Gohr } 10116b7d914SAndreas Gohr } 10216b7d914SAndreas Gohr 10316b7d914SAndreas Gohr return $flags; 10416b7d914SAndreas Gohr } 10516b7d914SAndreas Gohr 10616b7d914SAndreas Gohr /** 1075625b985SAndreas Gohr * Replaces placeholders in the given filter value by the proper value 1085625b985SAndreas Gohr * 1095625b985SAndreas Gohr * @param string $filter 11053528ecfSAndreas Gohr * @return string|string[] Result may be an array when a multi column placeholder is used 1115625b985SAndreas Gohr */ 112d6d97f60SAnna Dabrowska protected function applyFilterVars($filter) 113d6d97f60SAnna Dabrowska { 114ecf2cba2SAndreas Gohr global $INPUT; 11506fee43aSMichael Grosse global $INFO; 1161ca21e17SAnna Dabrowska if (!isset($INFO['id'])) { 1171ca21e17SAnna Dabrowska $INFO['id'] = null; 11834ea6e10SAnna Dabrowska } 1195625b985SAndreas Gohr 1205625b985SAndreas Gohr // apply inexpensive filters first 1215625b985SAndreas Gohr $filter = str_replace( 1225625b985SAndreas Gohr array( 1235625b985SAndreas Gohr '$ID$', 1245625b985SAndreas Gohr '$NS$', 1255625b985SAndreas Gohr '$PAGE$', 1265625b985SAndreas Gohr '$USER$', 1275625b985SAndreas Gohr '$TODAY$' 1285625b985SAndreas Gohr ), 1295625b985SAndreas Gohr array( 13006fee43aSMichael Grosse $INFO['id'], 13106fee43aSMichael Grosse getNS($INFO['id']), 13206fee43aSMichael Grosse noNS($INFO['id']), 133ecf2cba2SAndreas Gohr $INPUT->server->str('REMOTE_USER'), 1345625b985SAndreas Gohr date('Y-m-d') 1355625b985SAndreas Gohr ), 1365625b985SAndreas Gohr $filter 1375625b985SAndreas Gohr ); 1385625b985SAndreas Gohr 13953528ecfSAndreas Gohr // apply struct column placeholder (we support only one!) 14053528ecfSAndreas Gohr if (preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) { 141e983bcdaSSzymon Olewniczak $filter = $this->applyFilterVarsStruct($match); 142e983bcdaSSzymon Olewniczak } elseif (preg_match('/^(.*?)(?:\$USER\.(.*?)\$)(.*?)$/', $filter, $match)) { 143e983bcdaSSzymon Olewniczak $filter = $this->applyFilterVarsUser($match); 144e983bcdaSSzymon Olewniczak } 145e983bcdaSSzymon Olewniczak 146e983bcdaSSzymon Olewniczak return $filter; 147e983bcdaSSzymon Olewniczak } 148e983bcdaSSzymon Olewniczak 149e983bcdaSSzymon Olewniczak /** 150e983bcdaSSzymon Olewniczak * Replaces struct placeholders in the given filter value by the proper value 151e983bcdaSSzymon Olewniczak * 152e983bcdaSSzymon Olewniczak * @param string $match 153e983bcdaSSzymon Olewniczak * @return string|string[] Result may be an array when a multi column placeholder is used 154e983bcdaSSzymon Olewniczak */ 155d6d97f60SAnna Dabrowska protected function applyFilterVarsStruct($match) 156d6d97f60SAnna Dabrowska { 157e983bcdaSSzymon Olewniczak global $INFO; 158e983bcdaSSzymon Olewniczak 15953528ecfSAndreas Gohr $key = $match[2]; 160d19ba4b1SAndreas Gohr 1610280da9aSFrieder Schrempf // we try to resolve the key via the assigned schemas first, otherwise take it literally 1620280da9aSFrieder Schrempf $column = $this->findColumn($key, true); 1635625b985SAndreas Gohr if ($column) { 1645625b985SAndreas Gohr $label = $column->getLabel(); 1655625b985SAndreas Gohr $table = $column->getTable(); 166aec9051bSAndreas Gohr } else { 1671ca21e17SAnna Dabrowska list($table, $label) = array_pad(explode('.', $key), 2, ''); 168aec9051bSAndreas Gohr } 169aec9051bSAndreas Gohr 170aec9051bSAndreas Gohr // get the data from the current page 171aec9051bSAndreas Gohr if ($table && $label) { 1724cd5cc28SAnna Dabrowska $schemaData = AccessTable::getPageAccess($table, $INFO['id']); 1737717c082SMichael Große $data = $schemaData->getData(); 17434db2096SMichael Große if (!isset($data[$label])) { 175e87d1e74SMichael Große throw new StructException("column not in table", $label, $table); 17634db2096SMichael Große } 1777717c082SMichael Große $value = $data[$label]->getCompareValue(); 178c75f25cfSAndreas Gohr 179c75f25cfSAndreas Gohr if (is_array($value) && !count($value)) { 180c75f25cfSAndreas Gohr $value = ''; 181c75f25cfSAndreas Gohr } 1825625b985SAndreas Gohr } else { 1835625b985SAndreas Gohr $value = ''; 1845625b985SAndreas Gohr } 1858b8243b2SAndreas Gohr 18653528ecfSAndreas Gohr // apply any pre and postfixes, even when multi value 18753528ecfSAndreas Gohr if (is_array($value)) { 18853528ecfSAndreas Gohr $filter = array(); 18953528ecfSAndreas Gohr foreach ($value as $item) { 19053528ecfSAndreas Gohr $filter[] = $match[1] . $item . $match[3]; 19153528ecfSAndreas Gohr } 19253528ecfSAndreas Gohr } else { 19353528ecfSAndreas Gohr $filter = $match[1] . $value . $match[3]; 19453528ecfSAndreas Gohr } 195e983bcdaSSzymon Olewniczak 196e983bcdaSSzymon Olewniczak return $filter; 197e983bcdaSSzymon Olewniczak } 198e983bcdaSSzymon Olewniczak 199e983bcdaSSzymon Olewniczak /** 200e983bcdaSSzymon Olewniczak * Replaces user placeholders in the given filter value by the proper value 201e983bcdaSSzymon Olewniczak * 202e983bcdaSSzymon Olewniczak * @param string $match 203e983bcdaSSzymon Olewniczak * @return string|string[] String for name and mail, array for grps 204e983bcdaSSzymon Olewniczak */ 205d6d97f60SAnna Dabrowska protected function applyFilterVarsUser($match) 206d6d97f60SAnna Dabrowska { 207e983bcdaSSzymon Olewniczak global $INFO; 208e983bcdaSSzymon Olewniczak 209daa4b09dSSzymon Olewniczak $key = strtolower($match[2]); 210daa4b09dSSzymon Olewniczak 211daa4b09dSSzymon Olewniczak if (!in_array($key, array('name', 'mail', 'grps'))) { 212daa4b09dSSzymon Olewniczak throw new StructException('"%s" is not a valid USER key', $key); 213daa4b09dSSzymon Olewniczak } 214daa4b09dSSzymon Olewniczak 215daa4b09dSSzymon Olewniczak if (empty($INFO['userinfo'])) { 216daa4b09dSSzymon Olewniczak $filter = ''; 217daa4b09dSSzymon Olewniczak } else { 218daa4b09dSSzymon Olewniczak $filter = $INFO['userinfo'][$key]; 219daa4b09dSSzymon Olewniczak } 2205625b985SAndreas Gohr 2215625b985SAndreas Gohr return $filter; 2225625b985SAndreas Gohr } 2235625b985SAndreas Gohr 2245625b985SAndreas Gohr /** 22516b7d914SAndreas Gohr * @return int cacheflag for this search 22616b7d914SAndreas Gohr */ 227d6d97f60SAnna Dabrowska public function getCacheFlag() 228d6d97f60SAnna Dabrowska { 22916b7d914SAndreas Gohr return $this->cacheFlag; 23016b7d914SAndreas Gohr } 23116b7d914SAndreas Gohr 23216b7d914SAndreas Gohr /** 233*fd9c77d3SAndreas Gohr * Access the dynamic parameters of this search 23400f6af48SAndreas Gohr * 235668e4f8eSAndreas Gohr * Note: This call returns a clone of the parameters as they were initialized 23600f6af48SAndreas Gohr * 23700f6af48SAndreas Gohr * @return SearchConfigParameters 23800f6af48SAndreas Gohr */ 239d6d97f60SAnna Dabrowska public function getDynamicParameters() 240d6d97f60SAnna Dabrowska { 24100f6af48SAndreas Gohr return clone $this->dynamicParameters; 2425511bd5bSAndreas Gohr } 2435511bd5bSAndreas Gohr 24400f6af48SAndreas Gohr /** 24507993756SAndreas Gohr * @return array the current config 24600f6af48SAndreas Gohr */ 247d6d97f60SAnna Dabrowska public function getConf() 248d6d97f60SAnna Dabrowska { 2491a07b696SMichael Große return $this->config; 2501a07b696SMichael Große } 2515511bd5bSAndreas Gohr} 252