xref: /dokuwiki/inc/Action/Search.php (revision 06053dca2fac9a1da4eb1accf8c2488942da5d2a)
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        $highlight = [];
78        $this->fullTextResults = (new FulltextSearch)->pageSearch(
79                $QUERY, $highlight, $INPUT->str('srt'), $after, $before
80        );
81        $this->highlight = $highlight;
82    }
83
84    /**
85     * Adjust the global query accordingly to the config search_nslimit and search_fragment
86     *
87     * This will only do something if the search didn't originate from the form on the searchpage itself
88     */
89    protected function adjustGlobalQuery()
90    {
91        global $conf, $INPUT, $QUERY, $ID;
92
93        if ($INPUT->bool('sf')) {
94            return;
95        }
96
97        $parsedQuery = (new QueryParser)->convert($QUERY);
98
99        if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) {
100            if ($conf['search_nslimit'] > 0) {
101                if (getNS($ID) !== false) {
102                    $nsParts = explode(':', getNS($ID));
103                    $ns = implode(':', array_slice($nsParts, 0, $conf['search_nslimit']));
104                    $QUERY .= " @$ns";
105                }
106            }
107        }
108
109        if ($conf['search_fragment'] !== 'exact') {
110            if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) {
111                if (!str_contains($QUERY, '*')) {
112                    $queryParts = explode(' ', $QUERY);
113                    $queryParts = array_map(function ($part) {
114                        if (str_starts_with($part, '@')) {
115                            return $part;
116                        }
117                        if (str_starts_with($part, 'ns:')) {
118                            return $part;
119                        }
120                        if (str_starts_with($part, '^')) {
121                            return $part;
122                        }
123                        if (str_starts_with($part, '-ns:')) {
124                            return $part;
125                        }
126
127                        global $conf;
128
129                        if ($conf['search_fragment'] === 'starts_with') {
130                            return $part . '*';
131                        }
132                        if ($conf['search_fragment'] === 'ends_with') {
133                            return '*' . $part;
134                        }
135
136                        return '*' . $part . '*';
137                    }, $queryParts);
138                    $QUERY = implode(' ', $queryParts);
139                }
140            }
141        }
142    }
143}
144