1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Class SearchConfig 7 * 8 * The same as @see Search but can be initialized by a configuration array 9 * 10 * @package dokuwiki\plugin\struct\meta 11 */ 12class SearchConfig extends Search 13{ 14 /** @var int default aggregation caching (depends on last struct save) */ 15 public static $CACHE_DEFAULT = 1; 16 /** @var int caching depends on current user */ 17 public static $CACHE_USER = 2; 18 /** @var int caching depends on current date */ 19 public static $CACHE_DATE = 4; 20 21 /** 22 * @var array hold the configuration as parsed and extended by dynamic params 23 */ 24 protected $config; 25 26 /** 27 * @var SearchConfigParameters manages dynamic parameters 28 */ 29 protected $dynamicParameters; 30 31 /** 32 * @var int the cache flag to use (binary flags) 33 */ 34 protected $cacheFlag; 35 36 /** 37 * SearchConfig constructor. 38 * @param array $config The parsed configuration for this search 39 * @param bool $dynamic Should dynamic parameters be applied? 40 */ 41 public function __construct($config, $dynamic = true) 42 { 43 parent::__construct(); 44 45 // setup schemas and columns 46 if (!empty($config['schemas'])) foreach ($config['schemas'] as $schema) { 47 $this->addSchema($schema[0], $schema[1]); 48 } 49 if (!empty($config['cols'])) foreach ($config['cols'] as $col) { 50 $this->addColumn($col); 51 } 52 53 // cache flag setting 54 $this->cacheFlag = self::$CACHE_DEFAULT; 55 if (!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']); 56 57 // configure search from configuration 58 if (!empty($config['filter'])) foreach ($config['filter'] as $filter) { 59 $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]); 60 } 61 62 if (!empty($config['sort'])) foreach ($config['sort'] as $sort) { 63 $this->addSort($sort[0], $sort[1]); 64 } 65 66 if (!empty($config['limit'])) { 67 $this->setLimit($config['limit']); 68 } 69 70 if (!empty($config['offset'])) { 71 $this->setOffset($config['offset']); 72 } 73 74 // prepare dynamic parameters 75 $this->dynamicParameters = new SearchConfigParameters($this); 76 if($dynamic) { 77 $this->dynamicParameters->apply(); 78 } 79 80 $this->config = $config; 81 } 82 83 /** 84 * Set the cache flag accordingly to the set filter placeholders 85 * 86 * @param array $filters 87 * @return int 88 */ 89 protected function determineCacheFlag($filters) 90 { 91 $flags = self::$CACHE_DEFAULT; 92 93 foreach ($filters as $filter) { 94 if (is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser 95 96 if (strpos($filter, '$USER$') !== false) { 97 $flags |= self::$CACHE_USER; 98 } elseif (strpos($filter, '$TODAY$') !== false) { 99 $flags |= self::$CACHE_DATE; 100 } 101 } 102 103 return $flags; 104 } 105 106 /** 107 * Replaces placeholders in the given filter value by the proper value 108 * 109 * @param string $filter 110 * @return string|string[] Result may be an array when a multi column placeholder is used 111 */ 112 protected function applyFilterVars($filter) 113 { 114 global $INPUT; 115 global $INFO; 116 if (!isset($INFO['id'])) { 117 $INFO['id'] = null; 118 } 119 120 // apply inexpensive filters first 121 $filter = str_replace( 122 array( 123 '$ID$', 124 '$NS$', 125 '$PAGE$', 126 '$USER$', 127 '$TODAY$' 128 ), 129 array( 130 $INFO['id'], 131 getNS($INFO['id']), 132 noNS($INFO['id']), 133 $INPUT->server->str('REMOTE_USER'), 134 date('Y-m-d') 135 ), 136 $filter 137 ); 138 139 // apply struct column placeholder (we support only one!) 140 if (preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) { 141 $filter = $this->applyFilterVarsStruct($match); 142 } elseif (preg_match('/^(.*?)(?:\$USER\.(.*?)\$)(.*?)$/', $filter, $match)) { 143 $filter = $this->applyFilterVarsUser($match); 144 } 145 146 return $filter; 147 } 148 149 /** 150 * Replaces struct placeholders in the given filter value by the proper value 151 * 152 * @param string $match 153 * @return string|string[] Result may be an array when a multi column placeholder is used 154 */ 155 protected function applyFilterVarsStruct($match) 156 { 157 global $INFO; 158 159 $key = $match[2]; 160 161 // we try to resolve the key via the assigned schemas first, otherwise take it literally 162 $column = $this->findColumn($key, true); 163 if ($column) { 164 $label = $column->getLabel(); 165 $table = $column->getTable(); 166 } else { 167 list($table, $label) = array_pad(explode('.', $key), 2, ''); 168 } 169 170 // get the data from the current page 171 if ($table && $label) { 172 $schemaData = AccessTable::getPageAccess($table, $INFO['id']); 173 $data = $schemaData->getData(); 174 if (!isset($data[$label])) { 175 throw new StructException("column not in table", $label, $table); 176 } 177 $value = $data[$label]->getCompareValue(); 178 179 if (is_array($value) && !count($value)) { 180 $value = ''; 181 } 182 } else { 183 $value = ''; 184 } 185 186 // apply any pre and postfixes, even when multi value 187 if (is_array($value)) { 188 $filter = array(); 189 foreach ($value as $item) { 190 $filter[] = $match[1] . $item . $match[3]; 191 } 192 } else { 193 $filter = $match[1] . $value . $match[3]; 194 } 195 196 return $filter; 197 } 198 199 /** 200 * Replaces user placeholders in the given filter value by the proper value 201 * 202 * @param string $match 203 * @return string|string[] String for name and mail, array for grps 204 */ 205 protected function applyFilterVarsUser($match) 206 { 207 global $INFO; 208 209 $key = strtolower($match[2]); 210 211 if (!in_array($key, array('name', 'mail', 'grps'))) { 212 throw new StructException('"%s" is not a valid USER key', $key); 213 } 214 215 if (empty($INFO['userinfo'])) { 216 $filter = ''; 217 } else { 218 $filter = $INFO['userinfo'][$key]; 219 } 220 221 return $filter; 222 } 223 224 /** 225 * @return int cacheflag for this search 226 */ 227 public function getCacheFlag() 228 { 229 return $this->cacheFlag; 230 } 231 232 /** 233 * Access the dynamic parameters of this search 234 * 235 * Note: This call returns a clone of the parameters as they were initialized 236 * 237 * @return SearchConfigParameters 238 */ 239 public function getDynamicParameters() 240 { 241 return clone $this->dynamicParameters; 242 } 243 244 /** 245 * @return array the current config 246 */ 247 public function getConf() 248 { 249 return $this->config; 250 } 251} 252