/
Stopwords.php
289 lines (253 loc) · 6.99 KB
/
Stopwords.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* The main Stopwords class.
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Anatoliy Belsky
* @author Matteo Scaramuccia <matteo@phpmyfaq.de>
* @copyright 2009-2022 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2009-04-01
*/
namespace phpMyFAQ;
/**
* Class Stopwords
*
* @package phpMyFAQ
*/
class Stopwords
{
/**
* @var Configuration
*/
private $config;
/**
* @var string
*/
private $language;
/**
* Table name.
*
* @var string
*/
private $tableName;
/**
* Constructor.
*
* @param Configuration $config
*/
public function __construct(Configuration $config)
{
$this->config = $config;
$this->tableName = Database::getTablePrefix() . 'faqstopwords';
}
/**
* @return string
*/
public function getLanguage(): string
{
return $this->language;
}
/**
* @return string
*/
public function getTableName(): string
{
return $this->tableName;
}
/**
* @param string $language
*/
public function setLanguage(string $language): void
{
$this->language = $language;
}
/**
* @param string $tableName
*/
public function setTableName(string $tableName): void
{
$this->tableName = $tableName;
}
/**
* Add a word to the stop words dictionary.
* If the given word already exists, false is returned.
*
* @param string $word
* @return bool
*/
public function add(string $word): bool
{
if (!$this->match($word)) {
$sql = sprintf(
"INSERT INTO %s VALUES(%d, '%s', '%s')",
$this->tableName,
$this->config->getDb()->nextId($this->tableName, 'id'),
$this->language,
$word
);
$this->config->getDb()->query($sql);
return true;
}
return false;
}
/**
* Update a word in the stop words dictionary.
*
* @param int $id
* @param string $word
* @return bool
*/
public function update(int $id, string $word): bool
{
$sql = "UPDATE %s SET stopword = '%s' WHERE id = %d AND lang = '%s'";
$sql = sprintf(
$sql,
$this->tableName,
$word,
$id,
$this->language
);
return (bool) $this->config->getDb()->query($sql);
}
/**
* Remove a word from the stop word dictionary.
*
* @param int $id
* @return bool
*/
public function remove(int $id): bool
{
$sql = sprintf(
"DELETE FROM %s WHERE id = %d AND lang = '%s'",
$this->tableName,
$id,
$this->language
);
return (bool) $this->config->getDb()->query($sql);
}
/**
* Match a word against the stop words dictionary.
*
* @param string $word
* @return bool
*/
public function match(string $word): bool
{
$sql = sprintf(
"SELECT id FROM %s WHERE LOWER(stopword) = LOWER('%s') AND lang = '%s'",
$this->tableName,
$word,
$this->language
);
$result = $this->config->getDb()->query($sql);
return $this->config->getDb()->numRows($result) > 0;
}
/**
* Retrieve all the stop words by a certain language.
*
* @param string $lang Language to retrieve stop words by
* @param bool $wordsOnly
*
* @return string[]
*/
public function getByLang($lang = null, $wordsOnly = false): array
{
$lang = is_null($lang) ? $this->config->getLanguage()->getLanguage() : $lang;
$sql = sprintf(
"SELECT id, lang, LOWER(stopword) AS stopword FROM %s WHERE lang = '%s'",
$this->tableName,
$lang
);
$result = $this->config->getDb()->query($sql);
$stopWords = [];
if ($wordsOnly) {
while (($row = $this->config->getDb()->fetchObject($result)) == true) {
$stopWords[] = Strings::htmlentities($row->stopword);
}
} else {
return $this->config->getDb()->fetchAll($result);
}
return $stopWords;
}
/**
* Filter some text cutting out all non words and stop words.
*
* @param string $input text to filter
* @return string[]
*/
public function clean(string $input): array
{
$words = explode(' ', $input);
$stop_words = $this->getByLang(null, true);
$retval = [];
foreach ($words as $word) {
$word = Strings::strtolower($word);
if (
!is_numeric($word) && 1 < Strings::strlen($word)
&& !in_array($word, $stop_words) && !in_array($word, $retval)
) {
$retval[] = $word;
}
}
return $retval;
}
/**
* This function checks the content against a bad word list if the banned
* word spam protection has been activated from the general phpMyFAQ
* configuration.
*
* @param string $content
* @return bool
*/
public function checkBannedWord(string $content): bool
{
// Sanity checks
$content = Strings::strtolower(trim($content));
if (('' === $content) || (!$this->config->get('spam.checkBannedWords'))) {
return true;
}
// Check if we check more than one word
$checkWords = explode(' ', $content);
if (1 === count($checkWords)) {
$checkWords = [$content];
}
$bannedWords = $this->getBannedWords();
// We just search a match of, at least, one banned word into $content
if (is_array($bannedWords)) {
foreach ($bannedWords as $bannedWord) {
foreach ($checkWords as $word) {
if (Strings::strtolower($word) === Strings::strtolower($bannedWord)) {
return false;
}
}
}
}
return true;
}
/**
* This function returns the banned words dictionary as an array.
*
* @return string[]
*/
private function getBannedWords(): array
{
$bannedTrimmedWords = [];
$bannedWordsFile = PMF_SRC_DIR . '/blockedwords.txt';
$bannedWords = [];
// Read the dictionary
if (file_exists($bannedWordsFile) && is_readable($bannedWordsFile)) {
$bannedWords = file_get_contents($bannedWordsFile);
}
// Trim it
foreach (explode("\n", $bannedWords) as $word) {
$bannedTrimmedWords[] = trim($word);
}
return $bannedTrimmedWords;
}
}