| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\Filter\ClosestFilterVersionNotFoundException; |
| 6 |
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Filter\FilterNotFoundException; |
| 8 |
use MediaWiki\Extension\AbuseFilter\Filter\FilterVersionNotFoundException; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Filter\Flags; |
| 10 |
use MediaWiki\Extension\AbuseFilter\Filter\HistoryFilter; |
| 11 |
use MediaWiki\Extension\AbuseFilter\Filter\LastEditInfo; |
| 12 |
use MediaWiki\Extension\AbuseFilter\Filter\Specs; |
| 13 |
use RuntimeException; |
| 14 |
use stdClass; |
| 15 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 16 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 17 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 18 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 19 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class FilterLookup implements IDBAccessObject { |
| 27 |
public const SERVICE_NAME = 'AbuseFilterFilterLookup'; |
| 28 |
|
| 29 |
|
| 30 |
public const DIR_PREV = 'prev'; |
| 31 |
public const DIR_NEXT = 'next'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private $cache = []; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private $groupCache = [ 'local' => [], 'global' => [] ]; |
| 46 |
|
| 47 |
|
| 48 |
private $historyCache = []; |
| 49 |
|
| 50 |
|
| 51 |
private $firstVersionCache = []; |
| 52 |
|
| 53 |
|
| 54 |
private $lastVersionCache = []; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private $closestVersionsCache = []; |
| 61 |
|
| 62 |
|
| 63 |
private $loadBalancer; |
| 64 |
|
| 65 |
|
| 66 |
private $wanCache; |
| 67 |
|
| 68 |
|
| 69 |
private $centralDBManager; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private bool $localFiltersHiddenForTest = false; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function __construct( |
| 83 |
ILoadBalancer $loadBalancer, |
| 84 |
WANObjectCache $cache, |
| 85 |
CentralDBManager $centralDBManager |
| 86 |
) { |
| 87 |
$this->loadBalancer = $loadBalancer; |
| 88 |
$this->wanCache = $cache; |
| 89 |
$this->centralDBManager = $centralDBManager; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function getFilter( |
| 101 |
int $filterID, bool $global, int $flags = IDBAccessObject::READ_NORMAL |
| 102 |
): ExistingFilter { |
| 103 |
$cacheKey = $this->getCacheKey( $filterID, $global ); |
| 104 |
if ( $flags !== IDBAccessObject::READ_NORMAL || !isset( $this->cache[$cacheKey] ) ) { |
| 105 |
$dbr = ( $flags & IDBAccessObject::READ_LATEST ) |
| 106 |
? $this->getDBConnection( DB_PRIMARY, $global ) |
| 107 |
: $this->getDBConnection( DB_REPLICA, $global ); |
| 108 |
$row = $this->getAbuseFilterQueryBuilder( $dbr ) |
| 109 |
->where( [ 'af_id' => $filterID ] ) |
| 110 |
->recency( $flags ) |
| 111 |
->caller( __METHOD__ )->fetchRow(); |
| 112 |
|
| 113 |
if ( !$row ) { |
| 114 |
throw new FilterNotFoundException( $filterID, $global ); |
| 115 |
} |
| 116 |
$fname = __METHOD__; |
| 117 |
$getActionsCB = function () use ( $dbr, $fname, $row ): array { |
| 118 |
return $this->getActionsFromDB( $dbr, $fname, $row->af_id ); |
| 119 |
}; |
| 120 |
$this->cache[$cacheKey] = $this->filterFromRow( $row, $getActionsCB ); |
| 121 |
} |
| 122 |
|
| 123 |
return $this->cache[$cacheKey]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function getAllActiveFiltersInGroup( |
| 135 |
string $group, bool $global, int $flags = IDBAccessObject::READ_NORMAL |
| 136 |
): array { |
| 137 |
$domainKey = $global ? 'global' : 'local'; |
| 138 |
if ( $flags !== IDBAccessObject::READ_NORMAL || !isset( $this->groupCache[$domainKey][$group] ) ) { |
| 139 |
if ( $global ) { |
| 140 |
$globalRulesKey = $this->getGlobalRulesKey( $group ); |
| 141 |
$ret = $this->wanCache->getWithSetCallback( |
| 142 |
$globalRulesKey, |
| 143 |
WANObjectCache::TTL_WEEK, |
| 144 |
function () use ( $group, $global, $flags ) { |
| 145 |
return $this->getAllActiveFiltersInGroupFromDB( $group, $global, $flags ); |
| 146 |
}, |
| 147 |
[ |
| 148 |
'checkKeys' => [ $globalRulesKey ], |
| 149 |
'lockTSE' => 300, |
| 150 |
'version' => 3 |
| 151 |
] |
| 152 |
); |
| 153 |
} else { |
| 154 |
$ret = $this->getAllActiveFiltersInGroupFromDB( $group, $global, $flags ); |
| 155 |
} |
| 156 |
|
| 157 |
$this->groupCache[$domainKey][$group] = []; |
| 158 |
foreach ( $ret as $key => $filter ) { |
| 159 |
$this->groupCache[$domainKey][$group][$key] = $filter; |
| 160 |
$this->cache[$key] = $filter; |
| 161 |
} |
| 162 |
} |
| 163 |
return $this->groupCache[$domainKey][$group]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private function getAllActiveFiltersInGroupFromDB( string $group, bool $global, int $flags ): array { |
| 173 |
if ( $this->localFiltersHiddenForTest && !$global ) { |
| 174 |
return []; |
| 175 |
} |
| 176 |
$dbr = ( $flags & IDBAccessObject::READ_LATEST ) |
| 177 |
? $this->getDBConnection( DB_PRIMARY, $global ) |
| 178 |
: $this->getDBConnection( DB_REPLICA, $global ); |
| 179 |
$queryBuilder = $this->getAbuseFilterQueryBuilder( $dbr ) |
| 180 |
->where( [ 'af_enabled' => 1, 'af_deleted' => 0, 'af_group' => $group ] ) |
| 181 |
->recency( $flags ); |
| 182 |
|
| 183 |
if ( $global ) { |
| 184 |
$queryBuilder->andWhere( [ 'af_global' => 1 ] ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
$rows = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 190 |
|
| 191 |
$fname = __METHOD__; |
| 192 |
$ret = []; |
| 193 |
foreach ( $rows as $row ) { |
| 194 |
$filterKey = $this->getCacheKey( $row->af_id, $global ); |
| 195 |
$getActionsCB = function () use ( $dbr, $fname, $row ): array { |
| 196 |
return $this->getActionsFromDB( $dbr, $fname, $row->af_id ); |
| 197 |
}; |
| 198 |
$ret[$filterKey] = $this->filterFromRow( |
| 199 |
$row, |
| 200 |
|
| 201 |
$global ? $getActionsCB() : $getActionsCB |
| 202 |
); |
| 203 |
} |
| 204 |
return $ret; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
private function getDBConnection( int $dbIndex, bool $global ): IReadableDatabase { |
| 214 |
if ( $global ) { |
| 215 |
return $this->centralDBManager->getConnection( $dbIndex ); |
| 216 |
} else { |
| 217 |
return $this->loadBalancer->getConnection( $dbIndex ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private function getActionsFromDB( IReadableDatabase $db, string $fname, int $id ): array { |
| 228 |
$res = $db->newSelectQueryBuilder() |
| 229 |
->select( [ 'afa_consequence', 'afa_parameters' ] ) |
| 230 |
->from( 'abuse_filter_action' ) |
| 231 |
->where( [ 'afa_filter' => $id ] ) |
| 232 |
->caller( $fname ) |
| 233 |
->fetchResultSet(); |
| 234 |
|
| 235 |
$actions = []; |
| 236 |
foreach ( $res as $actionRow ) { |
| 237 |
$actions[$actionRow->afa_consequence] = $actionRow->afa_parameters !== '' |
| 238 |
? explode( "\n", $actionRow->afa_parameters ) |
| 239 |
: []; |
| 240 |
} |
| 241 |
return $actions; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function getFilterVersion( |
| 253 |
int $version, |
| 254 |
int $flags = IDBAccessObject::READ_NORMAL |
| 255 |
): HistoryFilter { |
| 256 |
if ( $flags !== IDBAccessObject::READ_NORMAL || !isset( $this->historyCache[$version] ) ) { |
| 257 |
$dbr = ( $flags & IDBAccessObject::READ_LATEST ) |
| 258 |
? $this->loadBalancer->getConnection( DB_PRIMARY ) |
| 259 |
: $this->loadBalancer->getConnection( DB_REPLICA ); |
| 260 |
$row = $this->getAbuseFilterHistoryQueryBuilder( $dbr ) |
| 261 |
->where( [ 'afh_id' => $version ] ) |
| 262 |
->recency( $flags ) |
| 263 |
->caller( __METHOD__ )->fetchRow(); |
| 264 |
if ( !$row ) { |
| 265 |
throw new FilterVersionNotFoundException( $version ); |
| 266 |
} |
| 267 |
$this->historyCache[$version] = $this->filterFromHistoryRow( $row ); |
| 268 |
} |
| 269 |
|
| 270 |
return $this->historyCache[$version]; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public function getLastHistoryVersion( int $filterID ): HistoryFilter { |
| 279 |
if ( !isset( $this->lastVersionCache[$filterID] ) ) { |
| 280 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 281 |
$row = $this->getAbuseFilterHistoryQueryBuilder( $dbr ) |
| 282 |
->where( [ 'afh_filter' => $filterID ] ) |
| 283 |
->orderBy( 'afh_id', SelectQueryBuilder::SORT_DESC ) |
| 284 |
->caller( __METHOD__ )->fetchRow(); |
| 285 |
if ( !$row ) { |
| 286 |
throw new FilterNotFoundException( $filterID, false ); |
| 287 |
} |
| 288 |
$filterObj = $this->filterFromHistoryRow( $row ); |
| 289 |
$this->lastVersionCache[$filterID] = $filterObj->getHistoryID(); |
| 290 |
$this->historyCache[$filterObj->getHistoryID()] = $filterObj; |
| 291 |
} |
| 292 |
return $this->historyCache[ $this->lastVersionCache[$filterID] ]; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function getClosestVersion( int $historyID, int $filterID, string $direction ): HistoryFilter { |
| 303 |
if ( !isset( $this->closestVersionsCache[$filterID][$historyID][$direction] ) ) { |
| 304 |
$comparison = $direction === self::DIR_PREV ? '<' : '>'; |
| 305 |
$order = $direction === self::DIR_PREV ? 'DESC' : 'ASC'; |
| 306 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 307 |
$row = $this->getAbuseFilterHistoryQueryBuilder( $dbr ) |
| 308 |
->where( [ 'afh_filter' => $filterID ] ) |
| 309 |
->andWhere( $dbr->expr( 'afh_id', $comparison, $historyID ) ) |
| 310 |
->orderBy( 'afh_timestamp', $order ) |
| 311 |
->caller( __METHOD__ )->fetchRow(); |
| 312 |
if ( !$row ) { |
| 313 |
throw new ClosestFilterVersionNotFoundException( $filterID, $historyID ); |
| 314 |
} |
| 315 |
$filterObj = $this->filterFromHistoryRow( $row ); |
| 316 |
$this->closestVersionsCache[$filterID][$historyID][$direction] = $filterObj->getHistoryID(); |
| 317 |
$this->historyCache[$filterObj->getHistoryID()] = $filterObj; |
| 318 |
} |
| 319 |
$histID = $this->closestVersionsCache[$filterID][$historyID][$direction]; |
| 320 |
return $this->historyCache[$histID]; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function getFirstFilterVersionID( int $filterID ): int { |
| 331 |
if ( !isset( $this->firstVersionCache[$filterID] ) ) { |
| 332 |
$dbr = $this->loadBalancer->getConnection( DB_REPLICA ); |
| 333 |
$historyID = $dbr->newSelectQueryBuilder() |
| 334 |
->select( 'MIN(afh_id)' ) |
| 335 |
->from( 'abuse_filter_history' ) |
| 336 |
->where( [ 'afh_filter' => $filterID ] ) |
| 337 |
->caller( __METHOD__ ) |
| 338 |
->fetchField(); |
| 339 |
if ( $historyID === false ) { |
| 340 |
throw new FilterNotFoundException( $filterID, false ); |
| 341 |
} |
| 342 |
$this->firstVersionCache[$filterID] = (int)$historyID; |
| 343 |
} |
| 344 |
|
| 345 |
return $this->firstVersionCache[$filterID]; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function clearLocalCache(): void { |
| 352 |
$this->cache = []; |
| 353 |
$this->groupCache = [ 'local' => [], 'global' => [] ]; |
| 354 |
$this->historyCache = []; |
| 355 |
$this->firstVersionCache = []; |
| 356 |
$this->lastVersionCache = []; |
| 357 |
$this->closestVersionsCache = []; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function purgeGroupWANCache( string $group ): void { |
| 366 |
$this->wanCache->touchCheckKey( $this->getGlobalRulesKey( $group ) ); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
private function getGlobalRulesKey( string $group ): string { |
| 374 |
if ( !$this->centralDBManager->filterIsCentral() ) { |
| 375 |
return $this->wanCache->makeGlobalKey( |
| 376 |
'abusefilter', |
| 377 |
'rules', |
| 378 |
$this->centralDBManager->getCentralDBName(), |
| 379 |
$group |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
return $this->wanCache->makeKey( 'abusefilter', 'rules', $group ); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
private function getPrivacyLevelFromFlags( $flags ): int { |
| 391 |
$hidden = in_array( 'hidden', $flags, true ) ? |
| 392 |
Flags::FILTER_HIDDEN : |
| 393 |
0; |
| 394 |
$protected = in_array( 'protected', $flags, true ) ? |
| 395 |
Flags::FILTER_USES_PROTECTED_VARS : |
| 396 |
0; |
| 397 |
return $hidden | $protected; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
private function filterFromHistoryRow( stdClass $row ): HistoryFilter { |
| 406 |
$actionsRaw = unserialize( $row->afh_actions ); |
| 407 |
$actions = is_array( $actionsRaw ) ? $actionsRaw : []; |
| 408 |
$flags = $row->afh_flags ? explode( ',', $row->afh_flags ) : []; |
| 409 |
return new HistoryFilter( |
| 410 |
new Specs( |
| 411 |
trim( $row->afh_pattern ), |
| 412 |
$row->afh_comments, |
| 413 |
|
| 414 |
(string)$row->afh_public_comments, |
| 415 |
array_keys( $actions ), |
| 416 |
|
| 417 |
$row->afh_group ?? 'default' |
| 418 |
), |
| 419 |
new Flags( |
| 420 |
in_array( 'enabled', $flags, true ), |
| 421 |
in_array( 'deleted', $flags, true ), |
| 422 |
$this->getPrivacyLevelFromFlags( $flags ), |
| 423 |
in_array( 'global', $flags, true ) |
| 424 |
), |
| 425 |
$actions, |
| 426 |
new LastEditInfo( |
| 427 |
(int)$row->afh_user, |
| 428 |
$row->afh_user_text, |
| 429 |
$row->afh_timestamp |
| 430 |
), |
| 431 |
(int)$row->afh_filter, |
| 432 |
$row->afh_id |
| 433 |
); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
private function filterFromRow( stdClass $row, $actions ): ExistingFilter { |
| 443 |
return new ExistingFilter( |
| 444 |
new Specs( |
| 445 |
trim( $row->af_pattern ), |
| 446 |
|
| 447 |
(string)$row->af_comments, |
| 448 |
(string)$row->af_public_comments, |
| 449 |
$row->af_actions !== '' ? explode( ',', $row->af_actions ) : [], |
| 450 |
$row->af_group |
| 451 |
), |
| 452 |
new Flags( |
| 453 |
(bool)$row->af_enabled, |
| 454 |
(bool)$row->af_deleted, |
| 455 |
(int)$row->af_hidden, |
| 456 |
(bool)$row->af_global |
| 457 |
), |
| 458 |
$actions, |
| 459 |
new LastEditInfo( |
| 460 |
(int)$row->af_user, |
| 461 |
$row->af_user_text, |
| 462 |
$row->af_timestamp |
| 463 |
), |
| 464 |
(int)$row->af_id, |
| 465 |
isset( $row->af_hit_count ) ? (int)$row->af_hit_count : null, |
| 466 |
isset( $row->af_throttled ) ? (bool)$row->af_throttled : null |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
private function getAbuseFilterQueryBuilder( IReadableDatabase $dbr ): SelectQueryBuilder { |
| 471 |
return $dbr->newSelectQueryBuilder() |
| 472 |
->select( [ |
| 473 |
'af_id', |
| 474 |
'af_pattern', |
| 475 |
'af_timestamp', |
| 476 |
'af_enabled', |
| 477 |
'af_comments', |
| 478 |
'af_public_comments', |
| 479 |
'af_hidden', |
| 480 |
'af_hit_count', |
| 481 |
'af_throttled', |
| 482 |
'af_deleted', |
| 483 |
'af_actions', |
| 484 |
'af_global', |
| 485 |
'af_group', |
| 486 |
'af_user' => 'actor_af_user.actor_user', |
| 487 |
'af_user_text' => 'actor_af_user.actor_name', |
| 488 |
'af_actor' => 'af_actor' |
| 489 |
] ) |
| 490 |
->from( 'abuse_filter' ) |
| 491 |
->join( 'actor', 'actor_af_user', 'actor_af_user.actor_id = af_actor' ); |
| 492 |
} |
| 493 |
|
| 494 |
private function getAbuseFilterHistoryQueryBuilder( IReadableDatabase $dbr ): SelectQueryBuilder { |
| 495 |
return $dbr->newSelectQueryBuilder() |
| 496 |
->select( [ |
| 497 |
'afh_id', |
| 498 |
'afh_pattern', |
| 499 |
'afh_timestamp', |
| 500 |
'afh_filter', |
| 501 |
'afh_comments', |
| 502 |
'afh_public_comments', |
| 503 |
'afh_flags', |
| 504 |
'afh_actions', |
| 505 |
'afh_group', |
| 506 |
'afh_user' => 'actor_afh_user.actor_user', |
| 507 |
'afh_user_text' => 'actor_afh_user.actor_name', |
| 508 |
'afh_actor' => 'afh_actor' |
| 509 |
] ) |
| 510 |
->from( 'abuse_filter_history' ) |
| 511 |
->join( 'actor', 'actor_afh_user', 'actor_afh_user.actor_id = afh_actor' ); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
private function getCacheKey( int $filterID, bool $global ): string { |
| 520 |
return GlobalNameUtils::buildGlobalName( $filterID, $global ); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
public function hideLocalFiltersForTesting(): void { |
| 529 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 530 |
throw new RuntimeException( 'Can only be called in tests' ); |
| 531 |
} |
| 532 |
$this->localFiltersHiddenForTest = true; |
| 533 |
} |
| 534 |
} |