xref: /plugin/struct/meta/SearchConfig.php (revision 1ca21e1738d74322247805adf021b3ed01d82538)
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;
113*1ca21e17SAnna Dabrowska        if (!isset($INFO['id'])) {
114*1ca21e17SAnna 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
143e983bcdaSSzymon Olewniczak        return $filter;
144e983bcdaSSzymon Olewniczak    }
145e983bcdaSSzymon Olewniczak
146e983bcdaSSzymon Olewniczak    /**
147e983bcdaSSzymon Olewniczak     * Replaces struct placeholders in the given filter value by the proper value
148e983bcdaSSzymon Olewniczak     *
149e983bcdaSSzymon Olewniczak     * @param string $match
150e983bcdaSSzymon Olewniczak     * @return string|string[] Result may be an array when a multi column placeholder is used
151e983bcdaSSzymon Olewniczak     */
152d6d97f60SAnna Dabrowska    protected function applyFilterVarsStruct($match)
153d6d97f60SAnna Dabrowska    {
154e983bcdaSSzymon Olewniczak        global $INFO;
155e983bcdaSSzymon Olewniczak
15653528ecfSAndreas Gohr        $key = $match[2];
157d19ba4b1SAndreas Gohr
1580280da9aSFrieder Schrempf        // we try to resolve the key via the assigned schemas first, otherwise take it literally
1590280da9aSFrieder Schrempf        $column = $this->findColumn($key, true);
1605625b985SAndreas Gohr        if ($column) {
1615625b985SAndreas Gohr            $label = $column->getLabel();
1625625b985SAndreas Gohr            $table = $column->getTable();
163aec9051bSAndreas Gohr        } else {
164*1ca21e17SAnna Dabrowska            list($table, $label) = array_pad(explode('.', $key), 2, '');
165aec9051bSAndreas Gohr        }
166aec9051bSAndreas Gohr
167aec9051bSAndreas Gohr        // get the data from the current page
168aec9051bSAndreas Gohr        if ($table && $label) {
1694cd5cc28SAnna Dabrowska            $schemaData = AccessTable::getPageAccess($table, $INFO['id']);
1707717c082SMichael Große            $data = $schemaData->getData();
17134db2096SMichael Große            if (!isset($data[$label])) {
172e87d1e74SMichael Große                throw new StructException("column not in table", $label, $table);
17334db2096SMichael Große            }
1747717c082SMichael Große            $value = $data[$label]->getCompareValue();
175c75f25cfSAndreas Gohr
176c75f25cfSAndreas Gohr            if (is_array($value) && !count($value)) {
177c75f25cfSAndreas Gohr                $value = '';
178c75f25cfSAndreas Gohr            }
1795625b985SAndreas Gohr        } else {
1805625b985SAndreas Gohr            $value = '';
1815625b985SAndreas Gohr        }
1828b8243b2SAndreas Gohr
18353528ecfSAndreas Gohr        // apply any pre and postfixes, even when multi value
18453528ecfSAndreas Gohr        if (is_array($value)) {
18553528ecfSAndreas Gohr            $filter = array();
18653528ecfSAndreas Gohr            foreach ($value as $item) {
18753528ecfSAndreas Gohr                $filter[] = $match[1] . $item . $match[3];
18853528ecfSAndreas Gohr            }
18953528ecfSAndreas Gohr        } else {
19053528ecfSAndreas Gohr            $filter = $match[1] . $value . $match[3];
19153528ecfSAndreas Gohr        }
192e983bcdaSSzymon Olewniczak
193e983bcdaSSzymon Olewniczak        return $filter;
194e983bcdaSSzymon Olewniczak    }
195e983bcdaSSzymon Olewniczak
196e983bcdaSSzymon Olewniczak    /**
197e983bcdaSSzymon Olewniczak     * Replaces user placeholders in the given filter value by the proper value
198e983bcdaSSzymon Olewniczak     *
199e983bcdaSSzymon Olewniczak     * @param string $match
200e983bcdaSSzymon Olewniczak     * @return string|string[] String for name and mail, array for grps
201e983bcdaSSzymon Olewniczak     */
202d6d97f60SAnna Dabrowska    protected function applyFilterVarsUser($match)
203d6d97f60SAnna Dabrowska    {
204e983bcdaSSzymon Olewniczak        global $INFO;
205e983bcdaSSzymon Olewniczak
206daa4b09dSSzymon Olewniczak        $key = strtolower($match[2]);
207daa4b09dSSzymon Olewniczak
208daa4b09dSSzymon Olewniczak        if (!in_array($key, array('name', 'mail', 'grps'))) {
209daa4b09dSSzymon Olewniczak            throw new StructException('"%s" is not a valid USER key', $key);
210daa4b09dSSzymon Olewniczak        }
211daa4b09dSSzymon Olewniczak
212daa4b09dSSzymon Olewniczak        if (empty($INFO['userinfo'])) {
213daa4b09dSSzymon Olewniczak            $filter = '';
214daa4b09dSSzymon Olewniczak        } else {
215daa4b09dSSzymon Olewniczak            $filter = $INFO['userinfo'][$key];
216daa4b09dSSzymon Olewniczak        }
2175625b985SAndreas Gohr
2185625b985SAndreas Gohr        return $filter;
2195625b985SAndreas Gohr    }
2205625b985SAndreas Gohr
2215625b985SAndreas Gohr    /**
22216b7d914SAndreas Gohr     * @return int cacheflag for this search
22316b7d914SAndreas Gohr     */
224d6d97f60SAnna Dabrowska    public function getCacheFlag()
225d6d97f60SAnna Dabrowska    {
22616b7d914SAndreas Gohr        return $this->cacheFlag;
22716b7d914SAndreas Gohr    }
22816b7d914SAndreas Gohr
22916b7d914SAndreas Gohr    /**
23000f6af48SAndreas Gohr     * Access the dynamic paramters of this search
23100f6af48SAndreas Gohr     *
232668e4f8eSAndreas Gohr     * Note: This call returns a clone of the parameters as they were initialized
23300f6af48SAndreas Gohr     *
23400f6af48SAndreas Gohr     * @return SearchConfigParameters
23500f6af48SAndreas Gohr     */
236d6d97f60SAnna Dabrowska    public function getDynamicParameters()
237d6d97f60SAnna Dabrowska    {
23800f6af48SAndreas Gohr        return clone $this->dynamicParameters;
2395511bd5bSAndreas Gohr    }
2405511bd5bSAndreas Gohr
24100f6af48SAndreas Gohr    /**
24207993756SAndreas Gohr     * @return array the current config
24300f6af48SAndreas Gohr     */
244d6d97f60SAnna Dabrowska    public function getConf()
245d6d97f60SAnna Dabrowska    {
2461a07b696SMichael Große        return $this->config;
2471a07b696SMichael Große    }
2485511bd5bSAndreas Gohr}
249