Merge "Add UpdateHitCountWatcher"

This commit is contained in:
jenkins-bot 2020-12-03 17:46:37 +00:00 committed by Gerrit Code Review
commit 6f7a9965a0
6 changed files with 89 additions and 35 deletions

View file

@ -15,7 +15,6 @@ use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGenerator;
use MediaWiki\Extension\AbuseFilter\Watcher\Watcher;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\IDatabase;
/**
* This class contains the logic for executing abuse filters and their actions. The entry points are
@ -109,7 +108,10 @@ class AbuseFilterRunner {
$this->changeTagger = AbuseFilterServices::getChangeTagger();
$this->filterLookup = AbuseFilterServices::getFilterLookup();
// TODO Inject, add a hook for custom watchers
$this->watchers = [ AbuseFilterServices::getEmergencyWatcher() ];
$this->watchers = [
AbuseFilterServices::getUpdateHitCountWatcher(),
AbuseFilterServices::getEmergencyWatcher()
];
}
/**
@ -237,17 +239,8 @@ class AbuseFilterRunner {
'global' => $loggedGlobalFilters
] = $abuseLogger->addLogEntries( $actionsTaken );
if ( count( $loggedLocalFilters ) ) {
$this->updateHitCounts( wfGetDB( DB_MASTER ), $loggedLocalFilters );
}
if ( count( $loggedGlobalFilters ) ) {
$fdb = AbuseFilterServices::getCentralDBManager()->getConnection( DB_MASTER );
$this->updateHitCounts( $fdb, $loggedGlobalFilters );
}
foreach ( $this->watchers as $watcher ) {
$watcher->run( $loggedLocalFilters, $this->group );
$watcher->run( $loggedLocalFilters, $loggedGlobalFilters, $this->group );
}
return $status;
@ -734,25 +727,6 @@ class AbuseFilterRunner {
return $status;
}
/**
* @param IDatabase $dbw
* @param array $loggedFilters
* @todo Move to a Watcher
*/
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ) : void {
$method = __METHOD__;
$dbw->onTransactionPreCommitOrIdle(
function () use ( $dbw, $loggedFilters, $method ) {
$dbw->update( 'abuse_filter',
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $loggedFilters ],
$method
);
},
$method
);
}
/**
* @return array
*/

View file

@ -7,6 +7,7 @@ use MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagsManager;
use MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagValidator;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\Watcher\EmergencyWatcher;
use MediaWiki\Extension\AbuseFilter\Watcher\UpdateHitCountWatcher;
use MediaWiki\MediaWikiServices;
class AbuseFilterServices {
@ -150,4 +151,11 @@ class AbuseFilterServices {
public static function getAbuseLoggerFactory() : AbuseLoggerFactory {
return MediaWikiServices::getInstance()->getService( AbuseLoggerFactory::SERVICE_NAME );
}
/**
* @return UpdateHitCountWatcher
*/
public static function getUpdateHitCountWatcher() : UpdateHitCountWatcher {
return MediaWikiServices::getInstance()->getService( UpdateHitCountWatcher::SERVICE_NAME );
}
}

View file

@ -23,6 +23,7 @@ use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner;
use MediaWiki\Extension\AbuseFilter\KeywordsManager;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\Watcher\EmergencyWatcher;
use MediaWiki\Extension\AbuseFilter\Watcher\UpdateHitCountWatcher;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Session\SessionManager;
@ -196,6 +197,12 @@ return [
RequestContext::getMain()->getRequest()->getIP()
);
},
UpdateHitCountWatcher::SERVICE_NAME => function ( MediaWikiServices $services ): UpdateHitCountWatcher {
return new UpdateHitCountWatcher(
$services->getDBLoadBalancer(),
$services->get( CentralDBManager::SERVICE_NAME )
);
},
];
// @codeCoverageIgnoreEnd

View file

@ -116,8 +116,8 @@ class EmergencyWatcher implements Watcher {
*
* @inheritDoc
*/
public function run( array $filters, string $group ) : void {
$throttleFilters = $this->getFiltersToThrottle( $filters, $group );
public function run( array $localFilters, array $globalFilters, string $group ) : void {
$throttleFilters = $this->getFiltersToThrottle( $localFilters, $group );
if ( !$throttleFilters ) {
return;
}

View file

@ -0,0 +1,64 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Watcher;
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 {
if ( count( $localFilters ) ) {
$this->updateHitCounts( $this->loadBalancer->getConnectionRef( DB_MASTER ), $localFilters );
}
if ( count( $globalFilters ) ) {
$fdb = $this->centralDBManager->getConnection( DB_MASTER );
$this->updateHitCounts( $fdb, $globalFilters );
}
}
/**
* @param IDatabase $dbw
* @param array $loggedFilters
*/
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ) : void {
$method = __METHOD__;
$dbw->onTransactionPreCommitOrIdle(
function () use ( $dbw, $loggedFilters, $method ) {
$dbw->update( 'abuse_filter',
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $loggedFilters ],
$method
);
},
$method
);
}
}

View file

@ -7,8 +7,9 @@ namespace MediaWiki\Extension\AbuseFilter\Watcher;
*/
interface Watcher {
/**
* @param int[] $filters The filters that matched the action
* @param int[] $localFilters The local filters that matched the action
* @param int[] $globalFilters The global filters that matched the action
* @param string $group
*/
public function run( array $filters, string $group ) : void;
public function run( array $localFilters, array $globalFilters, string $group ) : void;
}