2020-11-28 23:53:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Watcher;
|
|
|
|
|
2021-02-12 13:15:00 +00:00
|
|
|
use DeferredUpdates;
|
2020-11-28 23:53:42 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\CentralDBManager;
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watcher that updates hit counts of filters
|
|
|
|
*/
|
|
|
|
class UpdateHitCountWatcher implements Watcher {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterUpdateHitCountWatcher';
|
|
|
|
|
|
|
|
/** @var ILoadBalancer */
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
/** @var CentralDBManager */
|
|
|
|
private $centralDBManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ILoadBalancer $loadBalancer
|
|
|
|
* @param CentralDBManager $centralDBManager
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ILoadBalancer $loadBalancer,
|
|
|
|
CentralDBManager $centralDBManager
|
|
|
|
) {
|
|
|
|
$this->loadBalancer = $loadBalancer;
|
|
|
|
$this->centralDBManager = $centralDBManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function run( array $localFilters, array $globalFilters, string $group ) : void {
|
2021-02-12 13:15:00 +00:00
|
|
|
// Run in a DeferredUpdate to avoid master queries on raw/view requests (T274455)
|
|
|
|
DeferredUpdates::addCallableUpdate( function () use ( $localFilters, $globalFilters ) {
|
|
|
|
if ( $localFilters ) {
|
|
|
|
$this->updateHitCounts( $this->loadBalancer->getConnectionRef( DB_MASTER ), $localFilters );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $globalFilters ) {
|
|
|
|
$fdb = $this->centralDBManager->getConnection( DB_MASTER );
|
|
|
|
$this->updateHitCounts( $fdb, $globalFilters );
|
|
|
|
}
|
|
|
|
} );
|
2020-11-28 23:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
* @param array $loggedFilters
|
|
|
|
*/
|
|
|
|
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ) : void {
|
2021-02-12 13:15:00 +00:00
|
|
|
$dbw->update(
|
|
|
|
'abuse_filter',
|
|
|
|
[ 'af_hit_count=af_hit_count+1' ],
|
|
|
|
[ 'af_id' => $loggedFilters ],
|
|
|
|
__METHOD__
|
2020-11-28 23:53:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|