xref: /dokuwiki/inc/Action/Search.php (revision d22b78c8b597b55c8f2e9859f7d626efcef322ce)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
564ab5140SAndreas Gohruse dokuwiki\Action\Exception\ActionAbort;
664ab5140SAndreas Gohr
7ab583a1bSAndreas Gohr/**
8ab583a1bSAndreas Gohr * Class Search
9ab583a1bSAndreas Gohr *
10ab583a1bSAndreas Gohr * Search for pages and content
11ab583a1bSAndreas Gohr *
12ab583a1bSAndreas Gohr * @package dokuwiki\Action
13ab583a1bSAndreas Gohr */
1464ab5140SAndreas Gohrclass Search extends AbstractAction {
1564ab5140SAndreas Gohr
1664ab5140SAndreas Gohr    /** @inheritdoc */
17ec701221SAndreas Gohr    public function minimumPermission() {
1864ab5140SAndreas Gohr        return AUTH_NONE;
1964ab5140SAndreas Gohr    }
2064ab5140SAndreas Gohr
2164ab5140SAndreas Gohr    /**
2264ab5140SAndreas Gohr     * we only search if a search word was given
2364ab5140SAndreas Gohr     *
2464ab5140SAndreas Gohr     * @inheritdoc
2564ab5140SAndreas Gohr     */
2664ab5140SAndreas Gohr    public function checkPermissions() {
2764ab5140SAndreas Gohr        parent::checkPermissions();
2864ab5140SAndreas Gohr    }
2964ab5140SAndreas Gohr
30d09b5b64SMichael Große    public function preProcess()
31d09b5b64SMichael Große    {
32*d22b78c8SMichael Große        global $QUERY, $ID, $conf, $INPUT;
33*d22b78c8SMichael Große        $s = cleanID($QUERY);
34*d22b78c8SMichael Große
35*d22b78c8SMichael Große        if ($ID !== $conf['start'] && $s === '') {
36*d22b78c8SMichael Große            parse_str($INPUT->server->str('QUERY_STRING'), $urlParts);
37*d22b78c8SMichael Große            $urlParts['q'] = $urlParts['id'];
38*d22b78c8SMichael Große            $urlParts['id'] = $conf['start'];
39*d22b78c8SMichael Große            $url = DOKU_URL . DOKU_SCRIPT . '?' . http_build_query($urlParts, null, '&');
40*d22b78c8SMichael Große            send_redirect($url);
41*d22b78c8SMichael Große        }
42*d22b78c8SMichael Große
43*d22b78c8SMichael Große        if ($s === '') throw new ActionAbort();
44d09b5b64SMichael Große        $this->adjustGlobalQuery();
45d09b5b64SMichael Große    }
46d09b5b64SMichael Große
4764ab5140SAndreas Gohr    /** @inheritdoc */
48d09b5b64SMichael Große    public function tplContent()
49d09b5b64SMichael Große    {
50d09b5b64SMichael Große        $search = new \dokuwiki\Ui\Search();
5121fcef82SMichael Große        $search->execute();
5221fcef82SMichael Große        $search->show();
5364ab5140SAndreas Gohr    }
54d09b5b64SMichael Große
55d09b5b64SMichael Große    /**
56d09b5b64SMichael Große     * Adjust the global query accordingly to the config search_limit_to_first_ns and search_default_fragment_behaviour
57d09b5b64SMichael Große     *
58d09b5b64SMichael Große     * This will only do something if the search didn't originate from the form on the searchpage itself
59d09b5b64SMichael Große     */
60d09b5b64SMichael Große    protected function adjustGlobalQuery()
61d09b5b64SMichael Große    {
62d09b5b64SMichael Große        global $conf, $INPUT, $QUERY;
63d09b5b64SMichael Große
64d09b5b64SMichael Große        if ($INPUT->bool('searchPageForm')) {
65d09b5b64SMichael Große            return;
66d09b5b64SMichael Große        }
67d09b5b64SMichael Große
68d09b5b64SMichael Große        $Indexer = idx_get_indexer();
69d09b5b64SMichael Große        $parsedQuery = ft_queryParser($Indexer, $QUERY);
70d09b5b64SMichael Große
71d09b5b64SMichael Große        if (empty($parsedQuery['ns']) && empty($parsedQuery['notns'])) {
72d09b5b64SMichael Große            if ($conf['search_limit_to_first_ns'] > 0) {
73d09b5b64SMichael Große                $searchOriginPage = $INPUT->str('from');
74d09b5b64SMichael Große                if (getNS($searchOriginPage) !== false) {
75d09b5b64SMichael Große                    $nsParts = explode(':', getNS($searchOriginPage));
76d09b5b64SMichael Große                    $ns = implode(':', array_slice($nsParts, 0, $conf['search_limit_to_first_ns']));
77d09b5b64SMichael Große                    $QUERY .= " @$ns";
78d09b5b64SMichael Große                }
79d09b5b64SMichael Große            }
80d09b5b64SMichael Große        }
81d09b5b64SMichael Große
82d09b5b64SMichael Große        if ($conf['search_default_fragment_behaviour'] !== 'exact') {
83d09b5b64SMichael Große            if (empty(array_diff($parsedQuery['words'], $parsedQuery['and']))) {
84d09b5b64SMichael Große                if (strpos($QUERY, '*') === false) {
85d09b5b64SMichael Große                    $queryParts = explode(' ', $QUERY);
86d09b5b64SMichael Große                    $queryParts = array_map(function ($part) {
87d09b5b64SMichael Große                        if (strpos($part, '@') === 0) {
88d09b5b64SMichael Große                            return $part;
89d09b5b64SMichael Große                        }
90d09b5b64SMichael Große                        if (strpos($part, 'ns:') === 0) {
91d09b5b64SMichael Große                            return $part;
92d09b5b64SMichael Große                        }
93d09b5b64SMichael Große                        if (strpos($part, '^') === 0) {
94d09b5b64SMichael Große                            return $part;
95d09b5b64SMichael Große                        }
96d09b5b64SMichael Große                        if (strpos($part, '-ns:') === 0) {
97d09b5b64SMichael Große                            return $part;
98d09b5b64SMichael Große                        }
99d09b5b64SMichael Große
100d09b5b64SMichael Große                        global $conf;
101d09b5b64SMichael Große
102d09b5b64SMichael Große                        if ($conf['search_default_fragment_behaviour'] === 'starts_with') {
103d09b5b64SMichael Große                            return $part . '*';
104d09b5b64SMichael Große                        }
105d09b5b64SMichael Große                        if ($conf['search_default_fragment_behaviour'] === 'ends_with') {
106d09b5b64SMichael Große                            return '*' . $part;
107d09b5b64SMichael Große                        }
108d09b5b64SMichael Große
109d09b5b64SMichael Große                        return '*' . $part . '*';
110d09b5b64SMichael Große
111d09b5b64SMichael Große                    }, $queryParts);
112d09b5b64SMichael Große                    $QUERY = implode(' ', $queryParts);
113d09b5b64SMichael Große                }
114d09b5b64SMichael Große            }
115d09b5b64SMichael Große        }
116d09b5b64SMichael Große    }
11764ab5140SAndreas Gohr}
118