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