xref: /dokuwiki/inc/Action/Search.php (revision ede4646658cf51245060332d97a319a39c788ea1)
1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Action\Exception\ActionAbort;
6use dokuwiki\Search\FulltextSearch;
7use dokuwiki\Search\MetadataSearch;
8use dokuwiki\Search\Query\QueryParser;
9
10/**
11 * Class Search
12 *
13 * Search for pages and content
14 *
15 * @package dokuwiki\Action
16 */
17class Search extends AbstractAction
18{
19    protected $pageLookupResults = [];
20    protected $fullTextResults = [];
21    protected $highlight = [];
22
23    /** @inheritdoc */
24    public function minimumPermission()
25    {
26        return AUTH_NONE;
27    }
28
29    /**
30     * we only search if a search word was given
31     *
32     * @inheritdoc
33     */
34    public function checkPreconditions()
35    {
36        parent::checkPreconditions();
37    }
38
39    public function preProcess()
40    {
41        global $QUERY, $ID, $conf, $INPUT;
42        $s = cleanID($QUERY);
43
44        if ($ID !== $conf['start'] && !$INPUT->has('q')) {
45            parse_str($INPUT->server->str('QUERY_STRING'), $urlParts);
46            $urlParts['q'] = $urlParts['id'];
47            unset($urlParts['id']);
48            $url = wl($ID, $urlParts, true, '&');
49            send_redirect($url);
50        }
51
52        if ($s === '') throw new ActionAbort();
53        $this->adjustGlobalQuery();
54    }
55
56    /** @inheritdoc */
57    public function tplContent()
58    {
59        $this->execute();
60
61        $search = new \dokuwiki\Ui\Search($this->pageLookupResults, $this->fullTextResults, $this->highlight);
62        $search->show();
63    }
64
65
66    /**
67     * run the search
68     */
69    protected function execute()
70    {
71        global $INPUT, $QUERY;
72        $after = $INPUT->str('min');
73        $before = $INPUT->str('max');
74        $this->pageLookupResults = (new MetadataSearch)->pageLookup(
75                $QUERY, true, useHeading('navigation'), $after, $before
76        );
77        $this->fullTextResults = (new FulltextSearch)->pageSearch(
78                $QUERY, $highlight, $INPUT->str('srt'), $after, $before
79        );
80        $this->highlight = $highlight;
81    }
82
83    /**
84     * Adjust the global query accordingly to the config search_nslimit and search_fragment
85     *
86     * This will only do something if the search didn't originate from the form on the searchpage itself
87     */
88    protected function adjustGlobalQuery()
89    {
90        global $conf, $INPUT, $QUERY, $ID;
91
92        if ($INPUT->bool('sf')) {
93            return;
94        }
95
96        $parsedQuery = (new QueryParser)->convert($QUERY);
97
98        if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) {
99            if ($conf['search_nslimit'] > 0) {
100                if (getNS($ID) !== false) {
101                    $nsParts = explode(':', getNS($ID));
102                    $ns = implode(':', array_slice($nsParts, 0, $conf['search_nslimit']));
103                    $QUERY .= " @$ns";
104                }
105            }
106        }
107
108        if ($conf['search_fragment'] !== 'exact') {
109            if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) {
110                if (!str_contains($QUERY, '*')) {
111                    $queryParts = explode(' ', $QUERY);
112                    $queryParts = array_map(function ($part) {
113                        if (str_starts_with($part, '@')) {
114                            return $part;
115                        }
116                        if (str_starts_with($part, 'ns:')) {
117                            return $part;
118                        }
119                        if (str_starts_with($part, '^')) {
120                            return $part;
121                        }
122                        if (str_starts_with($part, '-ns:')) {
123                            return $part;
124                        }
125
126                        global $conf;
127
128                        if ($conf['search_fragment'] === 'starts_with') {
129                            return $part . '*';
130                        }
131                        if ($conf['search_fragment'] === 'ends_with') {
132                            return '*' . $part;
133                        }
134
135                        return '*' . $part . '*';
136                    }, $queryParts);
137                    $QUERY = implode(' ', $queryParts);
138                }
139            }
140        }
141    }
142}
143