| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class KeywordsManager { |
| 11 |
public const SERVICE_NAME = 'AbuseFilterKeywordsManager'; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private const BUILDER_VALUES = [ |
| 24 |
'op-arithmetic' => [ |
| 25 |
|
| 26 |
'+' => 'addition', |
| 27 |
|
| 28 |
'-' => 'subtraction', |
| 29 |
|
| 30 |
'*' => 'multiplication', |
| 31 |
|
| 32 |
'/' => 'divide', |
| 33 |
|
| 34 |
'%' => 'modulo', |
| 35 |
|
| 36 |
'**' => 'pow' |
| 37 |
], |
| 38 |
'op-comparison' => [ |
| 39 |
|
| 40 |
'==' => 'equal', |
| 41 |
|
| 42 |
'===' => 'equal-strict', |
| 43 |
|
| 44 |
'!=' => 'notequal', |
| 45 |
|
| 46 |
'!==' => 'notequal-strict', |
| 47 |
|
| 48 |
'<' => 'lt', |
| 49 |
|
| 50 |
'>' => 'gt', |
| 51 |
|
| 52 |
'<=' => 'lte', |
| 53 |
|
| 54 |
'>=' => 'gte' |
| 55 |
], |
| 56 |
'op-bool' => [ |
| 57 |
|
| 58 |
'!' => 'not', |
| 59 |
|
| 60 |
'&' => 'and', |
| 61 |
|
| 62 |
'|' => 'or', |
| 63 |
|
| 64 |
'^' => 'xor' |
| 65 |
], |
| 66 |
'misc' => [ |
| 67 |
|
| 68 |
'in' => 'in', |
| 69 |
|
| 70 |
'contains' => 'contains', |
| 71 |
|
| 72 |
'like' => 'like', |
| 73 |
|
| 74 |
'""' => 'stringlit', |
| 75 |
|
| 76 |
'rlike' => 'rlike', |
| 77 |
|
| 78 |
'irlike' => 'irlike', |
| 79 |
|
| 80 |
'cond ? iftrue : iffalse' => 'tern', |
| 81 |
|
| 82 |
'if cond then iftrue else iffalse end' => 'cond', |
| 83 |
|
| 84 |
'if cond then iftrue end' => 'cond-short', |
| 85 |
], |
| 86 |
'funcs' => [ |
| 87 |
|
| 88 |
'length(string)' => 'length', |
| 89 |
|
| 90 |
'lcase(string)' => 'lcase', |
| 91 |
|
| 92 |
'ucase(string)' => 'ucase', |
| 93 |
|
| 94 |
'ccnorm(string)' => 'ccnorm', |
| 95 |
|
| 96 |
'ccnorm_contains_any(haystack,needle1,needle2,..)' => 'ccnorm-contains-any', |
| 97 |
|
| 98 |
'ccnorm_contains_all(haystack,needle1,needle2,..)' => 'ccnorm-contains-all', |
| 99 |
|
| 100 |
'rmdoubles(string)' => 'rmdoubles', |
| 101 |
|
| 102 |
'specialratio(string)' => 'specialratio', |
| 103 |
|
| 104 |
'norm(string)' => 'norm', |
| 105 |
|
| 106 |
'count(needle,haystack)' => 'count', |
| 107 |
|
| 108 |
'rcount(needle,haystack)' => 'rcount', |
| 109 |
|
| 110 |
'get_matches(needle,haystack)' => 'get_matches', |
| 111 |
|
| 112 |
'rmwhitespace(text)' => 'rmwhitespace', |
| 113 |
|
| 114 |
'rmspecials(text)' => 'rmspecials', |
| 115 |
|
| 116 |
'ip_in_range(ip, range)' => 'ip_in_range', |
| 117 |
|
| 118 |
'ip_in_ranges(ip, range1, range2, ...)' => 'ip_in_ranges', |
| 119 |
|
| 120 |
'contains_any(haystack,needle1,needle2,...)' => 'contains-any', |
| 121 |
|
| 122 |
'contains_all(haystack,needle1,needle2,...)' => 'contains-all', |
| 123 |
|
| 124 |
'equals_to_any(haystack,needle1,needle2,...)' => 'equals-to-any', |
| 125 |
|
| 126 |
'substr(subject, offset, length)' => 'substr', |
| 127 |
|
| 128 |
'strpos(haystack, needle)' => 'strpos', |
| 129 |
|
| 130 |
'str_replace(subject, search, replace)' => 'str_replace', |
| 131 |
|
| 132 |
'str_replace_regexp(subject, search, replace)' => 'str_replace_regexp', |
| 133 |
|
| 134 |
'rescape(string)' => 'rescape', |
| 135 |
|
| 136 |
'set_var(var,value)' => 'set_var', |
| 137 |
|
| 138 |
'sanitize(string)' => 'sanitize', |
| 139 |
], |
| 140 |
'vars' => [ |
| 141 |
|
| 142 |
'timestamp' => 'timestamp', |
| 143 |
|
| 144 |
'accountname' => 'accountname', |
| 145 |
|
| 146 |
'action' => 'action', |
| 147 |
|
| 148 |
'added_lines' => 'addedlines', |
| 149 |
|
| 150 |
'edit_delta' => 'delta', |
| 151 |
|
| 152 |
'edit_diff' => 'diff', |
| 153 |
|
| 154 |
'new_size' => 'newsize', |
| 155 |
|
| 156 |
'old_size' => 'oldsize', |
| 157 |
|
| 158 |
'new_content_model' => 'new-content-model', |
| 159 |
|
| 160 |
'old_content_model' => 'old-content-model', |
| 161 |
|
| 162 |
'removed_lines' => 'removedlines', |
| 163 |
|
| 164 |
'summary' => 'summary', |
| 165 |
|
| 166 |
'page_id' => 'page-id', |
| 167 |
|
| 168 |
'page_namespace' => 'page-ns', |
| 169 |
|
| 170 |
'page_title' => 'page-title', |
| 171 |
|
| 172 |
'page_prefixedtitle' => 'page-prefixedtitle', |
| 173 |
|
| 174 |
'page_age' => 'page-age', |
| 175 |
|
| 176 |
'page_last_edit_age' => 'page-last-edit-age', |
| 177 |
|
| 178 |
'moved_from_id' => 'movedfrom-id', |
| 179 |
|
| 180 |
'moved_from_namespace' => 'movedfrom-ns', |
| 181 |
|
| 182 |
'moved_from_title' => 'movedfrom-title', |
| 183 |
|
| 184 |
'moved_from_prefixedtitle' => 'movedfrom-prefixedtitle', |
| 185 |
|
| 186 |
'moved_from_age' => 'movedfrom-age', |
| 187 |
|
| 188 |
'moved_from_last_edit_age' => 'movedfrom-last-edit-age', |
| 189 |
|
| 190 |
'moved_to_id' => 'movedto-id', |
| 191 |
|
| 192 |
'moved_to_namespace' => 'movedto-ns', |
| 193 |
|
| 194 |
'moved_to_title' => 'movedto-title', |
| 195 |
|
| 196 |
'moved_to_prefixedtitle' => 'movedto-prefixedtitle', |
| 197 |
|
| 198 |
'moved_to_age' => 'movedto-age', |
| 199 |
|
| 200 |
'moved_to_last_edit_age' => 'movedto-last-edit-age', |
| 201 |
|
| 202 |
'user_editcount' => 'user-editcount', |
| 203 |
|
| 204 |
'user_age' => 'user-age', |
| 205 |
|
| 206 |
'user_unnamed_ip' => 'user-unnamed-ip', |
| 207 |
|
| 208 |
'user_name' => 'user-name', |
| 209 |
|
| 210 |
'user_type' => 'user-type', |
| 211 |
|
| 212 |
'user_groups' => 'user-groups', |
| 213 |
|
| 214 |
'user_rights' => 'user-rights', |
| 215 |
|
| 216 |
'user_blocked' => 'user-blocked', |
| 217 |
|
| 218 |
'user_emailconfirm' => 'user-emailconfirm', |
| 219 |
|
| 220 |
'old_wikitext' => 'old-wikitext', |
| 221 |
|
| 222 |
'new_wikitext' => 'new-wikitext', |
| 223 |
|
| 224 |
'added_links' => 'added-links', |
| 225 |
|
| 226 |
'removed_links' => 'removed-links', |
| 227 |
|
| 228 |
'all_links' => 'all-links', |
| 229 |
|
| 230 |
'new_pst' => 'new-pst', |
| 231 |
|
| 232 |
'edit_diff_pst' => 'diff-pst', |
| 233 |
|
| 234 |
'added_lines_pst' => 'addedlines-pst', |
| 235 |
|
| 236 |
'new_text' => 'new-text', |
| 237 |
|
| 238 |
'new_html' => 'new-html', |
| 239 |
|
| 240 |
'page_restrictions_edit' => 'restrictions-edit', |
| 241 |
|
| 242 |
'page_restrictions_move' => 'restrictions-move', |
| 243 |
|
| 244 |
'page_restrictions_create' => 'restrictions-create', |
| 245 |
|
| 246 |
'page_restrictions_upload' => 'restrictions-upload', |
| 247 |
|
| 248 |
'page_recent_contributors' => 'recent-contributors', |
| 249 |
|
| 250 |
'page_first_contributor' => 'first-contributor', |
| 251 |
|
| 252 |
'moved_from_restrictions_edit' => 'movedfrom-restrictions-edit', |
| 253 |
|
| 254 |
'moved_from_restrictions_move' => 'movedfrom-restrictions-move', |
| 255 |
|
| 256 |
'moved_from_restrictions_create' => 'movedfrom-restrictions-create', |
| 257 |
|
| 258 |
'moved_from_restrictions_upload' => 'movedfrom-restrictions-upload', |
| 259 |
|
| 260 |
'moved_from_recent_contributors' => 'movedfrom-recent-contributors', |
| 261 |
|
| 262 |
'moved_from_first_contributor' => 'movedfrom-first-contributor', |
| 263 |
|
| 264 |
'moved_to_restrictions_edit' => 'movedto-restrictions-edit', |
| 265 |
|
| 266 |
'moved_to_restrictions_move' => 'movedto-restrictions-move', |
| 267 |
|
| 268 |
'moved_to_restrictions_create' => 'movedto-restrictions-create', |
| 269 |
|
| 270 |
'moved_to_restrictions_upload' => 'movedto-restrictions-upload', |
| 271 |
|
| 272 |
'moved_to_recent_contributors' => 'movedto-recent-contributors', |
| 273 |
|
| 274 |
'moved_to_first_contributor' => 'movedto-first-contributor', |
| 275 |
|
| 276 |
'old_links' => 'old-links', |
| 277 |
|
| 278 |
'file_sha1' => 'file-sha1', |
| 279 |
|
| 280 |
'file_size' => 'file-size', |
| 281 |
|
| 282 |
'file_mime' => 'file-mime', |
| 283 |
|
| 284 |
'file_mediatype' => 'file-mediatype', |
| 285 |
|
| 286 |
'file_width' => 'file-width', |
| 287 |
|
| 288 |
'file_height' => 'file-height', |
| 289 |
|
| 290 |
'file_bits_per_channel' => 'file-bits-per-channel', |
| 291 |
|
| 292 |
'wiki_name' => 'wiki-name', |
| 293 |
|
| 294 |
'wiki_language' => 'wiki-language', |
| 295 |
], |
| 296 |
]; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
private const DISABLED_VARS = [ |
| 309 |
|
| 310 |
'old_text' => 'old-text', |
| 311 |
|
| 312 |
'old_html' => 'old-html', |
| 313 |
|
| 314 |
'minor_edit' => 'minor-edit' |
| 315 |
]; |
| 316 |
|
| 317 |
private const DEPRECATED_VARS = [ |
| 318 |
'article_text' => 'page_title', |
| 319 |
'article_prefixedtext' => 'page_prefixedtitle', |
| 320 |
'article_namespace' => 'page_namespace', |
| 321 |
'article_articleid' => 'page_id', |
| 322 |
'article_restrictions_edit' => 'page_restrictions_edit', |
| 323 |
'article_restrictions_move' => 'page_restrictions_move', |
| 324 |
'article_restrictions_create' => 'page_restrictions_create', |
| 325 |
'article_restrictions_upload' => 'page_restrictions_upload', |
| 326 |
'article_recent_contributors' => 'page_recent_contributors', |
| 327 |
'article_first_contributor' => 'page_first_contributor', |
| 328 |
'moved_from_text' => 'moved_from_title', |
| 329 |
'moved_from_prefixedtext' => 'moved_from_prefixedtitle', |
| 330 |
'moved_from_articleid' => 'moved_from_id', |
| 331 |
'moved_to_text' => 'moved_to_title', |
| 332 |
'moved_to_prefixedtext' => 'moved_to_prefixedtitle', |
| 333 |
'moved_to_articleid' => 'moved_to_id', |
| 334 |
]; |
| 335 |
|
| 336 |
|
| 337 |
private $builderValues; |
| 338 |
|
| 339 |
|
| 340 |
private $deprecatedVars; |
| 341 |
|
| 342 |
|
| 343 |
private $hookRunner; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public function __construct( AbuseFilterHookRunner $hookRunner ) { |
| 349 |
$this->hookRunner = $hookRunner; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function getDisabledVariables(): array { |
| 356 |
return self::DISABLED_VARS; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public function getDeprecatedVariables(): array { |
| 363 |
if ( $this->deprecatedVars === null ) { |
| 364 |
$this->deprecatedVars = self::DEPRECATED_VARS; |
| 365 |
$this->hookRunner->onAbuseFilter_deprecatedVariables( $this->deprecatedVars ); |
| 366 |
} |
| 367 |
return $this->deprecatedVars; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public function getBuilderValues(): array { |
| 374 |
if ( $this->builderValues === null ) { |
| 375 |
$this->builderValues = self::BUILDER_VALUES; |
| 376 |
$this->hookRunner->onAbuseFilter_builder( $this->builderValues ); |
| 377 |
} |
| 378 |
return $this->builderValues; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function isVarDisabled( string $name ): bool { |
| 386 |
return array_key_exists( $name, self::DISABLED_VARS ); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function isVarDeprecated( string $name ): bool { |
| 394 |
return array_key_exists( $name, $this->getDeprecatedVariables() ); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function isVarInUse( string $name ): bool { |
| 402 |
return array_key_exists( $name, $this->getVarsMappings() ); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public function varExists( string $name ): bool { |
| 411 |
return $this->isVarInUse( $name ) || |
| 412 |
$this->isVarDisabled( $name ) || |
| 413 |
$this->isVarDeprecated( $name ); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public function getMessageKeyForVar( string $var ): ?string { |
| 424 |
if ( !$this->varExists( $var ) ) { |
| 425 |
return null; |
| 426 |
} |
| 427 |
if ( $this->isVarDeprecated( $var ) ) { |
| 428 |
$var = $this->getDeprecatedVariables()[$var]; |
| 429 |
} |
| 430 |
|
| 431 |
$key = self::DISABLED_VARS[$var] ?? |
| 432 |
$this->getVarsMappings()[$var]; |
| 433 |
return "abusefilter-edit-builder-vars-$key"; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public function getVarsMappings(): array { |
| 440 |
return $this->getBuilderValues()['vars']; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
public function getCoreVariables(): array { |
| 449 |
return array_keys( self::BUILDER_VALUES['vars'] ); |
| 450 |
} |
| 451 |
} |