GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( bef184...2c2010 )
by Thorsten
13:41
created

PMF_Helper_Search   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 371
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10
Metric Value
wmc 37
lcom 1
cbo 10
dl 0
loc 371
rs 8.6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setLanguage() 0 4 1
A setPagination() 0 4 1
A setSearchterm() 0 4 1
A renderOpenSearchLink() 0 8 1
B renderInstantResponseResult() 0 37 4
B renderAdminSuggestionResult() 0 31 4
C renderSearchResult() 0 108 12
B renderRelatedFaqs() 0 35 5
A renderMostPopularSearches() 0 18 3
A renderScore() 0 16 4
1
<?php
2
3
/**
4
 * Helper class for phpMyFAQ search.
5
 *
6
 * PHP Version 5.5
7
 *
8
 * This Source Code Form is subject to the terms of the Mozilla Public License,
9
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
10
 * obtain one at http://mozilla.org/MPL/2.0/.
11
 *
12
 * @category  phpMyFAQ
13
 * @author    Thorsten Rinne <[email protected]>
14
 * @copyright 2009-2016 phpMyFAQ Team
15
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
16
 * @link      http://www.phpmyfaq.de
17
 * @since     2009-09-07
18
 */
19
if (!defined('IS_VALID_PHPMYFAQ')) {
20
    exit();
21
}
22
23
/**
24
 * Helper.
25
 *
26
 * @category  phpMyFAQ
27
 * @author    Thorsten Rinne <[email protected]>
28
 * @copyright 2009-2016 phpMyFAQ Team
29
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
30
 * @link      http://www.phpmyfaq.de
31
 * @since     2009-09-07
32
 */
