Failed Conditions
Push — master ( 430a27...5c0b2e )
by Michael
14:35 queued 10:36
created

Search::adjustGlobalQuery()   C

Complexity

Conditions 15
Paths 17

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 33
nc 17
nop 0
dl 0
loc 56
rs 6.6357
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace dokuwiki\Action;
4
5
use dokuwiki\Action\Exception\ActionAbort;
6
7
/**
8
 * Class Search
9
 *
10
 * Search for pages and content
11
 *
12
 * @package dokuwiki\Action
13
 */
14
class 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 checkPermissions() {
31
        parent::checkPermissions();
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
            $urlParts['id'] = $conf['start'];
43
            $url = DOKU_URL . DOKU_SCRIPT . '?' . http_build_query($urlParts, null, '&');
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_limit_to_first_ns and search_default_fragment_behaviour
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_limit_to_first_ns'] > 0) {
92
                if (getNS($ID) !== false) {
93
                    $nsParts = explode(':', getNS($ID));
94
                    $ns = implode(':', array_slice($nsParts, 0, $conf['search_limit_to_first_ns']));
95
                    $QUERY .= " @$ns";
96
                }
97
            }
98
        }
99
100
        if ($conf['search_default_fragment_behaviour'] !== '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_default_fragment_behaviour'] === 'starts_with') {
121
                            return $part . '*';
122
                        }
123
                        if ($conf['search_default_fragment_behaviour'] === 'ends_with') {
124
                            return '*' . $part;
125
                        }
126
127
                        return '*' . $part . '*';
128
129
                    }, $queryParts);
130
                    $QUERY = implode(' ', $queryParts);
131
                }
132
            }
133
        }
134
    }
135
}
136