xref: /dokuwiki/inc/Ui/SearchState.php (revision 9329b002986cc3f43c18c207dd9d0fdfd0f8a5e8)
118856c5dSMichael Große<?php
218856c5dSMichael Große
318856c5dSMichael Großenamespace dokuwiki\Ui;
418856c5dSMichael Große
50cba610bSSatoshi Saharause dokuwiki\Search\QueryParser;
60cba610bSSatoshi Sahara
718856c5dSMichael Großeclass SearchState
818856c5dSMichael Große{
918856c5dSMichael Große    /**
1018856c5dSMichael Große     * @var array
1118856c5dSMichael Große     */
1218856c5dSMichael Große    protected $parsedQuery = [];
1318856c5dSMichael Große
1452d4cd42SMichael Große    /**
1552d4cd42SMichael Große     * SearchState constructor.
1652d4cd42SMichael Große     *
1752d4cd42SMichael Große     * @param array $parsedQuery
1852d4cd42SMichael Große     */
1918856c5dSMichael Große    public function __construct(array $parsedQuery)
2018856c5dSMichael Große    {
2118856c5dSMichael Große        global $INPUT;
2218856c5dSMichael Große
2318856c5dSMichael Große        $this->parsedQuery = $parsedQuery;
2452d4cd42SMichael Große        if (!isset($parsedQuery['after'])) {
25422bbbc6SMichael Große            $this->parsedQuery['after'] = $INPUT->str('min');
2652d4cd42SMichael Große        }
2752d4cd42SMichael Große        if (!isset($parsedQuery['before'])) {
28422bbbc6SMichael Große            $this->parsedQuery['before'] = $INPUT->str('max');
2952d4cd42SMichael Große        }
3052d4cd42SMichael Große        if (!isset($parsedQuery['sort'])) {
311265b193SMichael Große            $this->parsedQuery['sort'] = $INPUT->str('srt');
3218856c5dSMichael Große        }
3318856c5dSMichael Große    }
3418856c5dSMichael Große
3518856c5dSMichael Große    /**
3652d4cd42SMichael Große     * Get a search state for the current search limited to a new namespace
3718856c5dSMichael Große     *
3852d4cd42SMichael Große     * @param string $ns the namespace to which to limit the search, falsy to remove the limitation
3952d4cd42SMichael Große     * @param array  $notns
4052d4cd42SMichael Große     *
4152d4cd42SMichael Große     * @return SearchState
4252d4cd42SMichael Große     */
4352d4cd42SMichael Große    public function withNamespace($ns, array $notns = [])
4452d4cd42SMichael Große    {
4552d4cd42SMichael Große        $parsedQuery = $this->parsedQuery;
4652d4cd42SMichael Große        $parsedQuery['ns'] = $ns ? [$ns] : [];
4752d4cd42SMichael Große        $parsedQuery['notns'] = $notns;
4852d4cd42SMichael Große
4952d4cd42SMichael Große        return new SearchState($parsedQuery);
5052d4cd42SMichael Große    }
5152d4cd42SMichael Große
5252d4cd42SMichael Große    /**
5352d4cd42SMichael Große     * Get a search state for the current search with new search fragments and optionally phrases
5452d4cd42SMichael Große     *
5518856c5dSMichael Große     * @param array $and
56df977249SMichael Große     * @param array $not
5752d4cd42SMichael Große     * @param array $phrases
5852d4cd42SMichael Große     *
5952d4cd42SMichael Große     * @return SearchState
6018856c5dSMichael Große     */
6152d4cd42SMichael Große    public function withFragments(array $and, array $not, array $phrases = [])
6218856c5dSMichael Große    {
6318856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
6418856c5dSMichael Große        $parsedQuery['and'] = $and;
65df977249SMichael Große        $parsedQuery['not'] = $not;
6652d4cd42SMichael Große        $parsedQuery['phrases'] = $phrases;
6752d4cd42SMichael Große
6852d4cd42SMichael Große        return new SearchState($parsedQuery);
6918856c5dSMichael Große    }
7018856c5dSMichael Große
7118856c5dSMichael Große    /**
7252d4cd42SMichael Große     * Get a search state for the current search with with adjusted time limitations
7318856c5dSMichael Große     *
7452d4cd42SMichael Große     * @param $after
7552d4cd42SMichael Große     * @param $before
7652d4cd42SMichael Große     *
7752d4cd42SMichael Große     * @return SearchState
7818856c5dSMichael Große     */
7952d4cd42SMichael Große    public function withTimeLimitations($after, $before)
8018856c5dSMichael Große    {
8118856c5dSMichael Große        $parsedQuery = $this->parsedQuery;
8218856c5dSMichael Große        $parsedQuery['after'] = $after;
8318856c5dSMichael Große        $parsedQuery['before'] = $before;
8418856c5dSMichael Große
8552d4cd42SMichael Große        return new SearchState($parsedQuery);
8618856c5dSMichael Große    }
8718856c5dSMichael Große
888d0e286aSMichael Große    /**
8952d4cd42SMichael Große     * Get a search state for the current search with adjusted sort preference
908d0e286aSMichael Große     *
9152d4cd42SMichael Große     * @param $sort
9252d4cd42SMichael Große     *
9352d4cd42SMichael Große     * @return SearchState
948d0e286aSMichael Große     */
9552d4cd42SMichael Große    public function withSorting($sort)
968d0e286aSMichael Große    {
978d0e286aSMichael Große        $parsedQuery = $this->parsedQuery;
988d0e286aSMichael Große        $parsedQuery['sort'] = $sort;
998d0e286aSMichael Große
10052d4cd42SMichael Große        return new SearchState($parsedQuery);
1018d0e286aSMichael Große    }
1028d0e286aSMichael Große
10352d4cd42SMichael Große    /**
10452d4cd42SMichael Große     * Get a link that represents the current search state
10552d4cd42SMichael Große     *
10652d4cd42SMichael Große     * Note that this represents only a simplified version of the search state.
10752d4cd42SMichael Große     * Grouping with braces and "OR" conditions are not supported.
10852d4cd42SMichael Große     *
10952d4cd42SMichael Große     * @param $label
11052d4cd42SMichael Große     *
11152d4cd42SMichael Große     * @return string
11252d4cd42SMichael Große     */
11352d4cd42SMichael Große    public function getSearchLink($label)
11452d4cd42SMichael Große    {
11552d4cd42SMichael Große        global $ID, $conf;
11652d4cd42SMichael Große        $parsedQuery = $this->parsedQuery;
11752d4cd42SMichael Große
11852d4cd42SMichael Große        $tagAttributes = [
11952d4cd42SMichael Große            'target' => $conf['target']['wiki'],
12052d4cd42SMichael Große        ];
12118856c5dSMichael Große
122*9329b002SSatoshi Sahara        $newQuery = (new QueryParser)->revert(
12318856c5dSMichael Große            $parsedQuery['and'],
12418856c5dSMichael Große            $parsedQuery['not'],
12518856c5dSMichael Große            $parsedQuery['phrases'],
12618856c5dSMichael Große            $parsedQuery['ns'],
12718856c5dSMichael Große            $parsedQuery['notns']
12818856c5dSMichael Große        );
1291265b193SMichael Große        $hrefAttributes = ['do' => 'search', 'sf' => '1', 'q' => $newQuery];
13018856c5dSMichael Große        if ($parsedQuery['after']) {
131422bbbc6SMichael Große            $hrefAttributes['min'] = $parsedQuery['after'];
13218856c5dSMichael Große        }
13318856c5dSMichael Große        if ($parsedQuery['before']) {
134422bbbc6SMichael Große            $hrefAttributes['max'] = $parsedQuery['before'];
13518856c5dSMichael Große        }
1368d0e286aSMichael Große        if ($parsedQuery['sort']) {
1371265b193SMichael Große            $hrefAttributes['srt'] = $parsedQuery['sort'];
1388d0e286aSMichael Große        }
13952d4cd42SMichael Große
14052d4cd42SMichael Große        $href = wl($ID, $hrefAttributes, false, '&');
1415d021066SAnika Henke        return "<a href='$href' " . buildAttributes($tagAttributes, true) . ">$label</a>";
14218856c5dSMichael Große    }
14318856c5dSMichael Große}
144