33
class PMF_Helper_Search extends PMF_Helper
34
{
35
    /**
36
     * Language.
37
     *
38
     * @var PMF_Language
39
     */
40
    private $language = null;
41
42
    /**
43
     * PMF_Pagination object.
44
     *
45
     * @var PMF_Pagination
46
     */
47
    private $pagination = null;
48
49
    /**
50
     * Search term.
51
     *
52
     * @var string
53
     */
54
    private $searchterm = '';
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param PMF_Configuration $config
60
     *
61
     * @return PMF_Helper_Search
62
     */
63
    public function __construct(PMF_Configuration $config)
64
    {
65
        $this->_config = $config;
66
        $this->pmfLang = $this->getTranslations();
67
    }
68
69
    /**
70
     * Language setter.
71
     *
72
     * @param PMF_Language $language Language
73
     */
74
    public function setLanguage(PMF_Language $language)
75
    {
76
        $this->language = $language;
77
    }
78
79
    /**
80
     * PMF_Pagination setter.
81
     *
82
     * @param PMF_Pagination $pagination PMF_Pagination
83
     */
84
    public function setPagination(PMF_Pagination $pagination)
85
    {
86
        $this->pagination = $pagination;
87
    }
88
89
    /**
90
     * Searchterm setter.
91
     *
92
     * @param string $searchterm Searchterm
93
     */
94
    public function setSearchterm($searchterm)
95
    {
96
        $this->searchterm = $searchterm;
97
    }
98
99
    /**
100
     * Renders the OpenSearchLink.
101
     *
102
     * @return string
103
     */
104
    public function renderOpenSearchLink()
105
    {
106
        return sprintf(
107
            '<a class="searchplugin" href="#" onclick="window.external.AddSearchProvider(\'%s\'); return false;">%s</a>',
108
            $this->_config->getDefaultUrl().'opensearch.php',
109
            $this->translation['opensearch_plugin_install']
110
        );
111
    }
112
113
    /**
114
     * Renders the results for Typehead.
115
     *
116
     * @param PMF_Search_Resultset $resultSet PMF_Search_Resultset object
117
     *
118
     * @return string
119
     */
120
    public function renderInstantResponseResult(PMF_Search_Resultset $resultSet)
121
    {
122
        $results = [];
123
        $maxResults = $this->_config->get('records.numberOfRecordsPerPage');
124
        $numOfResults = $resultSet->getNumberOfResults();
125
126
        if (0 < $numOfResults) {
127
            $i = 0;
128
            foreach ($resultSet->getResultset() as $result) {
129
                if ($i > $maxResults) {
130
                    continue;
131
                }
132
133
                // Build the link to the faq record
134
                $currentUrl = sprintf('%s?%saction=artikel&cat=%d&id=%d&artlang=%s&highlight=%s',
135
                    PMF_Link::getSystemRelativeUri('ajaxresponse.php').'index.php',
136
                    $this->sessionId,
137
                    $result->category_id,
138
                    $result->id,
139
                    $result->lang,
140
                    urlencode($this->searchterm)
141
                );
142
143
                $link = new PMF_Link($currentUrl, $this->_config);
144
                $faq = new stdClass();
145
                $faq->categoryName = $this->Category->getPath($result->category_id);
146
                $faq->faqQuestion = PMF_Utils::chopString($result->question, 15);
147
                $faq->faqLink = $link->toUri();
148
149
                $results['results'][] = $faq;
150
            }
151
        } else {
152
            $results[] = $this->translation['err_noArticles'];
153
        }
154
155
        return json_encode($results);
156
    }
157
158
    /**
159
     * Renders the result page for Instant Response.
160
     *
161
     * @param PMF_Search_Resultset $resultSet PMF_Search_Resultset object
162
     *
163
     * @return string
164
     */
165
    public function renderAdminSuggestionResult(PMF_Search_Resultset $resultSet)
166
    {
167
        $html = '';
168
        $confPerPage = $this->_config->get('records.numberOfRecordsPerPage');
169
        $numOfResults = $resultSet->getNumberOfResults();
170
171
        if (0 < $numOfResults) {
172
            $i = 0;
173
            foreach ($resultSet->getResultset() as $result) {
174
                if ($i > $confPerPage) {
175
                    continue;
176
                }
177
178
                // Build the link to the faq record
179
                $currentUrl = sprintf('index.php?solution_id=%d', $result->solution_id);
180
181
                $html .= sprintf(
182
                    '<label for="%d"><input id="%d" type="radio" name="faqURL" value="%s"> %s</label><br>',
183
                    $result->id,
184
                    $result->id,
185
                    $currentUrl,
186
                    $result->question
187
                );
188
                ++$i;
189
            }
190
        } else {
191
            $html = $this->translation['err_noArticles'];
192
        }
193
194
        return $html;
195
    }
196
197
    /**
198
     * Renders the result page for the main search page.
199
     *
200
     * @param PMF_Search_Resultset $resultSet   PMF_Search_Resultset object
201
     * @param int                  $currentPage Current page number
202
     *
203
     * @return string
204
     */
205
    public function renderSearchResult(PMF_Search_Resultset $resultSet, $currentPage)
206
    {
207
        $html = '';
208
        $confPerPage = $this->_config->get('records.numberOfRecordsPerPage');
209
        $numOfResults = $resultSet->getNumberOfResults();
210
211
        $totalPages = ceil($numOfResults / $confPerPage);
212
        $lastPage = $currentPage * $confPerPage;
213
        $firstPage = $lastPage - $confPerPage;
214
        if ($lastPage > $numOfResults) {
215
            $lastPage = $numOfResults;
216
        }
217
218
        if (0 < $numOfResults) {
219
            $html .= sprintf(
220
                "<p>%s</p>\n",
221
                $this->plurals->GetMsg('plmsgSearchAmount', $numOfResults)
222
            );
223
224
            if (1 < $totalPages) {
225
                $html .= sprintf(
226
                    "<p><strong>%s%d %s %s</strong></p>\n",
227
                    $this->translation['msgPage'],
228
                    $currentPage,
229
                    $this->translation['msgVoteFrom'],
230
                    $this->plurals->GetMsg('plmsgPagesTotal', $totalPages)
231
                );
232
            }
233
234
            $html .= "<ul class=\"phpmyfaq-search-results list-unstyled\">\n";
235
236
            $counter = $displayedCounter = 0;
237
            $faqHelper = new PMF_Helper_Faq($this->_config);
238
            foreach ($resultSet->getResultset() as $result) {
239
                if ($displayedCounter >= $confPerPage) {
240
                    break;
241
                }
242
                ++$counter;
243
                if ($counter <= $firstPage) {
244
                    continue;
245
                }
246
                ++$displayedCounter;
247
248
                // Set language for current category to fetch the correct category name
249
                $this->Category->setLanguage($result->lang);
250
251
                $categoryInfo = $this->Category->getCategoriesFromArticle($result->id);
252
                $categoryInfo = array_values($categoryInfo); //Reset the array keys
253
                $question = PMF_Utils::chopString($result->question, 15);
254
                $answerPreview = $faqHelper->renderAnswerPreview($result->answer, 25);
255
256
                $searchterm = str_replace(
257
                    ['^', '.', '?', '*', '+', '{', '}', '(', ')', '[', ']', '"'],
258
                    '',
259
                    $this->searchterm
260
                );
261
                $searchterm = preg_quote($searchterm, '/');
262
                $searchItems = explode(' ', $searchterm);
263
264
                if ($this->_config->get('search.enableHighlighting') && PMF_String::strlen($searchItems[0]) > 1) {
265
                    foreach ($searchItems as $item) {
266
                        if (PMF_String::strlen($item) > 2) {
267
                            $question = PMF_Utils::setHighlightedString($question, $item);
0 ignored issues
show
Bug introduced by
It seems like $question defined by \PMF_Utils::setHighlightedString($question, $item) on line 267 can also be of type array; however, PMF_Utils::setHighlightedString() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
268
                            $answerPreview = PMF_Utils::setHighlightedString($answerPreview, $item);
0 ignored issues
show
Bug introduced by
It seems like $answerPreview defined by \PMF_Utils::setHighlight...($answerPreview, $item) on line 268 can also be of type array; however, PMF_Utils::setHighlightedString() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
269
                        }
270
                    }
271
                }
272
273
                // Build the link to the faq record
274
                $currentUrl = sprintf(
275
                    '%s?%saction=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s&amp;highlight=%s',
276
                    PMF_Link::getSystemRelativeUri(),
277
                    $this->sessionId,
278
                    $result->category_id,
279
                    $result->id,
280
                    $result->lang,
281
                    urlencode($searchterm)
282
                );
283
284
                $oLink = new PMF_Link($currentUrl, $this->_config);
285
                $oLink->text = $question;
286
                $oLink->itemTitle = $oLink->tooltip = $result->question;
287
288
                $html .= '<li>';
289
                $html .= $this->renderScore($result->score);
290
                $html .= sprintf('<strong>%s</strong>: %s<br />',
291
                    $categoryInfo[0]['name'],
292
                    $oLink->toHtmlAnchor()
293
                );
294
                $html .= sprintf(
295
                    "<small class=\"searchpreview\"><strong>%s</strong> %s...</small>\n",
296
                    $this->translation['msgSearchContent'],
297
                    $answerPreview
298
                );
299
                $html .= '</li>';
300
            }
301
302
            $html .= "</ul>\n";
303
304
            if (1 < $totalPages) {
305
                $html .= $this->pagination->render();
306
            }
307
        } else {
308
            $html = $this->translation['err_noArticles'];
309
        }
310
311
        return $html;
312
    }
313
314
    /**
315
     * @param PMF_Search_Resultset $resultSet
316
     * @param int                  $recordId
317
     *
318
     * @return string
319
     */
320
    public function renderRelatedFaqs(PMF_Search_Resultset $resultSet, $recordId)
321
    {
322
        $html = '';
323
        $numOfResults = $resultSet->getNumberOfResults();
324
325
        if ($numOfResults > 0) {
326
            $html   .= '<ul>';
327
            $counter = 0;
328
            foreach ($resultSet->getResultset() as $result) {
329
                if ($counter >= 5) {
330
                    continue;
331
                }
332
                if ($recordId == $result->id) {
333
                    continue;
334
                }
335
                ++$counter;
336
337
                $url = sprintf(
338
                    '%s?action=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
339
                    PMF_Link::getSystemRelativeUri(),
340
                    $result->category_id,
341
                    $result->id,
342
                    $result->lang
343
                );
344
                $oLink = new PMF_Link($url, $this->_config);
345
                $oLink->itemTitle = $result->question;
346
                $oLink->text = $result->question;
347
                $oLink->tooltip = $result->question;
348
                $html .= '<li>'.$oLink->toHtmlAnchor().'</li>';
349
            }
350
            $html .= '</ul>';
351
        }
352
353
        return $html;
354
    }
355
356
    /**
357
     * Renders the list of the most popular search terms.
358
     *
359
     * @param array $mostPopularSearches Array with popular search terms
360
     *
361
     * @return string
362
     */
363
    public function renderMostPopularSearches(Array $mostPopularSearches)
364
    {
365
        $html = '';
366
367
        foreach ($mostPopularSearches as $searchItem) {
368
            if (PMF_String::strlen($searchItem['searchterm']) > 0) {
369
370
                $html .= sprintf(
371
                    '<li><a class="pmf tag" href="?search=%s&submit=Search&action=search">%s <span class="badge">%dx</span> </a></li>',
372
                    urlencode($searchItem['searchterm']),
373
                    $searchItem['searchterm'],
374
                    $searchItem['number']
375
                );
376
            }
377
        }
378
379
        return $html;
380
    }
381
382
    /**
383
     * @param int $relevance
384
     *
385
     * @return string
386
     */
387
    private function renderScore($relevance = 0)
388
    {
389
        $html = sprintf('<span title="%s%%">', $relevance);
390
391
        if ($relevance == 0) {
392
            $html .= '<i class="fa fa-star-o"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>';
393
        } elseif ($relevance < 33) {
394
            $html .= '<i class="fa fa-star"></i><i class="fa fa-star-o"></i><i class="fa fa-star-o"></i>';
395
        } elseif ($relevance < 66) {
396
            $html .= '<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star-o"></i>';
397
        } else {
398
            $html .= '<i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i>';
399
        }
400
401
        return $html.'</span>';
402
    }
403
}
404