xref: /dokuwiki/inc/Action/Search.php (revision d09b5b6441fa2464fba24f84eff22348b879676c)
1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Action\Exception\ActionAbort;
6
7/**
8 * Class Search
9 *
10 * Search for pages and content
11 *
12 * @package dokuwiki\Action
13 */
14class Search extends AbstractAction {
15
16    /** @inheritdoc */
17    public function minimumPermission() {
18        return AUTH_NONE;
19    }
20
21    /**
22     * we only search if a search word was given
23     *
24     * @inheritdoc
25     */
26    public function checkPermissions() {
27        parent::checkPermissions();
28        global $QUERY;
29        $s = cleanID($QUERY);
30        if($s === '') throw new ActionAbort();
31    }
32
33    public function preProcess()
34    {
35        $this->adjustGlobalQuery();
36    }
37
38    /** @inheritdoc */
39    public function tplContent()
40    {
41        $search = new \dokuwiki\Ui\Search();
42        $search->execute();
43        $search->show();
44    }
45
46    /**
47     * Adjust the global query accordingly to the config search_limit_to_first_ns and search_default_fragment_behaviour
48     *
49     * This will only do something if the search didn't originate from the form on the searchpage itself
50     */
51    protected function adjustGlobalQuery()
52    {
53        global $conf, $INPUT, $QUERY;
54
55        if ($INPUT->bool('searchPageForm')) {
56            return;
57        }
58
59        $Indexer = idx_get_indexer();
60        $parsedQuery = ft_queryParser($Indexer, $QUERY);
61
62        if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) {
63            if ($conf['search_limit_to_first_ns'] > 0) {
64                $searchOriginPage = $INPUT->str('from');
65                if (getNS($searchOriginPage) !== false) {
66                    $nsParts = explode(':', getNS($searchOriginPage));
67                    $ns = implode(':', array_slice($nsParts, 0, $conf['search_limit_to_first_ns']));
68                    $QUERY .= " @$ns";
69                }
70            }
71        }
72
73        if ($conf['search_default_fragment_behaviour'] !== 'exact') {
74            if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) {
75                if (strpos($QUERY, '*') === false) {
76                    $queryParts = explode(' ', $QUERY);
77                    $queryParts = array_map(function ($part) {
78                        if (strpos($part, '@') === 0) {
79                            return $part;
80                        }
81                        if (strpos($part, 'ns:') === 0) {
82                            return $part;
83                        }
84                        if (strpos($part, '^') === 0) {
85                            return $part;
86                        }
87                        if (strpos($part, '-ns:') === 0) {
88                            return $part;
89                        }
90
91                        global $conf;
92
93                        if ($conf['search_default_fragment_behaviour'] === 'starts_with') {
94                            return $part . '*';
95                        }
96                        if ($conf['search_default_fragment_behaviour'] === 'ends_with') {
97                            return '*' . $part;
98                        }
99
100                        return '*' . $part . '*';
101
102                    }, $queryParts);
103                    $QUERY = implode(' ', $queryParts);
104                }
105            }
106        }
107    }
108}
109