xref: /plugin/struct/meta/SearchConfig.php (revision e87d1e74008a472055cf768a3cdfde449e458203)
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 */
125511bd5bSAndreas Gohrclass SearchConfig extends Search {
135511bd5bSAndreas Gohr
1416b7d914SAndreas Gohr    /** @var int default aggregation caching (depends on last struct save) */
1516b7d914SAndreas Gohr    static public $CACHE_DEFAULT = 1;
1616b7d914SAndreas Gohr    /** @var int caching depends on current user */
1716b7d914SAndreas Gohr    static public $CACHE_USER = 2;
1816b7d914SAndreas Gohr    /** @var int caching depends on current date */
1916b7d914SAndreas Gohr    static public $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     */
405511bd5bSAndreas Gohr    public function __construct($config) {
415511bd5bSAndreas Gohr        parent::__construct();
425511bd5bSAndreas Gohr
433ad292a8SAndreas Gohr        // setup schemas and columns
440215a637SAndreas Gohr        if(!empty($config['schemas'])) foreach($config['schemas'] as $schema) {
453ad292a8SAndreas Gohr            $this->addSchema($schema[0], $schema[1]);
463ad292a8SAndreas Gohr        }
470215a637SAndreas Gohr        if(!empty($config['cols'])) foreach($config['cols'] as $col) {
483ad292a8SAndreas Gohr            $this->addColumn($col);
493ad292a8SAndreas Gohr        }
503ad292a8SAndreas Gohr
5116b7d914SAndreas Gohr        // cache flag setting
5216b7d914SAndreas Gohr        $this->cacheFlag = self::$CACHE_DEFAULT;
5316b7d914SAndreas Gohr        if(!empty($config['filters'])) $this->cacheFlag = $this->determineCacheFlag($config['filters']);
5416b7d914SAndreas Gohr
5500f6af48SAndreas Gohr        // apply dynamic paramters
5600f6af48SAndreas Gohr        $this->dynamicParameters = new SearchConfigParameters($this);
57668e4f8eSAndreas Gohr        $config = $this->dynamicParameters->updateConfig($config);
5800f6af48SAndreas Gohr
5900f6af48SAndreas Gohr        // configure search from configuration
6000f6af48SAndreas Gohr        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
615625b985SAndreas Gohr            $this->addFilter($filter[0], $this->applyFilterVars($filter[2]), $filter[1], $filter[3]);
625511bd5bSAndreas Gohr        }
6300f6af48SAndreas Gohr
6400f6af48SAndreas Gohr        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
65aa124708SAndreas Gohr            $this->addSort($sort[0], $sort[1]);
661a07b696SMichael Große        }
671a07b696SMichael Große
681a07b696SMichael Große        if(!empty($config['limit'])) {
691a07b696SMichael Große            $this->setLimit($config['limit']);
701a07b696SMichael Große        }
7100f6af48SAndreas Gohr
7200f6af48SAndreas Gohr        if(!empty($config['offset'])) {
73d2a8ce05SPaweł Czochański            $this->setOffset($config['offset']);
7400f6af48SAndreas Gohr        }
75668e4f8eSAndreas Gohr
76668e4f8eSAndreas Gohr        $this->config = $config;
771a07b696SMichael Große    }
785511bd5bSAndreas Gohr
7900f6af48SAndreas Gohr    /**
8016b7d914SAndreas Gohr     * Set the cache flag accordingly to the set filter placeholders
8116b7d914SAndreas Gohr     *
8216b7d914SAndreas Gohr     * @param array $filters
8316b7d914SAndreas Gohr     * @return int
8416b7d914SAndreas Gohr     */
8516b7d914SAndreas Gohr    protected function determineCacheFlag($filters) {
8616b7d914SAndreas Gohr        $flags = self::$CACHE_DEFAULT;
8716b7d914SAndreas Gohr
8816b7d914SAndreas Gohr        foreach($filters as $filter) {
8916b7d914SAndreas Gohr            if(is_array($filter)) $filter = $filter[2]; // this is the format we get fro the config parser
9016b7d914SAndreas Gohr
9116b7d914SAndreas Gohr            if(strpos($filter, '$USER$') !== false) {
9216b7d914SAndreas Gohr                $flags |= self::$CACHE_USER;
9316b7d914SAndreas Gohr            } else if(strpos($filter, '$TODAY$') !== false) {
9416b7d914SAndreas Gohr                $flags |= self::$CACHE_DATE;
9516b7d914SAndreas Gohr            }
9616b7d914SAndreas Gohr        }
9716b7d914SAndreas Gohr
9816b7d914SAndreas Gohr        return $flags;
9916b7d914SAndreas Gohr    }
10016b7d914SAndreas Gohr
10116b7d914SAndreas Gohr    /**
1025625b985SAndreas Gohr     * Replaces placeholders in the given filter value by the proper value
1035625b985SAndreas Gohr     *
1045625b985SAndreas Gohr     * @param string $filter
10553528ecfSAndreas Gohr     * @return string|string[] Result may be an array when a multi column placeholder is used
1065625b985SAndreas Gohr     */
1075625b985SAndreas Gohr    protected function applyFilterVars($filter) {
10806fee43aSMichael Grosse        global $INFO;
1095625b985SAndreas Gohr
1105625b985SAndreas Gohr        // apply inexpensive filters first
1115625b985SAndreas Gohr        $filter = str_replace(
1125625b985SAndreas Gohr            array(
1135625b985SAndreas Gohr                '$ID$',
1145625b985SAndreas Gohr                '$NS$',
1155625b985SAndreas Gohr                '$PAGE$',
1165625b985SAndreas Gohr                '$USER$',
1175625b985SAndreas Gohr                '$TODAY$'
1185625b985SAndreas Gohr            ),
1195625b985SAndreas Gohr            array(
12006fee43aSMichael Grosse                $INFO['id'],
12106fee43aSMichael Grosse                getNS($INFO['id']),
12206fee43aSMichael Grosse                noNS($INFO['id']),
1235625b985SAndreas Gohr                isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '',
1245625b985SAndreas Gohr                date('Y-m-d')
1255625b985SAndreas Gohr            ),
1265625b985SAndreas Gohr            $filter
1275625b985SAndreas Gohr        );
1285625b985SAndreas Gohr
12953528ecfSAndreas Gohr        // apply struct column placeholder (we support only one!)
13053528ecfSAndreas Gohr        if(preg_match('/^(.*?)(?:\$STRUCT\.(.*?)\$)(.*?)$/', $filter, $match)) {
13153528ecfSAndreas Gohr            $key = $match[2];
132d19ba4b1SAndreas Gohr
133aec9051bSAndreas Gohr            // we try to resolve the key via current schema aliases first, otherwise take it literally
134aec9051bSAndreas Gohr            $column = $this->findColumn($key);
1355625b985SAndreas Gohr            if($column) {
1365625b985SAndreas Gohr                $label = $column->getLabel();
1375625b985SAndreas Gohr                $table = $column->getTable();
138aec9051bSAndreas Gohr            } else {
139aec9051bSAndreas Gohr                list($table, $label) = explode('.', $key);
140aec9051bSAndreas Gohr            }
141aec9051bSAndreas Gohr
142aec9051bSAndreas Gohr            // get the data from the current page
143aec9051bSAndreas Gohr            if($table && $label) {
14406fee43aSMichael Grosse                $schemaData = AccessTable::byTableName($table, $INFO['id'], 0);
1457717c082SMichael Große                $data = $schemaData->getData();
14634db2096SMichael Große                if (!isset($data[$label])) {
147*e87d1e74SMichael Große                    throw new StructException("column not in table", $label, $table);
14834db2096SMichael Große                }
1497717c082SMichael Große                $value = $data[$label]->getCompareValue();
150c75f25cfSAndreas Gohr
151c75f25cfSAndreas Gohr                if(is_array($value) && !count($value)) {
152c75f25cfSAndreas Gohr                    $value = '';
153c75f25cfSAndreas Gohr                }
1545625b985SAndreas Gohr            } else {
1555625b985SAndreas Gohr                $value = '';
1565625b985SAndreas Gohr            }
1578b8243b2SAndreas Gohr
15853528ecfSAndreas Gohr            // apply any pre and postfixes, even when multi value
15953528ecfSAndreas Gohr            if(is_array($value)) {
16053528ecfSAndreas Gohr                $filter = array();
16153528ecfSAndreas Gohr                foreach($value as $item) {
16253528ecfSAndreas Gohr                    $filter[] = $match[1] . $item . $match[3];
16353528ecfSAndreas Gohr                }
16453528ecfSAndreas Gohr            } else {
16553528ecfSAndreas Gohr                $filter = $match[1] . $value . $match[3];
16653528ecfSAndreas Gohr            }
1675625b985SAndreas Gohr        }
1685625b985SAndreas Gohr
1695625b985SAndreas Gohr        return $filter;
1705625b985SAndreas Gohr    }
1715625b985SAndreas Gohr
1725625b985SAndreas Gohr    /**
17316b7d914SAndreas Gohr     * @return int cacheflag for this search
17416b7d914SAndreas Gohr     */
17516b7d914SAndreas Gohr    public function getCacheFlag() {
17616b7d914SAndreas Gohr        return $this->cacheFlag;
17716b7d914SAndreas Gohr    }
17816b7d914SAndreas Gohr
17916b7d914SAndreas Gohr    /**
18000f6af48SAndreas Gohr     * Access the dynamic paramters of this search
18100f6af48SAndreas Gohr     *
182668e4f8eSAndreas Gohr     * Note: This call returns a clone of the parameters as they were initialized
18300f6af48SAndreas Gohr     *
18400f6af48SAndreas Gohr     * @return SearchConfigParameters
18500f6af48SAndreas Gohr     */
18600f6af48SAndreas Gohr    public function getDynamicParameters() {
18700f6af48SAndreas Gohr        return clone $this->dynamicParameters;
1885511bd5bSAndreas Gohr    }
1895511bd5bSAndreas Gohr
19000f6af48SAndreas Gohr    /**
19107993756SAndreas Gohr     * @return array the current config
19200f6af48SAndreas Gohr     */
19307993756SAndreas Gohr    public function getConf() {
1941a07b696SMichael Große        return $this->config;
1951a07b696SMichael Große    }
1961a07b696SMichael Große
1975511bd5bSAndreas Gohr}
198