| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Consequences; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\CentralDBManager; |
| 6 |
use MediaWiki\Extension\AbuseFilter\GlobalNameUtils; |
| 7 |
use Psr\Log\LoggerInterface; |
| 8 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 9 |
use Wikimedia\Rdbms\LBFactory; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ConsequencesLookup { |
| 16 |
public const SERVICE_NAME = 'AbuseFilterConsequencesLookup'; |
| 17 |
|
| 18 |
|
| 19 |
private $lbFactory; |
| 20 |
|
| 21 |
private $centralDBManager; |
| 22 |
|
| 23 |
private $consequencesRegistry; |
| 24 |
|
| 25 |
private $logger; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct( |
| 34 |
LBFactory $lbFactory, |
| 35 |
CentralDBManager $centralDBManager, |
| 36 |
ConsequencesRegistry $consequencesRegistry, |
| 37 |
LoggerInterface $logger |
| 38 |
) { |
| 39 |
$this->lbFactory = $lbFactory; |
| 40 |
$this->centralDBManager = $centralDBManager; |
| 41 |
$this->consequencesRegistry = $consequencesRegistry; |
| 42 |
$this->logger = $logger; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function getConsequencesForFilters( array $filters ): array { |
| 50 |
$globalFilters = []; |
| 51 |
$localFilters = []; |
| 52 |
|
| 53 |
foreach ( $filters as $filter ) { |
| 54 |
[ $filterID, $global ] = GlobalNameUtils::splitGlobalName( $filter ); |
| 55 |
|
| 56 |
if ( $global ) { |
| 57 |
$globalFilters[] = $filterID; |
| 58 |
} else { |
| 59 |
$localFilters[] = (int)$filter; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
$dbr = $this->lbFactory->getReplicaDatabase(); |
| 65 |
|
| 66 |
$consequences = []; |
| 67 |
|
| 68 |
if ( count( $localFilters ) ) { |
| 69 |
$consequences = $this->loadConsequencesFromDB( $dbr, $localFilters ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( count( $globalFilters ) ) { |
| 73 |
$consequences += $this->loadConsequencesFromDB( |
| 74 |
$this->centralDBManager->getConnection( DB_REPLICA ), |
| 75 |
$globalFilters, |
| 76 |
GlobalNameUtils::GLOBAL_FILTER_PREFIX |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
return $consequences; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private function loadConsequencesFromDB( IReadableDatabase $dbr, array $filters, string $prefix = '' ): array { |
| 90 |
$actionsByFilter = []; |
| 91 |
foreach ( $filters as $filter ) { |
| 92 |
$actionsByFilter[$prefix . $filter] = []; |
| 93 |
} |
| 94 |
|
| 95 |
$res = $dbr->newSelectQueryBuilder() |
| 96 |
->select( '*' ) |
| 97 |
->from( 'abuse_filter_action' ) |
| 98 |
->leftJoin( 'abuse_filter', null, 'afa_filter=af_id' ) |
| 99 |
->where( [ 'af_id' => $filters ] ) |
| 100 |
->caller( __METHOD__ ) |
| 101 |
->fetchResultSet(); |
| 102 |
|
| 103 |
$dangerousActions = $this->consequencesRegistry->getDangerousActionNames(); |
| 104 |
|
| 105 |
foreach ( $res as $row ) { |
| 106 |
if ( $row->af_throttled |
| 107 |
&& in_array( $row->afa_consequence, $dangerousActions ) |
| 108 |
) { |
| 109 |
|
| 110 |
$this->logger->info( |
| 111 |
'Filter {filter_id} is throttled, skipping action: {action}', |
| 112 |
[ |
| 113 |
'filter_id' => $row->af_id, |
| 114 |
'action' => $row->afa_consequence |
| 115 |
] |
| 116 |
); |
| 117 |
} elseif ( $row->afa_filter !== $row->af_id ) { |
| 118 |
|
| 119 |
continue; |
| 120 |
} else { |
| 121 |
$actionsByFilter[$prefix . $row->afa_filter][$row->afa_consequence] = |
| 122 |
$row->afa_parameters !== '' ? explode( "\n", $row->afa_parameters ) : []; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $actionsByFilter; |
| 127 |
} |
| 128 |
|
| 129 |
} |