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 AbuseFilterVariableHolder;
|
|
|
|
use AFComputedVariable;
|
|
|
|
use BadMethodCallException;
|
|
|
|
use BagOStuff;
|
|
|
|
use IBufferingStatsdDataFactory;
|
|
|
|
use InvalidArgumentException;
|
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;
|
2020-09-20 11:30:59 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Filter;
|
2020-06-03 00:43:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner;
|
2020-12-03 22:22:43 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterParser;
|
2020-11-27 14:49:41 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
|
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;
|
2020-11-27 14:49:41 +00:00
|
|
|
use NullStatsdDataFactory;
|
|
|
|
use ObjectCache;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Status;
|
|
|
|
use Title;
|
|
|
|
use 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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
/** @var AbuseFilterHookRunner */
|
|
|
|
private $hookRunner;
|
|
|
|
/** @var FilterProfiler */
|
|
|
|
private $filterProfiler;
|
|
|
|
/** @var ChangeTagger */
|
|
|
|
private $changeTagger;
|
|
|
|
/** @var FilterLookup */
|
|
|
|
private $filterLookup;
|
|
|
|
/** @var ParserFactory */
|
|
|
|
private $parserFactory;
|
|
|
|
/** @var ConsequencesExecutorFactory */
|
|
|
|
private $consExecutorFactory;
|
|
|
|
/** @var AbuseLoggerFactory */
|
|
|
|
private $abuseLoggerFactory;
|
|
|
|
/** @var Watcher[] */
|
|
|
|
private $watchers;
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
|
|
|
/** @var IBufferingStatsdDataFactory */
|
|
|
|
private $statsdDataFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var AbuseFilterParser
|
|
|
|
* @private Temporarily public for BC
|
|
|
|
*/
|
|
|
|
public $parser;
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2020-11-27 14:49:41 +00:00
|
|
|
* @param AbuseFilterHookRunner $hookRunner
|
|
|
|
* @param FilterProfiler $filterProfiler
|
|
|
|
* @param ChangeTagger $changeTagger
|
|
|
|
* @param FilterLookup $filterLookup
|
|
|
|
* @param ParserFactory $parserFactory
|
|
|
|
* @param ConsequencesExecutorFactory $consExecutorFactory
|
|
|
|
* @param AbuseLoggerFactory $abuseLoggerFactory
|
|
|
|
* @param Watcher[] $watchers
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param IBufferingStatsdDataFactory $statsdDataFactory
|
|
|
|
* @param array $validFilterGroups
|
|
|
|
* @param User $user
|
|
|
|
* @param Title $title
|
|
|
|
* @param AbuseFilterVariableHolder $vars
|
|
|
|
* @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,
|
|
|
|
ParserFactory $parserFactory,
|
|
|
|
ConsequencesExecutorFactory $consExecutorFactory,
|
|
|
|
AbuseLoggerFactory $abuseLoggerFactory,
|
|
|
|
array $watchers,
|
|
|
|
LoggerInterface $logger,
|
|
|
|
IBufferingStatsdDataFactory $statsdDataFactory,
|
|
|
|
array $validFilterGroups,
|
|
|
|
User $user,
|
|
|
|
Title $title,
|
|
|
|
AbuseFilterVariableHolder $vars,
|
|
|
|
string $group
|
|
|
|
) {
|
|
|
|
$this->hookRunner = $hookRunner;
|
|
|
|
$this->filterProfiler = $filterProfiler;
|
|
|
|
$this->changeTagger = $changeTagger;
|
|
|
|
$this->filterLookup = $filterLookup;
|
|
|
|
$this->parserFactory = $parserFactory;
|
|
|
|
$this->consExecutorFactory = $consExecutorFactory;
|
|
|
|
$this->abuseLoggerFactory = $abuseLoggerFactory;
|
|
|
|
$this->watchers = $watchers;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->statsdDataFactory = $statsdDataFactory;
|
|
|
|
|
|
|
|
if ( !in_array( $group, $validFilterGroups, true ) ) {
|
|
|
|
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
|
|
|
}
|
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;
|
2020-11-27 14:49:41 +00:00
|
|
|
$this->vars->setLogger( $logger );
|
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();
|
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 ) );
|
2020-11-27 14:49:41 +00:00
|
|
|
$this->parser = $this->parserFactory->newParser( $this->vars );
|
|
|
|
$this->parser->setStatsd( $this->statsdDataFactory );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:49:41 +00:00
|
|
|
$executor = $this->consExecutorFactory->newExecutor(
|
2020-10-16 22:10:37 +00:00
|
|
|
$this->user,
|
|
|
|
$this->title,
|
|
|
|
$this->vars
|
|
|
|
);
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
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()
|
2020-11-27 14:49:41 +00:00
|
|
|
: $this->statsdDataFactory;
|
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
|
|
|
|
|
|
|
$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
|
2020-12-02 22:47:40 +00:00
|
|
|
$matchedFilters[GlobalNameUtils::buildGlobalName( $filter->getID() )] =
|
2020-09-20 11:30:59 +00:00
|
|
|
$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
|
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
|
|
|
|
|
|
|
$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 );
|
2019-09-15 15:48:13 +00:00
|
|
|
$result = $this->parser->checkConditions( $filter->getRules(), true, $filterName )->getResult();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
}
|