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-11-27 14:49:41 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2021-01-06 15:40:23 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2020-11-26 15:23:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\ChangeTags\ChangeTagger;
|
2020-12-18 14:05:33 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesExecutorFactory;
|
2021-01-07 20:10:52 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter;
|
2020-06-03 00:43:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner;
|
2021-08-31 22:26:13 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\FilterEvaluator;
|
2021-09-01 11:53:38 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerFactory;
|
2021-01-03 13:10:20 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\VariableGenerator\VariableGeneratorFactory;
|
2021-01-02 13:41:31 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\LazyVariableComputer;
|
2021-01-02 14:01:00 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
2021-01-02 13:41:31 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager;
|
2020-11-19 15:32:39 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Watcher\Watcher;
|
2023-08-25 15:02:33 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2023-08-19 17:49:36 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\User\User;
|
2020-11-27 14:49:41 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
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 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.
|
2020-11-27 14:49:41 +00:00
|
|
|
* @internal Not stable yet
|
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-27 14:49:41 +00:00
|
|
|
class FilterRunner {
|
2021-01-06 15:40:23 +00:00
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'AbuseFilterValidGroups',
|
|
|
|
'AbuseFilterCentralDB',
|
|
|
|
'AbuseFilterIsCentral',
|
|
|
|
'AbuseFilterConditionLimit',
|
|
|
|
];
|
|
|
|
|
2020-11-27 14:49:41 +00:00
|
|
|
/** @var AbuseFilterHookRunner */
|
|
|
|
private $hookRunner;
|
|
|
|
/** @var FilterProfiler */
|
|
|
|
private $filterProfiler;
|
|
|
|
/** @var ChangeTagger */
|
|
|
|
private $changeTagger;
|
|
|
|
/** @var FilterLookup */
|
|
|
|
private $filterLookup;
|
2021-09-01 11:53:38 +00:00
|
|
|
/** @var RuleCheckerFactory */
|
|
|
|
private $ruleCheckerFactory;
|
2020-11-27 14:49:41 +00:00
|
|
|
/** @var ConsequencesExecutorFactory */
|
|
|
|
private $consExecutorFactory;
|
|
|
|
/** @var AbuseLoggerFactory */
|
|
|
|
private $abuseLoggerFactory;
|
2021-02-10 13:13:03 +00:00
|
|
|
/** @var EmergencyCache */
|
|
|
|
private $emergencyCache;
|
2020-11-27 14:49:41 +00:00
|
|
|
/** @var Watcher[] */
|
|
|
|
private $watchers;
|
2020-12-21 14:51:31 +00:00
|
|
|
/** @var EditStashCache */
|
|
|
|
private $stashCache;
|
2020-11-27 14:49:41 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
2020-10-18 22:25:05 +00:00
|
|
|
/** @var VariablesManager */
|
|
|
|
private $varManager;
|
2021-01-03 13:10:20 +00:00
|
|
|
/** @var VariableGeneratorFactory */
|
|
|
|
private $varGeneratorFactory;
|
2021-01-06 15:40:23 +00:00
|
|
|
/** @var ServiceOptions */
|
|
|
|
private $options;
|
2020-11-27 14:49:41 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-31 22:26:13 +00:00
|
|
|
* @var FilterEvaluator
|
2020-11-27 14:49:41 +00:00
|
|
|
*/
|
2021-09-01 11:53:38 +00:00
|
|
|
private $ruleChecker;
|
2020-11-27 14:49:41 +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
|
|
|
/**
|
|
|
|
* @var User The user who performed the action being filtered
|
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private $user;
|
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 Title The title where the action being filtered was performed
|
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private $title;
|
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
|
|
|
/**
|
2021-01-02 14:01:00 +00:00
|
|
|
* @var VariableHolder The variables for the current 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
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private $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
|
|
|
/**
|
|
|
|
* @var string The group of filters to check (as defined in $wgAbuseFilterValidGroups)
|
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private $group;
|
2020-03-06 16:21:51 +00:00
|
|
|
/**
|
|
|
|
* @var string The action we're filtering
|
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private $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
|
|
|
/**
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param AbuseFilterHookRunner $hookRunner
|
|
|
|
* @param FilterProfiler $filterProfiler
|
|
|
|
* @param ChangeTagger $changeTagger
|
|
|
|
* @param FilterLookup $filterLookup
|
2021-09-01 11:53:38 +00:00
|
|
|
* @param RuleCheckerFactory $ruleCheckerFactory
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param ConsequencesExecutorFactory $consExecutorFactory
|
|
|
|
* @param AbuseLoggerFactory $abuseLoggerFactory
|
2020-10-18 22:25:05 +00:00
|
|
|
* @param VariablesManager $varManager
|
2021-01-03 13:10:20 +00:00
|
|
|
* @param VariableGeneratorFactory $varGeneratorFactory
|
2021-02-10 13:13:03 +00:00
|
|
|
* @param EmergencyCache $emergencyCache
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param Watcher[] $watchers
|
2020-12-21 14:51:31 +00:00
|
|
|
* @param EditStashCache $stashCache
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param LoggerInterface $logger
|
2021-01-06 15:40:23 +00:00
|
|
|
* @param ServiceOptions $options
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param Title $title
|
2021-01-02 14:01:00 +00:00
|
|
|
* @param VariableHolder $vars
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param string $group
|
|
|
|
* @throws InvalidArgumentException If $group is invalid or the 'action' variable is unset
|
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-27 14:49:41 +00:00
|
|
|
public function __construct(
|
|
|
|
AbuseFilterHookRunner $hookRunner,
|
|
|
|
FilterProfiler $filterProfiler,
|
|
|
|
ChangeTagger $changeTagger,
|
|
|
|
FilterLookup $filterLookup,
|
2021-09-01 11:53:38 +00:00
|
|
|
RuleCheckerFactory $ruleCheckerFactory,
|
2020-11-27 14:49:41 +00:00
|
|
|
ConsequencesExecutorFactory $consExecutorFactory,
|
|
|
|
AbuseLoggerFactory $abuseLoggerFactory,
|
2020-10-18 22:25:05 +00:00
|
|
|
VariablesManager $varManager,
|
2021-01-03 13:10:20 +00:00
|
|
|
VariableGeneratorFactory $varGeneratorFactory,
|
2021-02-10 13:13:03 +00:00
|
|
|
EmergencyCache $emergencyCache,
|
2020-11-27 14:49:41 +00:00
|
|
|
array $watchers,
|
2020-12-21 14:51:31 +00:00
|
|
|
EditStashCache $stashCache,
|
2020-11-27 14:49:41 +00:00
|
|
|
LoggerInterface $logger,
|
2021-01-06 15:40:23 +00:00
|
|
|
ServiceOptions $options,
|
2020-11-27 14:49:41 +00:00
|
|
|
User $user,
|
|
|
|
Title $title,
|
2021-01-02 14:01:00 +00:00
|
|
|
VariableHolder $vars,
|
2020-11-27 14:49:41 +00:00
|
|
|
string $group
|
|
|
|
) {
|
|
|
|
$this->hookRunner = $hookRunner;
|
|
|
|
$this->filterProfiler = $filterProfiler;
|
|
|
|
$this->changeTagger = $changeTagger;
|
|
|
|
$this->filterLookup = $filterLookup;
|
2021-09-01 11:53:38 +00:00
|
|
|
$this->ruleCheckerFactory = $ruleCheckerFactory;
|
2020-11-27 14:49:41 +00:00
|
|
|
$this->consExecutorFactory = $consExecutorFactory;
|
|
|
|
$this->abuseLoggerFactory = $abuseLoggerFactory;
|
2021-01-03 13:10:20 +00:00
|
|
|
$this->varManager = $varManager;
|
|
|
|
$this->varGeneratorFactory = $varGeneratorFactory;
|
2021-02-10 13:13:03 +00:00
|
|
|
$this->emergencyCache = $emergencyCache;
|
2020-11-27 14:49:41 +00:00
|
|
|
$this->watchers = $watchers;
|
2020-12-21 14:51:31 +00:00
|
|
|
$this->stashCache = $stashCache;
|
2020-11-27 14:49:41 +00:00
|
|
|
$this->logger = $logger;
|
|
|
|
|
2021-01-06 15:40:23 +00:00
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
if ( !in_array( $group, $options->get( 'AbuseFilterValidGroups' ), true ) ) {
|
2020-11-27 14:49:41 +00:00
|
|
|
throw new InvalidArgumentException( "Group $group is not a valid group" );
|
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
|
|
|
}
|
2021-01-06 15:40:23 +00:00
|
|
|
$this->options = $options;
|
2023-05-30 12:32:48 +00:00
|
|
|
|
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;
|
|
|
|
$this->group = $group;
|
2020-10-18 22:25:05 +00:00
|
|
|
$this->action = $vars->getComputedVariable( 'action' )->toString();
|
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
|
2021-03-06 17:18:07 +00:00
|
|
|
$this->hookRunner->onAbuseFilter_filterAction(
|
2020-06-03 00:43:22 +00:00
|
|
|
$this->vars,
|
|
|
|
$this->title
|
|
|
|
);
|
|
|
|
$this->hookRunner->onAbuseFilterAlterVariables(
|
|
|
|
$this->vars,
|
|
|
|
$this->title,
|
|
|
|
$this->user
|
|
|
|
);
|
2021-01-03 13:10:20 +00:00
|
|
|
$generator = $this->varGeneratorFactory->newGenerator( $this->vars );
|
2019-12-17 15:06:44 +00:00
|
|
|
$this->vars = $generator->addGenericVars()->getVariableHolder();
|
2021-09-01 11:53:38 +00:00
|
|
|
$this->ruleChecker = $this->ruleCheckerFactory->newRuleChecker( $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.
|
|
|
|
* @return Status Good if no action has been taken, a fatal otherwise.
|
|
|
|
*/
|
2020-11-27 14:49:41 +00:00
|
|
|
public function run( $allowStash = true ): Status {
|
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 ) {
|
2020-11-27 14:49:41 +00:00
|
|
|
$this->logger->info(
|
2020-03-06 16:21:51 +00:00
|
|
|
'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
|
|
|
|
2021-02-09 11:03:24 +00:00
|
|
|
$runnerData = 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
|
|
|
if ( $useStash ) {
|
2020-12-21 14:51:31 +00:00
|
|
|
$cacheData = $this->stashCache->seek( $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
|
|
|
if ( $cacheData !== false ) {
|
2019-01-30 19:30:59 +00:00
|
|
|
// Use cached vars (T176291) and profiling data (T191430)
|
2021-01-02 14:01:00 +00:00
|
|
|
$this->vars = VariableHolder::newFromArray( $cacheData['vars'] );
|
2021-02-09 11:03:24 +00:00
|
|
|
$runnerData = RunnerData::fromArray( $cacheData['data'] );
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-12 08:27:03 +00:00
|
|
|
$runnerData ??= $this->checkAllFiltersInternal();
|
2021-02-09 11:03:24 +00:00
|
|
|
|
2023-08-18 20:47:19 +00:00
|
|
|
DeferredUpdates::addCallableUpdate( function () use ( $runnerData ) {
|
2021-02-28 10:56:49 +00:00
|
|
|
$this->profileExecution( $runnerData );
|
2021-02-10 13:13:03 +00:00
|
|
|
$this->updateEmergencyCache( $runnerData->getMatchesMap() );
|
2023-05-30 12:32:48 +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
|
|
|
|
2022-05-28 12:03:28 +00:00
|
|
|
// TODO: inject the action specifier to avoid this
|
|
|
|
$accountname = $this->varManager->getVar(
|
|
|
|
$this->vars,
|
|
|
|
'accountname',
|
|
|
|
VariablesManager::GET_BC
|
|
|
|
)->toNative();
|
2022-07-10 08:19:41 +00:00
|
|
|
$spec = new ActionSpecifier(
|
|
|
|
$this->action,
|
|
|
|
$this->title,
|
|
|
|
$this->user,
|
|
|
|
$this->user->getRequest()->getIP(),
|
|
|
|
$accountname
|
|
|
|
);
|
2022-05-28 12:03:28 +00:00
|
|
|
|
2021-02-08 10:51:40 +00:00
|
|
|
// Tag the action if the condition limit was hit
|
2021-02-09 13:36:02 +00:00
|
|
|
if ( $runnerData->getTotalConditions() > $this->options->get( 'AbuseFilterConditionLimit' ) ) {
|
2021-02-28 15:31:24 +00:00
|
|
|
$this->changeTagger->addConditionsLimitTag( $spec );
|
2020-10-13 12:22:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 13:36:02 +00:00
|
|
|
$matchedFilters = $runnerData->getMatchedFilters();
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-05-28 12:03:28 +00:00
|
|
|
$executor = $this->consExecutorFactory->newExecutor( $spec, $this->vars );
|
2020-10-16 22:10:37 +00:00
|
|
|
$status = $executor->executeFilterActions( $matchedFilters );
|
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 = $status->getValue();
|
|
|
|
|
2020-11-27 14:49:41 +00:00
|
|
|
// Note, it's important that we create an AbuseLogger now, after all lazy-loaded variables
|
|
|
|
// requested by active filters have been computed
|
|
|
|
$abuseLogger = $this->abuseLoggerFactory->newLogger( $this->title, $this->user, $this->vars );
|
2020-10-24 11:58:46 +00:00
|
|
|
[
|
|
|
|
'local' => $loggedLocalFilters,
|
|
|
|
'global' => $loggedGlobalFilters
|
|
|
|
] = $abuseLogger->addLogEntries( $actionsTaken );
|
|
|
|
|
|
|
|
foreach ( $this->watchers as $watcher ) {
|
2020-11-28 23:53:42 +00:00
|
|
|
$watcher->run( $loggedLocalFilters, $loggedGlobalFilters, $this->group );
|
2020-10-24 11:58:46 +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
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:51:31 +00:00
|
|
|
// XXX: We need a copy here because the cache key is computed
|
|
|
|
// from the variables, but some variables can be loaded lazily
|
|
|
|
// which would store the data with a key distinct from that
|
|
|
|
// computed by seek() in ::run().
|
|
|
|
// TODO: Find better way to generate the cache key.
|
|
|
|
$origVars = clone $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
|
|
|
|
2021-02-08 10:51:40 +00:00
|
|
|
$runnerData = $this->checkAllFiltersInternal();
|
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 = [
|
2020-10-18 22:25:05 +00:00
|
|
|
'vars' => $this->varManager->dumpAllVars( $this->vars ),
|
2021-02-09 11:03:24 +00:00
|
|
|
'data' => $runnerData->toArray(),
|
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-12-21 14:51:31 +00:00
|
|
|
$this->stashCache->store( $origVars, $cacheData );
|
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::newGood();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-07 14:45:57 +00:00
|
|
|
* Run all filters and return information about matches and 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
|
|
|
*
|
2021-02-07 14:45:57 +00:00
|
|
|
* @return RunnerData
|
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
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private function checkAllFiltersInternal(): RunnerData {
|
2021-02-07 14:45:57 +00:00
|
|
|
// Ensure there's no extra time leftover
|
|
|
|
LazyVariableComputer::$profilingExtraTime = 0;
|
|
|
|
|
|
|
|
$data = new RunnerData();
|
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-09-20 11:30:59 +00:00
|
|
|
foreach ( $this->filterLookup->getAllActiveFiltersInGroup( $this->group, false ) as $filter ) {
|
2021-09-01 11:18:23 +00:00
|
|
|
[ $status, $timeTaken ] = $this->checkFilter( $filter );
|
|
|
|
$data->record( $filter->getID(), false, $status, $timeTaken );
|
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
|
|
|
}
|
|
|
|
|
2021-01-06 15:40:23 +00:00
|
|
|
if ( $this->options->get( 'AbuseFilterCentralDB' ) && !$this->options->get( 'AbuseFilterIsCentral' ) ) {
|
2020-09-20 11:30:59 +00:00
|
|
|
foreach ( $this->filterLookup->getAllActiveFiltersInGroup( $this->group, true ) as $filter ) {
|
2021-09-01 11:18:23 +00:00
|
|
|
[ $status, $timeTaken ] = $this->checkFilter( $filter, true );
|
|
|
|
$data->record( $filter->getID(), true, $status, $timeTaken );
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 14:45:57 +00:00
|
|
|
return $data;
|
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
|
|
|
}
|
|
|
|
|
2021-02-07 13:49:24 +00:00
|
|
|
/**
|
|
|
|
* Returns an associative array of filters which were tripped
|
|
|
|
*
|
2021-09-01 11:53:38 +00:00
|
|
|
* @internal BC method
|
2021-02-07 13:49:24 +00:00
|
|
|
* @return bool[] Map of (filter ID => bool)
|
|
|
|
*/
|
2021-07-21 18:51:12 +00:00
|
|
|
public function checkAllFilters(): array {
|
2021-09-01 11:53:38 +00:00
|
|
|
$this->init();
|
2021-02-08 10:51:40 +00:00
|
|
|
return $this->checkAllFiltersInternal()->getMatchesMap();
|
2021-02-07 13:49:24 +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
|
|
|
/**
|
2021-02-07 11:28:45 +00:00
|
|
|
* Check the conditions of a single filter, and profile it
|
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
|
|
|
*
|
2021-01-07 20:10:52 +00:00
|
|
|
* @param ExistingFilter $filter
|
2019-01-30 19:30:59 +00:00
|
|
|
* @param bool $global
|
2021-09-01 11:18:23 +00:00
|
|
|
* @return array [ status, time taken ]
|
2021-09-06 20:40:36 +00:00
|
|
|
* @phan-return array{0:\MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerStatus,1:float}
|
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
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private function checkFilter( ExistingFilter $filter, bool $global = false ): array {
|
2020-12-02 22:47:40 +00:00
|
|
|
$filterName = GlobalNameUtils::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
|
|
|
|
|
|
|
$startTime = microtime( true );
|
2020-09-25 22:32:01 +00:00
|
|
|
$origExtraTime = LazyVariableComputer::$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
|
|
|
|
2021-09-01 11:53:38 +00:00
|
|
|
$status = $this->ruleChecker->checkConditions( $filter->getRules(), $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
|
|
|
|
2020-09-25 22:32:01 +00:00
|
|
|
$actualExtra = LazyVariableComputer::$profilingExtraTime - $origExtraTime;
|
2019-03-25 19:19:17 +00:00
|
|
|
$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
|
|
|
|
2021-09-01 11:18:23 +00:00
|
|
|
return [ $status, $timeTaken ];
|
2019-01-30 19:30:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 18:01:35 +00:00
|
|
|
/**
|
2021-02-09 13:36:02 +00:00
|
|
|
* @param RunnerData $data
|
2019-03-24 18:01:35 +00:00
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private function profileExecution( RunnerData $data ) {
|
2021-02-09 13:36:02 +00:00
|
|
|
$allFilters = $data->getAllFilters();
|
|
|
|
$matchedFilters = $data->getMatchedFilters();
|
2020-09-20 22:31:41 +00:00
|
|
|
$this->filterProfiler->recordRuntimeProfilingResult(
|
2019-09-29 09:02:29 +00:00
|
|
|
count( $allFilters ),
|
2021-02-09 13:36:02 +00:00
|
|
|
$data->getTotalConditions(),
|
2022-04-26 15:42:34 +00:00
|
|
|
$data->getTotalRuntime()
|
2019-03-24 18:01:35 +00:00
|
|
|
);
|
2021-02-09 13:36:02 +00:00
|
|
|
$this->filterProfiler->recordPerFilterProfiling( $this->title, $data->getProfilingData() );
|
2020-09-20 22:31:41 +00:00
|
|
|
$this->filterProfiler->recordStats(
|
|
|
|
$this->group,
|
2021-02-09 13:36:02 +00:00
|
|
|
$data->getTotalConditions(),
|
2022-04-26 15:42:34 +00:00
|
|
|
$data->getTotalRuntime(),
|
2020-09-20 22:31:41 +00:00
|
|
|
(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
|
|
|
}
|
|
|
|
|
2021-02-10 13:13:03 +00:00
|
|
|
/**
|
|
|
|
* @param bool[] $matches
|
|
|
|
*/
|
2023-06-23 10:28:06 +00:00
|
|
|
private function updateEmergencyCache( array $matches ): void {
|
2021-02-10 13:13:03 +00:00
|
|
|
$filters = $this->emergencyCache->getFiltersToCheckInGroup( $this->group );
|
|
|
|
foreach ( $filters as $filter ) {
|
|
|
|
if ( array_key_exists( "$filter", $matches ) ) {
|
|
|
|
$this->emergencyCache->incrementForFilter( $filter, $matches["$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
|
|
|
}
|