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 ( 57324c...7343d3 )
by Thorsten
13:59
created

PMF_Search::getCategoryId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    /**
100
     * The search function to handle the different search engines.
101
     *
102
     * @param string $searchTerm   Text/Number (solution id)
103
     * @param bool   $allLanguages true to search over all languages
104
     *
105
     * @return array
106
     */
107
    public function search($searchTerm, $allLanguages = true)
108
    {
109
        if ($this->_config->get('search.enableElasticsearch')) {
110
            return $this->searchElasticsearch($searchTerm, $allLanguages);
111
        } else {
112
            return $this->searchDatabase($searchTerm, $allLanguages);
113
        }
114
    }
115
116
    /**
117
     * The search function for the database powered full text search.
118
     *
119
     * @param string $searchTerm   Text/Number (solution id)
120
     * @param bool   $allLanguages true to search over all languages
121
     *
122
     * @return array
123
     */
124
    public function searchDatabase($searchTerm, $allLanguages = true)
125
    {
126
        $fdTable = PMF_Db::getTablePrefix() . 'faqdata AS fd';
127
        $fcrTable = PMF_Db::getTablePrefix() . 'faqcategoryrelations';
128
        $condition = ['fd.active' => "'yes'"];
129
        $search = PMF_Search_Factory::create($this->_config, ['database' => PMF_Db::getType()]);
130
131
        if (!is_null($this->getCategoryId()) && 0 < $this->getCategoryId()) {
132
            if ($this->getCategory() instanceof PMF_Category) {
133
                $children = $this->getCategory()->getChildNodes($this->getCategoryId());
134
                $selectedCategory = array(
135
                    $fcrTable.'.category_id' => array_merge((array) $this->getCategoryId(), $children),
136
                );
137
            } else {
138
                $selectedCategory = array(
139
                    $fcrTable.'.category_id' => $this->getCategoryId(),
140
                );
141
            }
142
            $condition = array_merge($selectedCategory, $condition);
143
        }
144
145
        if ((!$allLanguages) && (!is_numeric($searchTerm))) {
146
            $selectedLanguage = array('fd.lang' => "'" . $this->_config->getLanguage()->getLanguage() . "'");
147
            $condition        = array_merge($selectedLanguage, $condition);
148
        }
149
150
        $search->setTable($fdTable)
151
            ->setResultColumns(array(
152
                'fd.id AS id',
153
                'fd.lang AS lang',
154
                'fd.solution_id AS solution_id',
155
                $fcrTable . '.category_id AS category_id',
156
                'fd.thema AS question',
157
                'fd.content AS answer'))
158
            ->setJoinedTable($fcrTable)
159
            ->setJoinedColumns(array(
160
                'fd.id = ' . $fcrTable . '.record_id',
161
                'fd.lang = ' . $fcrTable . '.record_lang'
162
            ))
163
            ->setConditions($condition);
164
165
        if (is_numeric($searchTerm)) {
166
            $search->setMatchingColumns(array('fd.solution_id'));
167
        } else {
168
            $search->setMatchingColumns(array('fd.thema', 'fd.content', 'fd.keywords'));
169
        }
170
171
        $result = $search->search($searchTerm);
172
173
        if (!$this->_config->getDb()->numRows($result)) {
174
            return [];
175
        } else {
176
            return $this->_config->getDb()->fetchAll($result);
177
        }
178
    }
179
180
    /**
181
     * The search function for the Elasticsearch powered full text search.
182
     *
183
     * @param string $searchTerm   Text/Number (solution id)
184
     * @param bool   $allLanguages true to search over all languages
185
     *
186
     * @return array
187
     */
188
    public function searchElasticsearch($searchTerm, $allLanguages = true)
