mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-12-04 18:38:25 +00:00
06cdddc9d0
* mediawiki/mediawiki-codesniffer: 35.0.0 → 36.0.0 * php-parallel-lint/php-parallel-lint: 1.2.0 → 1.3.0 Change-Id: I92d6f6d6f817765df24f845103a489624f4290f2
64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Watcher;
|
|
|
|
use DeferredUpdates;
|
|
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 {
|
|
// 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_PRIMARY ), $localFilters );
|
|
}
|
|
|
|
if ( $globalFilters ) {
|
|
$fdb = $this->centralDBManager->getConnection( DB_PRIMARY );
|
|
$this->updateHitCounts( $fdb, $globalFilters );
|
|
}
|
|
} );
|
|
}
|
|
|
|
/**
|
|
* @param IDatabase $dbw
|
|
* @param array $loggedFilters
|
|
*/
|
|
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ) : void {
|
|
$dbw->update(
|
|
'abuse_filter',
|
|
[ 'af_hit_count=af_hit_count+1' ],
|
|
[ 'af_id' => $loggedFilters ],
|
|
__METHOD__
|
|
);
|
|
}
|
|
}
|