2008-06-27 06:18:51 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-03 11:12:16 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
2021-01-02 14:01:00 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
2020-01-21 07:38:52 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2021-01-03 11:12:16 +00:00
|
|
|
use RequestContext;
|
|
|
|
use Status;
|
|
|
|
use Title;
|
|
|
|
use User;
|
2016-06-13 11:53:50 +00:00
|
|
|
|
2013-10-24 22:10:36 +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
|
|
|
* This class contains most of the business logic of AbuseFilter. It consists of
|
|
|
|
* static functions for generic use (mostly utility functions).
|
2013-10-24 22:10:36 +00:00
|
|
|
*/
|
2008-06-27 06:18:51 +00:00
|
|
|
class AbuseFilter {
|
2016-06-28 22:50:38 +00:00
|
|
|
|
2019-01-05 18:28:03 +00:00
|
|
|
/**
|
|
|
|
* @var array IDs of logged filters like [ page title => [ 'local' => [ids], 'global' => [ids] ] ].
|
|
|
|
* @fixme avoid global state
|
|
|
|
*/
|
|
|
|
public static $logIds = [];
|
|
|
|
|
2021-01-07 20:10:52 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* @todo Phase out
|
|
|
|
*/
|
2019-11-16 15:32:36 +00:00
|
|
|
public const HISTORY_MAPPINGS = [
|
2009-02-07 09:34:11 +00:00
|
|
|
'af_pattern' => 'afh_pattern',
|
|
|
|
'af_user' => 'afh_user',
|
|
|
|
'af_user_text' => 'afh_user_text',
|
|
|
|
'af_timestamp' => 'afh_timestamp',
|
|
|
|
'af_comments' => 'afh_comments',
|
|
|
|
'af_public_comments' => 'afh_public_comments',
|
|
|
|
'af_deleted' => 'afh_deleted',
|
2012-05-06 06:44:45 +00:00
|
|
|
'af_id' => 'afh_filter',
|
|
|
|
'af_group' => 'afh_group',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2018-02-18 13:44:17 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Returns an associative array of filters which were tripped
|
|
|
|
*
|
2021-01-02 14:01:00 +00:00
|
|
|
* @param VariableHolder $vars
|
2018-10-03 09:59:31 +00:00
|
|
|
* @param Title $title
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2018-04-03 15:34:03 +00:00
|
|
|
* @param string $mode 'execute' for edits and logs, 'stash' for cached matches
|
2016-06-28 22:50:38 +00:00
|
|
|
* @return bool[] Map of (integer filter ID => bool)
|
2020-11-27 14:49:41 +00:00
|
|
|
* @deprecated Since 1.34 See comment on FilterRunner::checkAllFilters
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2018-04-04 21:24:41 +00:00
|
|
|
public static function checkAllFilters(
|
2021-01-02 14:01:00 +00:00
|
|
|
VariableHolder $vars,
|
2018-10-03 09:59:31 +00:00
|
|
|
Title $title,
|
2018-04-04 21:24:41 +00:00
|
|
|
$group = 'default',
|
2018-04-29 17:52:45 +00:00
|
|
|
$mode = 'execute'
|
|
|
|
) {
|
2019-12-16 16:19:48 +00:00
|
|
|
$parser = AbuseFilterServices::getParserFactory()->newParser( $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
|
|
|
$user = RequestContext::getMain()->getUser();
|
2020-11-27 14:49:41 +00:00
|
|
|
$runnerFactory = AbuseFilterServices::getFilterRunnerFactory();
|
|
|
|
$runner = $runnerFactory->newRunner( $user, $title, $vars, $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
|
|
|
$runner->parser = $parser;
|
|
|
|
return $runner->checkAllFilters();
|
2017-09-25 20:23:55 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2021-01-02 14:01:00 +00:00
|
|
|
* @param VariableHolder $vars
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param Title $title
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2018-09-13 12:00:53 +00:00
|
|
|
* @param User $user The user performing the action
|
2013-01-08 14:52:49 +00:00
|
|
|
* @return Status
|
2020-11-27 14:49:41 +00:00
|
|
|
* @deprecated Since 1.34 Build a FilterRunner instance and call run() on that.
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2016-06-13 11:53:50 +00:00
|
|
|
public static function filterAction(
|
2021-01-02 14:01:00 +00:00
|
|
|
VariableHolder $vars, Title $title, $group, User $user
|
2016-06-13 11:53:50 +00:00
|
|
|
) {
|
2020-11-27 14:49:41 +00:00
|
|
|
$runnerFactory = AbuseFilterServices::getFilterRunnerFactory();
|
|
|
|
$runner = $runnerFactory->newRunner( $user, $title, $vars, $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
|
|
|
return $runner->run();
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 16:46:24 +00:00
|
|
|
/**
|
|
|
|
* Shortcut for checking whether $user can view the given revision, with mask
|
|
|
|
* SUPPRESSED_ALL.
|
|
|
|
*
|
|
|
|
* @note This assumes that a revision with the given ID exists
|
|
|
|
*
|
|
|
|
* @param RevisionRecord $revRec
|
|
|
|
* @param User $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function userCanViewRev( RevisionRecord $revRec, User $user ) : bool {
|
|
|
|
return $revRec->audienceCan(
|
|
|
|
RevisionRecord::SUPPRESSED_ALL,
|
|
|
|
RevisionRecord::FOR_THIS_USER,
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2021-01-03 11:12:16 +00:00
|
|
|
|
|
|
|
class_alias( AbuseFilter::class, 'AbuseFilter' );
|