2008-06-27 06:18:51 +00:00
|
|
|
<?php
|
|
|
|
|
2017-08-20 12:38:40 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2016-06-13 11:53:50 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2018-03-01 22:44:46 +00:00
|
|
|
use MediaWiki\Session\SessionManager;
|
2016-06-13 11:53:50 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-12-22 22:09:33 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2016-06-13 11:53:50 +00:00
|
|
|
|
2013-10-24 22:10:36 +00:00
|
|
|
/**
|
|
|
|
* This class contains most of the business logic of AbuseFilter. It consists of mostly
|
|
|
|
* static functions that handle activities such as parsing edits, applying filters,
|
|
|
|
* logging actions, etc.
|
|
|
|
*/
|
2008-06-27 06:18:51 +00:00
|
|
|
class AbuseFilter {
|
2008-07-17 02:43:45 +00:00
|
|
|
public static $statsStoragePeriod = 86400;
|
2008-08-02 11:10:42 +00:00
|
|
|
public static $condLimitEnabled = true;
|
2016-06-28 22:50:38 +00:00
|
|
|
|
|
|
|
/** @var array Map of (filter ID => stdClass) */
|
|
|
|
private static $filterCache = [];
|
|
|
|
|
2008-08-02 11:10:42 +00:00
|
|
|
public static $condCount = 0;
|
2017-01-02 11:41:29 +00:00
|
|
|
|
|
|
|
/** @var array Map of (action ID => string[]) */
|
2018-04-04 21:14:25 +00:00
|
|
|
// FIXME: avoid global state here
|
|
|
|
public static $tagsToSet = [];
|
2016-06-28 22:50:38 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $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
|
|
|
];
|
|
|
|
public static $builderValues = [
|
|
|
|
'op-arithmetic' => [
|
2009-02-07 09:34:11 +00:00
|
|
|
'+' => 'addition',
|
2012-02-10 18:20:41 +00:00
|
|
|
'-' => 'subtraction',
|
|
|
|
'*' => 'multiplication',
|
|
|
|
'/' => 'divide',
|
2009-02-07 09:34:11 +00:00
|
|
|
'%' => 'modulo',
|
|
|
|
'**' => 'pow'
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
|
|
|
'op-comparison' => [
|
2009-02-07 09:34:11 +00:00
|
|
|
'==' => 'equal',
|
2018-04-02 15:08:08 +00:00
|
|
|
'===' => 'equal-strict',
|
2009-02-07 09:34:11 +00:00
|
|
|
'!=' => 'notequal',
|
2018-04-02 15:08:08 +00:00
|
|
|
'!==' => 'notequal-strict',
|
2009-02-07 09:34:11 +00:00
|
|
|
'<' => 'lt',
|
|
|
|
'>' => 'gt',
|
|
|
|
'<=' => 'lte',
|
|
|
|
'>=' => 'gte'
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
|
|
|
'op-bool' => [
|
2009-02-07 09:34:11 +00:00
|
|
|
'!' => 'not',
|
|
|
|
'&' => 'and',
|
|
|
|
'|' => 'or',
|
|
|
|
'^' => 'xor'
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
|
|
|
'misc' => [
|
2009-02-07 09:34:11 +00:00
|
|
|
'in' => 'in',
|
2009-03-19 05:11:55 +00:00
|
|
|
'contains' => 'contains',
|
2009-02-07 09:34:11 +00:00
|
|
|
'like' => 'like',
|
|
|
|
'""' => 'stringlit',
|
2009-03-19 05:11:55 +00:00
|
|
|
'rlike' => 'rlike',
|
2010-03-28 00:50:51 +00:00
|
|
|
'irlike' => 'irlike',
|
2009-03-19 05:11:55 +00:00
|
|
|
'cond ? iftrue : iffalse' => 'tern',
|
2011-06-17 16:25:46 +00:00
|
|
|
'if cond then iftrue elseiffalse end' => 'cond',
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
|
|
|
'funcs' => [
|
2009-02-07 09:34:11 +00:00
|
|
|
'length(string)' => 'length',
|
|
|
|
'lcase(string)' => 'lcase',
|
2013-04-17 16:36:10 +00:00
|
|
|
'ucase(string)' => 'ucase',
|
2009-02-07 09:34:11 +00:00
|
|
|
'ccnorm(string)' => 'ccnorm',
|
2017-09-19 23:54:03 +00:00
|
|
|
'ccnorm_contains_any(haystack,needle1,needle2,..)' => 'ccnorm-contains-any',
|
2017-11-13 19:13:07 +00:00
|
|
|
'ccnorm_contains_all(haystack,needle1,needle2,..)' => 'ccnorm-contains-all',
|
2009-02-07 09:34:11 +00:00
|
|
|
'rmdoubles(string)' => 'rmdoubles',
|
|
|
|
'specialratio(string)' => 'specialratio',
|
|
|
|
'norm(string)' => 'norm',
|
2009-03-07 01:26:42 +00:00
|
|
|
'count(needle,haystack)' => 'count',
|
|
|
|
'rcount(needle,haystack)' => 'rcount',
|
2017-11-07 18:44:10 +00:00
|
|
|
'get_matches(needle,haystack)' => 'get_matches',
|
2009-03-19 05:06:39 +00:00
|
|
|
'rmwhitespace(text)' => 'rmwhitespace',
|
|
|
|
'rmspecials(text)' => 'rmspecials',
|
|
|
|
'ip_in_range(ip, range)' => 'ip_in_range',
|
2017-09-19 23:54:03 +00:00
|
|
|
'contains_any(haystack,needle1,needle2,...)' => 'contains-any',
|
2017-11-13 19:13:07 +00:00
|
|
|
'contains_all(haystack,needle1,needle2,...)' => 'contains-all',
|
2018-02-09 11:24:01 +00:00
|
|
|
'equals_to_any(haystack,needle1,needle2,...)' => 'equals-to-any',
|
2009-04-01 05:05:23 +00:00
|
|
|
'substr(subject, offset, length)' => 'substr',
|
|
|
|
'strpos(haystack, needle)' => 'strpos',
|
|
|
|
'str_replace(subject, search, replace)' => 'str_replace',
|
2011-10-18 17:54:25 +00:00
|
|
|
'rescape(string)' => 'rescape',
|
2009-04-01 06:53:18 +00:00
|
|
|
'set_var(var,value)' => 'set_var',
|
2018-01-31 00:32:14 +00:00
|
|
|
'sanitize(string)' => 'sanitize',
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
|
|
|
'vars' => [
|
2009-04-01 03:59:58 +00:00
|
|
|
'timestamp' => 'timestamp',
|
2009-02-11 20:00:33 +00:00
|
|
|
'accountname' => 'accountname',
|
|
|
|
'action' => 'action',
|
|
|
|
'added_lines' => 'addedlines',
|
|
|
|
'edit_delta' => 'delta',
|
|
|
|
'edit_diff' => 'diff',
|
|
|
|
'new_size' => 'newsize',
|
|
|
|
'old_size' => 'oldsize',
|
2016-09-13 08:40:21 +00:00
|
|
|
'new_content_model' => 'new-content-model',
|
|
|
|
'old_content_model' => 'old-content-model',
|
2009-02-11 20:00:33 +00:00
|
|
|
'removed_lines' => 'removedlines',
|
|
|
|
'summary' => 'summary',
|
2018-02-18 13:44:17 +00:00
|
|
|
'page_id' => 'page-id',
|
|
|
|
'page_namespace' => 'page-ns',
|
|
|
|
'page_title' => 'page-title',
|
|
|
|
'page_prefixedtitle' => 'page-prefixedtitle',
|
2015-04-01 00:41:09 +00:00
|
|
|
'page_age' => 'page-age',
|
2018-02-18 13:44:17 +00:00
|
|
|
'moved_from_id' => 'movedfrom-id',
|
2009-02-11 20:00:33 +00:00
|
|
|
'moved_from_namespace' => 'movedfrom-ns',
|
2018-02-18 13:44:17 +00:00
|
|
|
'moved_from_title' => 'movedfrom-title',
|
|
|
|
'moved_from_prefixedtitle' => 'movedfrom-prefixedtitle',
|
2015-04-01 00:41:09 +00:00
|
|
|
'moved_from_age' => 'movedfrom-age',
|
2018-02-18 13:44:17 +00:00
|
|
|
'moved_to_id' => 'movedto-id',
|
2009-02-11 20:00:33 +00:00
|
|
|
'moved_to_namespace' => 'movedto-ns',
|
2018-02-18 13:44:17 +00:00
|
|
|
'moved_to_title' => 'movedto-title',
|
|
|
|
'moved_to_prefixedtitle' => 'movedto-prefixedtitle',
|
2015-04-01 00:41:09 +00:00
|
|
|
'moved_to_age' => 'movedto-age',
|
2010-08-19 21:12:09 +00:00
|
|
|
'user_editcount' => 'user-editcount',
|
2009-02-11 20:00:33 +00:00
|
|
|
'user_age' => 'user-age',
|
|
|
|
'user_name' => 'user-name',
|
|
|
|
'user_groups' => 'user-groups',
|
2014-03-31 04:59:36 +00:00
|
|
|
'user_rights' => 'user-rights',
|
2012-12-14 09:04:17 +00:00
|
|
|
'user_blocked' => 'user-blocked',
|
2009-02-11 20:00:33 +00:00
|
|
|
'user_emailconfirm' => 'user-emailconfirm',
|
|
|
|
'old_wikitext' => 'old-text',
|
|
|
|
'new_wikitext' => 'new-text',
|
|
|
|
'added_links' => 'added-links',
|
|
|
|
'removed_links' => 'removed-links',
|
|
|
|
'all_links' => 'all-links',
|
2013-04-24 14:53:12 +00:00
|
|
|
'new_pst' => 'new-pst',
|
2013-10-01 07:07:50 +00:00
|
|
|
'edit_diff_pst' => 'diff-pst',
|
|
|
|
'added_lines_pst' => 'addedlines-pst',
|
2009-02-11 20:00:33 +00:00
|
|
|
'new_text' => 'new-text-stripped',
|
|
|
|
'new_html' => 'new-html',
|
2018-02-18 13:44:17 +00:00
|
|
|
'page_restrictions_edit' => 'restrictions-edit',
|
|
|
|
'page_restrictions_move' => 'restrictions-move',
|
|
|
|
'page_restrictions_create' => 'restrictions-create',
|
|
|
|
'page_restrictions_upload' => 'restrictions-upload',
|
|
|
|
'page_recent_contributors' => 'recent-contributors',
|
|
|
|
'page_first_contributor' => 'first-contributor',
|
2017-08-24 07:44:31 +00:00
|
|
|
'moved_from_restrictions_edit' => 'movedfrom-restrictions-edit',
|
|
|
|
'moved_from_restrictions_move' => 'movedfrom-restrictions-move',
|
|
|
|
'moved_from_restrictions_create' => 'movedfrom-restrictions-create',
|
|
|
|
'moved_from_restrictions_upload' => 'movedfrom-restrictions-upload',
|
|
|
|
'moved_from_recent_contributors' => 'movedfrom-recent-contributors',
|
|
|
|
'moved_from_first_contributor' => 'movedfrom-first-contributor',
|
|
|
|
'moved_to_restrictions_edit' => 'movedto-restrictions-edit',
|
|
|
|
'moved_to_restrictions_move' => 'movedto-restrictions-move',
|
|
|
|
'moved_to_restrictions_create' => 'movedto-restrictions-create',
|
|
|
|
'moved_to_restrictions_upload' => 'movedto-restrictions-upload',
|
|
|
|
'moved_to_recent_contributors' => 'movedto-recent-contributors',
|
|
|
|
'moved_to_first_contributor' => 'movedto-first-contributor',
|
2009-02-26 12:15:14 +00:00
|
|
|
'old_links' => 'old-links',
|
2009-03-04 02:04:48 +00:00
|
|
|
'minor_edit' => 'minor-edit',
|
2009-06-03 15:10:44 +00:00
|
|
|
'file_sha1' => 'file-sha1',
|
2015-01-08 04:56:35 +00:00
|
|
|
'file_size' => 'file-size',
|
Add more file_* variables for file metadata
* file_mime
The MIME type of the file, e.g. 'image/png'.
* file_mediatype
The media type of the file, one of 'UNKNOWN', 'BITMAP', 'DRAWING',
'AUDIO', 'VIDEO', 'MULTIMEDIA', 'OFFICE', 'TEXT', 'EXECUTABLE', 'ARCHIVE'.
* file_width
Width of the image in pixels, or 0 if it's inapplicable (e.g. for
audio files).
* file_height
Height of the image in pixels, or 0 if it's inapplicable (e.g. for
audio files).
* file_bits_per_channel
Bits per color channel of the image, or 0 if it's inapplicable (e.g.
for audio files). The most common value is 8.
Bug: T131643
Change-Id: Id355515a18d3674393332c0f4094e34f9f522623
2016-04-04 19:12:08 +00:00
|
|
|
'file_mime' => 'file-mime',
|
|
|
|
'file_mediatype' => 'file-mediatype',
|
|
|
|
'file_width' => 'file-width',
|
|
|
|
'file_height' => 'file-height',
|
|
|
|
'file_bits_per_channel' => 'file-bits-per-channel',
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
|
|
|
];
|
2012-12-14 17:37:01 +00:00
|
|
|
|
2018-05-04 13:43:30 +00:00
|
|
|
/** @var array Old vars which aren't in use anymore */
|
|
|
|
public static $disabledVars = [
|
|
|
|
'old_text' => 'old-text-stripped',
|
|
|
|
'old_html' => 'old-html'
|
|
|
|
];
|
|
|
|
|
2018-02-18 13:44:17 +00:00
|
|
|
public static $deprecatedVars = [
|
|
|
|
'article_text' => 'page_title',
|
|
|
|
'article_prefixedtext' => 'page_prefixedtitle',
|
|
|
|
'article_namespace' => 'page_namespace',
|
|
|
|
'article_articleid' => 'page_id',
|
|
|
|
'article_restrictions_edit' => 'page_restrictions_edit',
|
|
|
|
'article_restrictions_move' => 'page_restrictions_move',
|
|
|
|
'article_restrictions_create' => 'page_restrictions_create',
|
|
|
|
'article_restrictions_upload' => 'page_restrictions_upload',
|
|
|
|
'article_recent_contributors' => 'page_recent_contributors',
|
|
|
|
'article_first_contributor' => 'page_first_contributor',
|
|
|
|
'moved_from_text' => 'moved_from_title',
|
|
|
|
'moved_from_prefixedtext' => 'moved_from_prefixedtitle',
|
|
|
|
'moved_from_articleid' => 'moved_from_id',
|
|
|
|
'moved_to_text' => 'moved_to_title',
|
|
|
|
'moved_to_prefixedtext' => 'moved_to_prefixedtitle',
|
|
|
|
'moved_to_articleid' => 'moved_to_id',
|
|
|
|
];
|
|
|
|
|
2011-08-26 20:12:34 +00:00
|
|
|
public static $editboxName = null;
|
2009-03-11 07:49:56 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param IContextSource $context
|
|
|
|
* @param string $pageType
|
2017-08-20 12:38:40 +00:00
|
|
|
* @param LinkRenderer $linkRenderer
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2017-08-20 12:38:40 +00:00
|
|
|
public static function addNavigationLinks(
|
|
|
|
IContextSource $context,
|
|
|
|
$pageType,
|
|
|
|
LinkRenderer $linkRenderer
|
|
|
|
) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$linkDefs = [
|
2009-10-07 13:57:06 +00:00
|
|
|
'home' => 'Special:AbuseFilter',
|
|
|
|
'recentchanges' => 'Special:AbuseFilter/history',
|
|
|
|
'examine' => 'Special:AbuseFilter/examine',
|
|
|
|
'log' => 'Special:AbuseLog',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-05-04 19:56:45 +00:00
|
|
|
if ( $context->getUser()->isAllowedAny( 'abusefilter-modify', 'abusefilter-view-private' ) ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$linkDefs = array_merge( $linkDefs, [
|
2015-09-28 18:03:35 +00:00
|
|
|
'test' => 'Special:AbuseFilter/test',
|
2018-05-04 19:56:45 +00:00
|
|
|
'tools' => 'Special:AbuseFilter/tools'
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $context->getUser()->isAllowed( 'abusefilter-modify' ) ) {
|
|
|
|
$linkDefs = array_merge( $linkDefs, [
|
|
|
|
'import' => 'Special:AbuseFilter/import'
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2009-07-17 16:59:14 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
// Re-use the message
|
2017-06-15 14:23:34 +00:00
|
|
|
$msgOverrides = [
|
2009-10-07 13:57:06 +00:00
|
|
|
'recentchanges' => 'abusefilter-filter-log',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$links = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $linkDefs as $name => $page ) {
|
2011-04-29 14:48:33 +00:00
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// abusefilter-topnav-home, abusefilter-topnav-test, abusefilter-topnav-examine
|
|
|
|
// abusefilter-topnav-log, abusefilter-topnav-tools, abusefilter-topnav-import
|
2009-03-11 05:55:06 +00:00
|
|
|
$msgName = "abusefilter-topnav-$name";
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( isset( $msgOverrides[$name] ) ) {
|
2009-03-11 05:55:06 +00:00
|
|
|
$msgName = $msgOverrides[$name];
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-06-01 12:43:37 +00:00
|
|
|
$msg = $context->msg( $msgName )->parse();
|
2009-03-11 05:55:06 +00:00
|
|
|
$title = Title::newFromText( $page );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $name == $pageType ) {
|
2009-03-11 05:55:06 +00:00
|
|
|
$links[] = Xml::tags( 'strong', null, $msg );
|
|
|
|
} else {
|
2016-12-04 15:13:16 +00:00
|
|
|
$links[] = $linkRenderer->makeLink( $title, new HtmlArmor( $msg ) );
|
2009-03-11 05:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 07:49:56 +00:00
|
|
|
|
2018-04-29 17:52:45 +00:00
|
|
|
$linkStr = $context->msg( 'parentheses' )
|
|
|
|
->rawParams( $context->getLanguage()->pipeList( $links ) )
|
|
|
|
->text();
|
2016-06-01 12:43:37 +00:00
|
|
|
$linkStr = $context->msg( 'abusefilter-topnav' )->parse() . " $linkStr";
|
2009-03-11 07:49:56 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$linkStr = Xml::tags( 'div', [ 'class' => 'mw-abusefilter-navigation' ], $linkStr );
|
2009-03-11 07:49:56 +00:00
|
|
|
|
2011-11-17 00:55:53 +00:00
|
|
|
$context->getOutput()->setSubtitle( $linkStr );
|
2009-03-11 05:55:06 +00:00
|
|
|
}
|
2008-07-15 08:46:17 +00:00
|
|
|
|
2011-02-10 17:32:57 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param User $user
|
2011-02-10 17:32:57 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function generateUserVars( $user ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_editcount',
|
|
|
|
'simple-user-accessor',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'user' => $user, 'method' => 'getEditCount' ]
|
2013-01-07 00:02:41 +00:00
|
|
|
);
|
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setVar( 'user_name', $user->getName() );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_emailconfirm',
|
|
|
|
'simple-user-accessor',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'user' => $user, 'method' => 'getEmailAuthenticationTimestamp' ]
|
2013-01-07 00:02:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_age',
|
|
|
|
'user-age',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'user' => $user, 'asof' => wfTimestampNow() ]
|
2013-01-07 00:02:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_groups',
|
|
|
|
'simple-user-accessor',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'user' => $user, 'method' => 'getEffectiveGroups' ]
|
2013-01-07 00:02:41 +00:00
|
|
|
);
|
|
|
|
|
2014-03-31 04:59:36 +00:00
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_rights',
|
|
|
|
'simple-user-accessor',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'user' => $user, 'method' => 'getRights' ]
|
2014-03-31 04:59:36 +00:00
|
|
|
);
|
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_blocked',
|
|
|
|
'simple-user-accessor',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'user' => $user, 'method' => 'isBlocked' ]
|
2013-01-07 00:02:41 +00:00
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
Hooks::run( 'AbuseFilter-generateUserVars', [ $vars, $user ] );
|
2012-12-21 17:43:48 +00:00
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-03-05 02:43:05 +00:00
|
|
|
public static function getBuilderValues() {
|
|
|
|
static $realValues = null;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $realValues ) {
|
|
|
|
return $realValues;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-05 02:43:05 +00:00
|
|
|
$realValues = self::$builderValues;
|
2017-09-02 17:15:25 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
Hooks::run( 'AbuseFilter-builder', [ &$realValues ] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-05 02:43:05 +00:00
|
|
|
return $realValues;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-02-18 13:44:17 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getDeprecatedVariables() {
|
|
|
|
static $deprecatedVars = null;
|
|
|
|
|
|
|
|
if ( $deprecatedVars ) {
|
|
|
|
return $deprecatedVars;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deprecatedVars = self::$deprecatedVars;
|
|
|
|
|
|
|
|
Hooks::run( 'AbuseFilter-deprecatedVariables', [ &$deprecatedVars ] );
|
|
|
|
|
|
|
|
return $deprecatedVars;
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $filter
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2009-03-17 13:18:33 +00:00
|
|
|
public static function filterHidden( $filter ) {
|
2012-01-03 17:29:10 +00:00
|
|
|
$globalIndex = self::decodeGlobalName( $filter );
|
|
|
|
if ( $globalIndex ) {
|
|
|
|
global $wgAbuseFilterCentralDB;
|
|
|
|
if ( !$wgAbuseFilterCentralDB ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA, [], $wgAbuseFilterCentralDB );
|
2012-01-03 17:29:10 +00:00
|
|
|
$filter = $globalIndex;
|
|
|
|
} else {
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2012-01-03 17:29:10 +00:00
|
|
|
}
|
2015-02-20 21:39:37 +00:00
|
|
|
if ( $filter === 'new' ) {
|
|
|
|
return false;
|
2018-02-20 12:36:32 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
$hidden = $dbr->selectField(
|
|
|
|
'abuse_filter',
|
|
|
|
'af_hidden',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'af_id' => $filter ],
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2015-06-28 01:22:04 +00:00
|
|
|
return (bool)$hidden;
|
2009-03-17 13:18:33 +00:00
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param int $val
|
2012-03-11 20:40:04 +00:00
|
|
|
* @throws MWException
|
|
|
|
*/
|
2009-01-23 19:23:19 +00:00
|
|
|
public static function triggerLimiter( $val = 1 ) {
|
|
|
|
self::$condCount += $val;
|
|
|
|
|
|
|
|
global $wgAbuseFilterConditionLimit;
|
|
|
|
|
2013-05-27 22:43:33 +00:00
|
|
|
if ( self::$condLimitEnabled && self::$condCount > $wgAbuseFilterConditionLimit ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new MWException( 'Condition limit reached.' );
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* For use in batch scripts and the like
|
|
|
|
*/
|
2008-07-18 02:18:58 +00:00
|
|
|
public static function disableConditionLimit() {
|
|
|
|
self::$condLimitEnabled = false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-02-10 17:32:57 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param Title|null $title
|
|
|
|
* @param string $prefix
|
2018-02-18 13:44:17 +00:00
|
|
|
* @param bool $transition Temporary parameter to help with T173889 and to be removed afterwards
|
2011-02-10 17:32:57 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2018-02-18 13:44:17 +00:00
|
|
|
public static function generateTitleVars( $title, $prefix, $transition = true ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder;
|
2009-02-03 18:48:16 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( !$title ) {
|
2015-06-28 01:22:04 +00:00
|
|
|
return $vars;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-02-18 13:44:17 +00:00
|
|
|
// Temporary overrides for T173889, necessary because Flow (and maybe
|
|
|
|
// other extensions) still pass old prefix/suffix and thus fail, since
|
|
|
|
// hybrid variables are generated (e.g. article_prefixedtitle).
|
|
|
|
// Once their variables will be renamed according to the new syntax,
|
|
|
|
// we should get rid of these if and just use the new prefix/suffix.
|
|
|
|
// Right now, what we want to do is:
|
|
|
|
// - Use new prefix/suffix for AF's own variables (they're handled at parser level)
|
|
|
|
// - Use old prefix/suffix for external variables (we don't handle them)
|
|
|
|
$titleSuffix = 'TITLE';
|
|
|
|
if ( $transition && $prefix === 'BOARD' ) {
|
|
|
|
$titleSuffix = 'TEXT';
|
|
|
|
}
|
|
|
|
if ( $transition && $prefix === 'ARTICLE' ) {
|
|
|
|
$prefix = 'PAGE';
|
|
|
|
}
|
|
|
|
|
|
|
|
$vars->setVar( $prefix . '_ID', $title->getArticleID() );
|
2009-10-07 13:57:06 +00:00
|
|
|
$vars->setVar( $prefix . '_NAMESPACE', $title->getNamespace() );
|
2018-02-18 13:44:17 +00:00
|
|
|
$vars->setVar( $prefix . "_$titleSuffix", $title->getText() );
|
|
|
|
$vars->setVar( $prefix . "_PREFIXED$titleSuffix", $title->getPrefixedText() );
|
2015-06-28 01:02:51 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
global $wgRestrictionTypes;
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $wgRestrictionTypes as $action ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( "{$prefix}_restrictions_$action", 'get-page-restrictions',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'title' => $title->getText(),
|
2015-09-28 18:03:35 +00:00
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'action' => $action
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2015-09-28 18:03:35 +00:00
|
|
|
);
|
2008-07-18 08:30:25 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( "{$prefix}_recent_contributors", 'load-recent-authors',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2015-09-28 18:03:35 +00:00
|
|
|
'title' => $title->getText(),
|
|
|
|
'namespace' => $title->getNamespace()
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2014-07-09 16:58:07 +00:00
|
|
|
|
2015-04-01 00:41:09 +00:00
|
|
|
$vars->setLazyLoadVar( "{$prefix}_age", 'page-age',
|
|
|
|
[
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'asof' => wfTimestampNow()
|
|
|
|
] );
|
|
|
|
|
2014-07-09 16:58:07 +00:00
|
|
|
$vars->setLazyLoadVar( "{$prefix}_first_contributor", 'load-first-author',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2015-09-28 18:03:35 +00:00
|
|
|
'title' => $title->getText(),
|
|
|
|
'namespace' => $title->getNamespace()
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
Hooks::run( 'AbuseFilter-generateTitleVars', [ $vars, $title, $prefix ] );
|
2012-12-21 17:43:48 +00:00
|
|
|
|
2008-08-02 11:10:42 +00:00
|
|
|
return $vars;
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $filter
|
2018-06-07 11:23:22 +00:00
|
|
|
* @return true|array True when successful, otherwise a two-element array with exception message
|
|
|
|
* and character position of the syntax error
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-08-03 14:04:26 +00:00
|
|
|
public static function checkSyntax( $filter ) {
|
|
|
|
global $wgAbuseFilterParserClass;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-10-15 12:35:03 +00:00
|
|
|
/** @var $parser AbuseFilterParser */
|
2008-08-03 14:04:26 +00:00
|
|
|
$parser = new $wgAbuseFilterParserClass;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-03 14:04:26 +00:00
|
|
|
return $parser->checkSyntax( $filter );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $expr
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-09-19 17:58:30 +00:00
|
|
|
public static function evaluateExpression( $expr ) {
|
2008-08-04 14:27:48 +00:00
|
|
|
global $wgAbuseFilterParserClass;
|
2009-01-30 23:23:52 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( self::checkSyntax( $expr ) !== true ) {
|
2009-03-07 01:26:42 +00:00
|
|
|
return 'BADSYNTAX';
|
2009-01-30 23:23:52 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-10-15 12:35:03 +00:00
|
|
|
/** @var $parser AbuseFilterParser */
|
2018-09-19 17:58:30 +00:00
|
|
|
$parser = new $wgAbuseFilterParserClass;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-04 14:27:48 +00:00
|
|
|
return $parser->evaluateExpression( $expr );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $conds
|
|
|
|
* @param AbuseFilterVariableHolder $vars
|
|
|
|
* @param bool $ignoreError
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2012-02-10 23:41:05 +00:00
|
|
|
public static function checkConditions(
|
2018-10-17 01:22:23 +00:00
|
|
|
$conds, $vars, $ignoreError = true
|
2012-02-10 23:41:05 +00:00
|
|
|
) {
|
2008-08-02 11:10:42 +00:00
|
|
|
global $wgAbuseFilterParserClass;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
Removing AbuseFilter::checkConditions' param
I'm not really sure why it exists; I assume to minimize overhead of init'ing
multiple AbuseFilterParser objects that would be exactly the same.
However, checkConditions() - which checks one specific condition - is not really
an "endpoint" you will want to call in <your-extension-that-uses-AbuseFilter>.
You're more likely to call something higher-lever (like
AbuseFilter::filterAction, which will take care of fetching all appropriate
filters, as well as run them through checkConditions, and much more). This will
trickle down to ::checkAllFilter, down to ::checkFilter, which would eventually
call ::checkConditions with the 'keepvars' argument.
Basically, unless you're re-implementing much of AbuseFilter yourself, you won't
get to pass anything other than 'keepvars' to checkFilter.
As a result, even though you may call AbuseFilter to call on multiple different
vars, it will re-use the same parser with the first vars.
I'm proposing to drop the 'keepvars' and just keep the vars around instead.
checkFilter can compare previous vars with new vars, and only init a new Parser
if the vars are different.
Change-Id: I96ccc60c77f3cdbb82c0f9f16782a1a44ffb1592
2013-12-30 11:06:34 +00:00
|
|
|
static $parser, $lastVars;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
Removing AbuseFilter::checkConditions' param
I'm not really sure why it exists; I assume to minimize overhead of init'ing
multiple AbuseFilterParser objects that would be exactly the same.
However, checkConditions() - which checks one specific condition - is not really
an "endpoint" you will want to call in <your-extension-that-uses-AbuseFilter>.
You're more likely to call something higher-lever (like
AbuseFilter::filterAction, which will take care of fetching all appropriate
filters, as well as run them through checkConditions, and much more). This will
trickle down to ::checkAllFilter, down to ::checkFilter, which would eventually
call ::checkConditions with the 'keepvars' argument.
Basically, unless you're re-implementing much of AbuseFilter yourself, you won't
get to pass anything other than 'keepvars' to checkFilter.
As a result, even though you may call AbuseFilter to call on multiple different
vars, it will re-use the same parser with the first vars.
I'm proposing to drop the 'keepvars' and just keep the vars around instead.
checkFilter can compare previous vars with new vars, and only init a new Parser
if the vars are different.
Change-Id: I96ccc60c77f3cdbb82c0f9f16782a1a44ffb1592
2013-12-30 11:06:34 +00:00
|
|
|
if ( is_null( $parser ) || $vars !== $lastVars ) {
|
2013-10-15 12:35:03 +00:00
|
|
|
/** @var $parser AbuseFilterParser */
|
2013-01-07 00:02:41 +00:00
|
|
|
$parser = new $wgAbuseFilterParserClass( $vars );
|
Removing AbuseFilter::checkConditions' param
I'm not really sure why it exists; I assume to minimize overhead of init'ing
multiple AbuseFilterParser objects that would be exactly the same.
However, checkConditions() - which checks one specific condition - is not really
an "endpoint" you will want to call in <your-extension-that-uses-AbuseFilter>.
You're more likely to call something higher-lever (like
AbuseFilter::filterAction, which will take care of fetching all appropriate
filters, as well as run them through checkConditions, and much more). This will
trickle down to ::checkAllFilter, down to ::checkFilter, which would eventually
call ::checkConditions with the 'keepvars' argument.
Basically, unless you're re-implementing much of AbuseFilter yourself, you won't
get to pass anything other than 'keepvars' to checkFilter.
As a result, even though you may call AbuseFilter to call on multiple different
vars, it will re-use the same parser with the first vars.
I'm proposing to drop the 'keepvars' and just keep the vars around instead.
checkFilter can compare previous vars with new vars, and only init a new Parser
if the vars are different.
Change-Id: I96ccc60c77f3cdbb82c0f9f16782a1a44ffb1592
2013-12-30 11:06:34 +00:00
|
|
|
$lastVars = $vars;
|
2009-03-26 04:09:07 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-26 04:09:07 +00:00
|
|
|
try {
|
2008-08-03 14:04:26 +00:00
|
|
|
$result = $parser->parse( $conds, self::$condCount );
|
2009-10-07 13:57:06 +00:00
|
|
|
} catch ( Exception $excep ) {
|
2008-08-03 14:04:26 +00:00
|
|
|
$result = false;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
|
|
|
$logger->debug( 'AbuseFilter parser error: ' . $excep->getMessage() );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( !$ignoreError ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
throw $excep;
|
|
|
|
}
|
2008-08-03 14:04:26 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-08-02 11:10:42 +00:00
|
|
|
return $result;
|
2008-07-18 08:30:25 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Returns an associative array of filters which were tripped
|
|
|
|
*
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2017-09-25 20:23:55 +00:00
|
|
|
* @param Title|null $title
|
2018-04-03 15:34:03 +00:00
|
|
|
* @param string $mode 'execute' for edits and logs, 'stash' for cached matches
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2016-06-28 22:50:38 +00:00
|
|
|
* @return bool[] Map of (integer filter ID => bool)
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2018-04-04 21:24:41 +00:00
|
|
|
public static function checkAllFilters(
|
2018-10-17 01:22:23 +00:00
|
|
|
$vars,
|
2018-04-04 21:24:41 +00:00
|
|
|
$group = 'default',
|
|
|
|
Title $title = null,
|
|
|
|
$mode = 'execute'
|
|
|
|
) {
|
2015-05-13 05:46:33 +00:00
|
|
|
global $wgAbuseFilterCentralDB, $wgAbuseFilterIsCentral;
|
2017-02-26 09:55:31 +00:00
|
|
|
global $wgAbuseFilterConditionLimit;
|
2015-05-13 05:46:33 +00:00
|
|
|
|
2018-04-30 16:42:24 +00:00
|
|
|
// Ensure that we start fresh, see T193374
|
|
|
|
self::$condCount = 0;
|
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
// Fetch filters to check from the database.
|
2017-06-15 14:23:34 +00:00
|
|
|
$filter_matched = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2018-06-26 13:25:03 +00:00
|
|
|
$fields = [
|
|
|
|
'af_id',
|
|
|
|
'af_pattern',
|
|
|
|
'af_public_comments',
|
|
|
|
'af_timestamp'
|
|
|
|
];
|
2010-08-19 21:12:09 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
'abuse_filter',
|
2018-06-26 13:25:03 +00:00
|
|
|
$fields,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2012-05-06 06:44:45 +00:00
|
|
|
'af_enabled' => 1,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_group' => $group,
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
2010-08-19 21:12:09 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2009-02-02 17:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $res as $row ) {
|
2018-03-29 15:33:20 +00:00
|
|
|
$filter_matched[$row->af_id] = self::checkFilter( $row, $vars, $title, '', $mode );
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $wgAbuseFilterCentralDB && !$wgAbuseFilterIsCentral ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
// Global filters
|
2012-08-03 21:55:35 +00:00
|
|
|
$globalRulesKey = self::getGlobalRulesKey( $group );
|
|
|
|
|
2015-05-13 05:46:33 +00:00
|
|
|
$fname = __METHOD__;
|
|
|
|
$res = ObjectCache::getMainWANInstance()->getWithSetCallback(
|
|
|
|
$globalRulesKey,
|
2015-10-08 02:11:54 +00:00
|
|
|
WANObjectCache::TTL_INDEFINITE,
|
2018-06-26 13:25:03 +00:00
|
|
|
function () use ( $group, $fname, $fields ) {
|
2015-05-13 05:46:33 +00:00
|
|
|
global $wgAbuseFilterCentralDB;
|
|
|
|
|
2018-05-04 19:35:11 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
$fdb = $lbFactory->getMainLB( $wgAbuseFilterCentralDB )->getConnectionRef(
|
2017-08-30 02:51:39 +00:00
|
|
|
DB_REPLICA, [], $wgAbuseFilterCentralDB
|
2015-05-13 05:46:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return iterator_to_array( $fdb->select(
|
|
|
|
'abuse_filter',
|
2018-06-26 13:25:03 +00:00
|
|
|
$fields,
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2015-05-13 05:46:33 +00:00
|
|
|
'af_enabled' => 1,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_global' => 1,
|
|
|
|
'af_group' => $group,
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
2015-05-13 05:46:33 +00:00
|
|
|
$fname
|
|
|
|
) );
|
|
|
|
},
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
|
|
|
'checkKeys' => [ $globalRulesKey ],
|
2015-10-08 02:11:54 +00:00
|
|
|
'lockTSE' => 300
|
2017-06-15 14:23:34 +00:00
|
|
|
]
|
2015-05-13 05:46:33 +00:00
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$filter_matched['global-' . $row->af_id] =
|
2018-03-29 15:33:20 +00:00
|
|
|
self::checkFilter( $row, $vars, $title, 'global-', $mode );
|
2009-03-19 02:40:48 +00:00
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2017-02-26 09:55:31 +00:00
|
|
|
if ( $title instanceof Title && self::$condCount > $wgAbuseFilterConditionLimit ) {
|
|
|
|
$actionID = implode( '-', [
|
|
|
|
$title->getPrefixedText(),
|
|
|
|
$vars->getVar( 'user_name' )->toString(),
|
|
|
|
$vars->getVar( 'action' )->toString()
|
|
|
|
] );
|
|
|
|
self::bufferTagsToSetByAction( [ $actionID => [ 'abusefilter-condition-limit' ] ] );
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:33:20 +00:00
|
|
|
if ( $mode === 'execute' ) {
|
|
|
|
// Update statistics, and disable filters which are over-blocking.
|
|
|
|
self::recordStats( $filter_matched, $group );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
return $filter_matched;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param stdClass $row
|
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2017-09-25 20:23:55 +00:00
|
|
|
* @param Title|null $title
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $prefix
|
2018-04-03 15:34:03 +00:00
|
|
|
* @param string $mode 'execute' for edits and logs, 'stash' for cached matches
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-29 17:52:45 +00:00
|
|
|
public static function checkFilter(
|
|
|
|
$row,
|
2018-10-17 01:22:23 +00:00
|
|
|
$vars,
|
2018-04-29 17:52:45 +00:00
|
|
|
Title $title = null,
|
|
|
|
$prefix = '',
|
|
|
|
$mode = 'execute'
|
|
|
|
) {
|
|
|
|
global $wgAbuseFilterProfile, $wgAbuseFilterRuntimeProfile,
|
|
|
|
$wgAbuseFilterSlowFilterRuntimeLimit;
|
2016-04-08 16:22:39 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
$filterID = $prefix . $row->af_id;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-03-29 15:33:20 +00:00
|
|
|
// Record data to be used if profiling is enabled and mode is 'execute'
|
|
|
|
$startConds = self::$condCount;
|
|
|
|
$startTime = microtime( true );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
// Store the row somewhere convenient
|
2016-06-28 22:50:38 +00:00
|
|
|
self::$filterCache[$filterID] = $row;
|
2009-03-30 06:12:12 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$pattern = trim( $row->af_pattern );
|
2015-09-28 18:03:35 +00:00
|
|
|
if (
|
|
|
|
self::checkConditions(
|
|
|
|
$pattern,
|
|
|
|
$vars,
|
2018-04-04 21:14:25 +00:00
|
|
|
// Ignore errors
|
|
|
|
true
|
2015-09-28 18:03:35 +00:00
|
|
|
)
|
|
|
|
) {
|
2009-03-30 06:12:12 +00:00
|
|
|
// Record match.
|
|
|
|
$result = true;
|
|
|
|
} else {
|
|
|
|
// Record non-match.
|
|
|
|
$result = false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-09-25 20:23:55 +00:00
|
|
|
$timeTaken = microtime( true ) - $startTime;
|
|
|
|
$condsUsed = self::$condCount - $startConds;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-03-29 15:33:20 +00:00
|
|
|
if ( $wgAbuseFilterProfile && $mode === 'execute' ) {
|
2016-04-08 13:51:54 +00:00
|
|
|
self::recordProfilingResult( $row->af_id, $timeTaken, $condsUsed );
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-09-25 20:23:55 +00:00
|
|
|
$runtime = $timeTaken * 1000;
|
2018-03-29 15:33:20 +00:00
|
|
|
if ( $mode === 'execute' && $wgAbuseFilterRuntimeProfile &&
|
|
|
|
$runtime > $wgAbuseFilterSlowFilterRuntimeLimit ) {
|
2017-09-25 20:23:55 +00:00
|
|
|
self::recordSlowFilter( $filterID, $runtime, $condsUsed, $result, $title );
|
|
|
|
}
|
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-09-25 20:23:55 +00:00
|
|
|
/**
|
|
|
|
* Logs slow filter's runtime data for later analysis
|
|
|
|
*
|
|
|
|
* @param string $filterId
|
|
|
|
* @param float $runtime
|
|
|
|
* @param int $totalConditions
|
2018-06-08 06:11:09 +00:00
|
|
|
* @param bool $matched
|
2017-09-25 20:23:55 +00:00
|
|
|
* @param Title|null $title
|
|
|
|
*/
|
|
|
|
private static function recordSlowFilter(
|
|
|
|
$filterId, $runtime, $totalConditions, $matched, Title $title = null
|
|
|
|
) {
|
|
|
|
$title = $title ? $title->getPrefixedText() : '';
|
|
|
|
|
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilterSlow' );
|
|
|
|
$logger->info(
|
|
|
|
'Edit filter {filter_id} on {wiki} is taking longer than expected',
|
|
|
|
[
|
|
|
|
'wiki' => wfWikiID(),
|
|
|
|
'filter_id' => $filterId,
|
|
|
|
'title' => $title,
|
|
|
|
'runtime' => $runtime,
|
|
|
|
'matched' => $matched,
|
|
|
|
'total_conditions' => $totalConditions
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-08 13:51:54 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param int $filter
|
2016-04-08 13:51:54 +00:00
|
|
|
*/
|
|
|
|
public static function resetFilterProfile( $filter ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
2016-04-08 13:51:54 +00:00
|
|
|
$countKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'count' );
|
|
|
|
$totalKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'total' );
|
2016-04-08 17:43:02 +00:00
|
|
|
$condsKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'conds' );
|
2016-04-08 13:51:54 +00:00
|
|
|
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->delete( $countKey );
|
|
|
|
$stash->delete( $totalKey );
|
|
|
|
$stash->delete( $condsKey );
|
2016-04-08 13:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param int $filter
|
|
|
|
* @param float $time
|
|
|
|
* @param int $conds
|
2016-04-08 13:51:54 +00:00
|
|
|
*/
|
|
|
|
public static function recordProfilingResult( $filter, $time, $conds ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
// Defer updates to avoid massive (~1 second) edit time increases
|
|
|
|
DeferredUpdates::addCallableUpdate( function () use ( $filter, $time, $conds ) {
|
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
|
|
|
$countKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'count' );
|
|
|
|
$totalKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'total' );
|
|
|
|
$condsKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'conds' );
|
|
|
|
|
|
|
|
$curCount = $stash->get( $countKey );
|
|
|
|
$curTotal = $stash->get( $totalKey );
|
|
|
|
$curConds = $stash->get( $condsKey );
|
|
|
|
|
|
|
|
if ( $curCount ) {
|
|
|
|
$stash->set( $condsKey, $curConds + $conds, 3600 );
|
|
|
|
$stash->set( $totalKey, $curTotal + $time, 3600 );
|
|
|
|
$stash->incr( $countKey );
|
|
|
|
} else {
|
|
|
|
$stash->set( $countKey, 1, 3600 );
|
|
|
|
$stash->set( $totalKey, $time, 3600 );
|
|
|
|
$stash->set( $condsKey, $conds, 3600 );
|
|
|
|
}
|
|
|
|
} );
|
2016-04-08 13:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $filter
|
2016-04-08 13:51:54 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getFilterProfile( $filter ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
2016-04-08 13:51:54 +00:00
|
|
|
$countKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'count' );
|
|
|
|
$totalKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'total' );
|
2016-04-08 17:43:02 +00:00
|
|
|
$condsKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'conds' );
|
2016-04-08 13:51:54 +00:00
|
|
|
|
2016-04-13 18:06:24 +00:00
|
|
|
$curCount = $stash->get( $countKey );
|
|
|
|
$curTotal = $stash->get( $totalKey );
|
|
|
|
$curConds = $stash->get( $condsKey );
|
2016-04-08 13:51:54 +00:00
|
|
|
|
|
|
|
if ( !$curCount ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
return [ 0, 0 ];
|
2016-04-08 13:51:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
// 1000 ms in a sec
|
|
|
|
$timeProfile = ( $curTotal / $curCount ) * 1000;
|
|
|
|
// Return in ms, rounded to 2dp
|
|
|
|
$timeProfile = round( $timeProfile, 2 );
|
2016-04-08 13:51:54 +00:00
|
|
|
|
2016-04-08 17:43:02 +00:00
|
|
|
$condProfile = ( $curConds / $curCount );
|
2016-04-08 13:51:54 +00:00
|
|
|
$condProfile = round( $condProfile, 0 );
|
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
return [ $timeProfile, $condProfile ];
|
2016-04-08 13:51:54 +00:00
|
|
|
}
|
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Utility function to decode global-$index to $index. Returns false if not global
|
|
|
|
*
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $filter
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return string|bool
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
public static function decodeGlobalName( $filter ) {
|
2018-08-25 15:35:15 +00:00
|
|
|
if ( strpos( $filter, 'global-' ) === 0 ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
return substr( $filter, strlen( 'global-' ) );
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string[] $filters
|
|
|
|
* @return array[]
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
public static function getConsequencesForFilters( $filters ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$globalFilters = [];
|
|
|
|
$localFilters = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $filters as $filter ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$globalIndex = self::decodeGlobalName( $filter );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $globalIndex ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$globalFilters[] = $globalIndex;
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-03-30 06:12:12 +00:00
|
|
|
$localFilters[] = $filter;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
global $wgAbuseFilterCentralDB;
|
|
|
|
// Load local filter info
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2008-06-27 06:18:51 +00:00
|
|
|
// Retrieve the consequences.
|
2017-06-15 14:23:34 +00:00
|
|
|
$consequences = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( count( $localFilters ) ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$consequences = self::loadConsequencesFromDB( $dbr, $localFilters );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( count( $globalFilters ) ) {
|
2017-08-30 02:51:39 +00:00
|
|
|
$fdb = wfGetDB( DB_REPLICA, [], $wgAbuseFilterCentralDB );
|
2013-07-09 11:57:22 +00:00
|
|
|
$consequences = $consequences + self::loadConsequencesFromDB( $fdb, $globalFilters, 'global-' );
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
return $consequences;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-12-22 22:09:33 +00:00
|
|
|
* @param IDatabase $dbr
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string[] $filters
|
|
|
|
* @param string $prefix
|
|
|
|
* @return array[]
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function loadConsequencesFromDB( $dbr, $filters, $prefix = '' ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$actionsByFilter = [];
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $filters as $filter ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$actionsByFilter[$prefix . $filter] = [];
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$res = $dbr->select(
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'abuse_filter_action', 'abuse_filter' ],
|
2009-10-07 13:57:06 +00:00
|
|
|
'*',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'af_id' => $filters ],
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__,
|
2017-06-15 14:23:34 +00:00
|
|
|
[],
|
|
|
|
[ 'abuse_filter_action' => [ 'LEFT JOIN', 'afa_filter=af_id' ] ]
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
// Categorise consequences by filter.
|
2016-06-03 18:01:56 +00:00
|
|
|
global $wgAbuseFilterRestrictions;
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
if ( $row->af_throttled
|
2016-06-03 18:01:56 +00:00
|
|
|
&& !empty( $wgAbuseFilterRestrictions[$row->afa_consequence] )
|
2015-09-28 18:03:35 +00:00
|
|
|
) {
|
2018-04-04 21:14:25 +00:00
|
|
|
// Don't do the action
|
2009-02-13 01:40:57 +00:00
|
|
|
} elseif ( $row->afa_filter != $row->af_id ) {
|
2018-06-26 13:25:03 +00:00
|
|
|
// We probably got a NULL, as it's a LEFT JOIN. Don't add it.
|
2009-01-27 20:18:58 +00:00
|
|
|
} else {
|
2017-06-15 14:23:34 +00:00
|
|
|
$actionsByFilter[$prefix . $row->afa_filter][$row->afa_consequence] = [
|
2009-02-07 09:34:11 +00:00
|
|
|
'action' => $row->afa_consequence,
|
2018-04-04 11:46:58 +00:00
|
|
|
'parameters' => array_filter( explode( "\n", $row->afa_parameters ) )
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-01-27 20:18:58 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
return $actionsByFilter;
|
|
|
|
}
|
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
2018-04-16 15:37:10 +00:00
|
|
|
* Executes a set of actions.
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string[] $filters
|
|
|
|
* @param Title $title
|
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2018-09-13 12:00:53 +00:00
|
|
|
* @param User $user
|
2013-01-08 14:52:49 +00:00
|
|
|
* @return Status returns the operation's status. $status->isOK() will return true if
|
|
|
|
* there were no actions taken, false otherwise. $status->getValue() will return
|
2017-01-02 11:41:29 +00:00
|
|
|
* an array listing the actions taken. $status->getErrors() etc. will provide
|
2013-01-08 14:52:49 +00:00
|
|
|
* the errors and warnings to be shown to the user to explain the actions.
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function executeFilterActions( $filters, $title, $vars, User $user ) {
|
2013-10-28 05:47:14 +00:00
|
|
|
global $wgMainCacheType;
|
2009-03-30 06:12:12 +00:00
|
|
|
|
|
|
|
$actionsByFilter = self::getConsequencesForFilters( $filters );
|
2017-06-15 14:23:34 +00:00
|
|
|
$actionsTaken = array_fill_keys( $filters, [] );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$messages = [];
|
2018-02-20 12:36:32 +00:00
|
|
|
// Accumulator to track max block to issue
|
|
|
|
$maxExpiry = -1;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2018-03-10 12:18:49 +00:00
|
|
|
global $wgAbuseFilterDisallowGlobalLocalBlocks, $wgAbuseFilterRestrictions,
|
2018-03-15 22:44:45 +00:00
|
|
|
$wgAbuseFilterBlockDuration, $wgAbuseFilterAnonBlockDuration;
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $actionsByFilter as $filter => $actions ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
// Special-case handling for warnings.
|
2018-03-10 12:18:49 +00:00
|
|
|
$filter_public_comments = self::getFilter( $filter )->af_public_comments;
|
2009-01-23 19:23:44 +00:00
|
|
|
|
2017-01-02 11:42:06 +00:00
|
|
|
$global_filter = self::decodeGlobalName( $filter ) !== false;
|
2012-08-01 21:29:06 +00:00
|
|
|
|
2018-10-03 12:02:00 +00:00
|
|
|
// If the filter has "throttle" enabled and throttling is available via object
|
2013-10-28 05:47:14 +00:00
|
|
|
// caching, check to see if the user has hit the throttle.
|
|
|
|
if ( !empty( $actions['throttle'] ) && $wgMainCacheType !== CACHE_NONE ) {
|
2009-01-23 19:23:44 +00:00
|
|
|
$parameters = $actions['throttle']['parameters'];
|
|
|
|
$throttleId = array_shift( $parameters );
|
|
|
|
list( $rateCount, $ratePeriod ) = explode( ',', array_shift( $parameters ) );
|
|
|
|
|
|
|
|
$hitThrottle = false;
|
|
|
|
|
|
|
|
// The rest are throttle-types.
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $parameters as $throttleType ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
$hitThrottle = $hitThrottle || self::isThrottled(
|
2015-09-28 18:03:35 +00:00
|
|
|
$throttleId, $throttleType, $title, $rateCount, $ratePeriod, $global_filter );
|
2009-01-23 19:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unset( $actions['throttle'] );
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( !$hitThrottle ) {
|
2009-01-23 19:23:44 +00:00
|
|
|
$actionsTaken[$filter][] = 'throttle';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-08-03 21:55:35 +00:00
|
|
|
if ( $wgAbuseFilterDisallowGlobalLocalBlocks && $global_filter ) {
|
2016-06-03 18:01:56 +00:00
|
|
|
$actions = array_diff_key( $actions, array_filter( $wgAbuseFilterRestrictions ) );
|
2012-08-03 21:55:35 +00:00
|
|
|
}
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
if ( !empty( $actions['warn'] ) ) {
|
|
|
|
$parameters = $actions['warn']['parameters'];
|
2018-07-14 14:15:52 +00:00
|
|
|
$action = $vars->getVar( 'action' )->toString();
|
|
|
|
// Generate a unique key to determine whether the user has already been warned.
|
|
|
|
// We'll warn again if one of these changes: session, page, triggered filter or action
|
|
|
|
$warnKey = 'abusefilter-warned-' . md5( $title->getPrefixedText() ) .
|
|
|
|
'-' . $filter . '-' . $action;
|
2013-07-31 16:46:43 +00:00
|
|
|
|
|
|
|
// Make sure the session is started prior to using it
|
2018-03-01 22:44:46 +00:00
|
|
|
$session = SessionManager::getGlobalSession();
|
|
|
|
$session->persist();
|
2013-07-31 16:46:43 +00:00
|
|
|
|
2018-03-01 22:44:46 +00:00
|
|
|
if ( !isset( $session[$warnKey] ) || !$session[$warnKey] ) {
|
|
|
|
$session[$warnKey] = true;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Threaten them a little bit
|
2014-10-04 14:42:46 +00:00
|
|
|
if ( isset( $parameters[0] ) ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
$msg = $parameters[0];
|
|
|
|
} else {
|
|
|
|
$msg = 'abusefilter-warning';
|
|
|
|
}
|
2018-03-10 12:18:49 +00:00
|
|
|
$messages[] = [ $msg, $filter_public_comments, $filter ];
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2009-01-23 19:23:44 +00:00
|
|
|
$actionsTaken[$filter][] = 'warn';
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
// Don't do anything else.
|
|
|
|
continue;
|
2009-01-23 19:23:19 +00:00
|
|
|
} else {
|
|
|
|
// We already warned them
|
2018-03-01 22:44:46 +00:00
|
|
|
$session[$warnKey] = false;
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
unset( $actions['warn'] );
|
|
|
|
}
|
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
// Prevent double warnings
|
2016-06-03 18:01:56 +00:00
|
|
|
if ( count( array_intersect_key( $actions, array_filter( $wgAbuseFilterRestrictions ) ) ) > 0 &&
|
2015-09-28 18:03:35 +00:00
|
|
|
!empty( $actions['disallow'] )
|
|
|
|
) {
|
2009-01-29 23:24:24 +00:00
|
|
|
unset( $actions['disallow'] );
|
|
|
|
}
|
|
|
|
|
2018-02-20 12:36:32 +00:00
|
|
|
// Find out the max expiry to issue the longest triggered block.
|
|
|
|
// Need to check here since methods like user->getBlock() aren't available
|
|
|
|
if ( !empty( $actions['block'] ) ) {
|
|
|
|
$parameters = $actions['block']['parameters'];
|
|
|
|
|
|
|
|
if ( count( $parameters ) === 3 ) {
|
|
|
|
// New type of filters with custom block
|
2018-09-13 12:00:53 +00:00
|
|
|
if ( $user->isAnon() ) {
|
2018-02-20 12:36:32 +00:00
|
|
|
$expiry = $parameters[1];
|
|
|
|
} else {
|
|
|
|
$expiry = $parameters[2];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Old type with fixed expiry
|
2018-09-13 12:00:53 +00:00
|
|
|
if ( $user->isAnon() && $wgAbuseFilterAnonBlockDuration !== null ) {
|
2018-02-20 12:36:32 +00:00
|
|
|
// The user isn't logged in and the anon block duration
|
|
|
|
// doesn't default to $wgAbuseFilterBlockDuration.
|
|
|
|
$expiry = $wgAbuseFilterAnonBlockDuration;
|
|
|
|
} else {
|
|
|
|
$expiry = $wgAbuseFilterBlockDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentExpiry = SpecialBlock::parseExpiryInput( $expiry );
|
|
|
|
if ( $currentExpiry > SpecialBlock::parseExpiryInput( $maxExpiry ) ) {
|
|
|
|
// Save the parameters to issue the block with
|
|
|
|
$maxExpiry = $expiry;
|
|
|
|
$blockValues = [
|
|
|
|
self::getFilter( $filter )->af_public_comments,
|
|
|
|
$filter,
|
|
|
|
is_array( $parameters ) && in_array( 'blocktalk', $parameters )
|
|
|
|
];
|
|
|
|
}
|
2018-03-16 09:01:33 +00:00
|
|
|
unset( $actions['block'] );
|
2018-02-20 12:36:32 +00:00
|
|
|
}
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
// Do the rest of the actions
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $actions as $action => $info ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
$newMsg = self::takeConsequenceAction(
|
2016-06-28 22:50:38 +00:00
|
|
|
$action,
|
|
|
|
$info['parameters'],
|
|
|
|
$title,
|
|
|
|
$vars,
|
|
|
|
self::getFilter( $filter )->af_public_comments,
|
2018-09-13 12:00:53 +00:00
|
|
|
$filter,
|
|
|
|
$user
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
if ( $newMsg !== null ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
$messages[] = $newMsg;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
$actionsTaken[$filter][] = $action;
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2018-02-20 12:36:32 +00:00
|
|
|
// Since every filter has been analysed, we now know what the
|
|
|
|
// longest block duration is, so we can issue the block if
|
|
|
|
// maxExpiry has been changed.
|
|
|
|
if ( $maxExpiry !== -1 ) {
|
|
|
|
self::doAbuseFilterBlock(
|
|
|
|
[
|
|
|
|
'desc' => $blockValues[0],
|
|
|
|
'number' => $blockValues[1]
|
|
|
|
],
|
2018-09-13 12:00:53 +00:00
|
|
|
$user->getName(),
|
2018-02-20 12:36:32 +00:00
|
|
|
$maxExpiry,
|
|
|
|
true,
|
|
|
|
$blockValues[2]
|
|
|
|
);
|
|
|
|
$message = [
|
|
|
|
'abusefilter-blocked-display',
|
|
|
|
$blockValues[0],
|
|
|
|
$blockValues[1]
|
|
|
|
];
|
|
|
|
// Manually add the message. If we're here, there is one.
|
|
|
|
$messages[] = $message;
|
|
|
|
$actionsTaken[ $blockValues[1] ][] = 'block';
|
|
|
|
}
|
|
|
|
|
2015-02-04 18:25:21 +00:00
|
|
|
return self::buildStatus( $actionsTaken, $messages );
|
2013-01-08 14:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a Status object as returned by executeFilterActions() from the list of
|
|
|
|
* actions taken and the corresponding list of messages.
|
|
|
|
*
|
|
|
|
* @param array[] $actionsTaken associative array mapping each filter to the list if
|
|
|
|
* actions taken because of that filter.
|
2018-04-16 15:37:10 +00:00
|
|
|
* @param array[] $messages a list of arrays, where each array contains a message key
|
2013-01-08 14:52:49 +00:00
|
|
|
* followed by any message parameters.
|
|
|
|
*
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
protected static function buildStatus( array $actionsTaken, array $messages ) {
|
|
|
|
$status = Status::newGood( $actionsTaken );
|
|
|
|
|
|
|
|
foreach ( $messages as $msg ) {
|
2018-06-08 03:16:42 +00:00
|
|
|
$status->fatal( ...$msg );
|
2013-01-08 14:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param AbuseFilterVariableHolder $vars
|
|
|
|
* @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
|
2016-06-13 11:53:50 +00:00
|
|
|
* @param string $mode Use 'execute' to run filters and log or 'stash' to only cache matches
|
2013-01-08 14:52:49 +00:00
|
|
|
* @return Status
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2016-06-13 11:53:50 +00:00
|
|
|
public static function filterAction(
|
2018-09-13 12:00:53 +00:00
|
|
|
AbuseFilterVariableHolder $vars, Title $title, $group, User $user, $mode = 'execute'
|
2016-06-13 11:53:50 +00:00
|
|
|
) {
|
2018-09-13 12:00:53 +00:00
|
|
|
global $wgRequest, $wgAbuseFilterRuntimeProfile, $wgAbuseFilterLogIP;
|
2016-05-15 15:35:33 +00:00
|
|
|
|
2016-06-28 22:50:38 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'StashEdit' );
|
|
|
|
$statsd = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
2009-03-05 02:43:05 +00:00
|
|
|
// Add vars from extensions
|
2017-06-15 14:23:34 +00:00
|
|
|
Hooks::run( 'AbuseFilter-filterAction', [ &$vars, $title ] );
|
2009-03-19 02:05:58 +00:00
|
|
|
$vars->setVar( 'context', 'filter' );
|
2009-04-01 03:59:58 +00:00
|
|
|
$vars->setVar( 'timestamp', time() );
|
2016-06-28 22:50:38 +00:00
|
|
|
|
2016-06-13 11:53:50 +00:00
|
|
|
// Get the stash key based on the relevant "input" variables
|
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
2017-03-09 22:08:04 +00:00
|
|
|
$stashKey = self::getStashKey( $cache, $vars, $group );
|
2016-06-28 22:50:38 +00:00
|
|
|
$isForEdit = ( $vars->getVar( 'action' )->toString() === 'edit' );
|
2016-06-13 11:53:50 +00:00
|
|
|
|
2017-08-24 14:52:49 +00:00
|
|
|
if ( $wgAbuseFilterRuntimeProfile ) {
|
|
|
|
$startTime = microtime( true );
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:53:50 +00:00
|
|
|
$filter_matched = false;
|
2016-06-28 22:50:38 +00:00
|
|
|
if ( $mode === 'execute' && $isForEdit ) {
|
2016-06-13 11:53:50 +00:00
|
|
|
// Check the filter edit stash results first
|
2016-06-28 22:50:38 +00:00
|
|
|
$cacheData = $cache->get( $stashKey );
|
|
|
|
if ( $cacheData ) {
|
|
|
|
$filter_matched = $cacheData['matches'];
|
|
|
|
// Merge in any tags to apply to recent changes entries
|
|
|
|
self::bufferTagsToSetByAction( $cacheData['tags'] );
|
|
|
|
}
|
2016-06-13 11:53:50 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2016-06-13 11:53:50 +00:00
|
|
|
if ( is_array( $filter_matched ) ) {
|
2016-08-30 03:37:14 +00:00
|
|
|
if ( $isForEdit && $mode !== 'stash' ) {
|
2016-06-28 22:50:38 +00:00
|
|
|
$logger->info( __METHOD__ . ": cache hit for '$title' (key $stashKey)." );
|
|
|
|
$statsd->increment( 'abusefilter.check-stash.hit' );
|
|
|
|
}
|
2016-06-13 11:53:50 +00:00
|
|
|
} else {
|
2018-03-29 15:33:20 +00:00
|
|
|
$filter_matched = self::checkAllFilters( $vars, $group, $title, $mode );
|
2016-08-30 03:37:14 +00:00
|
|
|
if ( $isForEdit && $mode !== 'stash' ) {
|
2016-06-28 22:50:38 +00:00
|
|
|
$logger->info( __METHOD__ . ": cache miss for '$title' (key $stashKey)." );
|
|
|
|
$statsd->increment( 'abusefilter.check-stash.miss' );
|
|
|
|
}
|
2016-06-13 11:53:50 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2016-06-13 11:53:50 +00:00
|
|
|
if ( $mode === 'stash' ) {
|
|
|
|
// Save the filter stash result and do nothing further
|
2016-06-28 22:50:38 +00:00
|
|
|
$cacheData = [ 'matches' => $filter_matched, 'tags' => self::$tagsToSet ];
|
2017-08-24 14:52:49 +00:00
|
|
|
|
|
|
|
// Add runtime metrics in cache for later use
|
|
|
|
if ( $wgAbuseFilterRuntimeProfile ) {
|
|
|
|
$cacheData['condCount'] = self::$condCount;
|
|
|
|
$cacheData['runtime'] = ( microtime( true ) - $startTime ) * 1000;
|
|
|
|
}
|
|
|
|
|
2016-06-28 22:50:38 +00:00
|
|
|
$cache->set( $stashKey, $cacheData, $cache::TTL_MINUTE );
|
2016-09-26 22:20:58 +00:00
|
|
|
$logger->debug( __METHOD__ . ": cache store for '$title' (key $stashKey)." );
|
2016-06-13 11:53:50 +00:00
|
|
|
$statsd->increment( 'abusefilter.check-stash.store' );
|
|
|
|
|
|
|
|
return Status::newGood();
|
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
$matched_filters = array_keys( array_filter( $filter_matched ) );
|
|
|
|
|
2017-08-24 14:52:49 +00:00
|
|
|
// Save runtime metrics only on edits
|
|
|
|
if ( $wgAbuseFilterRuntimeProfile && $mode === 'execute' && $isForEdit ) {
|
|
|
|
if ( $cacheData ) {
|
|
|
|
$runtime = $cacheData['runtime'];
|
|
|
|
$condCount = $cacheData['condCount'];
|
|
|
|
} else {
|
|
|
|
$runtime = ( microtime( true ) - $startTime ) * 1000;
|
|
|
|
$condCount = self::$condCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::recordRuntimeProfilingResult( count( $matched_filters ), $condCount, $runtime );
|
|
|
|
}
|
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
if ( count( $matched_filters ) == 0 ) {
|
2013-09-30 18:37:52 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
} else {
|
2018-09-13 12:00:53 +00:00
|
|
|
$status = self::executeFilterActions( $matched_filters, $title, $vars, $user );
|
2017-08-08 12:03:56 +00:00
|
|
|
$actions_taken = $status->getValue();
|
2013-09-30 18:37:52 +00:00
|
|
|
$action = $vars->getVar( 'ACTION' )->toString();
|
2013-01-08 14:52:49 +00:00
|
|
|
|
2018-09-13 12:00:53 +00:00
|
|
|
// If $user isn't safe to load (e.g. a failure during
|
2016-02-03 21:07:25 +00:00
|
|
|
// AbortAutoAccount), create a dummy anonymous user instead.
|
2016-05-15 15:35:33 +00:00
|
|
|
$user = $user->isSafeToLoad() ? $user : new User;
|
2016-02-03 21:07:25 +00:00
|
|
|
|
2013-09-30 18:37:52 +00:00
|
|
|
// Create a template
|
2017-06-15 14:23:34 +00:00
|
|
|
$log_template = [
|
2016-02-03 21:07:25 +00:00
|
|
|
'afl_user' => $user->getId(),
|
|
|
|
'afl_user_text' => $user->getName(),
|
2017-08-30 02:51:39 +00:00
|
|
|
'afl_timestamp' => wfGetDB( DB_REPLICA )->timestamp( wfTimestampNow() ),
|
2013-09-30 18:37:52 +00:00
|
|
|
'afl_namespace' => $title->getNamespace(),
|
|
|
|
'afl_title' => $title->getDBkey(),
|
2018-07-12 07:26:20 +00:00
|
|
|
'afl_action' => $action,
|
2018-02-23 06:39:38 +00:00
|
|
|
// DB field is not null, so nothing
|
|
|
|
'afl_ip' => ( $wgAbuseFilterLogIP ) ? $wgRequest->getIP() : ""
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2013-09-30 18:37:52 +00:00
|
|
|
// Hack to avoid revealing IPs of people creating accounts
|
2016-02-03 21:07:25 +00:00
|
|
|
if ( !$user->getId() && ( $action == 'createaccount' || $action == 'autocreateaccount' ) ) {
|
2013-09-30 18:37:52 +00:00
|
|
|
$log_template['afl_user_text'] = $vars->getVar( 'accountname' )->toString();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-07-12 07:26:20 +00:00
|
|
|
self::addLogEntries( $actions_taken, $log_template, $vars, $group );
|
2013-09-30 18:37:52 +00:00
|
|
|
}
|
2009-02-02 23:30:48 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
return $status;
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 22:50:38 +00:00
|
|
|
/**
|
|
|
|
* @param string $id Filter ID (integer or "global-<integer>")
|
|
|
|
* @return stdClass|null DB row
|
|
|
|
*/
|
2016-07-15 21:55:13 +00:00
|
|
|
public static function getFilter( $id ) {
|
2016-06-28 22:50:38 +00:00
|
|
|
global $wgAbuseFilterCentralDB;
|
|
|
|
|
|
|
|
if ( !isset( self::$filterCache[$id] ) ) {
|
2017-01-02 11:42:06 +00:00
|
|
|
$globalIndex = self::decodeGlobalName( $id );
|
|
|
|
if ( $globalIndex ) {
|
2016-06-28 22:50:38 +00:00
|
|
|
// Global wiki filter
|
|
|
|
if ( !$wgAbuseFilterCentralDB ) {
|
2018-04-04 21:14:25 +00:00
|
|
|
return null;
|
2016-06-28 22:50:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 11:42:06 +00:00
|
|
|
$id = $globalIndex;
|
2018-05-04 19:35:11 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
$lb = $lbFactory->getMainLB( $wgAbuseFilterCentralDB );
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = $lb->getConnectionRef( DB_REPLICA, [], $wgAbuseFilterCentralDB );
|
2016-06-28 22:50:38 +00:00
|
|
|
} else {
|
|
|
|
// Local wiki filter
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2016-06-28 22:50:38 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
$fields = [
|
|
|
|
'af_id',
|
|
|
|
'af_pattern',
|
|
|
|
'af_public_comments',
|
|
|
|
'af_timestamp'
|
|
|
|
];
|
|
|
|
|
|
|
|
$row = $dbr->selectRow(
|
|
|
|
'abuse_filter',
|
|
|
|
$fields,
|
|
|
|
[ 'af_id' => $id ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
2016-06-28 22:50:38 +00:00
|
|
|
self::$filterCache[$id] = $row ?: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$filterCache[$id];
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:53:50 +00:00
|
|
|
/**
|
2017-03-09 22:08:04 +00:00
|
|
|
* @param BagOStuff $cache
|
2017-03-02 16:30:30 +00:00
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2016-06-13 11:53:50 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-03-09 22:08:04 +00:00
|
|
|
private static function getStashKey(
|
|
|
|
BagOStuff $cache, AbuseFilterVariableHolder $vars, $group
|
|
|
|
) {
|
2016-06-28 20:46:01 +00:00
|
|
|
$inputVars = $vars->exportNonLazyVars();
|
2016-06-13 11:53:50 +00:00
|
|
|
// Exclude noisy fields that have superficial changes
|
2018-08-25 15:03:03 +00:00
|
|
|
$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 );
|
2016-06-13 11:53:50 +00:00
|
|
|
ksort( $inputVars );
|
|
|
|
$hash = md5( serialize( $inputVars ) );
|
|
|
|
|
2017-03-09 22:08:04 +00:00
|
|
|
return $cache->makeKey(
|
2016-06-13 11:53:50 +00:00
|
|
|
'abusefilter',
|
|
|
|
'check-stash',
|
|
|
|
$group,
|
2016-06-28 22:50:38 +00:00
|
|
|
$hash,
|
|
|
|
'v1'
|
2016-06-13 11:53:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param array[] $actions_taken
|
|
|
|
* @param array $log_template
|
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function addLogEntries( $actions_taken, $log_template, $vars, $group = 'default' ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$central_log_template = [
|
2009-10-07 13:57:06 +00:00
|
|
|
'afl_wiki' => wfWikiID(),
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$log_rows = [];
|
|
|
|
$central_log_rows = [];
|
|
|
|
$logged_local_filters = [];
|
|
|
|
$logged_global_filters = [];
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $actions_taken as $filter => $actions ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$globalIndex = self::decodeGlobalName( $filter );
|
2009-01-23 19:23:19 +00:00
|
|
|
$thisLog = $log_template;
|
|
|
|
$thisLog['afl_filter'] = $filter;
|
2009-01-23 19:23:44 +00:00
|
|
|
$thisLog['afl_actions'] = implode( ',', $actions );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Don't log if we were only throttling.
|
2009-02-13 01:40:57 +00:00
|
|
|
if ( $thisLog['afl_actions'] != 'throttle' ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
$log_rows[] = $thisLog;
|
2009-03-30 06:12:12 +00:00
|
|
|
// Global logging
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $globalIndex ) {
|
2012-03-02 22:02:40 +00:00
|
|
|
$title = Title::makeTitle( $thisLog['afl_namespace'], $thisLog['afl_title'] );
|
2009-03-30 06:12:12 +00:00
|
|
|
$centralLog = $thisLog + $central_log_template;
|
|
|
|
$centralLog['afl_filter'] = $globalIndex;
|
|
|
|
$centralLog['afl_title'] = $title->getPrefixedText();
|
|
|
|
$centralLog['afl_namespace'] = 0;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
$central_log_rows[] = $centralLog;
|
|
|
|
$logged_global_filters[] = $globalIndex;
|
2018-07-16 09:02:03 +00:00
|
|
|
} else {
|
|
|
|
$logged_local_filters[] = $filter;
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2008-09-21 13:08:10 +00:00
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !count( $log_rows ) ) {
|
2008-09-21 13:08:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
// Only store the var dump if we're actually going to add log rows.
|
|
|
|
$var_dump = self::storeVarDump( $vars );
|
2018-04-04 21:14:25 +00:00
|
|
|
// To distinguish from stuff stored directly
|
|
|
|
$var_dump = "stored-text:$var_dump";
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-13 01:40:57 +00:00
|
|
|
// Increment trigger counter
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->incr( self::filterMatchesKey() );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$local_log_ids = [];
|
2012-12-15 17:14:42 +00:00
|
|
|
global $wgAbuseFilterNotifications, $wgAbuseFilterNotificationsPrivate;
|
2013-10-14 19:39:35 +00:00
|
|
|
foreach ( $log_rows as $data ) {
|
2012-04-14 10:10:35 +00:00
|
|
|
$data['afl_var_dump'] = $var_dump;
|
|
|
|
$data['afl_id'] = $dbw->nextSequenceValue( 'abuse_filter_log_afl_id_seq' );
|
|
|
|
$dbw->insert( 'abuse_filter_log', $data, __METHOD__ );
|
2017-05-11 00:02:15 +00:00
|
|
|
$local_log_ids[] = $data['afl_id'] = $dbw->insertId();
|
2016-12-21 14:56:32 +00:00
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// logentry-abusefilter-hit
|
2013-10-26 07:01:14 +00:00
|
|
|
$entry = new ManualLogEntry( 'abusefilter', 'hit' );
|
|
|
|
// Construct a user object
|
|
|
|
$user = User::newFromId( $data['afl_user'] );
|
|
|
|
$user->setName( $data['afl_user_text'] );
|
|
|
|
$entry->setPerformer( $user );
|
|
|
|
$entry->setTarget( Title::makeTitle( $data['afl_namespace'], $data['afl_title'] ) );
|
|
|
|
// Additional info
|
2017-06-15 14:23:34 +00:00
|
|
|
$entry->setParameters( [
|
2015-09-28 18:03:35 +00:00
|
|
|
'action' => $data['afl_action'],
|
|
|
|
'filter' => $data['afl_filter'],
|
2013-10-26 07:01:14 +00:00
|
|
|
'actions' => $data['afl_actions'],
|
2015-09-28 18:03:35 +00:00
|
|
|
'log' => $data['afl_id'],
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2013-10-26 07:01:14 +00:00
|
|
|
|
|
|
|
// Send data to CheckUser if installed and we
|
|
|
|
// aren't already sending a notification to recentchanges
|
2018-02-03 03:01:26 +00:00
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'CheckUser' )
|
2015-09-28 18:03:35 +00:00
|
|
|
&& strpos( $wgAbuseFilterNotifications, 'rc' ) === false
|
|
|
|
) {
|
2013-10-26 07:01:14 +00:00
|
|
|
$rc = $entry->getRecentChange();
|
|
|
|
CheckUserHooks::updateCheckUserData( $rc );
|
|
|
|
}
|
|
|
|
|
2012-04-14 10:10:35 +00:00
|
|
|
if ( $wgAbuseFilterNotifications !== false ) {
|
2012-12-15 17:14:42 +00:00
|
|
|
if ( self::filterHidden( $data['afl_filter'] ) && !$wgAbuseFilterNotificationsPrivate ) {
|
2012-12-13 10:29:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-04-14 10:10:35 +00:00
|
|
|
$entry->publish( 0, $wgAbuseFilterNotifications );
|
|
|
|
}
|
|
|
|
}
|
2009-02-13 01:40:57 +00:00
|
|
|
|
2013-08-28 21:30:59 +00:00
|
|
|
$method = __METHOD__;
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $logged_local_filters ) ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
// Update hit-counter.
|
2013-08-28 21:30:59 +00:00
|
|
|
$dbw->onTransactionPreCommitOrIdle(
|
2015-09-28 18:03:35 +00:00
|
|
|
function () use ( $dbw, $logged_local_filters, $method ) {
|
2013-08-28 21:30:59 +00:00
|
|
|
$dbw->update( 'abuse_filter',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'af_hit_count=af_hit_count+1' ],
|
|
|
|
[ 'af_id' => $logged_local_filters ],
|
2013-08-28 21:30:59 +00:00
|
|
|
$method
|
|
|
|
);
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$global_log_ids = [];
|
2012-02-10 23:41:05 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
// Global stuff
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $logged_global_filters ) ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$vars->computeDBVars();
|
2013-01-07 00:02:41 +00:00
|
|
|
$global_var_dump = self::storeVarDump( $vars, true );
|
2009-03-30 06:12:12 +00:00
|
|
|
$global_var_dump = "stored-text:$global_var_dump";
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $central_log_rows as $index => $data ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$central_log_rows[$index]['afl_var_dump'] = $global_var_dump;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wgAbuseFilterCentralDB;
|
2017-06-15 14:23:34 +00:00
|
|
|
$fdb = wfGetDB( DB_MASTER, [], $wgAbuseFilterCentralDB );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $central_log_rows as $row ) {
|
2012-02-10 23:41:05 +00:00
|
|
|
$fdb->insert( 'abuse_filter_log', $row, __METHOD__ );
|
2018-09-13 09:57:55 +00:00
|
|
|
$global_log_ids[] = $fdb->insertId();
|
2012-02-10 23:41:05 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-08-28 21:30:59 +00:00
|
|
|
$fdb->onTransactionPreCommitOrIdle(
|
2015-09-28 18:03:35 +00:00
|
|
|
function () use ( $fdb, $logged_global_filters, $method ) {
|
2013-08-28 21:30:59 +00:00
|
|
|
$fdb->update( 'abuse_filter',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'af_hit_count=af_hit_count+1' ],
|
|
|
|
[ 'af_id' => $logged_global_filters ],
|
2013-08-28 21:30:59 +00:00
|
|
|
$method
|
|
|
|
);
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
$vars->setVar( 'global_log_ids', $global_log_ids );
|
|
|
|
$vars->setVar( 'local_log_ids', $local_log_ids );
|
|
|
|
|
2009-02-13 01:40:57 +00:00
|
|
|
// Check for emergency disabling.
|
2017-07-23 07:03:40 +00:00
|
|
|
$total = $stash->get( self::filterUsedKey( $group ) );
|
2012-11-07 13:45:58 +00:00
|
|
|
self::checkEmergencyDisable( $group, $logged_local_filters, $total );
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a var dump to External Storage or the text table
|
|
|
|
* Some of this code is stolen from Revision::insertOn and friends
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param AbuseFilterVariableHolder $vars
|
|
|
|
* @param bool $global
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return int|null
|
2009-10-07 13:57:06 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function storeVarDump( $vars, $global = false ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
global $wgCompressRevisions;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
// Get all variables yet set and compute old and new wikitext if not yet done
|
|
|
|
// as those are needed for the diff view on top of the abuse log pages
|
2017-06-15 14:23:34 +00:00
|
|
|
$vars = $vars->dumpAllVars( [ 'old_wikitext', 'new_wikitext' ] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
// Vars is an array with native PHP data types (non-objects) now
|
|
|
|
$text = serialize( $vars );
|
2017-06-15 14:23:34 +00:00
|
|
|
$flags = [ 'nativeDataArray' ];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( $wgCompressRevisions ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( function_exists( 'gzdeflate' ) ) {
|
2009-04-24 03:27:14 +00:00
|
|
|
$text = gzdeflate( $text );
|
|
|
|
$flags[] = 'gzip';
|
|
|
|
}
|
2009-02-27 03:06:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
// Store to ExternalStore if applicable
|
2009-03-30 06:12:12 +00:00
|
|
|
global $wgDefaultExternalStore, $wgAbuseFilterCentralDB;
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $wgDefaultExternalStore ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $global ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$text = ExternalStore::insertToForeignDefault( $text, $wgAbuseFilterCentralDB );
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-03-30 06:12:12 +00:00
|
|
|
$text = ExternalStore::insertToDefault( $text );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-02-27 03:06:19 +00:00
|
|
|
$flags[] = 'external';
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( !$text ) {
|
2009-02-27 03:06:19 +00:00
|
|
|
// Not mission-critical, just return nothing
|
|
|
|
return null;
|
2009-04-17 08:08:35 +00:00
|
|
|
}
|
2009-02-27 03:06:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
// Store to text table
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $global ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER, [], $wgAbuseFilterCentralDB );
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-03-30 06:12:12 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-11-27 15:36:30 +00:00
|
|
|
$old_id = $dbw->nextSequenceValue( 'text_old_id_seq' );
|
2009-02-27 03:06:19 +00:00
|
|
|
$dbw->insert( 'text',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2015-09-28 18:03:35 +00:00
|
|
|
'old_id' => $old_id,
|
|
|
|
'old_text' => $text,
|
2009-02-27 03:06:19 +00:00
|
|
|
'old_flags' => implode( ',', $flags ),
|
2017-06-15 14:23:34 +00:00
|
|
|
], __METHOD__
|
2009-02-27 03:06:19 +00:00
|
|
|
);
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2015-02-04 18:25:21 +00:00
|
|
|
return $dbw->insertId();
|
2009-02-27 03:06:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a var dump from External Storage or the text table
|
|
|
|
* Some of this code is stolen from Revision::loadText et al
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $stored_dump
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2018-04-29 17:52:45 +00:00
|
|
|
* @return array|object|AbuseFilterVariableHolder|bool
|
2009-10-07 13:57:06 +00:00
|
|
|
*/
|
2009-02-27 03:06:19 +00:00
|
|
|
public static function loadVarDump( $stored_dump ) {
|
2018-07-13 22:34:54 +00:00
|
|
|
// Backward compatibility
|
2015-03-21 23:13:02 +00:00
|
|
|
if ( substr( $stored_dump, 0, strlen( 'stored-text:' ) ) !== 'stored-text:' ) {
|
|
|
|
$data = unserialize( $stored_dump );
|
|
|
|
if ( is_array( $data ) ) {
|
|
|
|
$vh = new AbuseFilterVariableHolder;
|
|
|
|
foreach ( $data as $name => $value ) {
|
|
|
|
$vh->setVar( $name, $value );
|
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2015-03-21 23:13:02 +00:00
|
|
|
return $vh;
|
|
|
|
} else {
|
|
|
|
return $data;
|
|
|
|
}
|
2009-02-27 03:06:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
$text_id = substr( $stored_dump, strlen( 'stored-text:' ) );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
$text_row = $dbr->selectRow(
|
2009-10-07 13:57:06 +00:00
|
|
|
'text',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'old_text', 'old_flags' ],
|
|
|
|
[ 'old_id' => $text_id ],
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$text_row ) {
|
2009-02-27 03:06:19 +00:00
|
|
|
return new AbuseFilterVariableHolder;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
$flags = explode( ',', $text_row->old_flags );
|
|
|
|
$text = $text_row->old_text;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
if ( in_array( 'external', $flags ) ) {
|
|
|
|
$text = ExternalStore::fetchFromURL( $text );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
if ( in_array( 'gzip', $flags ) ) {
|
|
|
|
$text = gzinflate( $text );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
$obj = unserialize( $text );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
if ( in_array( 'nativeDataArray', $flags ) ) {
|
|
|
|
$vars = $obj;
|
|
|
|
$obj = new AbuseFilterVariableHolder();
|
2015-09-28 18:03:35 +00:00
|
|
|
foreach ( $vars as $key => $value ) {
|
2013-01-07 00:02:41 +00:00
|
|
|
$obj->setVar( $key, $value );
|
|
|
|
}
|
2018-09-13 16:42:47 +00:00
|
|
|
// If old variable names are used, make sure to keep them
|
|
|
|
if ( count( array_intersect_key( self::getDeprecatedVariables(), $obj->mVars ) ) !== 0 ) {
|
|
|
|
$obj->mVarsVersion = 1;
|
|
|
|
}
|
2013-01-07 00:02:41 +00:00
|
|
|
}
|
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
return $obj;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $action
|
|
|
|
* @param array $parameters
|
|
|
|
* @param Title $title
|
|
|
|
* @param AbuseFilterVariableHolder $vars
|
|
|
|
* @param string $rule_desc
|
|
|
|
* @param int|string $rule_number
|
2018-09-13 12:00:53 +00:00
|
|
|
* @param User $user
|
2013-01-08 14:52:49 +00:00
|
|
|
*
|
|
|
|
* @return array|null a message describing the action that was taken,
|
|
|
|
* or null if no action was taken. The message is given as an array
|
|
|
|
* containing the message key followed by any message parameters.
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function takeConsequenceAction( $action, $parameters, $title,
|
|
|
|
$vars, $rule_desc, $rule_number, User $user ) {
|
2012-05-08 07:48:19 +00:00
|
|
|
global $wgAbuseFilterCustomActionsHandlers, $wgRequest;
|
2011-12-28 00:26:13 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = null;
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
switch ( $action ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'disallow':
|
2018-07-15 15:01:32 +00:00
|
|
|
if ( isset( $parameters[0] ) ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$message = [ $parameters[0], $rule_desc, $rule_number ];
|
2008-06-27 06:18:51 +00:00
|
|
|
} else {
|
|
|
|
// Generic message.
|
2017-06-15 14:23:34 +00:00
|
|
|
$message = [
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-disallowed',
|
2013-06-30 17:32:31 +00:00
|
|
|
$rule_desc,
|
|
|
|
$rule_number
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-09-18 13:01:50 +00:00
|
|
|
case 'rangeblock':
|
2017-11-01 01:53:29 +00:00
|
|
|
global $wgAbuseFilterRangeBlockSize, $wgBlockCIDRLimit;
|
|
|
|
|
|
|
|
$ip = $wgRequest->getIP();
|
|
|
|
if ( IP::isIPv6( $ip ) ) {
|
|
|
|
$CIDRsize = max( $wgAbuseFilterRangeBlockSize['IPv6'], $wgBlockCIDRLimit['IPv6'] );
|
|
|
|
} else {
|
|
|
|
$CIDRsize = max( $wgAbuseFilterRangeBlockSize['IPv4'], $wgBlockCIDRLimit['IPv4'] );
|
|
|
|
}
|
|
|
|
$blockCIDR = $ip . '/' . $CIDRsize;
|
2016-03-14 17:17:23 +00:00
|
|
|
self::doAbuseFilterBlock(
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2016-03-14 17:17:23 +00:00
|
|
|
'desc' => $rule_desc,
|
|
|
|
'number' => $rule_number
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
2017-11-01 01:53:29 +00:00
|
|
|
IP::sanitizeRange( $blockCIDR ),
|
2016-03-14 17:17:23 +00:00
|
|
|
'1 week',
|
|
|
|
false
|
|
|
|
);
|
2008-09-18 13:01:50 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$message = [
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-blocked-display',
|
2013-06-30 17:32:31 +00:00
|
|
|
$rule_desc,
|
|
|
|
$rule_number
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2008-09-18 13:01:50 +00:00
|
|
|
break;
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'degroup':
|
2018-09-13 12:00:53 +00:00
|
|
|
if ( !$user->isAnon() ) {
|
2018-06-26 13:25:03 +00:00
|
|
|
// Remove all groups from the user.
|
2018-09-13 12:00:53 +00:00
|
|
|
$groups = $user->getGroups();
|
2018-07-12 22:25:02 +00:00
|
|
|
// Make sure that the stored var dump contains user groups, since we may
|
|
|
|
// need them if reverting this degroup via Special:AbuseFilter/revert
|
|
|
|
$vars->setVar( 'user_groups', $groups );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $groups as $group ) {
|
2018-09-13 12:00:53 +00:00
|
|
|
$user->removeGroup( $group );
|
2008-08-02 13:51:29 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$message = [
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-degrouped',
|
2013-06-30 17:32:31 +00:00
|
|
|
$rule_desc,
|
|
|
|
$rule_number
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2009-01-26 18:52:41 +00:00
|
|
|
// Don't log it if there aren't any groups being removed!
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !count( $groups ) ) {
|
2009-01-26 18:52:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-07-12 17:46:13 +00:00
|
|
|
$logEntry = new ManualLogEntry( 'rights', 'rights' );
|
|
|
|
$logEntry->setPerformer( self::getFilterUser() );
|
2018-09-13 12:00:53 +00:00
|
|
|
$logEntry->setTarget( $user->getUserPage() );
|
2018-07-12 17:46:13 +00:00
|
|
|
$logEntry->setComment(
|
2015-09-28 18:03:35 +00:00
|
|
|
wfMessage(
|
|
|
|
'abusefilter-degroupreason',
|
|
|
|
$rule_desc,
|
|
|
|
$rule_number
|
2018-07-12 17:46:13 +00:00
|
|
|
)->inContentLanguage()->text()
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
2018-07-12 17:46:13 +00:00
|
|
|
$logEntry->setParameters( [
|
|
|
|
'4::oldgroups' => $groups,
|
|
|
|
'5::newgroups' => []
|
|
|
|
] );
|
|
|
|
$logEntry->publish( $logEntry->insert() );
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
|
|
|
case 'blockautopromote':
|
2018-09-13 12:00:53 +00:00
|
|
|
if ( !$user->isAnon() ) {
|
2018-04-04 21:14:25 +00:00
|
|
|
// Block for 3-7 days.
|
|
|
|
$blockPeriod = (int)mt_rand( 3 * 86400, 7 * 86400 );
|
2015-05-21 21:54:30 +00:00
|
|
|
ObjectCache::getMainStashInstance()->set(
|
2018-09-13 12:00:53 +00:00
|
|
|
self::autoPromoteBlockKey( $user ), true, $blockPeriod
|
2015-05-21 21:54:30 +00:00
|
|
|
);
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$message = [
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-autopromote-blocked',
|
2013-06-30 17:32:31 +00:00
|
|
|
$rule_desc,
|
|
|
|
$rule_number
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2008-08-02 13:51:29 +00:00
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
|
|
|
|
2018-02-20 12:36:32 +00:00
|
|
|
case 'block':
|
|
|
|
// Do nothing, handled at the end of executeFilterActions. Here for completeness.
|
|
|
|
break;
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'flag':
|
|
|
|
// Do nothing. Here for completeness.
|
|
|
|
break;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2009-01-28 19:08:18 +00:00
|
|
|
case 'tag':
|
|
|
|
// Mark with a tag on recentchanges.
|
2017-06-15 14:23:34 +00:00
|
|
|
$actionID = implode( '-', [
|
2018-09-13 12:00:53 +00:00
|
|
|
$title->getPrefixedText(), $user->getName(),
|
2015-09-28 18:03:35 +00:00
|
|
|
$vars->getVar( 'ACTION' )->toString()
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2009-01-28 19:08:18 +00:00
|
|
|
|
2016-06-28 22:50:38 +00:00
|
|
|
self::bufferTagsToSetByAction( [ $actionID => $parameters ] );
|
2009-01-28 19:08:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( isset( $wgAbuseFilterCustomActionsHandlers[$action] ) ) {
|
2011-11-06 01:15:55 +00:00
|
|
|
$custom_function = $wgAbuseFilterCustomActionsHandlers[$action];
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( is_callable( $custom_function ) ) {
|
|
|
|
$msg = call_user_func(
|
|
|
|
$custom_function,
|
|
|
|
$action,
|
|
|
|
$parameters,
|
|
|
|
$title,
|
|
|
|
$vars,
|
|
|
|
$rule_desc,
|
|
|
|
$rule_number
|
|
|
|
);
|
2011-11-06 01:15:55 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( isset( $msg ) ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$message = [ $msg ];
|
2011-11-06 01:15:55 +00:00
|
|
|
}
|
2011-11-06 21:05:03 +00:00
|
|
|
} else {
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
|
|
|
$logger->debug( "Unrecognised action $action" );
|
2011-11-06 01:15:55 +00:00
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
return $message;
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-06-28 22:50:38 +00:00
|
|
|
/**
|
|
|
|
* @param array[] $tagsByAction Map of (integer => string[])
|
|
|
|
*/
|
2016-07-27 20:22:39 +00:00
|
|
|
private static function bufferTagsToSetByAction( array $tagsByAction ) {
|
2018-03-09 14:20:53 +00:00
|
|
|
global $wgAbuseFilterActions;
|
|
|
|
if ( isset( $wgAbuseFilterActions['tag'] ) && $wgAbuseFilterActions['tag'] ) {
|
2017-10-31 19:59:37 +00:00
|
|
|
foreach ( $tagsByAction as $actionID => $tags ) {
|
|
|
|
if ( !isset( self::$tagsToSet[$actionID] ) ) {
|
|
|
|
self::$tagsToSet[$actionID] = $tags;
|
|
|
|
} else {
|
|
|
|
self::$tagsToSet[$actionID] = array_merge( self::$tagsToSet[$actionID], $tags );
|
|
|
|
}
|
2016-06-28 22:50:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 17:17:23 +00:00
|
|
|
/**
|
|
|
|
* Perform a block by the AbuseFilter system user
|
|
|
|
* @param array $rule should have 'desc' and 'number'
|
|
|
|
* @param string $target
|
|
|
|
* @param string $expiry
|
|
|
|
* @param bool $isAutoBlock
|
2017-08-22 18:23:01 +00:00
|
|
|
* @param bool $preventEditOwnUserTalk
|
2016-03-14 17:17:23 +00:00
|
|
|
*/
|
2017-08-22 18:23:01 +00:00
|
|
|
protected static function doAbuseFilterBlock(
|
|
|
|
array $rule,
|
|
|
|
$target,
|
|
|
|
$expiry,
|
|
|
|
$isAutoBlock,
|
|
|
|
$preventEditOwnUserTalk = false
|
|
|
|
) {
|
2017-07-23 07:03:40 +00:00
|
|
|
$filterUser = self::getFilterUser();
|
2016-03-14 17:17:23 +00:00
|
|
|
$reason = wfMessage(
|
|
|
|
'abusefilter-blockreason',
|
|
|
|
$rule['desc'], $rule['number']
|
|
|
|
)->inContentLanguage()->text();
|
|
|
|
|
|
|
|
$block = new Block();
|
|
|
|
$block->setTarget( $target );
|
|
|
|
$block->setBlocker( $filterUser );
|
|
|
|
$block->mReason = $reason;
|
|
|
|
$block->isHardblock( false );
|
|
|
|
$block->isAutoblocking( $isAutoBlock );
|
|
|
|
$block->prevents( 'createaccount', true );
|
2017-08-22 18:23:01 +00:00
|
|
|
$block->prevents( 'editownusertalk', $preventEditOwnUserTalk );
|
2016-03-14 17:17:23 +00:00
|
|
|
$block->mExpiry = SpecialBlock::parseExpiryInput( $expiry );
|
|
|
|
|
|
|
|
$success = $block->insert();
|
|
|
|
|
|
|
|
if ( $success ) {
|
|
|
|
// Log it only if the block was successful
|
2017-06-15 14:23:34 +00:00
|
|
|
$logParams = [];
|
2016-03-14 17:17:23 +00:00
|
|
|
$logParams['5::duration'] = ( $block->mExpiry === 'infinity' )
|
|
|
|
? 'indefinite'
|
|
|
|
: $expiry;
|
2017-06-15 14:23:34 +00:00
|
|
|
$flags = [ 'nocreate' ];
|
2016-03-14 17:17:23 +00:00
|
|
|
if ( !$block->isAutoblocking() && !IP::isIPAddress( $target ) ) {
|
|
|
|
// Conditionally added same as SpecialBlock
|
|
|
|
$flags[] = 'noautoblock';
|
|
|
|
}
|
2018-03-06 16:07:49 +00:00
|
|
|
if ( $preventEditOwnUserTalk === true ) {
|
|
|
|
$flags[] = 'nousertalk';
|
|
|
|
}
|
2016-03-14 17:17:23 +00:00
|
|
|
$logParams['6::flags'] = implode( ',', $flags );
|
|
|
|
|
|
|
|
$logEntry = new ManualLogEntry( 'block', 'block' );
|
|
|
|
$logEntry->setTarget( Title::makeTitle( NS_USER, $target ) );
|
|
|
|
$logEntry->setComment( $reason );
|
|
|
|
$logEntry->setPerformer( $filterUser );
|
|
|
|
$logEntry->setParameters( $logParams );
|
2017-06-15 14:23:34 +00:00
|
|
|
$blockIds = array_merge( [ $success['id'] ], $success['autoIds'] );
|
|
|
|
$logEntry->setRelations( [ 'ipb_id' => $blockIds ] );
|
2016-03-14 17:17:23 +00:00
|
|
|
$logEntry->publish( $logEntry->insert() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $throttleId
|
2018-04-29 17:52:45 +00:00
|
|
|
* @param string $types
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param string $rateCount
|
|
|
|
* @param string $ratePeriod
|
|
|
|
* @param bool $global
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function isThrottled( $throttleId, $types, $title, $rateCount,
|
2015-09-28 18:03:35 +00:00
|
|
|
$ratePeriod, $global = false
|
|
|
|
) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
2012-08-01 21:29:06 +00:00
|
|
|
$key = self::throttleKey( $throttleId, $types, $title, $global );
|
2016-04-13 18:06:24 +00:00
|
|
|
$count = intval( $stash->get( $key ) );
|
2009-02-03 23:44:47 +00:00
|
|
|
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
|
|
|
$logger->debug( "Got value $count for throttle key $key" );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $count > 0 ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->incr( $key );
|
2009-02-03 23:44:47 +00:00
|
|
|
$count++;
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger->debug( "Incremented throttle key $key" );
|
2008-06-27 06:18:51 +00:00
|
|
|
} else {
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger->debug( "Added throttle key $key with value 1" );
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->add( $key, 1, $ratePeriod );
|
2009-02-03 23:44:47 +00:00
|
|
|
$count = 1;
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-02-03 23:44:47 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $count > $rateCount ) {
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger->debug( "Throttle $key hit value $count -- maximum is $rateCount." );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
// THROTTLED
|
|
|
|
return true;
|
2009-02-03 23:44:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger->debug( "Throttle $key not hit!" );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
// NOT THROTTLED
|
|
|
|
return false;
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param Title $title
|
2013-10-15 12:27:10 +00:00
|
|
|
* @return int|string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function throttleIdentifier( $type, $title ) {
|
2012-05-08 07:48:19 +00:00
|
|
|
global $wgUser, $wgRequest;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
switch ( $type ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'ip':
|
2012-05-08 07:48:19 +00:00
|
|
|
$identifier = $wgRequest->getIP();
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
$identifier = $wgUser->getId();
|
|
|
|
break;
|
|
|
|
case 'range':
|
2012-05-08 07:48:19 +00:00
|
|
|
$identifier = substr( IP::toHex( $wgRequest->getIP() ), 0, 4 );
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
|
|
|
case 'creationdate':
|
|
|
|
$reg = $wgUser->getRegistration();
|
2009-10-07 13:57:06 +00:00
|
|
|
$identifier = $reg - ( $reg % 86400 );
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
|
|
|
case 'editcount':
|
|
|
|
// Hack for detecting different single-purpose accounts.
|
|
|
|
$identifier = $wgUser->getEditCount();
|
|
|
|
break;
|
|
|
|
case 'site':
|
2013-10-15 12:27:10 +00:00
|
|
|
$identifier = 1;
|
|
|
|
break;
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'page':
|
2013-10-15 12:27:10 +00:00
|
|
|
$identifier = $title->getPrefixedText();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$identifier = 0;
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
return $identifier;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $throttleId
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param Title $title
|
|
|
|
* @param bool $global
|
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function throttleKey( $throttleId, $type, $title, $global = false ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$types = explode( ',', $type );
|
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$identifiers = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $types as $subtype ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
$identifiers[] = self::throttleIdentifier( $subtype, $title );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-09-17 23:52:05 +00:00
|
|
|
$identifier = sha1( implode( ':', $identifiers ) );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-08-01 21:29:06 +00:00
|
|
|
global $wgAbuseFilterIsCentral, $wgAbuseFilterCentralDB;
|
|
|
|
|
|
|
|
if ( $global && !$wgAbuseFilterIsCentral ) {
|
2017-08-11 02:57:33 +00:00
|
|
|
list( $globalSite, $globalPrefix ) = wfSplitWikiID( $wgAbuseFilterCentralDB );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2012-08-01 21:29:06 +00:00
|
|
|
return wfForeignMemcKey(
|
|
|
|
$globalSite, $globalPrefix,
|
|
|
|
'abusefilter', 'throttle', $throttleId, $type, $identifier );
|
|
|
|
}
|
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
return wfMemcKey( 'abusefilter', 'throttle', $throttleId, $type, $identifier );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-08-03 21:55:35 +00:00
|
|
|
/**
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return string
|
2012-08-03 21:55:35 +00:00
|
|
|
*/
|
|
|
|
public static function getGlobalRulesKey( $group ) {
|
|
|
|
global $wgAbuseFilterIsCentral, $wgAbuseFilterCentralDB;
|
|
|
|
|
|
|
|
if ( !$wgAbuseFilterIsCentral ) {
|
2017-08-11 02:57:33 +00:00
|
|
|
list( $globalSite, $globalPrefix ) = wfSplitWikiID( $wgAbuseFilterCentralDB );
|
2012-08-03 21:55:35 +00:00
|
|
|
|
|
|
|
return wfForeignMemcKey(
|
|
|
|
$globalSite, $globalPrefix,
|
|
|
|
'abusefilter', 'rules', $group
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return wfMemcKey( 'abusefilter', 'rules', $group );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param User $user
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function autoPromoteBlockKey( $user ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
return wfMemcKey( 'abusefilter', 'block-autopromote', $user->getId() );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-08-03 21:55:35 +00:00
|
|
|
* Update statistics, and disable filters which are over-blocking.
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param bool[] $filters
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function recordStats( $filters, $group = 'default' ) {
|
2018-04-04 18:29:50 +00:00
|
|
|
global $wgAbuseFilterConditionLimit, $wgAbuseFilterProfileActionsCap;
|
2016-04-13 18:06:24 +00:00
|
|
|
|
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
// Figure out if we've triggered overflows and blocks.
|
2009-10-07 13:57:06 +00:00
|
|
|
$overflow_triggered = ( self::$condCount > $wgAbuseFilterConditionLimit );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2008-07-17 02:43:45 +00:00
|
|
|
$overflow_key = self::filterLimitReachedKey();
|
2012-11-07 13:45:58 +00:00
|
|
|
$total_key = self::filterUsedKey( $group );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-04-13 18:06:24 +00:00
|
|
|
$total = $stash->get( $total_key );
|
2008-07-17 02:43:45 +00:00
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
$storage_period = self::$statsStoragePeriod;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-04-04 18:29:50 +00:00
|
|
|
if ( !$total || $total > $wgAbuseFilterProfileActionsCap ) {
|
2014-11-07 12:15:28 +00:00
|
|
|
// This is for if the total doesn't exist, or has gone past 10,000.
|
2009-10-07 13:57:06 +00:00
|
|
|
// Recreate all the keys at the same time, so they expire together.
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->set( $total_key, 0, $storage_period );
|
|
|
|
$stash->set( $overflow_key, 0, $storage_period );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $filters as $filter => $matched ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->set( self::filterMatchesKey( $filter ), 0, $storage_period );
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->set( self::filterMatchesKey(), 0, $storage_period );
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->incr( $total_key );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Increment overflow counter, if our condition limit overflowed
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $overflow_triggered ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->incr( $overflow_key );
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 14:52:49 +00:00
|
|
|
/**
|
|
|
|
* Record runtime profiling data
|
|
|
|
*
|
|
|
|
* @param int $totalFilters
|
|
|
|
* @param int $totalConditions
|
|
|
|
* @param float $runtime
|
|
|
|
*/
|
|
|
|
private static function recordRuntimeProfilingResult( $totalFilters, $totalConditions, $runtime ) {
|
|
|
|
$keyPrefix = 'abusefilter.runtime-profile.' . wfWikiID() . '.';
|
|
|
|
|
|
|
|
$statsd = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
$statsd->timing( $keyPrefix . 'runtime', $runtime );
|
|
|
|
$statsd->timing( $keyPrefix . 'total_filters', $totalFilters );
|
|
|
|
$statsd->timing( $keyPrefix . 'total_conditions', $totalConditions );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string[] $filters
|
|
|
|
* @param int $total
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function checkEmergencyDisable( $group, $filters, $total ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
global $wgAbuseFilterEmergencyDisableThreshold, $wgAbuseFilterEmergencyDisableCount,
|
2017-07-08 18:49:13 +00:00
|
|
|
$wgAbuseFilterEmergencyDisableAge;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash = ObjectCache::getMainStashInstance();
|
2012-11-07 13:45:58 +00:00
|
|
|
foreach ( $filters as $filter ) {
|
2018-06-26 13:25:03 +00:00
|
|
|
// Determine emergency disable values for this action
|
2015-09-28 18:03:35 +00:00
|
|
|
$emergencyDisableThreshold =
|
|
|
|
self::getEmergencyValue( $wgAbuseFilterEmergencyDisableThreshold, $group );
|
|
|
|
$filterEmergencyDisableCount =
|
|
|
|
self::getEmergencyValue( $wgAbuseFilterEmergencyDisableCount, $group );
|
|
|
|
$emergencyDisableAge =
|
|
|
|
self::getEmergencyValue( $wgAbuseFilterEmergencyDisableAge, $group );
|
2012-10-23 13:14:34 +00:00
|
|
|
|
2009-02-13 18:30:34 +00:00
|
|
|
// Increment counter
|
2016-04-13 18:06:24 +00:00
|
|
|
$matchCount = $stash->get( self::filterMatchesKey( $filter ) );
|
2009-02-13 18:30:34 +00:00
|
|
|
|
|
|
|
// Handle missing keys...
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !$matchCount ) {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->set( self::filterMatchesKey( $filter ), 1, self::$statsStoragePeriod );
|
2009-02-13 18:30:34 +00:00
|
|
|
} else {
|
2016-04-13 18:06:24 +00:00
|
|
|
$stash->incr( self::filterMatchesKey( $filter ) );
|
2009-02-13 18:30:34 +00:00
|
|
|
}
|
|
|
|
$matchCount++;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-13 18:30:34 +00:00
|
|
|
// Figure out if the filter is subject to being deleted.
|
2016-06-28 22:50:38 +00:00
|
|
|
$filter_age = wfTimestamp( TS_UNIX, self::getFilter( $filter )->af_timestamp );
|
2012-10-23 13:14:34 +00:00
|
|
|
$throttle_exempt_time = $filter_age + $emergencyDisableAge;
|
2009-02-13 18:30:34 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $total && $throttle_exempt_time > time()
|
2012-10-23 13:14:34 +00:00
|
|
|
&& $matchCount > $filterEmergencyDisableCount
|
2015-09-28 18:03:35 +00:00
|
|
|
&& ( $matchCount / $total ) > $emergencyDisableThreshold
|
|
|
|
) {
|
2009-02-13 18:30:34 +00:00
|
|
|
// More than $wgAbuseFilterEmergencyDisableCount matches,
|
2012-10-23 13:14:34 +00:00
|
|
|
// constituting more than $emergencyDisableThreshold
|
2009-02-13 18:30:34 +00:00
|
|
|
// (a fraction) of last few edits. Disable it.
|
2016-07-22 06:16:12 +00:00
|
|
|
DeferredUpdates::addUpdate(
|
|
|
|
new AutoCommitUpdate(
|
|
|
|
wfGetDB( DB_MASTER ),
|
|
|
|
__METHOD__,
|
|
|
|
function ( IDatabase $dbw, $fname ) use ( $filter ) {
|
|
|
|
$dbw->update( 'abuse_filter',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'af_throttled' => 1 ],
|
|
|
|
[ 'af_id' => $filter ],
|
2016-07-22 06:16:12 +00:00
|
|
|
$fname
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-10-23 13:14:34 +00:00
|
|
|
/**
|
|
|
|
* @param array $emergencyValue
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2012-10-23 13:14:34 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function getEmergencyValue( array $emergencyValue, $group ) {
|
2018-05-31 18:53:03 +00:00
|
|
|
return $emergencyValue[$group] ?? $emergencyValue['default'];
|
2012-10-23 13:14:34 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-07-17 02:43:45 +00:00
|
|
|
public static function filterLimitReachedKey() {
|
|
|
|
return wfMemcKey( 'abusefilter', 'stats', 'overflow' );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string|null $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function filterUsedKey( $group = null ) {
|
|
|
|
return wfMemcKey( 'abusefilter', 'stats', 'total', $group );
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string|null $filter
|
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2008-07-17 02:43:45 +00:00
|
|
|
public static function filterMatchesKey( $filter = null ) {
|
|
|
|
return wfMemcKey( 'abusefilter', 'stats', 'matches', $filter );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @return User
|
|
|
|
*/
|
2008-07-09 07:02:13 +00:00
|
|
|
public static function getFilterUser() {
|
2015-09-17 15:31:51 +00:00
|
|
|
$username = wfMessage( 'abusefilter-blocker' )->inContentLanguage()->text();
|
2017-08-08 12:03:56 +00:00
|
|
|
$user = User::newSystemUser( $username, [ 'steal' => true ] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-04-16 07:25:47 +00:00
|
|
|
// Promote user to 'sysop' so it doesn't look
|
|
|
|
// like an unprivileged account is blocking users
|
|
|
|
if ( !in_array( 'sysop', $user->getGroups() ) ) {
|
|
|
|
$user->addGroup( 'sysop' );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-07-09 07:02:13 +00:00
|
|
|
return $user;
|
|
|
|
}
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2018-03-30 06:55:03 +00:00
|
|
|
/**
|
|
|
|
* Extract values for syntax highlight
|
|
|
|
*
|
|
|
|
* @param bool $canEdit
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getAceConfig( $canEdit ) {
|
|
|
|
$values = self::getBuilderValues();
|
2018-02-18 13:44:17 +00:00
|
|
|
$deprecatedVars = self::getDeprecatedVariables();
|
2018-06-27 18:05:52 +00:00
|
|
|
|
|
|
|
$builderVariables = implode( '|', array_keys( $values['vars'] ) );
|
2018-03-30 06:55:03 +00:00
|
|
|
$builderFunctions = implode( '|', array_keys( AbuseFilterParser::$mFunctions ) );
|
2018-04-05 13:58:03 +00:00
|
|
|
// AbuseFilterTokenizer::$keywords also includes constants (true, false and null),
|
|
|
|
// but Ace redefines these constants afterwards so this will not be an issue
|
|
|
|
$builderKeywords = implode( '|', AbuseFilterTokenizer::$keywords );
|
2018-06-27 18:05:52 +00:00
|
|
|
// Extract operators from tokenizer like we do in AbuseFilterParserTest
|
|
|
|
$operators = implode( '|', array_map( function ( $op ) {
|
|
|
|
return preg_quote( $op, '/' );
|
|
|
|
}, AbuseFilterTokenizer::$operators ) );
|
|
|
|
$deprecatedVariables = implode( '|', array_keys( $deprecatedVars ) );
|
|
|
|
$disabledVariables = implode( '|', array_keys( self::$disabledVars ) );
|
2018-04-05 13:58:03 +00:00
|
|
|
|
2018-03-30 06:55:03 +00:00
|
|
|
return [
|
|
|
|
'variables' => $builderVariables,
|
|
|
|
'functions' => $builderFunctions,
|
2018-04-05 13:58:03 +00:00
|
|
|
'keywords' => $builderKeywords,
|
2018-06-27 18:05:52 +00:00
|
|
|
'operators' => $operators,
|
|
|
|
'deprecated' => $deprecatedVariables,
|
|
|
|
'disabled' => $disabledVariables,
|
2018-03-30 06:55:03 +00:00
|
|
|
'aceReadOnly' => !$canEdit
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:22:37 +00:00
|
|
|
/**
|
|
|
|
* Build input and button for loading a filter
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function buildFilterLoader() {
|
2018-03-15 17:22:37 +00:00
|
|
|
$loadText =
|
|
|
|
new OOUI\TextInputWidget(
|
|
|
|
[
|
|
|
|
'type' => 'number',
|
|
|
|
'name' => 'wpInsertFilter',
|
|
|
|
'id' => 'mw-abusefilter-load-filter'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$loadButton =
|
2018-04-29 17:52:45 +00:00
|
|
|
new OOUI\ButtonWidget(
|
2018-03-15 17:22:37 +00:00
|
|
|
[
|
|
|
|
'label' => wfMessage( 'abusefilter-test-load' )->text(),
|
|
|
|
'id' => 'mw-abusefilter-load'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$loadGroup =
|
|
|
|
new OOUI\ActionFieldLayout(
|
|
|
|
$loadText,
|
|
|
|
$loadButton,
|
|
|
|
[
|
|
|
|
'label' => wfMessage( 'abusefilter-test-load-filter' )->text()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
// CSS class for reducing default input field width
|
|
|
|
$loadDiv =
|
|
|
|
Xml::tags(
|
|
|
|
'div',
|
|
|
|
[ 'class' => 'mw-abusefilter-load-filter-id' ],
|
|
|
|
$loadGroup
|
|
|
|
);
|
|
|
|
return $loadDiv;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:24:46 +00:00
|
|
|
/**
|
|
|
|
* Check whether a filter is allowed to use a tag
|
|
|
|
*
|
|
|
|
* @param string $tag Tag name
|
|
|
|
* @return Status
|
|
|
|
*/
|
2018-07-06 14:43:12 +00:00
|
|
|
public static function isAllowedTag( $tag ) {
|
2018-05-02 19:24:46 +00:00
|
|
|
$tagNameStatus = ChangeTags::isTagNameValid( $tag );
|
|
|
|
|
|
|
|
if ( !$tagNameStatus->isGood() ) {
|
|
|
|
return $tagNameStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalStatus = Status::newGood();
|
|
|
|
|
|
|
|
$canAddStatus =
|
|
|
|
ChangeTags::canAddTagsAccompanyingChange(
|
|
|
|
[ $tag ]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $canAddStatus->isGood() ) {
|
|
|
|
return $finalStatus;
|
|
|
|
}
|
|
|
|
|
2018-07-06 14:43:12 +00:00
|
|
|
if ( $tag === 'abusefilter-condition-limit' ) {
|
2018-07-09 23:00:59 +00:00
|
|
|
$finalStatus->fatal( 'abusefilter-tag-reserved' );
|
2018-07-06 14:43:12 +00:00
|
|
|
return $finalStatus;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:24:46 +00:00
|
|
|
$alreadyDefinedTags = [];
|
|
|
|
AbuseFilterHooks::onListDefinedTags( $alreadyDefinedTags );
|
|
|
|
|
|
|
|
if ( in_array( $tag, $alreadyDefinedTags, true ) ) {
|
|
|
|
return $finalStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
$canCreateTagStatus = ChangeTags::canCreateTag( $tag );
|
|
|
|
if ( $canCreateTagStatus->isGood() ) {
|
|
|
|
return $finalStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finalStatus->fatal( 'abusefilter-edit-bad-tags' );
|
|
|
|
return $finalStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether user input for the filter editing form is valid and if so saves the filter
|
|
|
|
*
|
|
|
|
* @param AbuseFilterViewEdit $page
|
|
|
|
* @param int|string $filter
|
|
|
|
* @param WebRequest $request
|
|
|
|
* @param stdClass $newRow
|
|
|
|
* @param array $actions
|
|
|
|
* @return Status
|
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function saveFilter( $page, $filter, $request, $newRow, $actions ) {
|
2018-05-02 19:24:46 +00:00
|
|
|
$validationStatus = Status::newGood();
|
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
// Check the syntax
|
2018-05-02 19:24:46 +00:00
|
|
|
$syntaxerr = self::checkSyntax( $request->getVal( 'wpFilterRules' ) );
|
|
|
|
if ( $syntaxerr !== true ) {
|
|
|
|
$validationStatus->error( 'abusefilter-edit-badsyntax', $syntaxerr[0] );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
// Check for missing required fields (title and pattern)
|
|
|
|
$missing = [];
|
|
|
|
if ( !$request->getVal( 'wpFilterRules' ) ||
|
|
|
|
trim( $request->getVal( 'wpFilterRules' ) ) === '' ) {
|
|
|
|
$missing[] = wfMessage( 'abusefilter-edit-field-conditions' )->escaped();
|
|
|
|
}
|
|
|
|
if ( !$request->getVal( 'wpFilterDescription' ) ) {
|
|
|
|
$missing[] = wfMessage( 'abusefilter-edit-field-description' )->escaped();
|
|
|
|
}
|
|
|
|
if ( count( $missing ) !== 0 ) {
|
2018-07-05 17:57:30 +00:00
|
|
|
$missing = $page->getLanguage()->commaList( $missing );
|
2018-05-02 19:24:46 +00:00
|
|
|
$validationStatus->error( 'abusefilter-edit-missingfields', $missing );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow setting as deleted an active filter
|
2018-03-23 09:13:41 +00:00
|
|
|
if ( $request->getCheck( 'wpFilterEnabled' ) == true &&
|
|
|
|
$request->getCheck( 'wpFilterDeleted' ) == true ) {
|
2018-05-02 19:24:46 +00:00
|
|
|
$validationStatus->error( 'abusefilter-edit-deleting-enabled' );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've activated the 'tag' option, check the arguments for validity.
|
|
|
|
if ( !empty( $actions['tag'] ) ) {
|
|
|
|
foreach ( $actions['tag']['parameters'] as $tag ) {
|
|
|
|
$status = self::isAllowedTag( $tag );
|
|
|
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
$err = $status->getErrors();
|
|
|
|
$msg = $err[0]['message'];
|
|
|
|
$validationStatus->error( $msg );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$differences = self::compareVersions(
|
|
|
|
[ $newRow, $actions ],
|
|
|
|
[ $newRow->mOriginalRow, $newRow->mOriginalActions ]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Don't allow adding a new global rule, or updating a
|
|
|
|
// rule that is currently global, without permissions.
|
|
|
|
if ( !$page->canEditFilter( $newRow ) || !$page->canEditFilter( $newRow->mOriginalRow ) ) {
|
|
|
|
$validationStatus->fatal( 'abusefilter-edit-notallowed-global' );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow custom messages on global rules
|
2014-10-04 14:42:46 +00:00
|
|
|
if ( $newRow->af_global == 1 && (
|
|
|
|
$request->getVal( 'wpFilterWarnMessage' ) !== 'abusefilter-warning' ||
|
|
|
|
$request->getVal( 'wpFilterDisallowMessage' ) !== 'abusefilter-disallowed'
|
|
|
|
) ) {
|
2018-05-02 19:24:46 +00:00
|
|
|
$validationStatus->fatal( 'abusefilter-edit-notallowed-global-custom-msg' );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
$origActions = $newRow->mOriginalActions;
|
|
|
|
$wasGlobal = (bool)$newRow->mOriginalRow->af_global;
|
|
|
|
|
|
|
|
unset( $newRow->mOriginalRow );
|
|
|
|
unset( $newRow->mOriginalActions );
|
|
|
|
|
|
|
|
// Check for non-changes
|
|
|
|
if ( !count( $differences ) ) {
|
|
|
|
$validationStatus->setResult( true, false );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for restricted actions
|
2018-07-05 17:57:30 +00:00
|
|
|
$restrictions = $page->getConfig()->get( 'AbuseFilterRestrictions' );
|
2018-05-02 19:24:46 +00:00
|
|
|
if ( count( array_intersect_key(
|
2018-07-05 17:57:30 +00:00
|
|
|
array_filter( $restrictions ),
|
2018-05-02 19:24:46 +00:00
|
|
|
array_merge(
|
|
|
|
array_filter( $actions ),
|
|
|
|
array_filter( $origActions )
|
|
|
|
)
|
|
|
|
) )
|
2018-07-05 17:57:30 +00:00
|
|
|
&& !$page->getUser()->isAllowed( 'abusefilter-modify-restricted' )
|
2018-05-02 19:24:46 +00:00
|
|
|
) {
|
|
|
|
$validationStatus->error( 'abusefilter-edit-restricted' );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything went fine, so let's save the filter
|
|
|
|
list( $new_id, $history_id ) =
|
|
|
|
self::doSaveFilter( $newRow, $differences, $filter, $actions, $wasGlobal, $page );
|
|
|
|
$validationStatus->setResult( true, [ $new_id, $history_id ] );
|
|
|
|
return $validationStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves new filter's info to DB
|
|
|
|
*
|
|
|
|
* @param stdClass $newRow
|
|
|
|
* @param int|string $filter
|
|
|
|
* @param array $differences
|
|
|
|
* @param array $actions
|
|
|
|
* @param bool $wasGlobal
|
|
|
|
* @param AbuseFilterViewEdit $page
|
|
|
|
* @return int[] first element is new ID, second is history ID
|
|
|
|
*/
|
|
|
|
private static function doSaveFilter(
|
|
|
|
$newRow,
|
|
|
|
$differences,
|
|
|
|
$filter,
|
|
|
|
$actions,
|
|
|
|
$wasGlobal,
|
2018-10-17 01:22:23 +00:00
|
|
|
$page
|
2018-05-02 19:24:46 +00:00
|
|
|
) {
|
2018-07-05 17:57:30 +00:00
|
|
|
$user = $page->getUser();
|
2018-05-02 19:24:46 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
// Convert from object to array
|
|
|
|
$newRow = get_object_vars( $newRow );
|
|
|
|
|
|
|
|
// Set last modifier.
|
|
|
|
$newRow['af_timestamp'] = $dbw->timestamp( wfTimestampNow() );
|
2018-07-05 17:57:30 +00:00
|
|
|
$newRow['af_user'] = $user->getId();
|
|
|
|
$newRow['af_user_text'] = $user->getName();
|
2018-05-02 19:24:46 +00:00
|
|
|
|
|
|
|
$dbw->startAtomic( __METHOD__ );
|
|
|
|
|
|
|
|
// Insert MAIN row.
|
|
|
|
if ( $filter == 'new' ) {
|
|
|
|
$new_id = $dbw->nextSequenceValue( 'abuse_filter_af_id_seq' );
|
|
|
|
$is_new = true;
|
|
|
|
} else {
|
|
|
|
$new_id = $filter;
|
|
|
|
$is_new = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset throttled marker, if we're re-enabling it.
|
|
|
|
$newRow['af_throttled'] = $newRow['af_throttled'] && !$newRow['af_enabled'];
|
|
|
|
$newRow['af_id'] = $new_id;
|
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
// T67807: integer 1's & 0's might be better understood than booleans
|
2018-05-02 19:24:46 +00:00
|
|
|
$newRow['af_enabled'] = (int)$newRow['af_enabled'];
|
|
|
|
$newRow['af_hidden'] = (int)$newRow['af_hidden'];
|
|
|
|
$newRow['af_throttled'] = (int)$newRow['af_throttled'];
|
|
|
|
$newRow['af_deleted'] = (int)$newRow['af_deleted'];
|
|
|
|
$newRow['af_global'] = (int)$newRow['af_global'];
|
|
|
|
|
|
|
|
$dbw->replace( 'abuse_filter', [ 'af_id' ], $newRow, __METHOD__ );
|
|
|
|
|
|
|
|
if ( $is_new ) {
|
|
|
|
$new_id = $dbw->insertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions
|
2018-07-05 17:57:30 +00:00
|
|
|
$availableActions = $page->getConfig()->get( 'AbuseFilterActions' );
|
2018-05-02 19:24:46 +00:00
|
|
|
$actionsRows = [];
|
2018-07-05 17:57:30 +00:00
|
|
|
foreach ( array_filter( $availableActions ) as $action => $_ ) {
|
2018-05-02 19:24:46 +00:00
|
|
|
// Check if it's set
|
|
|
|
$enabled = isset( $actions[$action] ) && (bool)$actions[$action];
|
|
|
|
|
|
|
|
if ( $enabled ) {
|
|
|
|
$parameters = $actions[$action]['parameters'];
|
2018-09-05 09:35:07 +00:00
|
|
|
if ( $action === 'throttle' && $parameters[0] === 'new' ) {
|
|
|
|
// FIXME: Do we really need to keep the filter ID inside throttle parameters?
|
|
|
|
// We'd save space, keep things simpler and avoid this hack. Note: if removing
|
|
|
|
// it, a maintenance script will be necessary to clean up the table.
|
|
|
|
$parameters[0] = $new_id;
|
|
|
|
}
|
2018-05-02 19:24:46 +00:00
|
|
|
|
|
|
|
$thisRow = [
|
|
|
|
'afa_filter' => $new_id,
|
|
|
|
'afa_consequence' => $action,
|
|
|
|
'afa_parameters' => implode( "\n", $parameters )
|
|
|
|
];
|
|
|
|
$actionsRows[] = $thisRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a history row
|
|
|
|
$afh_row = [];
|
|
|
|
|
|
|
|
foreach ( self::$history_mappings as $af_col => $afh_col ) {
|
|
|
|
$afh_row[$afh_col] = $newRow[$af_col];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
$displayActions = [];
|
|
|
|
foreach ( $actions as $action ) {
|
|
|
|
$displayActions[$action['action']] = $action['parameters'];
|
|
|
|
}
|
|
|
|
$afh_row['afh_actions'] = serialize( $displayActions );
|
|
|
|
|
|
|
|
$afh_row['afh_changed_fields'] = implode( ',', $differences );
|
|
|
|
|
|
|
|
// Flags
|
|
|
|
$flags = [];
|
|
|
|
if ( $newRow['af_hidden'] ) {
|
|
|
|
$flags[] = 'hidden';
|
|
|
|
}
|
|
|
|
if ( $newRow['af_enabled'] ) {
|
|
|
|
$flags[] = 'enabled';
|
|
|
|
}
|
|
|
|
if ( $newRow['af_deleted'] ) {
|
|
|
|
$flags[] = 'deleted';
|
|
|
|
}
|
|
|
|
if ( $newRow['af_global'] ) {
|
|
|
|
$flags[] = 'global';
|
|
|
|
}
|
|
|
|
|
|
|
|
$afh_row['afh_flags'] = implode( ',', $flags );
|
|
|
|
|
|
|
|
$afh_row['afh_filter'] = $new_id;
|
|
|
|
$afh_row['afh_id'] = $dbw->nextSequenceValue( 'abuse_filter_af_id_seq' );
|
|
|
|
|
|
|
|
// Do the update
|
|
|
|
$dbw->insert( 'abuse_filter_history', $afh_row, __METHOD__ );
|
|
|
|
$history_id = $dbw->insertId();
|
|
|
|
if ( $filter != 'new' ) {
|
|
|
|
$dbw->delete(
|
|
|
|
'abuse_filter_action',
|
|
|
|
[ 'afa_filter' => $filter ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$dbw->insert( 'abuse_filter_action', $actionsRows, __METHOD__ );
|
|
|
|
|
|
|
|
$dbw->endAtomic( __METHOD__ );
|
|
|
|
|
|
|
|
// Invalidate cache if this was a global rule
|
|
|
|
if ( $wasGlobal || $newRow['af_global'] ) {
|
|
|
|
$group = 'default';
|
|
|
|
if ( isset( $newRow['af_group'] ) && $newRow['af_group'] != '' ) {
|
|
|
|
$group = $newRow['af_group'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$globalRulesKey = self::getGlobalRulesKey( $group );
|
|
|
|
ObjectCache::getMainWANInstance()->touchCheckKey( $globalRulesKey );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logging
|
|
|
|
$subtype = $filter === 'new' ? 'create' : 'modify';
|
|
|
|
$logEntry = new ManualLogEntry( 'abusefilter', $subtype );
|
2018-07-05 17:57:30 +00:00
|
|
|
$logEntry->setPerformer( $user );
|
2018-05-02 19:24:46 +00:00
|
|
|
$logEntry->setTarget( $page->getTitle( $new_id ) );
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
'historyId' => $history_id,
|
|
|
|
'newId' => $new_id
|
|
|
|
] );
|
|
|
|
$logid = $logEntry->insert();
|
|
|
|
$logEntry->publish( $logid );
|
|
|
|
|
|
|
|
// Purge the tag list cache so the fetchAllTags hook applies tag changes
|
|
|
|
if ( isset( $actions['tag'] ) ) {
|
|
|
|
AbuseFilterHooks::purgeTagCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::resetFilterProfile( $new_id );
|
|
|
|
return [ $new_id, $history_id ];
|
|
|
|
}
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
/**
|
|
|
|
* Each version is expected to be an array( $row, $actions )
|
|
|
|
* Returns an array of fields that are different.
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param array $version_1
|
|
|
|
* @param array $version_2
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2009-10-07 13:57:06 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function compareVersions( $version_1, $version_2 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$compareFields = [
|
2009-10-07 13:57:06 +00:00
|
|
|
'af_public_comments',
|
|
|
|
'af_pattern',
|
|
|
|
'af_comments',
|
|
|
|
'af_deleted',
|
|
|
|
'af_enabled',
|
2009-03-30 06:12:12 +00:00
|
|
|
'af_hidden',
|
|
|
|
'af_global',
|
2012-05-06 06:44:45 +00:00
|
|
|
'af_group',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
|
|
|
$differences = [];
|
2009-01-26 22:31:02 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
list( $row1, $actions1 ) = $version_1;
|
|
|
|
list( $row2, $actions2 ) = $version_2;
|
2009-01-26 22:31:02 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $compareFields as $field ) {
|
2012-09-27 10:10:10 +00:00
|
|
|
if ( !isset( $row2->$field ) || $row1->$field != $row2->$field ) {
|
2009-01-26 22:31:02 +00:00
|
|
|
$differences[] = $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 18:01:56 +00:00
|
|
|
global $wgAbuseFilterActions;
|
|
|
|
foreach ( array_filter( $wgAbuseFilterActions ) as $action => $_ ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !isset( $actions1[$action] ) && !isset( $actions2[$action] ) ) {
|
2009-01-26 22:31:02 +00:00
|
|
|
// They're both unset
|
2009-10-07 13:57:06 +00:00
|
|
|
} elseif ( isset( $actions1[$action] ) && isset( $actions2[$action] ) ) {
|
2018-06-26 13:25:03 +00:00
|
|
|
// They're both set. Double check needed, e.g. per T180194
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( array_diff( $actions1[$action]['parameters'],
|
2018-03-04 18:26:50 +00:00
|
|
|
$actions2[$action]['parameters'] ) ||
|
|
|
|
array_diff( $actions2[$action]['parameters'],
|
|
|
|
$actions1[$action]['parameters'] ) ) {
|
2009-01-26 22:31:02 +00:00
|
|
|
// Different parameters
|
|
|
|
$differences[] = 'actions';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// One's unset, one's set.
|
|
|
|
$differences[] = 'actions';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique( $differences );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param stdClass $row
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function translateFromHistory( $row ) {
|
2018-06-26 13:25:03 +00:00
|
|
|
// Manually translate into an abuse_filter row.
|
2017-08-07 23:22:48 +00:00
|
|
|
$af_row = new stdClass;
|
2009-01-26 22:31:02 +00:00
|
|
|
|
2009-02-07 09:34:11 +00:00
|
|
|
foreach ( self::$history_mappings as $af_col => $afh_col ) {
|
2009-01-26 22:31:02 +00:00
|
|
|
$af_row->$af_col = $row->$afh_col;
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
// Process flags
|
2009-01-26 22:31:02 +00:00
|
|
|
$af_row->af_deleted = 0;
|
|
|
|
$af_row->af_hidden = 0;
|
|
|
|
$af_row->af_enabled = 0;
|
|
|
|
|
2018-06-26 13:25:03 +00:00
|
|
|
if ( $row->afh_flags !== '' ) {
|
|
|
|
$flags = explode( ',', $row->afh_flags );
|
|
|
|
foreach ( $flags as $flag ) {
|
|
|
|
$col_name = "af_$flag";
|
|
|
|
$af_row->$col_name = 1;
|
|
|
|
}
|
2009-01-26 22:31:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
// Process actions
|
2009-10-07 13:57:06 +00:00
|
|
|
$actions_raw = unserialize( $row->afh_actions );
|
2017-06-15 14:23:34 +00:00
|
|
|
$actions_output = [];
|
2013-02-11 22:30:42 +00:00
|
|
|
if ( is_array( $actions_raw ) ) {
|
|
|
|
foreach ( $actions_raw as $action => $parameters ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$actions_output[$action] = [
|
2013-02-11 22:30:42 +00:00
|
|
|
'action' => $action,
|
|
|
|
'parameters' => $parameters
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2013-02-11 22:30:42 +00:00
|
|
|
}
|
2009-01-26 22:31:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
return [ $af_row, $actions_output ];
|
2009-01-26 22:31:02 +00:00
|
|
|
}
|
2009-01-28 01:26:38 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param string $action
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function getActionDisplay( $action ) {
|
2013-08-18 06:35:16 +00:00
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// abusefilter-action-tag, abusefilter-action-throttle, abusefilter-action-warn,
|
|
|
|
// abusefilter-action-blockautopromote, abusefilter-action-block, abusefilter-action-degroup,
|
|
|
|
// abusefilter-action-rangeblock, abusefilter-action-disallow
|
2018-05-01 12:13:57 +00:00
|
|
|
$display = wfMessage( "abusefilter-action-$action" )->escaped();
|
2018-07-04 15:04:05 +00:00
|
|
|
$display = wfMessage( "abusefilter-action-$action", $display )->isDisabled()
|
|
|
|
? htmlspecialchars( $action )
|
|
|
|
: $display;
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-01-28 01:26:38 +00:00
|
|
|
return $display;
|
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param stdClass $row
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AbuseFilterVariableHolder|null
|
|
|
|
*/
|
2009-01-28 23:54:41 +00:00
|
|
|
public static function getVarsFromRCRow( $row ) {
|
2017-07-13 15:31:19 +00:00
|
|
|
if ( $row->rc_log_type == 'move' ) {
|
2009-03-19 02:05:58 +00:00
|
|
|
$vars = self::getMoveVarsFromRCRow( $row );
|
2009-01-28 23:54:41 +00:00
|
|
|
} elseif ( $row->rc_log_type == 'newusers' ) {
|
2009-03-19 02:05:58 +00:00
|
|
|
$vars = self::getCreateVarsFromRCRow( $row );
|
2017-08-20 11:48:20 +00:00
|
|
|
} elseif ( $row->rc_log_type == 'delete' ) {
|
|
|
|
$vars = self::getDeleteVarsFromRCRow( $row );
|
2017-07-13 15:31:19 +00:00
|
|
|
} elseif ( $row->rc_this_oldid ) {
|
|
|
|
// It's an edit.
|
|
|
|
$vars = self::getEditVarsFromRCRow( $row );
|
2009-03-19 06:18:41 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $vars ) {
|
2009-03-22 02:12:51 +00:00
|
|
|
$vars->setVar( 'context', 'generated' );
|
2013-01-07 17:36:31 +00:00
|
|
|
$vars->setVar( 'timestamp', wfTimestamp( TS_UNIX, $row->rc_timestamp ) );
|
2009-04-01 03:59:58 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:05:58 +00:00
|
|
|
return $vars;
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param stdClass $row
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2009-01-28 23:54:41 +00:00
|
|
|
public static function getCreateVarsFromRCRow( $row ) {
|
2009-03-22 02:46:05 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
$vars->setVar( 'ACTION', ( $row->rc_log_action == 'autocreate' ) ?
|
|
|
|
'autocreateaccount' :
|
|
|
|
'createaccount' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-22 02:46:05 +00:00
|
|
|
$name = Title::makeTitle( $row->rc_namespace, $row->rc_title )->getText();
|
2011-08-24 00:29:26 +00:00
|
|
|
// Add user data if the account was created by a registered user
|
|
|
|
if ( $row->rc_user && $name != $row->rc_user_text ) {
|
|
|
|
$user = User::newFromName( $row->rc_user_text );
|
2013-03-06 06:21:55 +00:00
|
|
|
$vars->addHolders( self::generateUserVars( $user ) );
|
2011-08-24 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
2009-03-22 02:46:05 +00:00
|
|
|
$vars->setVar( 'accountname', $name );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-01-28 23:54:41 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2017-08-20 11:48:20 +00:00
|
|
|
/**
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
|
|
|
public static function getDeleteVarsFromRCRow( $row ) {
|
|
|
|
$vars = new AbuseFilterVariableHolder;
|
|
|
|
$title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
|
|
|
|
|
|
|
|
if ( $row->rc_user ) {
|
|
|
|
$user = User::newFromName( $row->rc_user_text );
|
|
|
|
} else {
|
|
|
|
$user = new User;
|
|
|
|
$user->setName( $row->rc_user_text );
|
|
|
|
}
|
|
|
|
|
|
|
|
$vars->addHolders(
|
|
|
|
self::generateUserVars( $user ),
|
2018-02-18 13:44:17 +00:00
|
|
|
self::generateTitleVars( $title, 'PAGE' )
|
2017-08-20 11:48:20 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$vars->setVar( 'ACTION', 'delete' );
|
2018-03-09 21:22:32 +00:00
|
|
|
$vars->setVar( 'SUMMARY', CommentStore::getStore()->getComment( 'rc_comment', $row )->text );
|
2017-08-20 11:48:20 +00:00
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param stdClass $row
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2009-01-28 23:54:41 +00:00
|
|
|
public static function getEditVarsFromRCRow( $row ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder;
|
2009-01-28 23:54:41 +00:00
|
|
|
$title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $row->rc_user ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$user = User::newFromName( $row->rc_user_text );
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-02-25 04:31:53 +00:00
|
|
|
$user = new User;
|
|
|
|
$user->setName( $row->rc_user_text );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-03-06 06:21:55 +00:00
|
|
|
$vars->addHolders(
|
|
|
|
self::generateUserVars( $user ),
|
2018-02-18 13:44:17 +00:00
|
|
|
self::generateTitleVars( $title, 'PAGE' )
|
2013-03-06 06:21:55 +00:00
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setVar( 'ACTION', 'edit' );
|
2018-03-09 21:22:32 +00:00
|
|
|
$vars->setVar( 'SUMMARY', CommentStore::getStore()->getComment( 'rc_comment', $row )->text );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'new_wikitext', 'revision-text-by-id',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'revid' => $row->rc_this_oldid ] );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $row->rc_last_oldid ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'old_wikitext', 'revision-text-by-id',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'revid' => $row->rc_last_oldid ] );
|
2009-01-28 23:54:41 +00:00
|
|
|
} else {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setVar( 'old_wikitext', '' );
|
2009-01-28 23:54:41 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 06:21:55 +00:00
|
|
|
$vars->addHolders( self::getEditVars( $title ) );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param stdClass $row
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2009-01-28 23:54:41 +00:00
|
|
|
public static function getMoveVarsFromRCRow( $row ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $row->rc_user ) {
|
2009-02-25 04:31:53 +00:00
|
|
|
$user = User::newFromId( $row->rc_user );
|
2010-08-19 21:12:09 +00:00
|
|
|
} else {
|
2009-02-25 04:31:53 +00:00
|
|
|
$user = new User;
|
|
|
|
$user->setName( $row->rc_user_text );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-12-31 03:30:49 +00:00
|
|
|
$params = array_values( DatabaseLogEntry::newFromRow( $row )->getParameters() );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-01-28 23:54:41 +00:00
|
|
|
$oldTitle = Title::makeTitle( $row->rc_namespace, $row->rc_title );
|
2016-04-04 18:27:51 +00:00
|
|
|
$newTitle = Title::newFromText( $params[0] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = AbuseFilterVariableHolder::merge(
|
2017-07-23 07:03:40 +00:00
|
|
|
self::generateUserVars( $user ),
|
|
|
|
self::generateTitleVars( $oldTitle, 'MOVED_FROM' ),
|
|
|
|
self::generateTitleVars( $newTitle, 'MOVED_TO' )
|
2009-02-26 12:15:14 +00:00
|
|
|
);
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2018-03-09 21:22:32 +00:00
|
|
|
$vars->setVar( 'SUMMARY', CommentStore::getStore()->getComment( 'rc_comment', $row )->text );
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setVar( 'ACTION', 'move' );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2011-02-10 17:25:25 +00:00
|
|
|
/**
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param Title|null $title
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param Page|null $page
|
2011-02-10 17:25:25 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function getEditVars( $title, Page $page = null ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder;
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2013-01-04 15:37:56 +00:00
|
|
|
// NOTE: $page may end up remaining null, e.g. if $title points to a special page.
|
2013-06-05 01:52:29 +00:00
|
|
|
if ( !$page && $title instanceof Title && $title->canExist() ) {
|
2013-01-04 15:37:56 +00:00
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
}
|
|
|
|
|
2018-04-08 15:38:39 +00:00
|
|
|
$vars->setLazyLoadVar( 'edit_diff', 'diff-array',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_wikitext' ] );
|
2018-04-08 15:38:39 +00:00
|
|
|
$vars->setLazyLoadVar( 'edit_diff_pst', 'diff-array',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_pst' ] );
|
|
|
|
$vars->setLazyLoadVar( 'new_size', 'length', [ 'length-var' => 'new_wikitext' ] );
|
|
|
|
$vars->setLazyLoadVar( 'old_size', 'length', [ 'length-var' => 'old_wikitext' ] );
|
2018-03-25 17:19:10 +00:00
|
|
|
$vars->setLazyLoadVar( 'edit_delta', 'subtract-int',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'val1-var' => 'new_size', 'val2-var' => 'old_size' ] );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
// Some more specific/useful details about the changes.
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'added_lines', 'diff-split',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'diff-var' => 'edit_diff', 'line-prefix' => '+' ] );
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'removed_lines', 'diff-split',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'diff-var' => 'edit_diff', 'line-prefix' => '-' ] );
|
2013-10-01 07:07:50 +00:00
|
|
|
$vars->setLazyLoadVar( 'added_lines_pst', 'diff-split',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'diff-var' => 'edit_diff_pst', 'line-prefix' => '+' ] );
|
2009-02-26 12:15:14 +00:00
|
|
|
|
|
|
|
// Links
|
|
|
|
$vars->setLazyLoadVar( 'added_links', 'link-diff-added',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'oldlink-var' => 'old_links', 'newlink-var' => 'all_links' ] );
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'removed_links', 'link-diff-removed',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'oldlink-var' => 'old_links', 'newlink-var' => 'all_links' ] );
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'new_text', 'strip-html',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'html-var' => 'new_html' ] );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2013-02-14 11:39:51 +00:00
|
|
|
if ( $title instanceof Title ) {
|
|
|
|
$vars->setLazyLoadVar( 'all_links', 'links-from-wikitext',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2013-02-14 11:39:51 +00:00
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'text-var' => 'new_wikitext',
|
|
|
|
'article' => $page
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2013-02-14 11:39:51 +00:00
|
|
|
$vars->setLazyLoadVar( 'old_links', 'links-from-wikitext-or-database',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2013-02-14 11:39:51 +00:00
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'text-var' => 'old_wikitext'
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2013-04-24 14:53:12 +00:00
|
|
|
$vars->setLazyLoadVar( 'new_pst', 'parse-wikitext',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2013-04-24 14:53:12 +00:00
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'wikitext-var' => 'new_wikitext',
|
|
|
|
'article' => $page,
|
|
|
|
'pst' => true,
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2013-02-14 11:39:51 +00:00
|
|
|
$vars->setLazyLoadVar( 'new_html', 'parse-wikitext',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2013-02-14 11:39:51 +00:00
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'wikitext-var' => 'new_wikitext',
|
|
|
|
'article' => $page
|
2017-06-15 14:23:34 +00:00
|
|
|
] );
|
2013-02-14 11:39:51 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 23:54:41 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param AbuseFilterVariableHolder|array $vars
|
2016-09-17 07:03:42 +00:00
|
|
|
* @param IContextSource $context
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-09-17 07:03:42 +00:00
|
|
|
public static function buildVarDumpTable( $vars, IContextSource $context ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
// Export all values
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( $vars instanceof AbuseFilterVariableHolder ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = $vars->exportAllVars();
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
$output = '';
|
|
|
|
|
|
|
|
// I don't want to change the names of the pre-existing messages
|
|
|
|
// describing the variables, nor do I want to rewrite them, so I'm just
|
|
|
|
// mapping the variable names to builder messages with a pre-existing array.
|
2009-03-05 02:43:05 +00:00
|
|
|
$variableMessageMappings = self::getBuilderValues();
|
|
|
|
$variableMessageMappings = $variableMessageMappings['vars'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$output .=
|
2017-06-15 14:23:34 +00:00
|
|
|
Xml::openElement( 'table', [ 'class' => 'mw-abuselog-details' ] ) .
|
2009-10-07 13:57:06 +00:00
|
|
|
Xml::openElement( 'tbody' ) .
|
2009-02-07 09:34:11 +00:00
|
|
|
"\n";
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$header =
|
2016-09-17 07:03:42 +00:00
|
|
|
Xml::element( 'th', null, $context->msg( 'abusefilter-log-details-var' )->text() ) .
|
|
|
|
Xml::element( 'th', null, $context->msg( 'abusefilter-log-details-val' )->text() );
|
2009-01-30 15:40:59 +00:00
|
|
|
$output .= Xml::tags( 'tr', null, $header ) . "\n";
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2012-07-24 16:34:28 +00:00
|
|
|
if ( !count( $vars ) ) {
|
|
|
|
$output .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2012-07-24 16:34:28 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
// Now, build the body of the table.
|
2018-02-18 13:44:17 +00:00
|
|
|
$deprecatedVars = self::getDeprecatedVariables();
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $vars as $key => $value ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$key = strtolower( $key );
|
|
|
|
|
2018-02-18 13:44:17 +00:00
|
|
|
if ( array_key_exists( $key, $deprecatedVars ) ) {
|
|
|
|
$key = $deprecatedVars[$key];
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !empty( $variableMessageMappings[$key] ) ) {
|
2009-01-29 22:44:31 +00:00
|
|
|
$mapping = $variableMessageMappings[$key];
|
2016-09-17 07:03:42 +00:00
|
|
|
$keyDisplay = $context->msg( "abusefilter-edit-builder-vars-$mapping" )->parse() .
|
2018-04-29 17:52:45 +00:00
|
|
|
' ' . Xml::element( 'code', null, $context->msg( 'parentheses' )->rawParams( $key )->text() );
|
2018-05-04 13:43:30 +00:00
|
|
|
} elseif ( !empty( self::$disabledVars[$key] ) ) {
|
|
|
|
$mapping = self::$disabledVars[$key];
|
|
|
|
$keyDisplay = $context->msg( "abusefilter-edit-builder-vars-$mapping" )->parse() .
|
|
|
|
' ' . Xml::element( 'code', null, $context->msg( 'parentheses' )->rawParams( $key )->text() );
|
2009-01-29 22:44:31 +00:00
|
|
|
} else {
|
2012-09-07 08:47:14 +00:00
|
|
|
$keyDisplay = Xml::element( 'code', null, $key );
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 20:17:41 +00:00
|
|
|
if ( is_null( $value ) ) {
|
2009-01-30 15:40:59 +00:00
|
|
|
$value = '';
|
2016-01-06 20:17:41 +00:00
|
|
|
}
|
2017-06-15 14:23:34 +00:00
|
|
|
$value = Xml::element( 'div', [ 'class' => 'mw-abuselog-var-value' ], $value, false );
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$trow =
|
2017-06-15 14:23:34 +00:00
|
|
|
Xml::tags( 'td', [ 'class' => 'mw-abuselog-var' ], $keyDisplay ) .
|
|
|
|
Xml::tags( 'td', [ 'class' => 'mw-abuselog-var-value' ], $value );
|
2010-02-13 14:10:36 +00:00
|
|
|
$output .=
|
2009-10-07 13:57:06 +00:00
|
|
|
Xml::tags( 'tr',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'class' => "mw-abuselog-details-$key mw-abuselog-value" ], $trow
|
2009-02-07 09:34:11 +00:00
|
|
|
) . "\n";
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$output .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
return $output;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $action
|
|
|
|
* @param string[] $parameters
|
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function formatAction( $action, $parameters ) {
|
2013-10-15 12:35:03 +00:00
|
|
|
/** @var $wgLang Language */
|
2009-03-12 05:04:39 +00:00
|
|
|
global $wgLang;
|
2018-03-14 16:50:58 +00:00
|
|
|
if ( count( $parameters ) === 0 ||
|
|
|
|
( $action === 'block' && count( $parameters ) !== 3 ) ) {
|
2017-07-23 07:03:40 +00:00
|
|
|
$displayAction = self::getActionDisplay( $action );
|
2009-03-12 05:04:39 +00:00
|
|
|
} else {
|
2018-02-20 12:36:32 +00:00
|
|
|
if ( $action === 'block' ) {
|
|
|
|
// Needs to be treated separately since the message is more complex
|
2018-05-03 16:59:37 +00:00
|
|
|
$messages = [
|
|
|
|
wfMessage( 'abusefilter-block-anon' )->escaped() .
|
|
|
|
wfMessage( 'colon-separator' )->escaped() .
|
|
|
|
$wgLang->translateBlockExpiry( $parameters[1] ),
|
|
|
|
wfMessage( 'abusefilter-block-user' )->escaped() .
|
|
|
|
wfMessage( 'colon-separator' )->escaped() .
|
|
|
|
$wgLang->translateBlockExpiry( $parameters[2] )
|
|
|
|
];
|
|
|
|
if ( $parameters[0] === 'blocktalk' ) {
|
|
|
|
$messages[] = wfMessage( 'abusefilter-block-talk' )->escaped();
|
|
|
|
}
|
|
|
|
$displayAction = $wgLang->commaList( $messages );
|
2018-05-08 20:04:05 +00:00
|
|
|
} elseif ( $action === 'throttle' ) {
|
|
|
|
array_shift( $parameters );
|
|
|
|
list( $actions, $time ) = explode( ',', array_shift( $parameters ) );
|
|
|
|
$groups = $wgLang->commaList( $parameters );
|
|
|
|
$displayAction = self::getActionDisplay( $action ) .
|
|
|
|
wfMessage( 'colon-separator' )->escaped() .
|
|
|
|
wfMessage( 'abusefilter-throttle-details' )->params( $actions, $time, $groups )->escaped();
|
2018-02-20 12:36:32 +00:00
|
|
|
} else {
|
2018-03-10 09:02:11 +00:00
|
|
|
$displayAction = self::getActionDisplay( $action ) .
|
2012-09-02 11:07:02 +00:00
|
|
|
wfMessage( 'colon-separator' )->escaped() .
|
2018-07-04 15:04:05 +00:00
|
|
|
$wgLang->semicolonList( array_map( 'htmlspecialchars', $parameters ) );
|
2018-02-20 12:36:32 +00:00
|
|
|
}
|
2009-03-12 05:04:39 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-03-12 05:04:39 +00:00
|
|
|
return $displayAction;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $value
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function formatFlags( $value ) {
|
2013-10-15 12:35:03 +00:00
|
|
|
/** @var $wgLang Language */
|
2009-03-12 05:04:39 +00:00
|
|
|
global $wgLang;
|
|
|
|
$flags = array_filter( explode( ',', $value ) );
|
2017-06-15 14:23:34 +00:00
|
|
|
$flags_display = [];
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $flags as $flag ) {
|
2018-05-01 12:13:57 +00:00
|
|
|
$flags_display[] = wfMessage( "abusefilter-history-$flag" )->escaped();
|
2009-03-12 05:04:39 +00:00
|
|
|
}
|
2015-09-28 18:03:35 +00:00
|
|
|
|
2009-03-12 05:04:39 +00:00
|
|
|
return $wgLang->commaList( $flags_display );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-01-02 11:41:29 +00:00
|
|
|
* @param string $filterID
|
|
|
|
* @return string
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function getGlobalFilterDescription( $filterID ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
global $wgAbuseFilterCentralDB;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( !$wgAbuseFilterCentralDB ) {
|
2012-03-11 20:40:04 +00:00
|
|
|
return '';
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-12-26 10:09:37 +00:00
|
|
|
static $cache = [];
|
|
|
|
if ( isset( $cache[$filterID] ) ) {
|
|
|
|
return $cache[$filterID];
|
|
|
|
}
|
|
|
|
|
2017-08-30 02:51:39 +00:00
|
|
|
$fdb = wfGetDB( DB_REPLICA, [], $wgAbuseFilterCentralDB );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2016-12-26 10:09:37 +00:00
|
|
|
$cache[$filterID] = $fdb->selectField(
|
2009-10-07 13:57:06 +00:00
|
|
|
'abuse_filter',
|
|
|
|
'af_public_comments',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'af_id' => $filterID ],
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2016-12-26 10:09:37 +00:00
|
|
|
|
|
|
|
return $cache[$filterID];
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2012-05-06 06:44:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gives either the user-specified name for a group,
|
|
|
|
* or spits the input back out
|
2014-11-07 12:21:45 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return string A name for that filter group, or the input.
|
2012-05-06 06:44:45 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function nameGroup( $group ) {
|
2013-08-18 06:35:16 +00:00
|
|
|
// Give grep a chance to find the usages: abusefilter-group-default
|
2012-05-06 06:44:45 +00:00
|
|
|
$msg = "abusefilter-group-$group";
|
2015-09-28 18:03:35 +00:00
|
|
|
|
|
|
|
return wfMessage( $msg )->exists() ? wfMessage( $msg )->escaped() : $group;
|
2012-05-06 06:44:45 +00:00
|
|
|
}
|
2012-11-20 15:16:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up some text of a revision from its revision id
|
|
|
|
*
|
|
|
|
* Note that this is really *some* text, we do not make *any* guarantee
|
|
|
|
* that this text will be even close to what the user actually sees, or
|
|
|
|
* that the form is fit for any intended purpose.
|
|
|
|
*
|
|
|
|
* Note also that if the revision for any reason is not an Revision
|
|
|
|
* the function returns with an empty string.
|
|
|
|
*
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param Revision|null $revision a valid revision
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param int $audience one of:
|
2012-11-20 15:16:58 +00:00
|
|
|
* Revision::FOR_PUBLIC to be displayed to all users
|
|
|
|
* Revision::FOR_THIS_USER to be displayed to the given user
|
|
|
|
* Revision::RAW get the text regardless of permissions
|
|
|
|
* @return string|null the content of the revision as some kind of string,
|
2015-09-28 18:03:35 +00:00
|
|
|
* or an empty string if it can not be found
|
2012-11-20 15:16:58 +00:00
|
|
|
*/
|
2018-10-17 01:22:23 +00:00
|
|
|
public static function revisionToString( $revision, $audience = Revision::FOR_THIS_USER ) {
|
|
|
|
if ( !$revision instanceof Revision ) {
|
2012-11-20 15:16:58 +00:00
|
|
|
return '';
|
|
|
|
}
|
2015-11-23 10:00:25 +00:00
|
|
|
|
|
|
|
$content = $revision->getContent( $audience );
|
|
|
|
if ( $content === null ) {
|
|
|
|
return '';
|
2012-11-20 15:16:58 +00:00
|
|
|
}
|
2015-11-23 10:00:25 +00:00
|
|
|
$result = self::contentToString( $content );
|
|
|
|
|
2012-11-20 15:16:58 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-01-04 15:37:56 +00:00
|
|
|
/**
|
|
|
|
* Converts the given Content object to a string.
|
|
|
|
*
|
|
|
|
* This uses Content::getNativeData() if $content is an instance of TextContent,
|
|
|
|
* or Content::getTextForSearchIndex() otherwise.
|
|
|
|
*
|
|
|
|
* The hook 'AbuseFilter::contentToString' can be used to override this
|
|
|
|
* behavior.
|
|
|
|
*
|
|
|
|
* @param Content $content
|
|
|
|
*
|
|
|
|
* @return string a suitable string representation of the content.
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function contentToString( Content $content ) {
|
2013-01-04 15:37:56 +00:00
|
|
|
$text = null;
|
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
if ( Hooks::run( 'AbuseFilter-contentToString', [ $content, &$text ] ) ) {
|
2013-01-04 15:37:56 +00:00
|
|
|
$text = $content instanceof TextContent
|
2015-09-28 18:03:35 +00:00
|
|
|
? $content->getNativeData()
|
|
|
|
: $content->getTextForSearchIndex();
|
2013-01-04 15:37:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 08:34:57 +00:00
|
|
|
// T22310
|
|
|
|
$text = TextContent::normalizeLineEndings( (string)$text );
|
2013-01-04 15:37:56 +00:00
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:52:31 +00:00
|
|
|
/**
|
2012-12-30 03:59:33 +00:00
|
|
|
* Get the history ID of the first change to a given filter
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param int $filterID Filter id
|
2017-01-02 11:41:29 +00:00
|
|
|
* @return int
|
2012-12-30 03:59:33 +00:00
|
|
|
*/
|
|
|
|
public static function getFirstFilterChange( $filterID ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
static $firstChanges = [];
|
2012-12-30 03:59:33 +00:00
|
|
|
|
2015-09-28 18:03:35 +00:00
|
|
|
if ( !isset( $firstChanges[$filterID] ) ) {
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2012-12-30 03:59:33 +00:00
|
|
|
$row = $dbr->selectRow(
|
|
|
|
'abuse_filter_history',
|
|
|
|
'afh_id',
|
2017-06-15 14:23:34 +00:00
|
|
|
[
|
2012-12-30 03:59:33 +00:00
|
|
|
'afh_filter' => $filterID,
|
2017-06-15 14:23:34 +00:00
|
|
|
],
|
2012-12-30 03:59:33 +00:00
|
|
|
__METHOD__,
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'ORDER BY' => 'afh_timestamp ASC' ]
|
2012-12-30 03:59:33 +00:00
|
|
|
);
|
|
|
|
$firstChanges[$filterID] = $row->afh_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $firstChanges[$filterID];
|
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|