Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
<?php
|
|
|
|
|
2020-09-20 22:31:41 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2020-11-26 15:23:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagger;
|
2020-10-11 21:17:41 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequence\BCConsequence;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequence\Consequence;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequence\ConsequencesDisablerConsequence;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequence\HookAborterConsequence;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequence\Parameters;
|
2020-09-20 11:30:59 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Filter;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\FilterLookup;
|
2020-09-20 22:31:41 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\FilterProfiler;
|
2020-06-03 00:43:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner;
|
2019-06-25 16:39:57 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGenerator;
|
2020-11-19 15:32:39 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Watcher\Watcher;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
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
|
|
|
|
* run() and runForStash(). Note that run() can only be executed once on a given instance.
|
|
|
|
*/
|
|
|
|
class AbuseFilterRunner {
|
|
|
|
/**
|
|
|
|
* @var User The user who performed the action being filtered
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
/**
|
|
|
|
* @var Title The title where the action being filtered was performed
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
/**
|
|
|
|
* @var AbuseFilterVariableHolder The variables for the current action
|
|
|
|
*/
|
|
|
|
protected $vars;
|
|
|
|
/**
|
|
|
|
* @var string The group of filters to check (as defined in $wgAbuseFilterValidGroups)
|
|
|
|
*/
|
|
|
|
protected $group;
|
2020-03-06 16:21:51 +00:00
|
|
|
/**
|
|
|
|
* @var string The action we're filtering
|
|
|
|
*/
|
|
|
|
protected $action;
|
2019-01-30 19:30:59 +00:00
|
|
|
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
/**
|
2019-01-30 19:30:59 +00:00
|
|
|
* @var array Data from per-filter profiling. Shape:
|
2019-10-09 10:37:38 +00:00
|
|
|
* [ filterName => [ 'time' => float, 'conds' => int, 'result' => bool ] ]
|
|
|
|
* @phan-var array<string,array{time:float,conds:int,result:bool}>
|
2019-01-30 19:30:59 +00:00
|
|
|
*
|
|
|
|
* Where 'timeTaken' is in seconds, 'result' is a boolean indicating whether the filter matched
|
|
|
|
* the action, and 'filterID' is "{prefix}-{ID}" ; Prefix should be empty for local
|
|
|
|
* filters. In stash mode this member is saved in cache, while in execute mode it's used to
|
|
|
|
* update profiling after checking all filters.
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
*/
|
2019-01-30 19:30:59 +00:00
|
|
|
protected $profilingData;
|
|
|
|
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
/**
|
|
|
|
* @var AbuseFilterParser The parser instance to use to check all filters
|
|
|
|
* @protected Public for back-compat only, will be made protected. self::init already handles
|
|
|
|
* building a parser object.
|
|
|
|
*/
|
|
|
|
public $parser;
|
|
|
|
/**
|
|
|
|
* @var bool Whether a run() was already performed. Used to avoid multiple executions with the
|
|
|
|
* same members.
|
|
|
|
*/
|
|
|
|
private $executed = false;
|
|
|
|
|
2020-06-03 00:43:22 +00:00
|
|
|
/** @var AbuseFilterHookRunner */
|
|
|
|
private $hookRunner;
|
|
|
|
|
2020-09-20 22:31:41 +00:00
|
|
|
/** @var FilterProfiler */
|
|
|
|
private $filterProfiler;
|
|
|
|
|
2020-10-13 16:01:18 +00:00
|
|
|
/** @var ChangeTagger */
|
|
|
|
private $changeTagger;
|
|
|
|
|
2020-09-20 11:30:59 +00:00
|
|
|
/** @var FilterLookup */
|
|
|
|
private $filterLookup;
|
|
|
|
|
2020-11-19 15:32:39 +00:00
|
|
|
/** @var Watcher[] */
|
|
|
|
private $watchers;
|
2020-09-19 09:45:25 +00:00
|
|
|
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
/**
|
|
|
|
* @param User $user The user who performed the action being filtered
|
|
|
|
* @param Title $title The title where the action being filtered was performed
|
|
|
|
* @param AbuseFilterVariableHolder $vars The variables for the current action
|
|
|
|
* @param string $group The group of filters to check. It must be defined as so in
|
|
|
|
* $wgAbuseFilterValidGroups, or this will throw.
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function __construct( User $user, Title $title, AbuseFilterVariableHolder $vars, $group ) {
|
|
|
|
global $wgAbuseFilterValidGroups;
|
|
|
|
if ( !in_array( $group, $wgAbuseFilterValidGroups ) ) {
|
|
|
|
throw new InvalidArgumentException( '$group must be defined in $wgAbuseFilterValidGroups' );
|
|
|
|
}
|
2020-03-06 16:21:51 +00:00
|
|
|
if ( !$vars->varIsSet( 'action' ) ) {
|
|
|
|
throw new InvalidArgumentException( "The 'action' variable is not set." );
|
|
|
|
}
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$this->user = $user;
|
|
|
|
$this->title = $title;
|
|
|
|
$this->vars = $vars;
|
2019-08-11 13:11:20 +00:00
|
|
|
$this->vars->setLogger( LoggerFactory::getInstance( 'AbuseFilter' ) );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$this->group = $group;
|
2020-03-06 16:21:51 +00:00
|
|
|
$this->action = $vars->getVar( 'action' )->toString();
|
2020-06-03 00:43:22 +00:00
|
|
|
$this->hookRunner = AbuseFilterHookRunner::getRunner();
|
2020-09-20 22:31:41 +00:00
|
|
|
$this->filterProfiler = AbuseFilterServices::getFilterProfiler();
|
2020-10-13 16:01:18 +00:00
|
|
|
$this->changeTagger = AbuseFilterServices::getChangeTagger();
|
2020-09-20 11:30:59 +00:00
|
|
|
$this->filterLookup = AbuseFilterServices::getFilterLookup();
|
2020-11-19 15:32:39 +00:00
|
|
|
// TODO Inject, add a hook for custom watchers
|
|
|
|
$this->watchers = [ AbuseFilterServices::getEmergencyWatcher() ];
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inits variables and parser right before running
|
|
|
|
*/
|
|
|
|
private function init() {
|
|
|
|
// Add vars from extensions
|
2020-06-03 00:43:22 +00:00
|
|
|
$this->hookRunner->onAbuseFilterFilterAction(
|
|
|
|
$this->vars,
|
|
|
|
$this->title
|
|
|
|
);
|
|
|
|
$this->hookRunner->onAbuseFilterAlterVariables(
|
|
|
|
$this->vars,
|
|
|
|
$this->title,
|
|
|
|
$this->user
|
|
|
|
);
|
2019-06-25 16:39:57 +00:00
|
|
|
$generator = new VariableGenerator( $this->vars );
|
2019-12-17 15:06:44 +00:00
|
|
|
$this->vars = $generator->addGenericVars()->getVariableHolder();
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
|
|
|
$this->vars->forFilter = true;
|
|
|
|
$this->vars->setVar( 'timestamp', (int)wfTimestamp( TS_UNIX ) );
|
|
|
|
$this->parser = $this->getParser();
|
2019-09-19 17:06:14 +00:00
|
|
|
$this->parser->setStatsd( MediaWikiServices::getInstance()->getStatsdDataFactory() );
|
2019-01-30 19:30:59 +00:00
|
|
|
$this->profilingData = [];
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-21 10:04:10 +00:00
|
|
|
* Shortcut method, so that it can be overridden in mocks.
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @return AbuseFilterParser
|
|
|
|
*/
|
|
|
|
protected function getParser() : AbuseFilterParser {
|
2019-12-16 16:19:48 +00:00
|
|
|
return AbuseFilterServices::getParserFactory()->newParser( $this->vars );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main entry point of this class. This method runs all filters and takes their consequences.
|
|
|
|
*
|
|
|
|
* @param bool $allowStash Whether we are allowed to check the cache to see if there's a cached
|
|
|
|
* result of a previous execution for the same edit.
|
|
|
|
* @throws BadMethodCallException If run() was already called on this instance
|
|
|
|
* @return Status Good if no action has been taken, a fatal otherwise.
|
|
|
|
*/
|
|
|
|
public function run( $allowStash = true ) : Status {
|
|
|
|
if ( $this->executed ) {
|
|
|
|
throw new BadMethodCallException( 'run() was already called on this instance.' );
|
|
|
|
}
|
|
|
|
$this->executed = true;
|
|
|
|
$this->init();
|
|
|
|
|
2019-07-29 16:56:21 +00:00
|
|
|
$skipReasons = [];
|
2020-06-03 00:43:22 +00:00
|
|
|
$shouldFilter = $this->hookRunner->onAbuseFilterShouldFilterAction(
|
|
|
|
$this->vars, $this->title, $this->user, $skipReasons
|
2019-07-29 16:56:21 +00:00
|
|
|
);
|
|
|
|
if ( !$shouldFilter ) {
|
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
2020-03-06 16:21:51 +00:00
|
|
|
$logger->info(
|
|
|
|
'Skipping action {action}. Reasons provided: {reasons}',
|
|
|
|
[ 'action' => $this->action, 'reasons' => implode( ', ', $skipReasons ) ]
|
|
|
|
);
|
2019-07-29 16:56:21 +00:00
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:21:51 +00:00
|
|
|
$useStash = $allowStash && $this->action === 'edit';
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
|
|
|
$fromCache = false;
|
|
|
|
$result = [];
|
|
|
|
if ( $useStash ) {
|
|
|
|
$cacheData = $this->seekCache();
|
|
|
|
if ( $cacheData !== false ) {
|
2019-01-30 19:30:59 +00:00
|
|
|
// Use cached vars (T176291) and profiling data (T191430)
|
|
|
|
$this->vars = AbuseFilterVariableHolder::newFromArray( $cacheData['vars'] );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$result = [
|
2020-10-13 12:22:17 +00:00
|
|
|
'hitCondLimit' => $cacheData['hitCondLimit'],
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
'matches' => $cacheData['matches'],
|
|
|
|
'runtime' => $cacheData['runtime'],
|
|
|
|
'condCount' => $cacheData['condCount'],
|
2019-01-30 19:30:59 +00:00
|
|
|
'profiling' => $cacheData['profiling']
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
];
|
|
|
|
$fromCache = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$fromCache ) {
|
2019-01-30 19:30:59 +00:00
|
|
|
$startTime = microtime( true );
|
2019-03-25 19:19:17 +00:00
|
|
|
// Ensure there's no extra time leftover
|
|
|
|
AFComputedVariable::$profilingExtraTime = 0;
|
|
|
|
|
2020-10-13 12:22:17 +00:00
|
|
|
$hitCondLimit = false;
|
2019-01-30 19:30:59 +00:00
|
|
|
// This also updates $this->profilingData and $this->parser->mCondCount used later
|
2020-10-13 12:22:17 +00:00
|
|
|
$matches = $this->checkAllFilters( $hitCondLimit );
|
2019-03-25 19:19:17 +00:00
|
|
|
$timeTaken = ( microtime( true ) - $startTime - AFComputedVariable::$profilingExtraTime ) * 1000;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$result = [
|
2020-10-13 12:22:17 +00:00
|
|
|
'hitCondLimit' => $hitCondLimit,
|
2019-01-30 19:30:59 +00:00
|
|
|
'matches' => $matches,
|
|
|
|
'runtime' => $timeTaken,
|
|
|
|
'condCount' => $this->parser->getCondCount(),
|
|
|
|
'profiling' => $this->profilingData
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
];
|
|
|
|
}
|
2020-10-13 12:22:17 +00:00
|
|
|
'@phan-var array{hitCondLimit:bool,matches:array,runtime:int,condCount:int,profiling:array} $result';
|
2019-03-24 18:01:35 +00:00
|
|
|
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$matchedFilters = array_keys( array_filter( $result['matches'] ) );
|
2019-03-24 18:01:35 +00:00
|
|
|
$allFilters = array_keys( $result['matches'] );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-03-24 18:01:35 +00:00
|
|
|
$this->profileExecution( $result, $matchedFilters, $allFilters );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2020-10-13 12:22:17 +00:00
|
|
|
if ( $result['hitCondLimit'] ) {
|
2020-10-13 16:01:18 +00:00
|
|
|
$this->changeTagger->addConditionsLimitTag( $this->getSpecsForTagger() );
|
2020-10-13 12:22:17 +00:00
|
|
|
}
|
|
|
|
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
if ( count( $matchedFilters ) === 0 ) {
|
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
|
|
|
$status = $this->executeFilterActions( $matchedFilters );
|
|
|
|
$actionsTaken = $status->getValue();
|
|
|
|
|
2019-01-30 19:30:59 +00:00
|
|
|
$this->addLogEntries( $actionsTaken );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to run(), but runs in "stash" mode, which means filters are executed, no actions are
|
|
|
|
* taken, and the result is saved in cache to be later reused. This can only be used for edits,
|
|
|
|
* and not doing so will throw.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @return Status Always a good status, since we're only saving data.
|
|
|
|
*/
|
|
|
|
public function runForStash() : Status {
|
2020-03-06 16:21:51 +00:00
|
|
|
if ( $this->action !== 'edit' ) {
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
throw new InvalidArgumentException(
|
2020-03-06 16:21:51 +00:00
|
|
|
__METHOD__ . " can only be called for edits, called for action {$this->action}."
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
2019-07-29 16:56:21 +00:00
|
|
|
$skipReasons = [];
|
2020-06-03 00:43:22 +00:00
|
|
|
$shouldFilter = $this->hookRunner->onAbuseFilterShouldFilterAction(
|
|
|
|
$this->vars, $this->title, $this->user, $skipReasons
|
2019-07-29 16:56:21 +00:00
|
|
|
);
|
|
|
|
if ( !$shouldFilter ) {
|
|
|
|
// Don't log it yet
|
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
$stashKey = $this->getStashKey( $cache );
|
|
|
|
|
|
|
|
$startTime = microtime( true );
|
2019-03-25 19:19:17 +00:00
|
|
|
// Ensure there's no extra time leftover
|
|
|
|
AFComputedVariable::$profilingExtraTime = 0;
|
|
|
|
|
2020-10-13 12:22:17 +00:00
|
|
|
$hitCondLimit = false;
|
|
|
|
$matchedFilters = $this->checkAllFilters( $hitCondLimit );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
// Save the filter stash result and do nothing further
|
|
|
|
$cacheData = [
|
|
|
|
'matches' => $matchedFilters,
|
2020-10-13 12:22:17 +00:00
|
|
|
'hitCondLimit' => $hitCondLimit,
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
'condCount' => $this->parser->getCondCount(),
|
2019-03-25 19:19:17 +00:00
|
|
|
'runtime' => ( microtime( true ) - $startTime - AFComputedVariable::$profilingExtraTime ) * 1000,
|
2019-01-30 19:30:59 +00:00
|
|
|
'vars' => $this->vars->dumpAllVars(),
|
|
|
|
'profiling' => $this->profilingData
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$cache->set( $stashKey, $cacheData, $cache::TTL_MINUTE );
|
|
|
|
$this->logCache( 'store', $stashKey );
|
|
|
|
|
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search the cache to find data for a previous execution done for the current edit.
|
|
|
|
*
|
|
|
|
* @return false|array False on failure, the array with data otherwise
|
|
|
|
*/
|
|
|
|
protected function seekCache() {
|
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
$stashKey = $this->getStashKey( $cache );
|
|
|
|
|
|
|
|
$ret = $cache->get( $stashKey );
|
|
|
|
$status = $ret !== false ? 'hit' : 'miss';
|
|
|
|
$this->logCache( $status, $stashKey );
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the stash key for the current variables
|
|
|
|
*
|
|
|
|
* @param BagOStuff $cache
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getStashKey( BagOStuff $cache ) {
|
|
|
|
$inputVars = $this->vars->exportNonLazyVars();
|
|
|
|
// Exclude noisy fields that have superficial changes
|
|
|
|
$excludedVars = [
|
|
|
|
'old_html' => true,
|
|
|
|
'new_html' => true,
|
|
|
|
'user_age' => true,
|
|
|
|
'timestamp' => true,
|
|
|
|
'page_age' => true,
|
|
|
|
'moved_from_age' => true,
|
|
|
|
'moved_to_age' => true
|
|
|
|
];
|
|
|
|
|
|
|
|
$inputVars = array_diff_key( $inputVars, $excludedVars );
|
|
|
|
ksort( $inputVars );
|
|
|
|
$hash = md5( serialize( $inputVars ) );
|
|
|
|
|
|
|
|
return $cache->makeKey(
|
|
|
|
'abusefilter',
|
|
|
|
'check-stash',
|
|
|
|
$this->group,
|
|
|
|
$hash,
|
2019-08-04 16:07:54 +00:00
|
|
|
'v2'
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log cache operations related to stashed edits, i.e. store, hit and miss
|
|
|
|
*
|
|
|
|
* @param string $type Either 'store', 'hit' or 'miss'
|
|
|
|
* @param string $key The cache key used
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function logCache( $type, $key ) {
|
|
|
|
if ( !in_array( $type, [ 'store', 'hit', 'miss' ] ) ) {
|
|
|
|
throw new InvalidArgumentException( '$type must be either "store", "hit" or "miss"' );
|
|
|
|
}
|
|
|
|
$logger = LoggerFactory::getInstance( 'StashEdit' );
|
|
|
|
// Bots do not use edit stashing, so avoid distorting the stats
|
|
|
|
$statsd = $this->user->isBot()
|
|
|
|
? new NullStatsdDataFactory()
|
|
|
|
: MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
|
|
|
$logger->debug( __METHOD__ . ": cache $type for '{$this->title}' (key $key)." );
|
|
|
|
$statsd->increment( "abusefilter.check-stash.$type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an associative array of filters which were tripped
|
|
|
|
*
|
|
|
|
* @protected Public for back compat only; this will actually be made protected in the future.
|
|
|
|
* You should either rely on $this->run() or subclass this class.
|
2020-10-13 12:22:17 +00:00
|
|
|
* @param bool|null &$hitCondLimit TEMPORARY
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @return bool[] Map of (integer filter ID => bool)
|
|
|
|
*/
|
2020-10-13 12:22:17 +00:00
|
|
|
public function checkAllFilters( &$hitCondLimit = false ) : array {
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
global $wgAbuseFilterCentralDB, $wgAbuseFilterIsCentral, $wgAbuseFilterConditionLimit;
|
|
|
|
|
|
|
|
// Ensure that we start fresh, see T193374
|
|
|
|
$this->parser->resetCondCount();
|
|
|
|
|
|
|
|
$matchedFilters = [];
|
2020-09-20 11:30:59 +00:00
|
|
|
foreach ( $this->filterLookup->getAllActiveFiltersInGroup( $this->group, false ) as $filter ) {
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchDimAssignment
|
|
|
|
$matchedFilters[$filter->getID()] = $this->checkFilter( $filter );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $wgAbuseFilterCentralDB && !$wgAbuseFilterIsCentral ) {
|
2020-09-20 11:30:59 +00:00
|
|
|
foreach ( $this->filterLookup->getAllActiveFiltersInGroup( $this->group, true ) as $filter ) {
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
|
|
|
|
$matchedFilters[ AbuseFilter::buildGlobalName( $filter->getID() ) ] =
|
|
|
|
$this->checkFilter( $filter, true );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:51:16 +00:00
|
|
|
// Tag the action if the condition limit was hit
|
2020-10-13 12:22:17 +00:00
|
|
|
$hitCondLimit = $this->parser->getCondCount() > $wgAbuseFilterConditionLimit;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
|
|
|
return $matchedFilters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the conditions of a single filter, and profile it if $this->executeMode is true
|
|
|
|
*
|
2020-09-20 11:30:59 +00:00
|
|
|
* @param Filter $filter
|
2019-01-30 19:30:59 +00:00
|
|
|
* @param bool $global
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2020-09-20 11:30:59 +00:00
|
|
|
protected function checkFilter( Filter $filter, $global = false ) {
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
|
|
|
|
$filterName = AbuseFilter::buildGlobalName( $filter->getID(), $global );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
|
|
|
$startConds = $this->parser->getCondCount();
|
|
|
|
$startTime = microtime( true );
|
2019-03-25 19:19:17 +00:00
|
|
|
$origExtraTime = AFComputedVariable::$profilingExtraTime;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-08-10 16:34:42 +00:00
|
|
|
$this->parser->setFilter( $filterName );
|
2020-09-20 11:30:59 +00:00
|
|
|
$result = $this->parser->checkConditions( $filter->getRules(), true, $filterName );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-03-25 19:19:17 +00:00
|
|
|
$actualExtra = AFComputedVariable::$profilingExtraTime - $origExtraTime;
|
|
|
|
$timeTaken = 1000 * ( microtime( true ) - $startTime - $actualExtra );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$condsUsed = $this->parser->getCondCount() - $startConds;
|
|
|
|
|
2019-01-30 19:30:59 +00:00
|
|
|
$this->profilingData[$filterName] = [
|
|
|
|
'time' => $timeTaken,
|
|
|
|
'conds' => $condsUsed,
|
|
|
|
'result' => $result
|
|
|
|
];
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-01-30 19:30:59 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-03-24 18:01:35 +00:00
|
|
|
/**
|
|
|
|
* @param array $result Result of the execution, as created in run()
|
|
|
|
* @param string[] $matchedFilters
|
|
|
|
* @param string[] $allFilters
|
|
|
|
*/
|
|
|
|
protected function profileExecution( array $result, array $matchedFilters, array $allFilters ) {
|
2020-09-20 22:31:41 +00:00
|
|
|
$this->filterProfiler->checkResetProfiling( $this->group, $allFilters );
|
|
|
|
$this->filterProfiler->recordRuntimeProfilingResult(
|
2019-09-29 09:02:29 +00:00
|
|
|
count( $allFilters ),
|
2019-03-24 18:01:35 +00:00
|
|
|
$result['condCount'],
|
|
|
|
$result['runtime']
|
|
|
|
);
|
2020-09-20 22:31:41 +00:00
|
|
|
$this->filterProfiler->recordPerFilterProfiling( $this->title, $result['profiling'] );
|
|
|
|
$this->filterProfiler->recordStats(
|
|
|
|
$this->group,
|
|
|
|
$result['condCount'],
|
|
|
|
$result['runtime'],
|
|
|
|
(bool)$matchedFilters
|
2015-04-01 04:51:16 +00:00
|
|
|
);
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a set of actions.
|
|
|
|
*
|
|
|
|
* @param string[] $filters
|
|
|
|
* @return Status returns the operation's status. $status->isOK() will return true if
|
|
|
|
* there were no actions taken, false otherwise. $status->getValue() will return
|
|
|
|
* an array listing the actions taken. $status->getErrors() etc. will provide
|
|
|
|
* the errors and warnings to be shown to the user to explain the actions.
|
|
|
|
*/
|
|
|
|
protected function executeFilterActions( array $filters ) : Status {
|
2019-03-17 15:43:19 +00:00
|
|
|
$actionsByFilter = AbuseFilter::getConsequencesForFilters( $filters );
|
2020-10-11 21:17:41 +00:00
|
|
|
$consequences = $this->replaceArraysWithConsequences( $actionsByFilter );
|
2020-10-23 14:56:21 +00:00
|
|
|
$actionsToTake = $this->getFilteredConsequences( $consequences );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$actionsTaken = array_fill_keys( $filters, [] );
|
|
|
|
|
|
|
|
$messages = [];
|
2018-09-26 11:38:51 +00:00
|
|
|
foreach ( $actionsToTake as $filter => $actions ) {
|
|
|
|
foreach ( $actions as $action => $info ) {
|
2020-10-11 21:17:41 +00:00
|
|
|
[ $executed, $newMsg ] = $this->takeConsequenceAction( $info );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2018-09-26 11:38:51 +00:00
|
|
|
if ( $newMsg !== null ) {
|
|
|
|
$messages[] = $newMsg;
|
|
|
|
}
|
2020-10-11 21:17:41 +00:00
|
|
|
if ( $executed ) {
|
2018-09-26 11:38:51 +00:00
|
|
|
$actionsTaken[$filter][] = $action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->buildStatus( $actionsTaken, $messages );
|
|
|
|
}
|
|
|
|
|
2020-10-23 14:56:21 +00:00
|
|
|
/**
|
|
|
|
* Remove consequences that we already know won't be executed. This includes:
|
|
|
|
* - Only keep the longest block from all filters
|
|
|
|
* - For global filters, remove locally disabled actions
|
|
|
|
* - For every filter, remove "disallow" if a blocking action will be executed
|
2020-10-11 21:17:41 +00:00
|
|
|
* Then, convert the remaining ones to Consequence objects.
|
2020-10-23 14:56:21 +00:00
|
|
|
*
|
|
|
|
* @param array[] $actionsByFilter
|
2020-10-11 21:17:41 +00:00
|
|
|
* @return Consequence[][]
|
2020-10-23 14:56:21 +00:00
|
|
|
* @internal Temporarily public
|
|
|
|
*/
|
2020-10-11 21:17:41 +00:00
|
|
|
public function replaceArraysWithConsequences( array $actionsByFilter ) : array {
|
2020-10-23 14:56:21 +00:00
|
|
|
global $wgAbuseFilterLocallyDisabledGlobalActions,
|
|
|
|
$wgAbuseFilterBlockDuration, $wgAbuseFilterAnonBlockDuration;
|
|
|
|
|
|
|
|
// Keep track of the longest block
|
|
|
|
$maxBlock = [ 'id' => null, 'expiry' => -1, 'blocktalk' => null ];
|
|
|
|
|
|
|
|
foreach ( $actionsByFilter as $filter => &$actions ) {
|
|
|
|
$isGlobalFilter = AbuseFilter::splitGlobalName( $filter )[1];
|
|
|
|
|
|
|
|
if ( $isGlobalFilter ) {
|
|
|
|
$actions = array_diff_key( $actions, array_filter( $wgAbuseFilterLocallyDisabledGlobalActions ) );
|
|
|
|
}
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2018-09-22 10:18:50 +00:00
|
|
|
// Don't show the disallow message if a blocking action is executed
|
2020-10-17 14:37:49 +00:00
|
|
|
if ( array_intersect( array_keys( $actions ), AbuseFilter::getDangerousActions() )
|
2020-10-17 13:09:33 +00:00
|
|
|
&& isset( $actions['disallow'] )
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
) {
|
|
|
|
unset( $actions['disallow'] );
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:17:41 +00:00
|
|
|
foreach ( $actions as $name => $parameters ) {
|
|
|
|
switch ( $name ) {
|
|
|
|
case 'throttle':
|
|
|
|
case 'warn':
|
|
|
|
case 'disallow':
|
|
|
|
case 'rangeblock':
|
|
|
|
case 'degroup':
|
|
|
|
case 'blockautopromote':
|
|
|
|
case 'tag':
|
|
|
|
$actions[$name] = $this->actionsParamsToConsequence( $name, $parameters, $filter );
|
|
|
|
break;
|
|
|
|
case 'block':
|
|
|
|
// TODO Move to a dedicated method and/or create a generic interface
|
|
|
|
if ( count( $parameters ) === 3 ) {
|
|
|
|
// New type of filters with custom block
|
|
|
|
if ( $this->user->isAnon() ) {
|
|
|
|
$expiry = $parameters[1];
|
|
|
|
} else {
|
|
|
|
$expiry = $parameters[2];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Old type with fixed expiry
|
|
|
|
if ( $this->user->isAnon() && $wgAbuseFilterAnonBlockDuration !== null ) {
|
|
|
|
// The user isn't logged in and the anon block duration
|
|
|
|
// doesn't default to $wgAbuseFilterBlockDuration.
|
|
|
|
$expiry = $wgAbuseFilterAnonBlockDuration;
|
|
|
|
} else {
|
|
|
|
$expiry = $wgAbuseFilterBlockDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedExpiry = SpecialBlock::parseExpiryInput( $expiry );
|
|
|
|
if (
|
|
|
|
$maxBlock['expiry'] === -1 ||
|
|
|
|
$parsedExpiry > SpecialBlock::parseExpiryInput( $maxBlock['expiry'] )
|
|
|
|
) {
|
|
|
|
// Save the parameters to issue the block with
|
|
|
|
$maxBlock = [
|
|
|
|
'id' => $filter,
|
|
|
|
'expiry' => $expiry,
|
|
|
|
'blocktalk' => is_array( $parameters ) && in_array( 'blocktalk', $parameters )
|
|
|
|
];
|
|
|
|
}
|
|
|
|
// We'll re-add it later
|
|
|
|
unset( $actions['block'] );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$cons = $this->actionsParamsToConsequence( $name, $parameters, $filter );
|
|
|
|
if ( $cons !== null ) {
|
|
|
|
$actions[$name] = $cons;
|
|
|
|
} else {
|
|
|
|
unset( $actions[$name] );
|
|
|
|
}
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 11:38:51 +00:00
|
|
|
unset( $actions );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2018-09-26 11:38:51 +00:00
|
|
|
if ( $maxBlock['id'] !== null ) {
|
|
|
|
$id = $maxBlock['id'];
|
|
|
|
unset( $maxBlock['id'] );
|
2020-10-11 21:17:41 +00:00
|
|
|
$actionsByFilter[$id]['block'] = $this->actionsParamsToConsequence( 'block', $maxBlock, $id );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 11:38:51 +00:00
|
|
|
return $actionsByFilter;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-11 21:17:41 +00:00
|
|
|
* @param string $actionName
|
|
|
|
* @param array $rawParams
|
|
|
|
* @param int|string $filter
|
|
|
|
* @return Consequence|null
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
*/
|
2020-10-11 21:17:41 +00:00
|
|
|
private function actionsParamsToConsequence( string $actionName, array $rawParams, $filter ) : ?Consequence {
|
|
|
|
global $wgAbuseFilterBlockAutopromoteDuration, $wgAbuseFilterCustomActionsHandlers;
|
|
|
|
[ $filterID, $isGlobalFilter ] = AbuseFilter::splitGlobalName( $filter );
|
|
|
|
$filterObj = $this->filterLookup->getFilter( $filterID, $isGlobalFilter );
|
|
|
|
$consFactory = AbuseFilterServices::getConsequencesFactory();
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2020-10-11 21:17:41 +00:00
|
|
|
$baseConsParams = new Parameters(
|
|
|
|
$filterObj,
|
|
|
|
$isGlobalFilter,
|
|
|
|
$this->user,
|
|
|
|
$this->title,
|
|
|
|
$this->action
|
2020-10-17 13:24:08 +00:00
|
|
|
);
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2020-10-11 21:17:41 +00:00
|
|
|
switch ( $actionName ) {
|
2018-09-26 11:38:51 +00:00
|
|
|
case 'throttle':
|
2020-10-11 21:17:41 +00:00
|
|
|
$throttleId = array_shift( $rawParams );
|
|
|
|
list( $rateCount, $ratePeriod ) = explode( ',', array_shift( $rawParams ) );
|
2018-09-26 11:38:51 +00:00
|
|
|
|
2020-10-11 21:17:41 +00:00
|
|
|
$throttleParams = [
|
|
|
|
'id' => $throttleId,
|
|
|
|
'count' => (int)$rateCount,
|
|
|
|
'period' => (int)$ratePeriod,
|
|
|
|
'groups' => $rawParams,
|
|
|
|
'global' => $isGlobalFilter
|
|
|
|
];
|
|
|
|
return $consFactory->newThrottle( $baseConsParams, $throttleParams );
|
|
|
|
case 'warn':
|
|
|
|
return $consFactory->newWarn( $baseConsParams, $rawParams[0] ?? 'abusefilter-warning' );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
case 'disallow':
|
2020-10-11 21:17:41 +00:00
|
|
|
return $consFactory->newDisallow( $baseConsParams, $rawParams[0] ?? 'abusefilter-disallowed' );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
case 'rangeblock':
|
2020-10-11 21:17:41 +00:00
|
|
|
return $consFactory->newRangeBlock( $baseConsParams, '1 week' );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
case 'degroup':
|
2020-10-11 21:17:41 +00:00
|
|
|
return $consFactory->newDegroup( $baseConsParams, $this->vars );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
case 'blockautopromote':
|
2020-10-11 21:17:41 +00:00
|
|
|
$duration = $wgAbuseFilterBlockAutopromoteDuration * 86400;
|
|
|
|
return $consFactory->newBlockAutopromote( $baseConsParams, $duration );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
case 'block':
|
2020-10-11 21:17:41 +00:00
|
|
|
return $consFactory->newBlock( $baseConsParams, $rawParams['expiry'], $rawParams['blocktalk'] );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
case 'tag':
|
2020-10-11 21:17:41 +00:00
|
|
|
$accountName = $this->vars->getVar( 'accountname', AbuseFilterVariableHolder::GET_BC )->toNative();
|
|
|
|
return $consFactory->newTag( $baseConsParams, $accountName, $rawParams );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
default:
|
2020-10-11 21:17:41 +00:00
|
|
|
if ( isset( $wgAbuseFilterCustomActionsHandlers[$actionName] ) ) {
|
|
|
|
$customFunction = $wgAbuseFilterCustomActionsHandlers[$actionName];
|
|
|
|
return new BCConsequence( $baseConsParams, $rawParams, $this->vars, $customFunction );
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
} else {
|
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
2020-10-11 21:17:41 +00:00
|
|
|
$logger->warning( "Unrecognised action $actionName" );
|
|
|
|
return null;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 10:26:09 +00:00
|
|
|
/**
|
2020-10-11 21:17:41 +00:00
|
|
|
* Pre-check any "special" consequence and remove any further actions prevented by them. Specifically:
|
|
|
|
* should be actually executed. Normalizations done here:
|
|
|
|
* - For every filter with "throttle" enabled, remove other actions if the throttle counter hasn't been reached
|
|
|
|
* - For every filter with "warn" enabled, remove other actions if the warning hasn't been shown
|
|
|
|
*
|
|
|
|
* @param Consequence[][] $actionsByFilter
|
|
|
|
* @return Consequence[][]
|
|
|
|
* @internal Temporary method
|
2019-09-30 10:26:09 +00:00
|
|
|
*/
|
2020-10-11 21:17:41 +00:00
|
|
|
public function getFilteredConsequences( array $actionsByFilter ) : array {
|
|
|
|
foreach ( $actionsByFilter as $filter => $actions ) {
|
|
|
|
/** @var ConsequencesDisablerConsequence[] $consequenceDisablers */
|
|
|
|
$consequenceDisablers = array_filter( $actions, function ( $el ) {
|
|
|
|
return $el instanceof ConsequencesDisablerConsequence;
|
|
|
|
} );
|
|
|
|
'@phan-var ConsequencesDisablerConsequence[] $consequenceDisablers';
|
|
|
|
uasort(
|
|
|
|
$consequenceDisablers,
|
|
|
|
function ( ConsequencesDisablerConsequence $x, ConsequencesDisablerConsequence $y ) {
|
|
|
|
return $x->getSort() - $y->getSort();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
foreach ( $consequenceDisablers as $name => $consequence ) {
|
|
|
|
if ( $consequence->shouldDisableOtherConsequences() ) {
|
|
|
|
$actionsByFilter[$filter] = [ $name => $consequence ];
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 10:26:09 +00:00
|
|
|
|
2020-10-11 21:17:41 +00:00
|
|
|
return $actionsByFilter;
|
2019-09-30 10:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-11 21:17:41 +00:00
|
|
|
* @param Consequence $consequence
|
|
|
|
* @return array [ Executed (bool), Message (?array) ] The message is given as an array
|
|
|
|
* containing the message key followed by any message parameters.
|
|
|
|
* @todo Improve return value
|
|
|
|
*/
|
|
|
|
protected function takeConsequenceAction( Consequence $consequence ) : array {
|
|
|
|
// Special case
|
|
|
|
if ( $consequence instanceof BCConsequence ) {
|
|
|
|
$consequence->execute();
|
|
|
|
try {
|
|
|
|
$message = $consequence->getMessage();
|
|
|
|
} catch ( LogicException $_ ) {
|
|
|
|
// Swallow. Sigh.
|
|
|
|
$message = null;
|
|
|
|
}
|
|
|
|
return [ true, $message ];
|
|
|
|
}
|
2019-09-30 10:26:09 +00:00
|
|
|
|
2020-10-11 21:17:41 +00:00
|
|
|
$res = $consequence->execute();
|
|
|
|
if ( $res && $consequence instanceof HookAborterConsequence ) {
|
|
|
|
$message = $consequence->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ $res, $message ?? null ];
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a Status object as returned by executeFilterActions() from the list of
|
|
|
|
* actions taken and the corresponding list of messages.
|
|
|
|
*
|
|
|
|
* @param array[] $actionsTaken associative array mapping each filter to the list if
|
|
|
|
* actions taken because of that filter.
|
|
|
|
* @param array[] $messages a list of arrays, where each array contains a message key
|
|
|
|
* followed by any message parameters.
|
|
|
|
*
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
protected function buildStatus( array $actionsTaken, array $messages ) : Status {
|
|
|
|
$status = Status::newGood( $actionsTaken );
|
|
|
|
|
|
|
|
foreach ( $messages as $msg ) {
|
|
|
|
$status->fatal( ...$msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a template to use for logging taken actions
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function buildLogTemplate() : array {
|
|
|
|
global $wgAbuseFilterLogIP;
|
|
|
|
|
|
|
|
$request = RequestContext::getMain()->getRequest();
|
|
|
|
// If $this->user isn't safe to load (e.g. a failure during
|
|
|
|
// AbortAutoAccount), create a dummy anonymous user instead.
|
|
|
|
$user = $this->user->isSafeToLoad() ? $this->user : new User;
|
|
|
|
// Create a template
|
|
|
|
$logTemplate = [
|
|
|
|
'afl_user' => $user->getId(),
|
|
|
|
'afl_user_text' => $user->getName(),
|
|
|
|
'afl_timestamp' => wfGetDB( DB_REPLICA )->timestamp(),
|
|
|
|
'afl_namespace' => $this->title->getNamespace(),
|
|
|
|
'afl_title' => $this->title->getDBkey(),
|
2020-03-06 16:21:51 +00:00
|
|
|
'afl_action' => $this->action,
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
'afl_ip' => $wgAbuseFilterLogIP ? $request->getIP() : ''
|
|
|
|
];
|
|
|
|
// Hack to avoid revealing IPs of people creating accounts
|
2020-03-06 16:21:51 +00:00
|
|
|
if (
|
|
|
|
!$user->getId() &&
|
|
|
|
( $this->action === 'createaccount' || $this->action === 'autocreateaccount' )
|
|
|
|
) {
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$logTemplate['afl_user_text'] = $this->vars->getVar( 'accountname' )->toString();
|
|
|
|
}
|
|
|
|
return $logTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and publish log entries for taken actions
|
|
|
|
*
|
|
|
|
* @param array[] $actionsTaken
|
|
|
|
*/
|
|
|
|
protected function addLogEntries( array $actionsTaken ) {
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
$logTemplate = $this->buildLogTemplate();
|
|
|
|
$centralLogTemplate = [
|
2020-01-08 13:33:10 +00:00
|
|
|
'afl_wiki' => WikiMap::getCurrentWikiDbDomain()->getId(),
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$logRows = [];
|
|
|
|
$centralLogRows = [];
|
|
|
|
$loggedLocalFilters = [];
|
|
|
|
$loggedGlobalFilters = [];
|
|
|
|
|
|
|
|
foreach ( $actionsTaken as $filter => $actions ) {
|
|
|
|
list( $filterID, $global ) = AbuseFilter::splitGlobalName( $filter );
|
|
|
|
$thisLog = $logTemplate;
|
|
|
|
$thisLog['afl_filter'] = $filter;
|
|
|
|
$thisLog['afl_actions'] = implode( ',', $actions );
|
|
|
|
|
|
|
|
// Don't log if we were only throttling.
|
2020-10-11 21:17:41 +00:00
|
|
|
// TODO This check should be removed or rewritten using Consequence objects
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
if ( $thisLog['afl_actions'] !== 'throttle' ) {
|
|
|
|
$logRows[] = $thisLog;
|
|
|
|
// Global logging
|
|
|
|
if ( $global ) {
|
|
|
|
$centralLog = $thisLog + $centralLogTemplate;
|
|
|
|
$centralLog['afl_filter'] = $filterID;
|
|
|
|
$centralLog['afl_title'] = $this->title->getPrefixedText();
|
|
|
|
$centralLog['afl_namespace'] = 0;
|
|
|
|
|
|
|
|
$centralLogRows[] = $centralLog;
|
|
|
|
$loggedGlobalFilters[] = $filterID;
|
|
|
|
} else {
|
|
|
|
$loggedLocalFilters[] = $filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !count( $logRows ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-28 13:44:06 +00:00
|
|
|
$localLogIDs = $this->insertLocalLogEntries( $logRows, $dbw );
|
|
|
|
if ( count( $loggedLocalFilters ) ) {
|
|
|
|
$this->updateHitCounts( $dbw, $loggedLocalFilters );
|
|
|
|
}
|
|
|
|
|
|
|
|
$globalLogIDs = [];
|
|
|
|
if ( count( $loggedGlobalFilters ) ) {
|
|
|
|
$fdb = AbuseFilterServices::getCentralDBManager()->getConnection( DB_MASTER );
|
|
|
|
$globalLogIDs = $this->insertGlobalLogEntries( $centralLogRows, $fdb );
|
|
|
|
$this->updateHitCounts( $fdb, $loggedGlobalFilters );
|
|
|
|
}
|
|
|
|
|
|
|
|
AbuseFilter::$logIds[ $this->title->getPrefixedText() ] = [
|
|
|
|
'local' => $localLogIDs,
|
|
|
|
'global' => $globalLogIDs
|
|
|
|
];
|
|
|
|
|
2020-11-19 15:32:39 +00:00
|
|
|
foreach ( $this->watchers as $watcher ) {
|
|
|
|
$watcher->run( $loggedLocalFilters, $this->group );
|
|
|
|
}
|
2019-09-28 13:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array[] $logRows
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function insertLocalLogEntries( array $logRows, IDatabase $dbw ) {
|
|
|
|
global $wgAbuseFilterNotifications, $wgAbuseFilterNotificationsPrivate;
|
|
|
|
|
2020-10-17 08:27:02 +00:00
|
|
|
$lookup = AbuseFilterServices::getFilterLookup();
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$varDump = AbuseFilter::storeVarDump( $this->vars );
|
2019-01-06 14:20:10 +00:00
|
|
|
$varDump = "tt:$varDump";
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-09-28 13:44:06 +00:00
|
|
|
$loggedIDs = [];
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
foreach ( $logRows as $data ) {
|
|
|
|
$data['afl_var_dump'] = $varDump;
|
|
|
|
$dbw->insert( 'abuse_filter_log', $data, __METHOD__ );
|
2019-09-28 13:44:06 +00:00
|
|
|
$loggedIDs[] = $data['afl_id'] = $dbw->insertId();
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// logentry-abusefilter-hit
|
|
|
|
$entry = new ManualLogEntry( 'abusefilter', 'hit' );
|
|
|
|
// Construct a user object
|
|
|
|
$user = User::newFromId( $data['afl_user'] );
|
|
|
|
$user->setName( $data['afl_user_text'] );
|
|
|
|
$entry->setPerformer( $user );
|
|
|
|
$entry->setTarget( $this->title );
|
|
|
|
// Additional info
|
|
|
|
$entry->setParameters( [
|
|
|
|
'action' => $data['afl_action'],
|
|
|
|
'filter' => $data['afl_filter'],
|
|
|
|
'actions' => $data['afl_actions'],
|
|
|
|
'log' => $data['afl_id'],
|
|
|
|
] );
|
|
|
|
|
|
|
|
// Send data to CheckUser if installed and we
|
|
|
|
// aren't already sending a notification to recentchanges
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'CheckUser' )
|
|
|
|
&& strpos( $wgAbuseFilterNotifications, 'rc' ) === false
|
|
|
|
) {
|
2020-08-13 12:15:59 +00:00
|
|
|
global $wgCheckUserLogAdditionalRights;
|
|
|
|
$wgCheckUserLogAdditionalRights[] = 'abusefilter-view';
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
$rc = $entry->getRecentChange();
|
|
|
|
CheckUserHooks::updateCheckUserData( $rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $wgAbuseFilterNotifications !== false ) {
|
|
|
|
list( $filterID, $global ) = AbuseFilter::splitGlobalName( $data['afl_filter'] );
|
2020-10-17 08:27:02 +00:00
|
|
|
if ( $lookup->getFilter( $filterID, $global )->isHidden() && !$wgAbuseFilterNotificationsPrivate ) {
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->publishEntry( $dbw, $entry, $wgAbuseFilterNotifications );
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 13:44:06 +00:00
|
|
|
return $loggedIDs;
|
|
|
|
}
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-09-28 13:44:06 +00:00
|
|
|
/**
|
|
|
|
* @param array[] $centralLogRows
|
|
|
|
* @param IDatabase $fdb
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function insertGlobalLogEntries( array $centralLogRows, IDatabase $fdb ) {
|
|
|
|
$this->vars->computeDBVars();
|
|
|
|
$globalVarDump = AbuseFilter::storeVarDump( $this->vars, true );
|
|
|
|
$globalVarDump = "tt:$globalVarDump";
|
|
|
|
foreach ( $centralLogRows as $index => $data ) {
|
|
|
|
$centralLogRows[$index]['afl_var_dump'] = $globalVarDump;
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 13:44:06 +00:00
|
|
|
$loggedIDs = [];
|
|
|
|
foreach ( $centralLogRows as $row ) {
|
|
|
|
$fdb->insert( 'abuse_filter_log', $row, __METHOD__ );
|
|
|
|
$loggedIDs[] = $fdb->insertId();
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
2019-09-28 13:44:06 +00:00
|
|
|
return $loggedIDs;
|
|
|
|
}
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
|
2019-09-28 13:44:06 +00:00
|
|
|
/**
|
|
|
|
* @param IDatabase $dbw
|
|
|
|
* @param array $loggedFilters
|
|
|
|
*/
|
|
|
|
private function updateHitCounts( IDatabase $dbw, $loggedFilters ) {
|
|
|
|
$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
|
|
|
|
);
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-02 20:53:51 +00:00
|
|
|
* Like ManualLogEntry::publish, but doesn't require an ID (which we don't have) and skips the
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
* tagging part
|
|
|
|
*
|
|
|
|
* @param IDatabase $dbw To cancel the callback if the log insertion fails
|
|
|
|
* @param ManualLogEntry $entry
|
|
|
|
* @param string $to One of 'udp', 'rc' and 'rcandudp'
|
|
|
|
*/
|
|
|
|
private function publishEntry( IDatabase $dbw, ManualLogEntry $entry, $to ) {
|
|
|
|
DeferredUpdates::addCallableUpdate(
|
|
|
|
function () use ( $entry, $to ) {
|
|
|
|
$rc = $entry->getRecentChange();
|
|
|
|
|
|
|
|
if ( $to === 'rc' || $to === 'rcandudp' ) {
|
|
|
|
$rc->save( $rc::SEND_NONE );
|
|
|
|
}
|
|
|
|
if ( $to === 'udp' || $to === 'rcandudp' ) {
|
|
|
|
$rc->notifyRCFeeds();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DeferredUpdates::POSTSEND,
|
|
|
|
$dbw
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-13 16:01:18 +00:00
|
|
|
* @return array
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
*/
|
2020-10-13 16:01:18 +00:00
|
|
|
private function getSpecsForTagger() : array {
|
|
|
|
return [
|
|
|
|
'action' => $this->action,
|
|
|
|
'username' => $this->user->getName(),
|
|
|
|
'target' => $this->title,
|
|
|
|
'accountname' => $this->vars->getVar(
|
|
|
|
'accountname',
|
|
|
|
AbuseFilterVariableHolder::GET_BC
|
|
|
|
)->toNative()
|
|
|
|
];
|
Add a new class for methods related to running filters
Currently we strongly abuse (pardon the pun) the AbuseFilter class: its
purpose should be to hold static functions intended as generic utility
functions (e.g. to format messages, determine whether a filter is global
etc.), but we actually use it for all methods related to running filters.
This patch creates a new class, AbuseFilterRunner, containing all such
methods, which have been made non-static. This leads to several
improvements (also for related methods and the parser), and opens the
way to further improve the code.
Aside from making the code prettier, less global and easier to test,
this patch could also produce a performance improvement, although I
don't have tools to measure that.
Also note that many public methods have been removed, and almost any of
them has been made protected; a couple of them (the ones used from outside)
are left for back-compat, and will be removed in the future.
Change-Id: I2eab2e50356eeb5224446ee2d0df9c787ae95b80
2018-12-07 17:46:02 +00:00
|
|
|
}
|
|
|
|
}
|