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 ( 0e9895...938d91 )
by Thorsten
14:53
created

PMF_Search::setCategory()   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 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
    /**
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)
0 ignored issues
show
Unused Code introduced by
The parameter $allLanguages is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
    {
190
        $esSearch = new PMF_Search_Elasticsearch($this->_config);
191
192
        $result = $esSearch->search($searchTerm);
193
194
        return $result;
195
    }
196
197
    /**
198
     * Logging of search terms for improvements.
199
     *
200
     * @param string $searchTerm Search term
201
     */
202
    public function logSearchTerm($searchTerm)
203
    {
204
        if (PMF_String::strlen($searchTerm) == 0) {
205
            return;
206
        }
207
208
        $date = new DateTime();
209
        $query = sprintf("
210
            INSERT INTO
211
                %s
212
            (id, lang, searchterm, searchdate)
213
                VALUES
214
            (%d, '%s', '%s', '%s')",
215
            $this->_table,
216
            $this->_config->getDb()->nextId($this->_table, 'id'),
217
            $this->_config->getLanguage()->getLanguage(),
218
            $this->_config->getDb()->escape($searchTerm),
219
            $date->format('Y-m-d H:i:s')
220
        );
221
222
        $this->_config->getDb()->query($query);
223
    }
224
225
    /**
226
     * Deletes a search term.
227
     *
228
     * @param string $searchTerm
229
     *
230
     * @return bool
231
     */
232
    public function deleteSearchTerm($searchTerm)
233
    {
234
        $query = sprintf("
235
            DELETE FROM
236
                %s
237
            WHERE
238
                searchterm = '%s'",
239
            $this->_table,
240
            $searchTerm
241
        );
242
243
        return $this->_config->getDb()->query($query);
244
    }
245
246
    /**
247
     * Deletes all search terms.
248
     *
249
     * @return bool
250
     */
251
    public function deleteAllSearchTerms()
252
    {
253
        $query = sprintf('DELETE FROM %s', $this->_table);
254
255
        return $this->_config->getDb()->query($query);
256
    }
257
258
    /**
259
     * Returns the most popular searches.
260
     *
261
     * @param int  $numResults Number of Results, default: 7
262
     * @param bool $withLang   Should the language be included in the result?
263
     *
264
     * @return array
265
     */
266
    public function getMostPopularSearches($numResults = 7, $withLang = false)
267
    {
268
        $searchResult = [];
269
270
        $byLang = $withLang ? ', lang' : '';
271
        $query = sprintf('
272
            SELECT 
273
                MIN(id) as id, searchterm, COUNT(searchterm) AS number %s
274
            FROM
275
                %s
276
            GROUP BY
277
                searchterm %s
278
            ORDER BY
279
                number
280
            DESC',
281
            $byLang,
282
            $this->_table,
283
            $byLang
284
        );
285
286
        $result = $this->_config->getDb()->query($query);
287
288
        if (false !== $result) {
289
            $i = 0;
290
            while ($row = $this->_config->getDb()->fetchObject($result)) {
291
                if ($i < $numResults) {
292
                    $searchResult[] = (array) $row;
293
                }
294
                ++$i;
295
            }
296
        }
297
298
        return $searchResult;
299
    }
300
301
    /**
302
     * Returns row count from the "faqsearches" table.
303
     *
304
     * @return int
305
     */
306
    public function getSearchesCount()
307
    {
308
        $sql = sprintf(
309
            'SELECT COUNT(1) AS count FROM %s',
310
            $this->_table
311
        );
312
313
        $result = $this->_config->getDb()->query($sql);
314
315
        return (int) $this->_config->getDb()->fetchObject($result)->count;
316
    }
317
318
    /**
319
     * Sets the Category object.
320
     *
321
     * @param PMF_Category $category
322
     */
323
    public function setCategory($category)
324
    {
325
        $this->category = $category;
326
    }
327
328
    /**
329
     * Returns the Category object.
330
     *
331
     * @return PMF_Category
332
     */
333
    public function getCategory()
334
    {
335
        return $this->category;
336
    }
337
}
338