2020-11-28 23:53:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter\Watcher;
|
|
|
|
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2020-11-28 23:53:42 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\CentralDBManager;
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2023-02-26 12:51:08 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2020-11-28 23:53:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Watcher that updates hit counts of filters
|
|
|
|
*/
|
|
|
|
class UpdateHitCountWatcher implements Watcher {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterUpdateHitCountWatcher';
|
|
|
|
|
2023-02-26 12:51:08 +00:00
|
|
|
/** @var LBFactory */
|
|
|
|
private $lbFactory;
|
2020-11-28 23:53:42 +00:00
|
|
|
|
|
|
|
/** @var CentralDBManager */
|
|
|
|
private $centralDBManager;
|
|
|
|
|
|
|
|
/**
|
2023-02-26 12:51:08 +00:00
|
|
|
* @param LBFactory $lbFactory
|
2020-11-28 23:53:42 +00:00
|
|
|
* @param CentralDBManager $centralDBManager
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2023-02-26 12:51:08 +00:00
|
|
|
LBFactory $lbFactory,
|
2023-08-18 20:47:19 +00:00
|
|
|
CentralDBManager $centralDBManager
|
2020-11-28 23:53:42 +00:00
|
|
|
) {
|
2023-02-26 12:51:08 +00:00
|
|
|
$this->lbFactory = $lbFactory;
|
2020-11-28 23:53:42 +00:00
|
|
|
$this->centralDBManager = $centralDBManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function run( array $localFilters, array $globalFilters, string $group ): void {
|
2021-05-12 23:43:37 +00:00
|
|
|
// Run in a DeferredUpdate to avoid primary database queries on raw/view requests (T274455)
|
2023-08-18 20:47:19 +00:00
|
|
|
DeferredUpdates::addCallableUpdate( function () use ( $localFilters, $globalFilters ) {
|
2021-02-12 13:15:00 +00:00
|
|
|
if ( $localFilters ) {
|
2023-02-26 12:51:08 +00:00
|
|
|
$this->updateHitCounts( $this->lbFactory->getPrimaryDatabase(), $localFilters );
|
2021-02-12 13:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $globalFilters ) {
|
2021-05-02 06:41:53 +00:00
|
|
|
$fdb = $this->centralDBManager->getConnection( DB_PRIMARY );
|
2021-02-12 13:15:00 +00:00
|
|
|
$this->updateHitCounts( $fdb, $globalFilters );
|
|
|
|
}
|
|
|
|
} );
|
2020-11-28 23:53:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
* @param array $loggedFilters
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ): void {
|
2024-04-15 19:49:52 +00:00
|
|
|
$dbw->newUpdateQueryBuilder()
|
|
|
|
->update( 'abuse_filter' )
|
|
|
|
->set( [ 'af_hit_count=af_hit_count+1' ] )
|
|
|
|
->where( [ 'af_id' => $loggedFilters ] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2020-11-28 23:53:42 +00:00
|
|
|
}
|
|
|
|
}
|