Passed
Pull Request — master (#753)
by Matthew
04:17
created

PageSearch::getCommentSearchResults()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 9
c 2
b 1
f 0
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.9666
cc 3
nc 4
nop 2
crap 12
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Waca\DataObjects\Request;
12
use Waca\DataObjects\User;
13
use Waca\Exceptions\AccessDeniedException;
14
use Waca\Exceptions\ApplicationLogicException;
15
use Waca\Fragments\RequestListData;
16
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
17
use Waca\SessionAlert;
18
use Waca\Tasks\PagedInternalPageBase;
19
use Waca\WebRequest;
20
21
class PageSearch extends PagedInternalPageBase
22
{
23
    use RequestListData;
24
25
    /**
26
     * Main function for this page, when no specific actions are called.
27
     */
28
    protected function main()
29
    {
30
        $this->setHtmlTitle('Search');
31
32
        $database = $this->getDatabase();
33
        $currentUser = User::getCurrent($database);
34
35
        $this->assign('canSearchByComment', $this->barrierTest('byComment', $currentUser));
36
        $this->assign('canSearchByEmail', $this->barrierTest('byEmail', $currentUser));
37
        $this->assign('canSearchByIp', $this->barrierTest('byIp', $currentUser));
38
        $this->assign('canSearchByName', $this->barrierTest('byName', $currentUser));
39
        $this->assign('canSeeNonConfirmed', $this->barrierTest('allowNonConfirmed', $currentUser));
40
41
        $this->setTemplate('search/main.tpl');
42
43
        // Dual-mode page
44
        if (WebRequest::getString('type') !== null) {
45
            $searchType = WebRequest::getString('type');
46
            $searchTerm = WebRequest::getString('term');
47
48
            $excludeNonConfirmed = true;
49
            if ($this->barrierTest('allowNonConfirmed', $currentUser)) {
50
                $excludeNonConfirmed = WebRequest::getBoolean('excludeNonConfirmed');
51
            }
52
53
            $validationError = "";
54
            if (!$this->validateSearchParameters($searchType, $searchTerm, $validationError)) {
55
                SessionAlert::error($validationError, "Search error");
56
57
                $this->assign('term', $searchTerm);
58
                $this->assign('target', $searchType);
59
                $this->assign('excludeNonConfirmed', $excludeNonConfirmed);
60
                $this->assign('hasResultset', false);
61
62
                return;
63
            }
64
65
            // searchType known to be sane from the validate step above
66
            if (!$this->barrierTest('by' . ucfirst($searchType), User::getCurrent($this->getDatabase()))) {
0 ignored issues
show
Bug introduced by
It seems like $searchType can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            if (!$this->barrierTest('by' . ucfirst(/** @scrutinizer ignore-type */ $searchType), User::getCurrent($this->getDatabase()))) {
Loading history...
67
                // only accessible by url munging, don't care about the UX
68
                throw new AccessDeniedException($this->getSecurityManager(), $this->getDomainAccessManager());
69
            }
70
71
            $requestSearch = RequestSearchHelper::get($database);
72
73
            $this->setSearchHelper($requestSearch);
74
            $this->setupLimits();
75
76
            if ($excludeNonConfirmed) {
77
                $requestSearch->withConfirmedEmail();
78
            }
79
80
            switch ($searchType) {
81
                case 'name':
82
                    $this->getNameSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getNameSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                    $this->getNameSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
83
                    break;
84
                case 'email':
85
                    $this->getEmailSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getEmailSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                    $this->getEmailSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
86
                    break;
87
                case 'ip':
88
                    $this->getIpSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getIpSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                    $this->getIpSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
89
                    break;
90
                case 'comment':
91
                    $this->getCommentSearchResults($requestSearch, $searchTerm);
0 ignored issues
show
Bug introduced by
It seems like $searchTerm can also be of type null; however, parameter $searchTerm of Waca\Pages\PageSearch::getCommentSearchResults() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
                    $this->getCommentSearchResults($requestSearch, /** @scrutinizer ignore-type */ $searchTerm);
Loading history...
92
                    break;
93
            }
94
95
            /** @var Request[] $results */
96
            $results = $requestSearch->getRecordCount($count)->fetch();
97
98
            $formParameters = [
99
                'term' => $searchTerm,
100
                'type' => $searchType,
101
            ];
102
103
            if ($excludeNonConfirmed) {
104
                $formParameters['excludeNonConfirmed'] = true;
105
            }
106
107
            $this->setupPageData($count, $formParameters);
108
109
            // deal with results
110
            $this->assign('requests', $this->prepareRequestData($results));
111
            $this->assign('resultCount', count($results));
112
            $this->assign('hasResultset', true);
113
114
            list($defaultSort, $defaultSortDirection) = WebRequest::requestListDefaultSort();
115
            $this->assign('defaultSort', $defaultSort);
116
            $this->assign('defaultSortDirection', $defaultSortDirection);
117
        }
118
        else {
119
            $this->assign('target', 'name');
120
            $this->assign('hasResultset', false);
121
            $this->assign('limit', 50);
122
            $this->assign('excludeNonConfirmed', true);
123
        }
124
    }
125
126
    /**
127
     * Gets search results by name
128
     *
129
     * @param RequestSearchHelper $searchHelper
130
     * @param string              $searchTerm
131
     */
132
    private function getNameSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
133
    {
134
        $padded = '%' . $searchTerm . '%';
135
        $searchHelper->byName($padded);
136
    }
137
138
    /**
139
     * Gets search results by comment
140
     *
141
     * @param RequestSearchHelper $searchHelper
142
     * @param string              $searchTerm
143
     */
144
    private function getCommentSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
145
    {
146
        $padded = '%' . $searchTerm . '%';
147
        $searchHelper->byComment($padded);
148
149
        $currentUser = User::getCurrent($this->getDatabase());
150
        $commentSecurity = ['requester', 'user'];
151
152
        if ($this->barrierTest('seeRestrictedComments', $currentUser, 'RequestData')) {
153
            $commentSecurity[] = 'admin';
154
        }
155
156
        if ($this->barrierTest('seeCheckuserComments', $currentUser, 'RequestData')) {
157
            $commentSecurity[] = 'checkuser';
158
        }
159
160
        $searchHelper->byCommentSecurity($commentSecurity);
161
    }
162
163
    /**
164
     * Gets search results by email
165
     *
166
     * @param RequestSearchHelper $searchHelper
167
     * @param string              $searchTerm
168
     *
169
     * @throws ApplicationLogicException
170
     */
171
    private function getEmailSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
172
    {
173
        if ($searchTerm === "@") {
174
            throw new ApplicationLogicException('The search term "@" is not valid for email address searches!');
175
        }
176
177
        $padded = '%' . $searchTerm . '%';
178
179
        $searchHelper->byEmailAddress($padded)->excludingPurgedData($this->getSiteConfiguration());
180
    }
181
182
    /**
183
     * Gets search results by IP address or XFF IP address
184
     *
185
     * @param RequestSearchHelper $searchHelper
186
     * @param string              $searchTerm
187
     */
188
    private function getIpSearchResults(RequestSearchHelper $searchHelper, string $searchTerm)
189
    {
190
        $searchHelper
191
            ->byIp($searchTerm)
192
            ->excludingPurgedData($this->getSiteConfiguration());
193
    }
194
195
    /**
196
     * @param string $searchType
197
     * @param string $searchTerm
198
     *
199
     * @param string $errorMessage
200
     *
201
     * @return bool true if parameters are valid
202
     */
203
    protected function validateSearchParameters($searchType, $searchTerm, &$errorMessage)
204
    {
205
        if (!in_array($searchType, array('name', 'email', 'ip', 'comment'))) {
206
            $errorMessage = 'Unknown search type';
207
208
            return false;
209
        }
210
211
        if ($searchTerm === '%' || $searchTerm === '' || $searchTerm === null) {
212
            $errorMessage = 'No search term specified entered';
213
214
            return false;
215
        }
216
217
        $errorMessage = "";
218
219
        return true;
220
    }
221
}
222