| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\SpamBlacklist; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Content\TextContent; |
| 7 |
use MediaWiki\MediaWikiServices; |
| 8 |
use MediaWiki\Revision\SlotRecord; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\User\User; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
abstract class BaseBlacklist { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public $files = []; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
protected $regexes = false; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public $warningChance = 100; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public $warningTime = 600; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public $expiryTime = 900; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private static $blacklistTypes = [ |
| 53 |
'spam' => SpamBlacklist::class, |
| 54 |
'email' => EmailBlacklist::class, |
| 55 |
]; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private static $instances = []; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct( $settings = [] ) { |
| 68 |
foreach ( $settings as $name => $value ) { |
| 69 |
$this->$name = $value; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
abstract public function filter( |
| 81 |
array $links, |
| 82 |
?Title $title, |
| 83 |
User $user, |
| 84 |
$preventLog = false |
| 85 |
); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static function addBlacklistType( $type, $class ) { |
| 94 |
self::$blacklistTypes[$type] = $class; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static function getBlacklistTypes() { |
| 103 |
return self::$blacklistTypes; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static function getSpamBlacklist() { |
| 110 |
|
| 111 |
return self::getInstance( 'spam' ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static function getEmailBlacklist() { |
| 118 |
|
| 119 |
return self::getInstance( 'email' ); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static function getInstance( $type ) { |
| 130 |
if ( !isset( self::$blacklistTypes[$type] ) ) { |
| 131 |
throw new InvalidArgumentException( "Invalid blacklist type '$type' passed to " . __METHOD__ ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( !isset( self::$instances[$type] ) ) { |
| 135 |
global $wgBlacklistSettings; |
| 136 |
|
| 137 |
|
| 138 |
if ( !isset( $wgBlacklistSettings[$type] ) ) { |
| 139 |
$wgBlacklistSettings[$type] = []; |
| 140 |
} |
| 141 |
|
| 142 |
$class = self::$blacklistTypes[$type]; |
| 143 |
self::$instances[$type] = new $class( $wgBlacklistSettings[$type] ); |
| 144 |
} |
| 145 |
|
| 146 |
return self::$instances[$type]; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static function clearInstanceCache() { |
| 153 |
self::$instances = []; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
abstract protected function getBlacklistType(); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public static function isLocalSource( Title $title ) { |
| 170 |
global $wgDBname, $wgBlacklistSettings; |
| 171 |
|
| 172 |
if ( $title->inNamespace( NS_MEDIAWIKI ) ) { |
| 173 |
$sources = []; |
| 174 |
foreach ( self::$blacklistTypes as $type => $class ) { |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
$type = ucfirst( $type ); |
| 179 |
$sources[] = "$type-blacklist"; |
| 180 |
$sources[] = "$type-whitelist"; |
| 181 |
} |
| 182 |
|
| 183 |
if ( in_array( $title->getDBkey(), $sources ) ) { |
| 184 |
return true; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
$thisHttp = wfExpandUrl( $title->getFullUrl( 'action=raw' ), PROTO_HTTP ); |
| 189 |
$thisHttpRegex = '/^' . preg_quote( $thisHttp, '/' ) . '(?:&.*)?$/'; |
| 190 |
|
| 191 |
$files = []; |
| 192 |
foreach ( self::$blacklistTypes as $type => $class ) { |
| 193 |
if ( isset( $wgBlacklistSettings[$type]['files'] ) ) { |
| 194 |
$files += $wgBlacklistSettings[$type]['files']; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
foreach ( $files as $fileName ) { |
| 199 |
$matches = []; |
| 200 |
if ( preg_match( '/^DB: (\w*) (.*)$/', $fileName, $matches ) ) { |
| 201 |
if ( $wgDBname === $matches[1] && $matches[2] === $title->getPrefixedDbKey() ) { |
| 202 |
|
| 203 |
return true; |
| 204 |
} |
| 205 |
} elseif ( preg_match( $thisHttpRegex, $fileName ) ) { |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public static function getTypeFromTitle( Title $title ) { |
| 222 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 223 |
|
| 224 |
$types = array_map( [ $contLang, 'ucfirst' ], array_keys( self::$blacklistTypes ) ); |
| 225 |
$regex = '/(' . implode( '|', $types ) . ')-(?:blacklist|whitelist)/'; |
| 226 |
|
| 227 |
if ( preg_match( $regex, $title->getDBkey(), $m ) ) { |
| 228 |
return strtolower( $m[1] ); |
| 229 |
} |
| 230 |
|
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function getBlacklists() { |
| 240 |
if ( $this->regexes === false ) { |
| 241 |
$this->regexes = array_merge( |
| 242 |
$this->getLocalBlacklists(), |
| 243 |
$this->getSharedBlacklists() |
| 244 |
); |
| 245 |
} |
| 246 |
return $this->regexes; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function getLocalBlacklists() { |
| 255 |
$type = $this->getBlacklistType(); |
| 256 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 257 |
|
| 258 |
return $cache->getWithSetCallback( |
| 259 |
$cache->makeKey( 'spamblacklist', $type, 'blacklist-regex' ), |
| 260 |
$this->expiryTime, |
| 261 |
function () use ( $type ) { |
| 262 |
return SpamRegexBatch::regexesFromMessage( "{$type}-blacklist", $this ); |
| 263 |
} |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public function getWhitelists() { |
| 273 |
$type = $this->getBlacklistType(); |
| 274 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 275 |
|
| 276 |
return $cache->getWithSetCallback( |
| 277 |
$cache->makeKey( 'spamblacklist', $type, 'whitelist-regex' ), |
| 278 |
$this->expiryTime, |
| 279 |
function () use ( $type ) { |
| 280 |
return SpamRegexBatch::regexesFromMessage( "{$type}-whitelist", $this ); |
| 281 |
} |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
private function getSharedBlacklists() { |
| 290 |
$listType = $this->getBlacklistType(); |
| 291 |
|
| 292 |
wfDebugLog( 'SpamBlacklist', "Loading $listType regex..." ); |
| 293 |
|
| 294 |
if ( !$this->files ) { |
| 295 |
|
| 296 |
wfDebugLog( 'SpamBlacklist', "no files specified\n" ); |
| 297 |
return []; |
| 298 |
} |
| 299 |
|
| 300 |
if ( defined( 'MW_PHPUNIT_TEST' ) ) { |
| 301 |
wfDebugLog( 'SpamBlacklist', 'remote loading disabled during PHPUnit test' ); |
| 302 |
return []; |
| 303 |
} |
| 304 |
|
| 305 |
$miss = false; |
| 306 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 307 |
$regexes = $cache->getWithSetCallback( |
| 308 |
|
| 309 |
|
| 310 |
$cache->makeKey( 'spamblacklist', $listType, 'shared-blacklist-regex' ), |
| 311 |
$this->expiryTime, |
| 312 |
function () use ( &$miss ) { |
| 313 |
$miss = true; |
| 314 |
return $this->buildSharedBlacklists(); |
| 315 |
} |
| 316 |
); |
| 317 |
|
| 318 |
if ( !$miss ) { |
| 319 |
wfDebugLog( 'SpamBlacklist', "Got shared spam regexes from cache\n" ); |
| 320 |
} |
| 321 |
|
| 322 |
return $regexes; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function clearCache() { |
| 329 |
$listType = $this->getBlacklistType(); |
| 330 |
|
| 331 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 332 |
$cache->delete( $cache->makeKey( 'spamblacklist', $listType, 'shared-blacklist-regex' ) ); |
| 333 |
$cache->delete( $cache->makeKey( 'spamblacklist', $listType, 'blacklist-regex' ) ); |
| 334 |
$cache->delete( $cache->makeKey( 'spamblacklist', $listType, 'whitelist-regex' ) ); |
| 335 |
|
| 336 |
wfDebugLog( 'SpamBlacklist', "$listType blacklist local cache cleared.\n" ); |
| 337 |
} |
| 338 |
|
| 339 |
private function buildSharedBlacklists() { |
| 340 |
$regexes = []; |
| 341 |
$listType = $this->getBlacklistType(); |
| 342 |
|
| 343 |
wfDebugLog( 'SpamBlacklist', "Constructing $listType blacklist\n" ); |
| 344 |
foreach ( $this->files as $fileName ) { |
| 345 |
$matches = []; |
| 346 |
if ( preg_match( '/^DB: ([\w-]*) (.*)$/', $fileName, $matches ) ) { |
| 347 |
$text = $this->getArticleText( $matches[1], $matches[2] ); |
| 348 |
} elseif ( preg_match( '/^(https?:)?\/\//', $fileName ) ) { |
| 349 |
$text = $this->getHttpText( $fileName ); |
| 350 |
} else { |
| 351 |
$text = file_get_contents( $fileName ); |
| 352 |
wfDebugLog( 'SpamBlacklist', "got from file $fileName\n" ); |
| 353 |
} |
| 354 |
|
| 355 |
if ( $text ) { |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
$regexes = array_merge( |
| 362 |
$regexes, |
| 363 |
SpamRegexBatch::regexesFromText( $text, $this, $fileName ) |
| 364 |
); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
return $regexes; |
| 369 |
} |
| 370 |
|
| 371 |
private function getHttpText( $fileName ) { |
| 372 |
global $wgMessageCacheType; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
$services = MediaWikiServices::getInstance()->getObjectCacheFactory(); |
| 377 |
$cache = $services->getInstance( $wgMessageCacheType ); |
| 378 |
|
| 379 |
$listType = $this->getBlacklistType(); |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
$key = $cache->makeGlobalKey( "blacklist_file_{$listType}", $fileName ); |
| 384 |
$warningKey = $cache->makeKey( "filewarning_{$listType}", $fileName ); |
| 385 |
$httpText = $cache->get( $key ); |
| 386 |
$warning = $cache->get( $warningKey ); |
| 387 |
|
| 388 |
if ( !is_string( $httpText ) || ( !$warning && !mt_rand( 0, $this->warningChance ) ) ) { |
| 389 |
wfDebugLog( 'SpamBlacklist', "Loading $listType blacklist from $fileName\n" ); |
| 390 |
$httpText = MediaWikiServices::getInstance()->getHttpRequestFactory() |
| 391 |
->get( $fileName, [], __METHOD__ ); |
| 392 |
if ( $httpText === false ) { |
| 393 |
wfDebugLog( 'SpamBlacklist', "Error loading $listType blacklist from $fileName\n" ); |
| 394 |
} |
| 395 |
$cache->set( $warningKey, 1, $this->warningTime ); |
| 396 |
$cache->set( $key, $httpText, $this->expiryTime ); |
| 397 |
} else { |
| 398 |
wfDebugLog( 'SpamBlacklist', "Got $listType blacklist from HTTP cache for $fileName\n" ); |
| 399 |
} |
| 400 |
return $httpText; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
private function getArticleText( $wiki, $pagename ) { |
| 411 |
wfDebugLog( 'SpamBlacklist', |
| 412 |
"Fetching {$this->getBlacklistType()} blacklist from '$pagename' on '$wiki'...\n" ); |
| 413 |
|
| 414 |
$services = MediaWikiServices::getInstance(); |
| 415 |
|
| 416 |
|
| 417 |
$title = $services->getTitleParser()->parseTitle( $pagename ); |
| 418 |
$store = $services->getRevisionStoreFactory()->getRevisionStore( $wiki ); |
| 419 |
$rev = $store->getRevisionByTitle( $title ); |
| 420 |
|
| 421 |
$content = $rev ? $rev->getContent( SlotRecord::MAIN ) : null; |
| 422 |
|
| 423 |
if ( !( $content instanceof TextContent ) ) { |
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
return $content->getText(); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public function getRegexStart() { |
| 436 |
return '/[a-z0-9_\-.]*'; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public function getRegexEnd( $batchSize ) { |
| 446 |
return ( $batchSize > 0 ) ? '/Sim' : '/im'; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
public function warmCachesForFilter( Title $title, array $entries, User $user ) { |
| 455 |
|
| 456 |
} |
| 457 |
} |