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