mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-15 02:03:53 +00:00
124 lines
3 KiB
PHP
124 lines
3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace MediaWiki\Extension\AbuseFilter;
|
||
|
|
||
|
use AbuseFilter;
|
||
|
use Psr\Log\LoggerInterface;
|
||
|
use Wikimedia\Rdbms\IDatabase;
|
||
|
use Wikimedia\Rdbms\ILoadBalancer;
|
||
|
|
||
|
/**
|
||
|
* Class for retrieving actions and parameters from the database
|
||
|
*/
|
||
|
class ConsequencesLookup {
|
||
|
public const SERVICE_NAME = 'AbuseFilterConsequencesLookup';
|
||
|
|
||
|
/** @var ILoadBalancer */
|
||
|
private $loadBalancer;
|
||
|
/** @var CentralDBManager */
|
||
|
private $centralDBManager;
|
||
|
/** @var LoggerInterface */
|
||
|
private $logger;
|
||
|
|
||
|
/**
|
||
|
* @param ILoadBalancer $loadBalancer
|
||
|
* @param CentralDBManager $centralDBManager
|
||
|
* @param LoggerInterface $logger
|
||
|
*/
|
||
|
public function __construct(
|
||
|
ILoadBalancer $loadBalancer,
|
||
|
CentralDBManager $centralDBManager,
|
||
|
LoggerInterface $logger
|
||
|
) {
|
||
|
$this->loadBalancer = $loadBalancer;
|
||
|
$this->centralDBManager = $centralDBManager;
|
||
|
$this->logger = $logger;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string[] $filters
|
||
|
* @return array[][]
|
||
|
*/
|
||
|
public function getConsequencesForFilters( array $filters ) : array {
|
||
|
$globalFilters = [];
|
||
|
$localFilters = [];
|
||
|
|
||
|
foreach ( $filters as $filter ) {
|
||
|
list( $filterID, $global ) = AbuseFilter::splitGlobalName( $filter );
|
||
|
|
||
|
if ( $global ) {
|
||
|
$globalFilters[] = $filterID;
|
||
|
} else {
|
||
|
$localFilters[] = $filter;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Load local filter info
|
||
|
$dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
|
||
|
// Retrieve the consequences.
|
||
|
$consequences = [];
|
||
|
|
||
|
if ( count( $localFilters ) ) {
|
||
|
$consequences = $this->loadConsequencesFromDB( $dbr, $localFilters );
|
||
|
}
|
||
|
|
||
|
if ( count( $globalFilters ) ) {
|
||
|
$consequences += $this->loadConsequencesFromDB(
|
||
|
$this->centralDBManager->getConnection( DB_REPLICA ),
|
||
|
$globalFilters,
|
||
|
AbuseFilter::GLOBAL_FILTER_PREFIX
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return $consequences;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param IDatabase $dbr
|
||
|
* @param string[] $filters
|
||
|
* @param string $prefix
|
||
|
* @return array[][]
|
||
|
*/
|
||
|
private function loadConsequencesFromDB( IDatabase $dbr, array $filters, string $prefix = '' ) : array {
|
||
|
$actionsByFilter = [];
|
||
|
foreach ( $filters as $filter ) {
|
||
|
$actionsByFilter[$prefix . $filter] = [];
|
||
|
}
|
||
|
|
||
|
$res = $dbr->select(
|
||
|
[ 'abuse_filter_action', 'abuse_filter' ],
|
||
|
'*',
|
||
|
[ 'af_id' => $filters ],
|
||
|
__METHOD__,
|
||
|
[],
|
||
|
[ 'abuse_filter_action' => [ 'LEFT JOIN', 'afa_filter=af_id' ] ]
|
||
|
);
|
||
|
|
||
|
$dangerousActions = AbuseFilter::getDangerousActions();
|
||
|
// Categorise consequences by filter.
|
||
|
foreach ( $res as $row ) {
|
||
|
if ( $row->af_throttled
|
||
|
&& in_array( $row->afa_consequence, $dangerousActions )
|
||
|
) {
|
||
|
// Don't do the action, just log
|
||
|
$this->logger->info(
|
||
|
'Filter {filter_id} is throttled, skipping action: {action}',
|
||
|
[
|
||
|
'filter_id' => $row->af_id,
|
||
|
'action' => $row->afa_consequence
|
||
|
]
|
||
|
);
|
||
|
} elseif ( $row->afa_filter !== $row->af_id ) {
|
||
|
// We probably got a NULL, as it's a LEFT JOIN. Don't add it.
|
||
|
continue;
|
||
|
} else {
|
||
|
$actionsByFilter[$prefix . $row->afa_filter][$row->afa_consequence] =
|
||
|
array_filter( explode( "\n", $row->afa_parameters ) );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $actionsByFilter;
|
||
|
}
|
||
|
|
||
|
}
|