189
    {
190
        $esSearch = new PMF_Search_Elasticsearch($this->_config);
191
192
        if (!is_null($this->getCategoryId()) && 0 < $this->getCategoryId()) {
193
            if ($this->getCategory() instanceof PMF_Category) {
194
                $children = $this->getCategory()->getChildNodes($this->getCategoryId());
195
                $esSearch->setCategoryIds(array_merge([$this->getCategoryId()], $children));
196
            }
197
        } else {
198
            $allCategories = $this->getCategory()->getAllCategoryIds();
199
            $esSearch->setCategoryIds($allCategories);
200
        }
201
202
        if (!$allLanguages) {
203
            $esSearch->setLanguage($this->_config->getLanguage()->getLanguage());
204
        }
205
206
        $result = $esSearch->search($searchTerm);
207
208
        return $result;
209
    }
210
211
    /**
212
     * Logging of search terms for improvements.
213
     *
214
     * @param string $searchTerm Search term
215
     */
216
    public function logSearchTerm($searchTerm)
217
    {
218
        if (PMF_String::strlen($searchTerm) == 0) {
219
            return;
220
        }
221
222
        $date = new DateTime();
223
        $query = sprintf("
224
            INSERT INTO
225
                %s
226
            (id, lang, searchterm, searchdate)
227
                VALUES
228
            (%d, '%s', '%s', '%s')",
229
            $this->_table,
230
            $this->_config->getDb()->nextId($this->_table, 'id'),
231
            $this->_config->getLanguage()->getLanguage(),
232
            $this->_config->getDb()->escape($searchTerm),
233
            $date->format('Y-m-d H:i:s')
234
        );
235
236
        $this->_config->getDb()->query($query);
237
    }
238
239
    /**
240
     * Deletes a search term.
241
     *
242
     * @param string $searchTerm
243
     *
244
     * @return bool
245
     */
246
    public function deleteSearchTerm($searchTerm)
247
    {
248
        $query = sprintf("
249
            DELETE FROM
250
                %s
251
            WHERE
252
                searchterm = '%s'",
253
            $this->_table,
254
            $searchTerm
255
        );
256
257
        return $this->_config->getDb()->query($query);
258
    }
259
260
    /**
261
     * Deletes all search terms.
262
     *
263
     * @return bool
264
     */
265
    public function deleteAllSearchTerms()
266
    {
267
        $query = sprintf('DELETE FROM %s', $this->_table);
268
269
        return $this->_config->getDb()->query($query);
270
    }
271
272
    /**
273
     * Returns the most popular searches.
274
     *
275
     * @param int  $numResults Number of Results, default: 7
276
     * @param bool $withLang   Should the language be included in the result?
277
     *
278
     * @return array
279
     */
280
    public function getMostPopularSearches($numResults = 7, $withLang = false)
281
    {
282
        $searchResult = [];
283
284
        $byLang = $withLang ? ', lang' : '';
285
        $query = sprintf('
286
            SELECT 
287
                MIN(id) as id, searchterm, COUNT(searchterm) AS number %s
288
            FROM
289
                %s
290
            GROUP BY
291
                searchterm %s
292
            ORDER BY
293
                number
294
            DESC',
295
            $byLang,
296
            $this->_table,
297
            $byLang
298
        );
299
300
        $result = $this->_config->getDb()->query($query);
301
302
        if (false !== $result) {
303
            $i = 0;
304
            while ($row = $this->_config->getDb()->fetchObject($result)) {
305
                if ($i < $numResults) {
306
                    $searchResult[] = (array) $row;
307
                }
308
                ++$i;
309
            }
310
        }
311
312
        return $searchResult;
313
    }
314
315
    /**
316
     * Returns row count from the "faqsearches" table.
317
     *
318
     * @return int
319
     */
320
    public function getSearchesCount()
321
    {
322
        $sql = sprintf(
323
            'SELECT COUNT(1) AS count FROM %s',
324
            $this->_table
325
        );
326
327
        $result = $this->_config->getDb()->query($sql);
328
329
        return (int) $this->_config->getDb()->fetchObject($result)->count;
330
    }
331
332
    /**
333
     * Sets the Category object.
334
     *
335
     * @param PMF_Category $category
336
     */
337
    public function setCategory($category)
338
    {
339
        $this->category = $category;
340
    }
341
342
    /**
343
     * Returns the Category object.
344
     *
345
     * @return PMF_Category
346
     */
347
    public function getCategory()
348
    {
349
        return $this->category;
350
    }
351
}
352