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 ( e1b57e...0e03d0 )
by Thorsten
15:43
created

PMF_Search::autocomplete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4286
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
/**
4
 * The phpMyFAQ Search class.
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
 * @author    Matteo Scaramuccia <[email protected]>
15
 * @author    Adrianna Musiol <[email protected]>
16
 * @copyright 2008-2015 phpMyFAQ Team
17
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
18
 * @link      http://www.phpmyfaq.de
19
 * @since     2008-01-26
20
 */
21
if (!defined('IS_VALID_PHPMYFAQ')) {
22
    exit();
23
}
24
25
/**
26
 * Search.
27
 *
28
 * @category  phpMyFAQ
29
 * @author    Thorsten Rinne <[email protected]>
30
 * @author    Matteo Scaramuccia <[email protected]>
31
 * @author    Adrianna Musiol <[email protected]>
32
 * @copyright 2008-2015 phpMyFAQ Team
33
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
34
 * @link      http://www.phpmyfaq.de
35
 * @since     2008-01-26
36
 */
37
class PMF_Search
38
{
39
    /**
40
     * @var PMF_Configuration
41
     */
42
    private $_config;
43
44
    /**
45
     * Category ID.
46
     *
47
     * @var int
48
     */
49
    private $categoryId = null;
50
51
    /**
52
     * Category object.
53
     *
54
     * @var PMF_Category
55
     */
56
    private $category = null;
57
58
    /**
59
     * Search table.
60
     *
61
     * @var string
62
     */
63
    private $_table = null;
64
65
    /**
66
     * Constructor.
67
     *
68
     * @param PMF_Configuration $config
69
     *
70
     * @return PMF_Search
71
     */
72
    public function __construct(PMF_Configuration $config)
73
    {
74
        $this->_config = $config;
75
        $this->_table = PMF_Db::getTablePrefix().'faqsearches';
76
    }
77
78
    /**
79
     * Setter for category.
80
     *
81
     * @param int $categoryId Category ID
82
     */
83
    public function setCategoryId($categoryId)
84
    {
85
        $this->categoryId = (int) $categoryId;
86
    }
87
88
    /**
89
     * Getter for category.
90
     *
91
     * @return int
92
     */
93
    public function getCategoryId()
94
    {
95
        return $this->categoryId;
96
    }
97
98
    /**
99
     * The search function to handle the different search engines.
100
     *
101
     * @param string $searchTerm   Text/Number (solution id)
102
     * @param bool   $allLanguages true to search over all languages
103
     *
104
     * @return array
105
     */
106
    public function search($searchTerm, $allLanguages = true)
107
    {
108
        if ($this->_config->get('search.enableElasticsearch')) {
109
            return $this->searchElasticsearch($searchTerm, $allLanguages);
110
        } else {
111
            return $this->searchDatabase($searchTerm, $allLanguages);
112
        }
113
    }
114
115
    /**
116
     * The autocomplete function to handle the different search engines.
117
     *
118
     * @param string $searchTerm Text to auto complete
119
     *
120
     * @return array
121
     */
122
    public function autocomplete($searchTerm)
123
    {
124
        if ($this->_config->get('search.enableElasticsearch')) {
125
126
            $esSearch = new PMF_Search_Elasticsearch($this->_config);
127
            $allCategories = $this->getCategory()->getAllCategoryIds();
128
129
            $esSearch->setCategoryIds($allCategories);
130
            $esSearch->setLanguage($this->_config->getLanguage()->getLanguage());
131
132
            return $esSearch->autocomplete($searchTerm);
133
134
        } else {
135
136
            return $this->searchDatabase($searchTerm, false);
137
138
        }
139
    }
140
141
    /**
142
     * The search function for the database powered full text search.
143
     *
144
     * @param string $searchTerm   Text/Number (solution id)
145
     * @param bool   $allLanguages true to search over all languages
146
     *
147
     * @return array
148
     */
149
    public function searchDatabase($searchTerm, $allLanguages = true)
150
    {
151
        $fdTable = PMF_Db::getTablePrefix() . 'faqdata AS fd';
152
        $fcrTable = PMF_Db::getTablePrefix() . 'faqcategoryrelations';
153
        $condition = ['fd.active' => "'yes'"];
154
        $search = PMF_Search_Factory::create($this->_config, ['database' => PMF_Db::getType()]);
155
156
        if (!is_null($this->getCategoryId()) && 0 < $this->getCategoryId()) {
157
            if ($this->getCategory() instanceof PMF_Category) {
158
                $children = $this->getCategory()->getChildNodes($this->getCategoryId());
159
                $selectedCategory = array(
160
                    $fcrTable.'.category_id' => array_merge((array) $this->getCategoryId(), $children),
161
                );
162
            } else {
163
                $selectedCategory = array(
164
                    $fcrTable.'.category_id' => $this->getCategoryId(),
165
                );
166
            }
167
            $condition = array_merge($selectedCategory, $condition);
168
        }
169
170
        if ((!$allLanguages) && (!is_numeric($searchTerm))) {
171
            $selectedLanguage = array('fd.lang' => "'" . $this->_config->getLanguage()->getLanguage() . "'");
172
            $condition        = array_merge($selectedLanguage, $condition);
173
        }
174
175
        $search->setTable($fdTable)
176
            ->setResultColumns(array(
177
                'fd.id AS id',
178
                'fd.lang AS lang',
179
                'fd.solution_id AS solution_id',
180
                $fcrTable . '.category_id AS category_id',
181
                'fd.thema AS question',
182
                'fd.content AS answer'))
183
            ->setJoinedTable($fcrTable)
184
            ->setJoinedColumns(array(
185
                'fd.id = ' . $fcrTable . '.record_id',
186
                'fd.lang = ' . $fcrTable . '.record_lang'
187
            ))
188
            ->setConditions($condition);
189
190
        if (is_numeric($searchTerm)) {
191
            $search->setMatchingColumns(array('fd.solution_id'));
192
        } else {
193
            $search->setMatchingColumns(array('fd.thema', 'fd.content', 'fd.keywords'));
194
        }
195
196
        $result = $search->search($searchTerm);
197
198
        if (!$this->_config->getDb()->numRows($result)) {
199
            return [];
200
        } else {
201
            return $this->_config->getDb()->fetchAll($result);
202
        }
203
    }
204
205
    /**
206
     * The search function for the Elasticsearch powered full text search.
207
     *
208
     * @param string $searchTerm   Text/Number (solution id)
209
     * @param bool   $allLanguages true to search over all languages
210
     *
211
     * @return array
212
     */
213
    public function searchElasticsearch($searchTerm, $allLanguages = true)
214
    {
215
        $esSearch = new PMF_Search_Elasticsearch($this->_config);
216
217
        if (!is_null($this->getCategoryId()) && 0 < $this->getCategoryId()) {
218
            if ($this->getCategory() instanceof PMF_Category) {
219
                $children = $this->getCategory()->getChildNodes($this->getCategoryId());
220
                $esSearch->setCategoryIds(array_merge([$this->getCategoryId()], $children));
221
            }
222
        } else {
223
            $allCategories = $this->getCategory()->getAllCategoryIds();
224
            $esSearch->setCategoryIds($allCategories);
225
        }
226
227
        if (!$allLanguages) {
228
            $esSearch->setLanguage($this->_config->getLanguage()->getLanguage());
229
        }
230
231
        $result = $esSearch->search($searchTerm);
232
233
        return $result;
234
    }
235
236
    /**
237
     * Logging of search terms for improvements.
238
     *
239
     * @param string $searchTerm Search term
240
     */
241
    public function logSearchTerm($searchTerm)
242
    {
243
        if (PMF_String::strlen($searchTerm) == 0) {
244
            return;
245
        }
246
247
        $date = new DateTime();
248
        $query = sprintf("
249
            INSERT INTO
250
                %s
251
            (id, lang, searchterm, searchdate)
252
                VALUES
253
            (%d, '%s', '%s', '%s')",
254
            $this->_table,
255
            $this->_config->getDb()->nextId($this->_table, 'id'),
256
            $this->_config->getLanguage()->getLanguage(),
257
            $this->_config->getDb()->escape($searchTerm),
258
            $date->format('Y-m-d H:i:s')
259
        );
260
261
        $this->_config->getDb()->query($query);
262
    }
263
264
    /**
265
     * Deletes a search term.
266
     *
267
     * @param string $searchTerm
268
     *
269
     * @return bool
270
     */
271
    public function deleteSearchTerm($searchTerm)
272
    {
273
        $query = sprintf("
274
            DELETE FROM
275
                %s
276
            WHERE
277
                searchterm = '%s'",
278
            $this->_table,
279
            $searchTerm
280
        );
281
282
        return $this->_config->getDb()->query($query);
283
    }
284
285
    /**
286
     * Deletes all search terms.
287
     *
288
     * @return bool
289
     */
290
    public function deleteAllSearchTerms()
291
    {
292
        $query = sprintf('DELETE FROM %s', $this->_table);
293
294
        return $this->_config->getDb()->query($query);
295
    }
296
297
    /**
298
     * Returns the most popular searches.
299
     *
300
     * @param int  $numResults Number of Results, default: 7
301
     * @param bool $withLang   Should the language be included in the result?
302
     *
303
     * @return array
304
     */
305
    public function getMostPopularSearches($numResults = 7, $withLang = false)
306
    {
307
        $searchResult = [];
308
309
        $byLang = $withLang ? ', lang' : '';
310
        $query = sprintf('
311
            SELECT 
312
                MIN(id) as id, searchterm, COUNT(searchterm) AS number %s
313
            FROM
314
                %s
315
            GROUP BY
316
                searchterm %s
317
            ORDER BY
318
                number
319
            DESC',
320
            $byLang,
321
            $this->_table,
322
            $byLang
323
        );
324
325
        $result = $this->_config->getDb()->query($query);
326
327
        if (false !== $result) {
328
            $i = 0;
329
            while ($row = $this->_config->getDb()->fetchObject($result)) {
330
                if ($i < $numResults) {
331
                    $searchResult[] = (array) $row;
332
                }
333
                ++$i;
334
            }
335
        }
336
337
        return $searchResult;
338
    }
339
340
    /**
341
     * Returns row count from the "faqsearches" table.
342
     *
343
     * @return int
344
     */
345
    public function getSearchesCount()
346
    {
347
        $sql = sprintf(
348
            'SELECT COUNT(1) AS count FROM %s',
349
            $this->_table
350
        );
351
352
        $result = $this->_config->getDb()->query($sql);
353
354
        return (int) $this->_config->getDb()->fetchObject($result)->count;
355
    }
356
357
    /**
358
     * Sets the Category object.
359
     *
360
     * @param PMF_Category $category
361
     */
362
    public function setCategory($category)
363
    {
364
        $this->category = $category;
365
    }
366
367
    /**
368
     * Returns the Category object.
369
     *
370
     * @return PMF_Category
371
     */
372
    public function getCategory()
373
    {
374
        return $this->category;
375
    }
376
}
377