xref: /plugin/struct/meta/SearchConfig.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
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(
122*7234bfb1Ssplitbrain            ['$ID$', '$NS$', '$PAGE$', '$USER$', '$TODAY$'],
123*7234bfb1Ssplitbrain            [$INFO['id'], getNS($INFO['id']), noNS($INFO['id']), $INPUT->server->str('REMOTE_USER'), date('Y-m-d')],
1245625b985SAndreas Gohr            $filter
1255625b985SAndreas Gohr        );
1265625b985SAndreas Gohr
12753528ecfSAndreas Gohr        // apply struct column placeholder (we support only one!)
128888559e1Ssaggi-dw        // or apply date formula, given as strtotime
12953528ecfSAndreas Gohr        if (preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) {
130e983bcdaSSzymon Olewniczak            $filter = $this->applyFilterVarsStruct($match);
131e983bcdaSSzymon Olewniczak        } elseif (preg_match('/^(.*?)(?:\$USER\.(.*?)\$)(.*?)$/', $filter, $match)) {
132e983bcdaSSzymon Olewniczak            $filter = $this->applyFilterVarsUser($match);
133888559e1Ssaggi-dw        } elseif (preg_match('/^(.*?)(?:\$DATE\((.*?)\)\$?)(.*?)$/', $filter, $match)) {
1347f610bd5Ssaggi-dw            $toparse = $match[2];
135888559e1Ssaggi-dw            if ($toparse == '') {
136888559e1Ssaggi-dw                $toparse = 'now';
137888559e1Ssaggi-dw            }
138888559e1Ssaggi-dw            $timestamp = strtotime($toparse);
139888559e1Ssaggi-dw            if ($timestamp === false) {
1407f610bd5Ssaggi-dw                throw new StructException('datefilter', hsc($toparse));
1417f610bd5Ssaggi-dw            } else {
1427f610bd5Ssaggi-dw                $filter = str_replace($filter, date('Y-m-d', $timestamp), $filter);
1437f610bd5Ssaggi-dw            }
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 {
167*7234bfb1Ssplitbrain            [$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
179*7234bfb1Ssplitbrain            if (is_array($value) && $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)) {
188*7234bfb1Ssplitbrain            $filter = [];
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
211*7234bfb1Ssplitbrain        if (!in_array($key, ['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    /**
233fd9c77d3SAndreas 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    /**
245fdf37115SAndreas Gohr     * Get the config this search was initialized with
246fdf37115SAndreas Gohr     *
247fdf37115SAndreas Gohr     * Note that the search may have been modified by dynamic parameters or additional member calls
248fdf37115SAndreas Gohr     *
249fdf37115SAndreas Gohr     * @return array
25000f6af48SAndreas Gohr     */
251d6d97f60SAnna Dabrowska    public function getConf()
252d6d97f60SAnna Dabrowska    {
2531a07b696SMichael Große        return $this->config;
2541a07b696SMichael Große    }
2555511bd5bSAndreas Gohr}
256