2008-06-27 06:18:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AbuseFilter {
|
2008-07-17 02:43:45 +00:00
|
|
|
public static $statsStoragePeriod = 86400;
|
2008-07-18 02:18:58 +00:00
|
|
|
public static $tokenCache = array();
|
|
|
|
public static $modifyCache = array();
|
2008-08-02 11:10:42 +00:00
|
|
|
public static $condLimitEnabled = true;
|
|
|
|
public static $condCount = 0;
|
2008-09-18 13:01:50 +00:00
|
|
|
public static $filters = array();
|
2009-01-23 19:23:19 +00:00
|
|
|
public static $tagsToSet = array();
|
2009-02-07 09:34:11 +00:00
|
|
|
public static $history_mappings = array(
|
|
|
|
'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',
|
2009-02-07 09:34:11 +00:00
|
|
|
);
|
2009-01-27 01:31:42 +00:00
|
|
|
public static $builderValues = array(
|
2009-02-07 09:34:11 +00:00
|
|
|
'op-arithmetic' => array(
|
|
|
|
'+' => 'addition',
|
2012-02-10 18:20:41 +00:00
|
|
|
'-' => 'subtraction',
|
|
|
|
'*' => 'multiplication',
|
|
|
|
'/' => 'divide',
|
2009-02-07 09:34:11 +00:00
|
|
|
'%' => 'modulo',
|
|
|
|
'**' => 'pow'
|
|
|
|
),
|
|
|
|
'op-comparison' => array(
|
|
|
|
'==' => 'equal',
|
|
|
|
'!=' => 'notequal',
|
|
|
|
'<' => 'lt',
|
|
|
|
'>' => 'gt',
|
|
|
|
'<=' => 'lte',
|
|
|
|
'>=' => 'gte'
|
|
|
|
),
|
|
|
|
'op-bool' => array(
|
|
|
|
'!' => 'not',
|
|
|
|
'&' => 'and',
|
|
|
|
'|' => 'or',
|
|
|
|
'^' => 'xor'
|
|
|
|
),
|
|
|
|
'misc' => array(
|
|
|
|
'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',
|
2009-02-07 09:34:11 +00:00
|
|
|
),
|
|
|
|
'funcs' => array(
|
|
|
|
'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',
|
|
|
|
'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',
|
2009-03-19 05:06:39 +00:00
|
|
|
'rmwhitespace(text)' => 'rmwhitespace',
|
|
|
|
'rmspecials(text)' => 'rmspecials',
|
|
|
|
'ip_in_range(ip, range)' => 'ip_in_range',
|
2009-03-26 02:03:32 +00:00
|
|
|
'contains_any(haystack,needle1,needle2,needle3)' => 'contains-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',
|
2009-02-07 09:34:11 +00:00
|
|
|
),
|
|
|
|
'vars' => array(
|
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',
|
|
|
|
'removed_lines' => 'removedlines',
|
|
|
|
'summary' => 'summary',
|
|
|
|
'article_articleid' => 'article-id',
|
|
|
|
'article_namespace' => 'article-ns',
|
|
|
|
'article_text' => 'article-text',
|
|
|
|
'article_prefixedtext' => 'article-prefixedtext',
|
2012-12-14 17:37:01 +00:00
|
|
|
# 'article_views' => 'article-views', # May not be enabled, defined in getBuilderValues()
|
2009-02-11 20:00:33 +00:00
|
|
|
'moved_from_articleid' => 'movedfrom-id',
|
|
|
|
'moved_from_namespace' => 'movedfrom-ns',
|
|
|
|
'moved_from_text' => 'movedfrom-text',
|
|
|
|
'moved_from_prefixedtext' => 'movedfrom-prefixedtext',
|
|
|
|
'moved_to_articleid' => 'movedto-id',
|
|
|
|
'moved_to_namespace' => 'movedto-ns',
|
|
|
|
'moved_to_text' => 'movedto-text',
|
|
|
|
'moved_to_prefixedtext' => 'movedto-prefixedtext',
|
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',
|
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',
|
|
|
|
'new_text' => 'new-text-stripped',
|
|
|
|
'new_html' => 'new-html',
|
|
|
|
'article_restrictions_edit' => 'restrictions-edit',
|
|
|
|
'article_restrictions_move' => 'restrictions-move',
|
2012-10-25 14:02:40 +00:00
|
|
|
'article_restrictions_create' => 'restrictions-create',
|
|
|
|
'article_restrictions_upload' => 'restrictions-upload',
|
2009-02-11 20:00:33 +00:00
|
|
|
'article_recent_contributors' => 'recent-contributors',
|
2010-08-19 21:12:09 +00:00
|
|
|
# 'old_text' => 'old-text-stripped', # Disabled, performance
|
|
|
|
# 'old_html' => 'old-html', # Disabled, performance
|
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',
|
2009-02-07 09:34:11 +00:00
|
|
|
),
|
2009-01-27 01:31:42 +00:00
|
|
|
);
|
2012-12-14 17:37:01 +00:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $context IContextSource
|
|
|
|
* @param $pageType
|
|
|
|
*/
|
2011-11-17 00:55:53 +00:00
|
|
|
public static function addNavigationLinks( IContextSource $context, $pageType ) {
|
2009-03-11 05:55:06 +00:00
|
|
|
$linkDefs = array(
|
2009-10-07 13:57:06 +00:00
|
|
|
'home' => 'Special:AbuseFilter',
|
|
|
|
'recentchanges' => 'Special:AbuseFilter/history',
|
|
|
|
'examine' => 'Special:AbuseFilter/examine',
|
|
|
|
'log' => 'Special:AbuseLog',
|
|
|
|
);
|
|
|
|
|
2011-11-17 00:55:53 +00:00
|
|
|
if ( $context->getUser()->isAllowed( 'abusefilter-modify' ) ) {
|
2009-07-17 16:59:14 +00:00
|
|
|
$linkDefs = array_merge( $linkDefs, array(
|
2012-12-30 21:23:36 +00:00
|
|
|
'test' => 'Special:AbuseFilter/test',
|
2009-03-11 05:55:06 +00:00
|
|
|
'tools' => 'Special:AbuseFilter/tools',
|
2009-04-23 04:23:56 +00:00
|
|
|
'import' => 'Special:AbuseFilter/import',
|
2009-07-17 16:59:14 +00:00
|
|
|
) );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-11 05:55:06 +00:00
|
|
|
// Save some translator work
|
|
|
|
$msgOverrides = array(
|
2009-10-07 13:57:06 +00:00
|
|
|
'recentchanges' => 'abusefilter-filter-log',
|
|
|
|
);
|
|
|
|
|
2009-03-11 05:55:06 +00:00
|
|
|
$links = array();
|
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
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
$msg = wfMessage( $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 {
|
2012-03-11 20:40:04 +00:00
|
|
|
$links[] = Linker::link( $title, $msg );
|
2009-03-11 05:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 07:49:56 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
$linkStr = wfMessage( 'parentheses', $context->getLanguage()->pipeList( $links ) )->text();
|
|
|
|
$linkStr = wfMessage( 'abusefilter-topnav' )->parse() . " $linkStr";
|
2009-03-11 07:49:56 +00:00
|
|
|
|
2009-03-11 05:55:06 +00:00
|
|
|
$linkStr = Xml::tags( 'div', array( '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
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param $user User
|
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2008-06-27 06:18:51 +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',
|
|
|
|
array( 'user' => $user, 'method' => 'getEditCount' )
|
|
|
|
);
|
|
|
|
|
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',
|
|
|
|
array( 'user' => $user, 'method' => 'getEmailAuthenticationTimestamp' )
|
|
|
|
);
|
|
|
|
|
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_age',
|
|
|
|
'user-age',
|
|
|
|
array( 'user' => $user, 'asof' => wfTimestampNow() )
|
|
|
|
);
|
|
|
|
|
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_groups',
|
|
|
|
'simple-user-accessor',
|
|
|
|
array( 'user' => $user, 'method' => 'getEffectiveGroups' )
|
|
|
|
);
|
|
|
|
|
|
|
|
$vars->setLazyLoadVar(
|
|
|
|
'user_blocked',
|
|
|
|
'simple-user-accessor',
|
|
|
|
array( 'user' => $user, 'method' => 'isBlocked' )
|
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-12-21 17:43:48 +00:00
|
|
|
wfRunHooks( 'AbuseFilter-generateUserVars', array( $vars, $user ) );
|
|
|
|
|
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;
|
2012-12-14 17:37:01 +00:00
|
|
|
global $wgDisableCounters;
|
|
|
|
if ( !$wgDisableCounters ) {
|
|
|
|
$realValues['vars']['article_views'] = 'article-views';
|
|
|
|
}
|
2009-03-05 02:43:05 +00:00
|
|
|
wfRunHooks( 'AbuseFilter-builder', array( &$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
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
$dbr = wfGetDB( DB_SLAVE, array(), $wgAbuseFilterCentralDB );
|
|
|
|
$filter = $globalIndex;
|
|
|
|
} else {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
$hidden = $dbr->selectField(
|
|
|
|
'abuse_filter',
|
|
|
|
'af_hidden',
|
|
|
|
array( 'af_id' => $filter ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2009-03-17 13:18:33 +00:00
|
|
|
return $hidden ? true : false;
|
|
|
|
}
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $val int
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-18 02:18:58 +00:00
|
|
|
public static function disableConditionLimit() {
|
|
|
|
// For use in batch scripts and the like
|
|
|
|
self::$condLimitEnabled = false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-02-10 17:32:57 +00:00
|
|
|
/**
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $title Title
|
|
|
|
* @param $prefix
|
2011-02-10 17:32:57 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2008-06-27 06:18:51 +00:00
|
|
|
public static function generateTitleVars( $title, $prefix ) {
|
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 ) {
|
2009-03-09 13:22:58 +00:00
|
|
|
return new AbuseFilterVariableHolder;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 19:04:37 +00:00
|
|
|
$vars->setVar( $prefix . '_ARTICLEID', $title->getArticleID() );
|
2009-10-07 13:57:06 +00:00
|
|
|
$vars->setVar( $prefix . '_NAMESPACE', $title->getNamespace() );
|
2010-08-19 21:12:09 +00:00
|
|
|
$vars->setVar( $prefix . '_TEXT', $title->getText() );
|
2009-10-07 13:57:06 +00:00
|
|
|
$vars->setVar( $prefix . '_PREFIXEDTEXT', $title->getPrefixedText() );
|
2012-12-14 17:37:01 +00:00
|
|
|
global $wgDisableCounters;
|
2013-01-21 19:56:23 +00:00
|
|
|
if ( !$wgDisableCounters && !$title->isSpecialPage() ) {
|
2012-12-14 17:37:01 +00:00
|
|
|
$vars->setVar( $prefix . '_VIEWS', WikiPage::factory( $title )->getCount() );
|
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Use restrictions.
|
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',
|
|
|
|
array( 'title' => $title->getText(),
|
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'action' => $action
|
|
|
|
)
|
|
|
|
);
|
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',
|
|
|
|
array(
|
|
|
|
'cutoff' => wfTimestampNow(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'namespace' => $title->getNamespace()
|
|
|
|
) );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-12-21 17:43:48 +00:00
|
|
|
wfRunHooks( 'AbuseFilter-generateTitleVars', array( $vars, $title, $prefix ) );
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-08-03 14:04:26 +00:00
|
|
|
public static function checkSyntax( $filter ) {
|
|
|
|
global $wgAbuseFilterParserClass;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-09-02 11:07:02 +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
|
|
|
/**
|
|
|
|
* @param $expr
|
|
|
|
* @param array $vars
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-08-04 14:27:48 +00:00
|
|
|
public static function evaluateExpression( $expr, $vars = array() ) {
|
|
|
|
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
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
/**
|
|
|
|
* @var $parser AbuseFilterParser
|
|
|
|
*/
|
2013-01-07 00:02:41 +00:00
|
|
|
$parser = new $wgAbuseFilterParserClass( $vars );
|
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
|
|
|
/**
|
|
|
|
* @param $conds
|
|
|
|
* @param $vars
|
|
|
|
* @param $ignoreError bool
|
|
|
|
* @param $keepVars string
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2012-02-10 23:41:05 +00:00
|
|
|
public static function checkConditions(
|
|
|
|
$conds, $vars, $ignoreError = true, $keepVars = 'resetvars'
|
|
|
|
) {
|
2008-08-02 11:10:42 +00:00
|
|
|
global $wgAbuseFilterParserClass;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-26 04:09:07 +00:00
|
|
|
static $parser;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-07-18 08:30:25 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( is_null( $parser ) || $keepVars == 'resetvars' ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
/**
|
|
|
|
* @var $parser AbuseFilterParser
|
|
|
|
*/
|
2013-01-07 00:02:41 +00:00
|
|
|
$parser = new $wgAbuseFilterParserClass( $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
|
|
|
// Sigh.
|
|
|
|
$result = false;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
wfDebugLog( 'AbuseFilter', 'AbuseFilter parser error: ' . $excep->getMessage() . "\n" );
|
|
|
|
|
|
|
|
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-07-18 08:30:25 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
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
|
|
|
|
*
|
|
|
|
* @param $vars array
|
2012-06-21 13:31:09 +00:00
|
|
|
* @param $group string The filter group to check against.
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-05-06 06:44:45 +00:00
|
|
|
public static function checkAllFilters( $vars, $group = 'default' ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
// Fetch from the database.
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2009-02-02 17:57:06 +00:00
|
|
|
$filter_matched = array();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2010-08-19 21:12:09 +00:00
|
|
|
$res = $dbr->select(
|
|
|
|
'abuse_filter',
|
|
|
|
'*',
|
2012-05-06 06:44:45 +00:00
|
|
|
array(
|
|
|
|
'af_enabled' => 1,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_group' => $group,
|
|
|
|
),
|
2010-08-19 21:12:09 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2009-02-02 17:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
foreach( $res as $row ) {
|
2009-05-26 13:08:15 +00:00
|
|
|
$filter_matched[$row->af_id] = self::checkFilter( $row, $vars, true );
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-08-03 21:55:35 +00:00
|
|
|
global $wgAbuseFilterCentralDB, $wgAbuseFilterIsCentral, $wgMemc;
|
|
|
|
|
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 );
|
|
|
|
$memcacheRules = $wgMemc->get( $globalRulesKey );
|
|
|
|
|
|
|
|
if ( $memcacheRules ) {
|
|
|
|
$res = $memcacheRules;
|
|
|
|
} else {
|
|
|
|
$fdb = wfGetDB( DB_SLAVE, array(), $wgAbuseFilterCentralDB );
|
|
|
|
$res = $fdb->select(
|
|
|
|
'abuse_filter',
|
|
|
|
'*',
|
|
|
|
array(
|
|
|
|
'af_enabled' => 1,
|
|
|
|
'af_deleted' => 0,
|
|
|
|
'af_global' => 1,
|
|
|
|
'af_group' => $group,
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
$memcacheRules = array();
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$memcacheRules[] = $row;
|
|
|
|
}
|
|
|
|
$wgMemc->set( $globalRulesKey, $memcacheRules );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
foreach( $res as $row ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$filter_matched['global-' . $row->af_id] =
|
2009-05-26 13:08:15 +00:00
|
|
|
self::checkFilter( $row, $vars, true, 'global-' );
|
2009-03-19 02:40:48 +00:00
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Update statistics, and disable filters which are over-blocking.
|
2012-11-07 13:45:58 +00:00
|
|
|
self::recordStats( $filter_matched, $group );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
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
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @param $row
|
|
|
|
* @param $vars
|
|
|
|
* @param $profile bool
|
|
|
|
* @param $prefix string
|
|
|
|
* @return bool
|
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
public static function checkFilter( $row, $vars, $profile = false, $prefix = '' ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
$filterID = $prefix . $row->af_id;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $profile ) {
|
2009-07-03 13:46:51 +00:00
|
|
|
$startConds = self::$condCount;
|
2009-10-07 13:57:06 +00:00
|
|
|
$startTime = microtime( true );
|
2009-07-03 13:46:51 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
// Store the row somewhere convenient
|
|
|
|
self::$filters[$filterID] = $row;
|
|
|
|
|
|
|
|
// Check conditions...
|
2009-10-07 13:57:06 +00:00
|
|
|
$pattern = trim( $row->af_pattern );
|
2012-02-10 23:41:05 +00:00
|
|
|
if ( self::checkConditions(
|
|
|
|
$pattern,
|
|
|
|
$vars,
|
|
|
|
true /* ignore errors */,
|
|
|
|
'keepvars'
|
|
|
|
) ) {
|
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
|
|
|
|
|
|
|
if ( $profile ) {
|
|
|
|
$endTime = microtime( true );
|
2009-07-03 13:46:51 +00:00
|
|
|
$endConds = self::$condCount;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
$timeTaken = $endTime - $startTime;
|
2009-07-03 13:46:51 +00:00
|
|
|
$condsUsed = $endConds - $startConds;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-07-03 13:46:51 +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
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
*/
|
2009-03-19 02:40:48 +00:00
|
|
|
public static function resetFilterProfile( $filter ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
global $wgMemc;
|
2009-03-19 02:40:48 +00:00
|
|
|
$countKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'count' );
|
|
|
|
$totalKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'total' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:40:48 +00:00
|
|
|
$wgMemc->delete( $countKey );
|
|
|
|
$wgMemc->delete( $totalKey );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @param $time
|
|
|
|
* @param $conds
|
|
|
|
*/
|
2009-07-03 13:46:51 +00:00
|
|
|
public static function recordProfilingResult( $filter, $time, $conds ) {
|
2009-03-19 02:40:48 +00:00
|
|
|
global $wgMemc;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:40:48 +00:00
|
|
|
$countKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'count' );
|
|
|
|
$totalKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'total' );
|
2009-07-03 13:46:51 +00:00
|
|
|
$totalCondKey = wfMemcKey( 'abusefilter', 'profile-conds', 'total' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:40:48 +00:00
|
|
|
$curCount = $wgMemc->get( $countKey );
|
|
|
|
$curTotal = $wgMemc->get( $totalKey );
|
2009-07-03 13:46:51 +00:00
|
|
|
$curTotalConds = $wgMemc->get( $totalCondKey );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $curCount ) {
|
2009-07-03 13:46:51 +00:00
|
|
|
$wgMemc->set( $totalCondKey, $curTotalConds + $conds, 3600 );
|
2010-02-13 14:10:36 +00:00
|
|
|
$wgMemc->set( $totalKey, $curTotal + $time, 3600 );
|
2009-03-19 02:40:48 +00:00
|
|
|
$wgMemc->incr( $countKey );
|
2009-03-22 02:12:51 +00:00
|
|
|
} else {
|
2009-03-19 02:40:48 +00:00
|
|
|
$wgMemc->set( $countKey, 1, 3600 );
|
2009-07-03 13:46:51 +00:00
|
|
|
$wgMemc->set( $totalKey, $time, 3600 );
|
|
|
|
$wgMemc->set( $totalCondKey, $conds, 3600 );
|
2009-03-22 02:12:51 +00:00
|
|
|
}
|
2009-03-19 02:40:48 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-03-19 02:40:48 +00:00
|
|
|
public static function getFilterProfile( $filter ) {
|
|
|
|
global $wgMemc;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:40:48 +00:00
|
|
|
$countKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'count' );
|
|
|
|
$totalKey = wfMemcKey( 'abusefilter', 'profile', $filter, 'total' );
|
2009-07-03 13:46:51 +00:00
|
|
|
$totalCondKey = wfMemcKey( 'abusefilter', 'profile-conds', 'total' );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:40:48 +00:00
|
|
|
$curCount = $wgMemc->get( $countKey );
|
|
|
|
$curTotal = $wgMemc->get( $totalKey );
|
2009-07-03 13:46:51 +00:00
|
|
|
$curTotalConds = $wgMemc->get( $totalCondKey );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( !$curCount ) {
|
2009-07-03 13:46:51 +00:00
|
|
|
return array( 0, 0 );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$timeProfile = ( $curTotal / $curCount ) * 1000; // 1000 ms in a sec
|
2010-02-13 14:10:36 +00:00
|
|
|
$timeProfile = round( $timeProfile, 2 ); // Return in ms, rounded to 2dp
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$condProfile = ( $curTotalConds / $curCount );
|
2009-07-03 13:46:51 +00:00
|
|
|
$condProfile = round( $condProfile, 0 );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-07-03 13:46:51 +00:00
|
|
|
return array( $timeProfile, $condProfile );
|
2009-03-19 02:40:48 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* Utility function to decode global-$index to $index. Returns false if not global
|
|
|
|
*
|
|
|
|
* @param $filter string
|
|
|
|
*
|
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 ) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $filters array
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
public static function getConsequencesForFilters( $filters ) {
|
|
|
|
$globalFilters = array();
|
|
|
|
$localFilters = array();
|
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
|
2009-01-23 19:23:19 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2008-06-27 06:18:51 +00:00
|
|
|
// Retrieve the consequences.
|
2009-03-30 06:12:12 +00:00
|
|
|
$consequences = array();
|
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 ) ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$fdb = wfGetDB( DB_SLAVE, array(), $wgAbuseFilterCentralDB );
|
2010-08-19 21:12:09 +00:00
|
|
|
$consequences = array_merge(
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @param $dbr DatabaseBase
|
|
|
|
* @param $filters array
|
|
|
|
* @param $prefix string
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-10-07 13:57:06 +00:00
|
|
|
public static function loadConsequencesFromDB( $dbr, $filters, $prefix = '' ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$actionsByFilter = array();
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $filters as $filter ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$actionsByFilter[$prefix . $filter] = array();
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$res = $dbr->select(
|
|
|
|
array( 'abuse_filter_action', 'abuse_filter' ),
|
|
|
|
'*',
|
|
|
|
array( 'af_id' => $filters ),
|
|
|
|
__METHOD__,
|
|
|
|
array(),
|
|
|
|
array( 'abuse_filter_action' => array( 'LEFT JOIN', 'afa_filter=af_id' ) )
|
|
|
|
);
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
// Categorise consequences by filter.
|
2009-01-27 20:18:58 +00:00
|
|
|
global $wgAbuseFilterRestrictedActions;
|
2010-08-19 21:12:09 +00:00
|
|
|
foreach( $res as $row ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
if ( $row->af_throttled
|
|
|
|
&& in_array( $row->afa_consequence, $wgAbuseFilterRestrictedActions ) )
|
|
|
|
{
|
2010-08-19 21:12:09 +00:00
|
|
|
# Don't do the action
|
2009-02-13 01:40:57 +00:00
|
|
|
} elseif ( $row->afa_filter != $row->af_id ) {
|
|
|
|
// We probably got a NULL, as it's a LEFT JOIN.
|
2009-10-07 13:57:06 +00:00
|
|
|
// Don't add it.
|
2009-01-27 20:18:58 +00:00
|
|
|
} else {
|
2009-10-07 13:57:06 +00:00
|
|
|
$actionsByFilter[$prefix . $row->afa_filter][$row->afa_consequence] = array(
|
2009-02-07 09:34:11 +00:00
|
|
|
'action' => $row->afa_consequence,
|
|
|
|
'parameters' => explode( "\n", $row->afa_parameters )
|
|
|
|
);
|
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
|
|
|
/**
|
2013-01-08 14:52:49 +00:00
|
|
|
* Executes a list of actions.
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
|
|
|
* @param $filters array
|
|
|
|
* @param $title Title
|
|
|
|
* @param $vars array
|
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
|
|
|
|
* an array listing the actions taken. $status-getErrors(), etc, will provide
|
|
|
|
* the errors and warnings to be shown to the user to explain the actions.
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
public static function executeFilterActions( $filters, $title, $vars ) {
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
|
|
|
$actionsByFilter = self::getConsequencesForFilters( $filters );
|
|
|
|
$actionsTaken = array_fill_keys( $filters, array() );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
$messages = array();
|
|
|
|
|
2013-02-08 08:10:41 +00:00
|
|
|
global $wgOut, $wgAbuseFilterDisallowGlobalLocalBlocks, $wgAbuseFilterRestrictedActions;
|
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.
|
2009-02-07 09:34:11 +00:00
|
|
|
$parsed_public_comments = $wgOut->parseInline(
|
2009-10-07 13:57:06 +00:00
|
|
|
self::$filters[$filter]->af_public_comments );
|
2009-01-23 19:23:44 +00:00
|
|
|
|
2012-08-01 21:29:06 +00:00
|
|
|
$global_filter = ( preg_match( '/^global-/', $filter ) == 1);
|
|
|
|
|
2009-01-23 19:23:44 +00:00
|
|
|
if ( !empty( $actions['throttle'] ) ) {
|
|
|
|
$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(
|
2012-08-01 21:29:06 +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 ) {
|
2013-02-08 08:10:41 +00:00
|
|
|
foreach ( $wgAbuseFilterRestrictedActions as $blockingAction ) {
|
2012-08-03 21:55:35 +00:00
|
|
|
unset( $actions[$blockingAction] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
if ( !empty( $actions['warn'] ) ) {
|
|
|
|
$parameters = $actions['warn']['parameters'];
|
2012-02-10 23:45:04 +00:00
|
|
|
$warnKey = 'abusefilter-warned-' . md5($title->getPrefixedText()) . '-' . $filter;
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !isset( $_SESSION[$warnKey] ) || !$_SESSION[$warnKey] ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
$_SESSION[$warnKey] = true;
|
|
|
|
|
|
|
|
// Threaten them a little bit
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !empty( $parameters[0] ) && strlen( $parameters[0] ) ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
$msg = $parameters[0];
|
|
|
|
} else {
|
|
|
|
$msg = 'abusefilter-warning';
|
|
|
|
}
|
2013-01-08 14:52:49 +00:00
|
|
|
$messages[] = array( $msg, $parsed_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
|
|
|
|
|
|
|
continue; // Don't do anything else.
|
|
|
|
} else {
|
|
|
|
// We already warned them
|
|
|
|
$_SESSION[$warnKey] = false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
unset( $actions['warn'] );
|
|
|
|
}
|
|
|
|
|
2009-03-13 08:02:05 +00:00
|
|
|
// prevent double warnings
|
2013-02-08 08:10:41 +00:00
|
|
|
if ( count( array_intersect( array_keys( $actions ), $wgAbuseFilterRestrictedActions ) ) > 0 &&
|
2009-03-13 08:02:05 +00:00
|
|
|
!empty( $actions['disallow'] ) ) {
|
2009-01-29 23:24:24 +00:00
|
|
|
unset( $actions['disallow'] );
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
$action, $info['parameters'], $title, $vars,
|
2009-10-07 13:57:06 +00:00
|
|
|
self::$filters[$filter]->af_public_comments
|
|
|
|
);
|
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
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$status = self::buildStatus( $actionsTaken, $messages );
|
|
|
|
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-01-08 14:52:49 +00:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a Status object as returned by executeFilterActions() from the list of
|
|
|
|
* actions taken and the corresponding list of messages.
|
|
|
|
*
|
|
|
|
* @param array[] $actionsTaken associative array mapping each filter to the list if
|
|
|
|
* actions taken because of that filter.
|
|
|
|
* @param array[] $messages a list if arrays, where each array contains a message key
|
|
|
|
* followed by any message parameters.
|
|
|
|
*
|
|
|
|
* @todo: change this to accept Message objects. This is only possible from 1.21 onward,
|
|
|
|
* because before that, Status::fatal does not accept Message objects.
|
|
|
|
*
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
protected static function buildStatus( array $actionsTaken, array $messages ) {
|
|
|
|
$status = Status::newGood( $actionsTaken );
|
|
|
|
|
|
|
|
foreach ( $messages as $msg ) {
|
|
|
|
// Since MW 1.21, we could just pass Message objects, but in 1.20,
|
|
|
|
// we still have to rely on arrays.
|
|
|
|
call_user_func_array( array( $status, 'fatal' ), $msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $vars AbuseFilterVariableHolder
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $title Title
|
2012-11-07 13:45:58 +00:00
|
|
|
* @param $group string
|
2013-01-08 14:52:49 +00:00
|
|
|
* @return Status
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function filterAction( $vars, $title, $group = 'default' ) {
|
2012-05-08 07:48:19 +00:00
|
|
|
global $wgUser, $wgTitle, $wgRequest;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( !$wgTitle ) {
|
2009-03-16 23:02:18 +00:00
|
|
|
$wgTitle = SpecialPage::getTitleFor( 'AbuseFilter' );
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-05 02:43:05 +00:00
|
|
|
// Add vars from extensions
|
|
|
|
wfRunHooks( 'AbuseFilter-filterAction', array( &$vars, $title ) );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:05:58 +00:00
|
|
|
// Set context
|
|
|
|
$vars->setVar( 'context', 'filter' );
|
2009-04-01 03:59:58 +00:00
|
|
|
$vars->setVar( 'timestamp', time() );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
2012-11-07 13:45:58 +00:00
|
|
|
$filter_matched = self::checkAllFilters( $vars, $group );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
$matched_filters = array_keys( array_filter( $filter_matched ) );
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
// Short-cut any remaining code if no filters were hit.
|
2012-02-10 23:41:05 +00:00
|
|
|
if ( count( $matched_filters ) == 0 ) {
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2013-01-08 14:52:49 +00:00
|
|
|
return Status::newGood();
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ . '-block' );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$status = self::executeFilterActions(
|
2012-02-10 23:41:05 +00:00
|
|
|
$matched_filters, $title, $vars );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$actions_taken = $status->value; // getValue() was introduced only in 1.20
|
|
|
|
|
2009-03-18 23:24:10 +00:00
|
|
|
$action = $vars->getVar( 'ACTION' )->toString();
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Create a template
|
2009-02-07 09:34:11 +00:00
|
|
|
$log_template = array(
|
|
|
|
'afl_user' => $wgUser->getId(),
|
|
|
|
'afl_user_text' => $wgUser->getName(),
|
2010-02-13 14:10:36 +00:00
|
|
|
'afl_timestamp' => $dbr->timestamp( wfTimestampNow() ),
|
2009-02-07 09:34:11 +00:00
|
|
|
'afl_namespace' => $title->getNamespace(),
|
2009-05-24 08:33:57 +00:00
|
|
|
'afl_title' => $title->getDBkey(),
|
2012-05-08 07:48:19 +00:00
|
|
|
'afl_ip' => $wgRequest->getIP()
|
2009-10-07 13:57:06 +00:00
|
|
|
);
|
|
|
|
|
2009-03-18 23:24:10 +00:00
|
|
|
// Hack to avoid revealing IPs of people creating accounts
|
2012-05-10 08:08:48 +00:00
|
|
|
if ( !$wgUser->getId() && ( $action == 'createaccount' || $action == 'autocreateaccount' ) ) {
|
2009-03-18 23:24:10 +00:00
|
|
|
$log_template['afl_user_text'] = $vars->getVar( 'accountname' )->toString();
|
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2012-11-07 13:45:58 +00:00
|
|
|
self::addLogEntries( $actions_taken, $log_template, $action, $vars, $group );
|
2009-02-02 23:30:48 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-block' );
|
|
|
|
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
return $status;
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $actions_taken
|
|
|
|
* @param $log_template
|
|
|
|
* @param $action
|
|
|
|
* @param $vars AbuseFilterVariableHolder
|
2013-04-01 09:04:03 +00:00
|
|
|
* @param string $group
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function addLogEntries( $actions_taken, $log_template, $action, $vars, $group = 'default' ) {
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2008-06-27 06:18:51 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
$central_log_template = array(
|
2009-10-07 13:57:06 +00:00
|
|
|
'afl_wiki' => wfWikiID(),
|
2009-03-30 06:12:12 +00:00
|
|
|
);
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
$log_rows = array();
|
2009-03-30 06:12:12 +00:00
|
|
|
$central_log_rows = array();
|
|
|
|
$logged_local_filters = array();
|
|
|
|
$logged_global_filters = array();
|
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;
|
|
|
|
$thisLog['afl_action'] = $action;
|
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-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
if ( !$globalIndex ) {
|
2012-11-07 13:45:58 +00:00
|
|
|
$logged_local_filters[] = $filter;
|
2010-08-19 21:12:09 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
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;
|
|
|
|
}
|
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 ) ) {
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
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 );
|
|
|
|
$var_dump = "stored-text:$var_dump"; // To distinguish from stuff stored directly
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ . '-hitstats' );
|
|
|
|
|
2009-02-13 01:40:57 +00:00
|
|
|
global $wgMemc;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-13 01:40:57 +00:00
|
|
|
// Increment trigger counter
|
|
|
|
$wgMemc->incr( self::filterMatchesKey() );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
$local_log_ids = array();
|
2012-12-15 17:14:42 +00:00
|
|
|
global $wgAbuseFilterNotifications, $wgAbuseFilterNotificationsPrivate;
|
2012-04-14 10:10:35 +00:00
|
|
|
foreach ( $log_rows as $index => $data ) {
|
|
|
|
$data['afl_var_dump'] = $var_dump;
|
|
|
|
$data['afl_id'] = $dbw->nextSequenceValue( 'abuse_filter_log_afl_id_seq' );
|
|
|
|
$dbw->insert( 'abuse_filter_log', $data, __METHOD__ );
|
2012-07-24 17:55:58 +00:00
|
|
|
$local_log_ids[] = $dbw->insertId();
|
2012-04-14 10:10:35 +00:00
|
|
|
if ( $data['afl_id'] === null ) {
|
|
|
|
$data['afl_id'] = $dbw->insertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $wgAbuseFilterNotifications !== false ) {
|
|
|
|
$entry = new ManualLogEntry( 'abusefilter', 'hit' );
|
|
|
|
// Construct a user object
|
|
|
|
$user = new User();
|
|
|
|
$user->setId( $data['afl_user'] );
|
2013-06-26 22:58:36 +00:00
|
|
|
$user->setName( $data['afl_user_text'] );
|
2012-04-14 10:10:35 +00:00
|
|
|
$entry->setPerformer( $user );
|
|
|
|
// Set action target
|
|
|
|
$entry->setTarget( Title::makeTitle( $data['afl_namespace'], $data['afl_title'] ) );
|
|
|
|
// Additional info
|
|
|
|
$entry->setParameters( array(
|
|
|
|
'action' => $data['afl_action'],
|
|
|
|
'filter' => $data['afl_filter'],
|
|
|
|
'actions' => $data['afl_actions'],
|
|
|
|
'log' => $data['afl_id'],
|
|
|
|
) );
|
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
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( count( $logged_local_filters ) ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
// Update hit-counter.
|
2009-10-07 13:57:06 +00:00
|
|
|
$dbw->update( 'abuse_filter',
|
|
|
|
array( 'af_hit_count=af_hit_count+1' ),
|
2012-11-07 13:45:58 +00:00
|
|
|
array( 'af_id' => $logged_local_filters ),
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2009-03-30 06:12:12 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
$global_log_ids = array();
|
|
|
|
|
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;
|
|
|
|
$fdb = wfGetDB( DB_MASTER, array(), $wgAbuseFilterCentralDB );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-02-10 23:41:05 +00:00
|
|
|
foreach( $central_log_rows as $row ) {
|
|
|
|
$fdb->insert( 'abuse_filter_log', $row, __METHOD__ );
|
|
|
|
$global_log_ids[] = $dbw->insertId();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$fdb->update( 'abuse_filter',
|
|
|
|
array( 'af_hit_count=af_hit_count+1' ),
|
2009-03-30 06:12:12 +00:00
|
|
|
array( 'af_id' => $logged_global_filters ),
|
2009-10-07 13:57:06 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
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.
|
2012-11-07 13:45:58 +00:00
|
|
|
$total = $wgMemc->get( AbuseFilter::filterUsedKey( $group ) );
|
|
|
|
self::checkEmergencyDisable( $group, $logged_local_filters, $total );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ . '-hitstats' );
|
|
|
|
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
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
|
|
|
*
|
2013-01-07 00:02:41 +00:00
|
|
|
* @param $vars AbuseFilterVariableHolder
|
2011-08-24 22:11:52 +00:00
|
|
|
* @param $global bool
|
|
|
|
*
|
|
|
|
* @return int
|
2009-10-07 13:57:06 +00:00
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
public static function storeVarDump( $vars, $global = false ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
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
|
|
|
|
$vars = $vars->dumpAllVars( array( '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 );
|
|
|
|
$flags = array( '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
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
// Store to ES 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
|
2009-04-17 08:08:35 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2009-02-27 03:06:19 +00:00
|
|
|
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 ) {
|
2009-03-30 06:12:12 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER, array(), $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',
|
|
|
|
array(
|
|
|
|
'old_id' => $old_id,
|
|
|
|
'old_text' => $text,
|
|
|
|
'old_flags' => implode( ',', $flags ),
|
|
|
|
), __METHOD__
|
|
|
|
);
|
|
|
|
$text_id = $dbw->insertId();
|
|
|
|
wfProfileOut( __METHOD__ );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
return $text_id;
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* @param $stored_dump
|
|
|
|
*
|
2012-07-24 13:50:01 +00:00
|
|
|
* @return 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 ) {
|
|
|
|
wfProfileIn( __METHOD__ );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
// Back-compat
|
|
|
|
if ( strpos( $stored_dump, 'stored-text:' ) === false ) {
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return unserialize( $stored_dump );
|
|
|
|
}
|
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
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
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',
|
|
|
|
array( 'old_text', 'old_flags' ),
|
|
|
|
array( 'old_id' => $text_id ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$text_row ) {
|
2009-02-27 03:06:19 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
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();
|
|
|
|
foreach( $vars as $key => $value ) {
|
|
|
|
$obj->setVar( $key, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-27 03:06:19 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $obj;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-03-11 21:01:29 +00:00
|
|
|
* @param $action string
|
|
|
|
* @param $parameters array
|
|
|
|
* @param $title Title
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $vars AbuseFilterVariableHolder
|
|
|
|
* @param $rule_desc
|
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.
|
|
|
|
*
|
|
|
|
* @note: Returning the message as an array instead of a Message object is
|
|
|
|
* needed for compatibility with MW 1.20: we will be constructing a
|
|
|
|
* Status object from these messages, and before 1.21, Status did
|
|
|
|
* not accept Message objects to be added directly.
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2009-02-07 09:34:11 +00:00
|
|
|
public static function takeConsequenceAction( $action, $parameters, $title,
|
|
|
|
$vars, $rule_desc )
|
|
|
|
{
|
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':
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( strlen( $parameters[0] ) ) {
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array( $parameters[0], $rule_desc );
|
2008-06-27 06:18:51 +00:00
|
|
|
} else {
|
|
|
|
// Generic message.
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array(
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-disallowed',
|
2013-01-08 14:52:49 +00:00
|
|
|
$rule_desc
|
|
|
|
);
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'block':
|
2013-02-08 10:47:26 +00:00
|
|
|
global $wgUser, $wgAbuseFilterBlockDuration, $wgAbuseFilterAnonBlockDuration;
|
2008-07-09 07:02:13 +00:00
|
|
|
$filterUser = AbuseFilter::getFilterUser();
|
2008-06-27 06:18:51 +00:00
|
|
|
|
|
|
|
// Create a block.
|
|
|
|
$block = new Block;
|
2011-03-21 19:12:41 +00:00
|
|
|
$block->setTarget( $wgUser->getName() );
|
|
|
|
$block->setBlocker( $filterUser );
|
2012-09-02 11:07:02 +00:00
|
|
|
$block->mReason = wfMessage(
|
|
|
|
'abusefilter-blockreason',
|
|
|
|
$rule_desc
|
|
|
|
)->inContentLanguage()->text();
|
2011-03-19 23:47:08 +00:00
|
|
|
$block->isHardblock( false );
|
2012-07-25 18:39:20 +00:00
|
|
|
$block->isAutoblocking( true );
|
2011-03-19 23:47:08 +00:00
|
|
|
$block->prevents( 'createaccount', true );
|
2012-07-25 18:39:20 +00:00
|
|
|
$block->prevents( 'editownusertalk', false );
|
2008-06-27 06:18:51 +00:00
|
|
|
|
2013-02-22 23:20:29 +00:00
|
|
|
if ( $wgUser->isAnon() && $wgAbuseFilterAnonBlockDuration !== null ) {
|
|
|
|
// The user isn't logged in and the anon block duration doesn't default to $wgAbuseFilterBlockDuration
|
|
|
|
$expiry = $wgAbuseFilterAnonBlockDuration;
|
|
|
|
} else {
|
|
|
|
$expiry = $wgAbuseFilterBlockDuration;
|
|
|
|
}
|
|
|
|
|
2013-02-08 10:47:26 +00:00
|
|
|
$block->mExpiry = SpecialBlock::parseExpiryInput( $expiry );
|
2008-06-27 06:18:51 +00:00
|
|
|
$block->insert();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-07-09 07:02:13 +00:00
|
|
|
// Log it
|
|
|
|
# Prepare log parameters
|
|
|
|
$logParams = array();
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $block->mExpiry == 'infinity' ) {
|
2009-06-03 15:52:53 +00:00
|
|
|
$logParams[] = 'indefinite';
|
|
|
|
} else {
|
2013-02-08 10:47:26 +00:00
|
|
|
$logParams[] = $expiry;
|
2009-06-03 15:52:53 +00:00
|
|
|
}
|
2012-09-21 15:21:09 +00:00
|
|
|
$logParams[] = 'nocreate';
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-07-09 07:02:13 +00:00
|
|
|
$log = new LogPage( 'block' );
|
2009-02-07 09:34:11 +00:00
|
|
|
$log->addEntry( 'block',
|
|
|
|
Title::makeTitle( NS_USER, $wgUser->getName() ),
|
2012-09-02 11:07:02 +00:00
|
|
|
wfMessage( 'abusefilter-blockreason', $rule_desc )->inContentLanguage()->text(),
|
2009-10-07 13:57:06 +00:00
|
|
|
$logParams, self::getFilterUser()
|
|
|
|
);
|
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array(
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-blocked-display',
|
2013-01-08 14:52:49 +00:00
|
|
|
$rule_desc
|
|
|
|
);
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
2008-09-18 13:01:50 +00:00
|
|
|
case 'rangeblock':
|
|
|
|
$filterUser = AbuseFilter::getFilterUser();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-05-08 07:48:19 +00:00
|
|
|
$range = IP::sanitizeRange( $wgRequest->getIP() . '/16' );
|
2008-09-18 13:01:50 +00:00
|
|
|
|
|
|
|
// Create a block.
|
|
|
|
$block = new Block;
|
2011-03-21 19:12:41 +00:00
|
|
|
$block->setTarget( $range );
|
|
|
|
$block->setBlocker( $filterUser );
|
2012-09-02 11:07:02 +00:00
|
|
|
$block->mReason = wfMessage(
|
|
|
|
'abusefilter-blockreason',
|
|
|
|
$rule_desc
|
|
|
|
)->inContentLanguage()->text();
|
2011-03-19 23:47:08 +00:00
|
|
|
$block->isHardblock( false );
|
|
|
|
$block->prevents( 'createaccount', true );
|
2012-07-25 18:39:20 +00:00
|
|
|
$block->prevents( 'editownusertalk', false );
|
2011-03-18 16:35:22 +00:00
|
|
|
$block->mExpiry = SpecialBlock::parseExpiryInput( '1 week' );
|
2008-09-18 13:01:50 +00:00
|
|
|
|
|
|
|
$block->insert();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-09-18 13:01:50 +00:00
|
|
|
// Log it
|
|
|
|
# Prepare log parameters
|
|
|
|
$logParams = array();
|
|
|
|
$logParams[] = 'indefinite';
|
2012-09-21 15:21:09 +00:00
|
|
|
$logParams[] = 'nocreate';
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-09-18 13:01:50 +00:00
|
|
|
$log = new LogPage( 'block' );
|
2008-09-18 13:33:39 +00:00
|
|
|
$log->addEntry( 'block', Title::makeTitle( NS_USER, $range ),
|
2012-09-02 11:07:02 +00:00
|
|
|
wfMessage( 'abusefilter-blockreason', $rule_desc )->inContentLanguage()->text(),
|
2009-10-07 13:57:06 +00:00
|
|
|
$logParams, self::getFilterUser()
|
|
|
|
);
|
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array(
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-blocked-display',
|
|
|
|
$rule_desc
|
2013-01-08 14:52:49 +00:00
|
|
|
);
|
2008-09-18 13:01:50 +00:00
|
|
|
break;
|
2008-06-27 06:18:51 +00:00
|
|
|
case 'degroup':
|
|
|
|
global $wgUser;
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !$wgUser->isAnon() ) {
|
2008-08-02 13:51:29 +00:00
|
|
|
// Remove all groups from the user. Ouch.
|
|
|
|
$groups = $wgUser->getGroups();
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $groups as $group ) {
|
2008-08-02 13:51:29 +00:00
|
|
|
$wgUser->removeGroup( $group );
|
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array(
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-degrouped',
|
2013-01-08 14:52:49 +00:00
|
|
|
$rule_desc
|
|
|
|
);
|
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
|
|
|
|
2008-08-02 13:51:29 +00:00
|
|
|
// Log it.
|
|
|
|
$log = new LogPage( 'rights' );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2008-08-02 13:51:29 +00:00
|
|
|
$log->addEntry( 'rights',
|
|
|
|
$wgUser->getUserPage(),
|
2012-09-02 11:07:02 +00:00
|
|
|
wfMessage( 'abusefilter-degroupreason', $rule_desc )->inContentLanguage()->text(),
|
2008-08-02 13:51:29 +00:00
|
|
|
array(
|
|
|
|
implode( ', ', $groups ),
|
2012-01-20 12:53:02 +00:00
|
|
|
''
|
2009-10-07 13:57:06 +00:00
|
|
|
),
|
|
|
|
self::getFilterUser()
|
|
|
|
);
|
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':
|
|
|
|
global $wgUser, $wgMemc;
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !$wgUser->isAnon() ) {
|
2010-02-13 14:10:36 +00:00
|
|
|
$blockPeriod = (int)mt_rand( 3 * 86400, 7 * 86400 ); // Block for 3-7 days.
|
2008-08-02 13:51:29 +00:00
|
|
|
$wgMemc->set( self::autoPromoteBlockKey( $wgUser ), true, $blockPeriod );
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array(
|
2012-09-02 11:07:02 +00:00
|
|
|
'abusefilter-autopromote-blocked',
|
2013-01-08 14:52:49 +00:00
|
|
|
$rule_desc
|
|
|
|
);
|
2008-08-02 13:51:29 +00:00
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
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.
|
|
|
|
global $wgUser;
|
|
|
|
|
|
|
|
$actionID = implode( '-', array(
|
2009-02-26 12:15:14 +00:00
|
|
|
$title->getPrefixedText(), $wgUser->getName(),
|
|
|
|
$vars->getVar( 'ACTION' )->toString()
|
2009-01-28 19:08:18 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
AbuseFilter::$tagsToSet[$actionID] = $parameters;
|
|
|
|
break;
|
|
|
|
default:
|
2011-11-06 21:05:03 +00:00
|
|
|
if( isset( $wgAbuseFilterCustomActionsHandlers[$action] ) ) {
|
2011-11-06 01:15:55 +00:00
|
|
|
$custom_function = $wgAbuseFilterCustomActionsHandlers[$action];
|
|
|
|
if( is_callable( $custom_function ) ) {
|
2011-11-06 21:05:03 +00:00
|
|
|
$msg = call_user_func( $custom_function, $action, $parameters, $title, $vars, $rule_desc );
|
2011-11-06 01:15:55 +00:00
|
|
|
}
|
2011-11-06 21:05:03 +00:00
|
|
|
if( isset( $msg ) ) {
|
2013-01-08 14:52:49 +00:00
|
|
|
$message = array( $msg );
|
2011-11-06 01:15:55 +00:00
|
|
|
}
|
2011-11-06 21:05:03 +00:00
|
|
|
} else {
|
|
|
|
wfDebugLog( 'AbuseFilter', "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
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $throttleId
|
|
|
|
* @param $types
|
|
|
|
* @param $title
|
|
|
|
* @param $rateCount
|
|
|
|
* @param $ratePeriod
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $global bool
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2012-08-01 21:29:06 +00:00
|
|
|
public static function isThrottled( $throttleId, $types, $title, $rateCount, $ratePeriod, $global=false ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
global $wgMemc;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-08-01 21:29:06 +00:00
|
|
|
$key = self::throttleKey( $throttleId, $types, $title, $global );
|
2009-02-03 23:44:47 +00:00
|
|
|
$count = intval( $wgMemc->get( $key ) );
|
|
|
|
|
|
|
|
wfDebugLog( 'AbuseFilter', "Got value $count for throttle key $key\n" );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( $count > 0 ) {
|
2008-06-27 06:18:51 +00:00
|
|
|
$wgMemc->incr( $key );
|
2009-02-03 23:44:47 +00:00
|
|
|
$count++;
|
|
|
|
wfDebugLog( 'AbuseFilter', "Incremented throttle key $key" );
|
2008-06-27 06:18:51 +00:00
|
|
|
} else {
|
2009-02-03 23:44:47 +00:00
|
|
|
wfDebugLog( 'AbuseFilter', "Added throttle key $key with value 1" );
|
2008-06-27 06:18:51 +00:00
|
|
|
$wgMemc->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 ) {
|
2009-02-03 23:44:47 +00:00
|
|
|
wfDebugLog( 'AbuseFilter', "Throttle $key hit value $count -- maximum is $rateCount." );
|
|
|
|
return true; // THROTTLED
|
|
|
|
}
|
|
|
|
|
|
|
|
wfDebugLog( 'AbuseFilter', "Throttle $key not hit!" );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
return false; // NOT THROTTLED
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $title Title
|
|
|
|
* @return Int|string
|
|
|
|
*/
|
2008-06-27 06:18:51 +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':
|
|
|
|
return 1;
|
|
|
|
case 'page':
|
|
|
|
return $title->getPrefixedText();
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* @param $throttleId
|
|
|
|
* @param $type
|
|
|
|
* @param $title Title
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $global bool
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return String
|
|
|
|
*/
|
2012-08-01 21:29:06 +00:00
|
|
|
public static function throttleKey( $throttleId, $type, $title, $global=false ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$types = explode( ',', $type );
|
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
$identifiers = array();
|
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
|
|
|
|
2008-06-27 06:18:51 +00:00
|
|
|
$identifier = implode( ':', $identifiers );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-08-01 21:29:06 +00:00
|
|
|
global $wgAbuseFilterIsCentral, $wgAbuseFilterCentralDB;
|
|
|
|
|
|
|
|
if ( $global && !$wgAbuseFilterIsCentral ) {
|
|
|
|
list ( $globalSite, $globalPrefix ) = wfSplitWikiID( $wgAbuseFilterCentralDB );
|
|
|
|
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
|
|
|
/**
|
2012-09-02 11:07:02 +00:00
|
|
|
* @param $group
|
2012-08-03 21:55:35 +00:00
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public static function getGlobalRulesKey( $group ) {
|
|
|
|
global $wgAbuseFilterIsCentral, $wgAbuseFilterCentralDB;
|
|
|
|
|
|
|
|
if ( !$wgAbuseFilterIsCentral ) {
|
|
|
|
list ( $globalSite, $globalPrefix ) = wfSplitWikiID( $wgAbuseFilterCentralDB );
|
|
|
|
|
|
|
|
return wfForeignMemcKey(
|
|
|
|
$globalSite, $globalPrefix,
|
|
|
|
'abusefilter', 'rules', $group
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return wfMemcKey( 'abusefilter', 'rules', $group );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $user User
|
|
|
|
* @return String
|
|
|
|
*/
|
2008-06-27 06:18:51 +00:00
|
|
|
public static function autoPromoteBlockKey( $user ) {
|
|
|
|
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.
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $filters
|
2012-11-07 13:45:58 +00:00
|
|
|
* @param $group
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2012-11-07 13:45:58 +00:00
|
|
|
public static function recordStats( $filters, $group = 'default' ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
global $wgAbuseFilterConditionLimit, $wgMemc;
|
|
|
|
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
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
|
|
|
|
|
|
|
// Store some keys...
|
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
|
|
|
|
2008-07-17 02:43:45 +00:00
|
|
|
$total = $wgMemc->get( $total_key );
|
|
|
|
|
2009-01-23 19:23:19 +00:00
|
|
|
$storage_period = self::$statsStoragePeriod;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( !$total || $total > 10000 ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
// This is for if the total doesn't exist, or has gone past 1000.
|
2009-10-07 13:57:06 +00:00
|
|
|
// Recreate all the keys at the same time, so they expire together.
|
2009-01-23 19:23:19 +00:00
|
|
|
$wgMemc->set( $total_key, 0, $storage_period );
|
|
|
|
$wgMemc->set( $overflow_key, 0, $storage_period );
|
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $filters as $filter => $matched ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
$wgMemc->set( self::filterMatchesKey( $filter ), 0, $storage_period );
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
$wgMemc->set( self::filterMatchesKey(), 0, $storage_period );
|
2008-07-17 02:43:45 +00:00
|
|
|
}
|
2009-01-23 19:23:19 +00:00
|
|
|
|
|
|
|
// Increment total
|
2008-07-17 02:43:45 +00:00
|
|
|
$wgMemc->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 ) {
|
2008-07-17 02:43:45 +00:00
|
|
|
$wgMemc->incr( $overflow_key );
|
|
|
|
}
|
2009-03-10 22:56:51 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2012-11-07 13:45:58 +00:00
|
|
|
* @param $group
|
2012-03-11 20:40:04 +00:00
|
|
|
* @param $filters
|
|
|
|
* @param $total
|
|
|
|
*/
|
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,
|
|
|
|
$wgAbuseFilterEmergencyDisableAge, $wgMemc;
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2012-11-07 13:45:58 +00:00
|
|
|
foreach ( $filters as $filter ) {
|
2012-10-23 13:14:34 +00:00
|
|
|
// determine emergency disable values for this action
|
2012-11-07 13:45:58 +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
|
|
|
|
$matchCount = $wgMemc->get( self::filterMatchesKey( $filter ) );
|
|
|
|
|
|
|
|
// Handle missing keys...
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !$matchCount ) {
|
2009-02-13 18:30:34 +00:00
|
|
|
$wgMemc->set( self::filterMatchesKey( $filter ), 1, self::$statsStoragePeriod );
|
|
|
|
} else {
|
|
|
|
$wgMemc->incr( self::filterMatchesKey( $filter ) );
|
|
|
|
}
|
|
|
|
$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.
|
2013-01-07 17:36:31 +00:00
|
|
|
$filter_age = wfTimestamp( TS_UNIX, self::$filters[$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
|
|
|
|
&& ( $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.
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2009-10-07 13:57:06 +00:00
|
|
|
$dbw->update( 'abuse_filter',
|
|
|
|
array( 'af_throttled' => 1 ),
|
|
|
|
array( 'af_id' => $filter ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
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
|
2012-11-07 13:45:58 +00:00
|
|
|
* @param string $group
|
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 ) {
|
|
|
|
return isset( $emergencyValue[$group] ) ? $emergencyValue[$group] : $emergencyValue['default'];
|
2012-10-23 13:14:34 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
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
|
|
|
/**
|
2012-11-07 13:45:58 +00:00
|
|
|
* @param string $group The filter's group (as defined in $wgAbuseFilterValidGroups)
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return String
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* @param $filter
|
|
|
|
* @return String
|
|
|
|
*/
|
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() {
|
2012-09-02 11:07:02 +00:00
|
|
|
$user = User::newFromName( wfMessage( 'abusefilter-blocker' )->inContentLanguage()->text() );
|
2008-07-09 07:02:13 +00:00
|
|
|
$user->load();
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $user->getId() && $user->mPassword == '' ) {
|
2008-07-09 07:02:13 +00:00
|
|
|
// Already set up.
|
|
|
|
return $user;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2008-07-09 07:02:13 +00:00
|
|
|
// Not set up. Create it.
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !$user->getId() ) {
|
|
|
|
print 'Trying to create account -- user id is ' . $user->getId();
|
2008-07-09 07:02:13 +00:00
|
|
|
$user->addToDatabase();
|
|
|
|
$user->saveSettings();
|
2010-08-19 21:12:09 +00:00
|
|
|
// Increment site_stats.ss_users
|
2009-01-28 00:10:35 +00:00
|
|
|
$ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
|
|
|
|
$ssu->doUpdate();
|
2008-07-09 07:02:13 +00:00
|
|
|
} else {
|
|
|
|
// Take over the account
|
|
|
|
$user->setPassword( null );
|
|
|
|
$user->setEmail( null );
|
|
|
|
$user->saveSettings();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
// Promote user so it doesn't look too crazy.
|
2008-07-09 07:02:13 +00:00
|
|
|
$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
|
|
|
|
2012-02-09 17:00:34 +00:00
|
|
|
/**
|
|
|
|
* @param $rules String
|
|
|
|
* @param $textName String
|
|
|
|
* @param $addResultDiv Boolean
|
|
|
|
* @param $canEdit Boolean
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return string
|
2012-02-09 17:00:34 +00:00
|
|
|
*/
|
2009-04-01 04:34:21 +00:00
|
|
|
static function buildEditBox( $rules, $textName = 'wpFilterRules', $addResultDiv = true,
|
2009-10-07 13:57:06 +00:00
|
|
|
$canEdit = true ) {
|
2009-01-23 22:49:13 +00:00
|
|
|
global $wgOut;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-02-09 17:00:34 +00:00
|
|
|
$textareaAttrib = array( 'dir' => 'ltr' ); # Rules are in English
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
$textareaAttrib['readonly'] = 'readonly';
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-01 04:34:21 +00:00
|
|
|
global $wgUser;
|
|
|
|
$noTestAttrib = array();
|
|
|
|
if ( !$wgUser->isAllowed( 'abusefilter-modify' ) ) {
|
|
|
|
$noTestAttrib['disabled'] = 'disabled';
|
|
|
|
$addResultDiv = false;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
$rules = rtrim( $rules ) . "\n";
|
2012-02-09 17:00:34 +00:00
|
|
|
$rules = Xml::textarea( $textName, $rules, 40, 5, $textareaAttrib );
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2012-02-10 00:26:39 +00:00
|
|
|
if ( $canEdit ) {
|
|
|
|
$dropDown = self::getBuilderValues();
|
|
|
|
// Generate builder drop-down
|
|
|
|
$builder = '';
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
$builder .= Xml::option( wfMessage( 'abusefilter-edit-builder-select' )->text() );
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2012-02-10 00:26:39 +00:00
|
|
|
foreach ( $dropDown as $group => $values ) {
|
2009-02-07 09:34:11 +00:00
|
|
|
$builder .=
|
2012-02-10 00:26:39 +00:00
|
|
|
Xml::openElement(
|
|
|
|
'optgroup',
|
2012-09-02 11:07:02 +00:00
|
|
|
array( 'label' => wfMessage( "abusefilter-edit-builder-group-$group" )->text() )
|
2009-02-07 09:34:11 +00:00
|
|
|
) . "\n";
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2012-02-10 00:26:39 +00:00
|
|
|
foreach ( $values as $content => $name ) {
|
|
|
|
$builder .=
|
|
|
|
Xml::option(
|
2012-09-02 11:07:02 +00:00
|
|
|
wfMessage( "abusefilter-edit-builder-$group-$name" )->text(),
|
2012-02-10 00:26:39 +00:00
|
|
|
$content
|
|
|
|
) . "\n";
|
|
|
|
}
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2012-02-10 00:26:39 +00:00
|
|
|
$builder .= Xml::closeElement( 'optgroup' ) . "\n";
|
|
|
|
}
|
2009-01-23 22:49:13 +00:00
|
|
|
|
2012-02-10 00:26:39 +00:00
|
|
|
$rules .=
|
|
|
|
Xml::tags(
|
|
|
|
'select',
|
|
|
|
array( 'id' => 'wpFilterBuilder', ),
|
|
|
|
$builder
|
|
|
|
) . ' ';
|
|
|
|
|
|
|
|
// Add syntax checking
|
|
|
|
$rules .= Xml::element( 'input',
|
|
|
|
array(
|
|
|
|
'type' => 'button',
|
2012-09-02 11:07:02 +00:00
|
|
|
'value' => wfMessage( 'abusefilter-edit-check' )->text(),
|
2012-02-10 00:26:39 +00:00
|
|
|
'id' => 'mw-abusefilter-syntaxcheck'
|
|
|
|
) + $noTestAttrib );
|
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $addResultDiv )
|
2010-02-13 14:10:36 +00:00
|
|
|
$rules .= Xml::element( 'div',
|
|
|
|
array( 'id' => 'mw-abusefilter-syntaxresult', 'style' => 'display: none;' ),
|
2010-05-30 17:33:59 +00:00
|
|
|
' ' );
|
2009-01-23 22:49:13 +00:00
|
|
|
|
|
|
|
// Add script
|
2011-08-26 20:12:34 +00:00
|
|
|
$wgOut->addModules( 'ext.abuseFilter.edit' );
|
|
|
|
self::$editboxName = $textName;
|
2009-01-23 22:49:13 +00:00
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
2009-01-26 22:31:02 +00:00
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param $version_1
|
|
|
|
* @param $version_2
|
|
|
|
*
|
|
|
|
* @return array
|
2009-10-07 13:57:06 +00:00
|
|
|
*/
|
2009-01-26 22:31:02 +00:00
|
|
|
static function compareVersions( $version_1, $version_2 ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$compareFields = array(
|
|
|
|
'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',
|
2009-02-07 09:34:11 +00:00
|
|
|
);
|
2009-01-26 22:31:02 +00:00
|
|
|
$differences = array();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wgAbuseFilterAvailableActions;
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $wgAbuseFilterAvailableActions 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] ) ) {
|
2009-01-26 22:31:02 +00:00
|
|
|
// They're both set.
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( array_diff( $actions1[$action]['parameters'],
|
|
|
|
$actions2[$action]['parameters'] ) )
|
2009-02-07 09:34:11 +00:00
|
|
|
{
|
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
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-01-26 22:31:02 +00:00
|
|
|
static function translateFromHistory( $row ) {
|
2010-08-19 21:12:09 +00:00
|
|
|
# Translate into an abuse_filter row with some black magic.
|
|
|
|
# This is ever so slightly evil!
|
2009-01-26 22:31:02 +00:00
|
|
|
$af_row = new StdClass;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-19 21:12:09 +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;
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$flags = explode( ',', $row->afh_flags );
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $flags as $flag ) {
|
2009-01-26 22:31:02 +00:00
|
|
|
$col_name = "af_$flag";
|
|
|
|
$af_row->$col_name = 1;
|
|
|
|
}
|
|
|
|
|
2010-08-19 21:12:09 +00:00
|
|
|
# Process actions
|
2009-10-07 13:57:06 +00:00
|
|
|
$actions_raw = unserialize( $row->afh_actions );
|
2009-01-26 22:31:02 +00:00
|
|
|
$actions_output = array();
|
2013-02-11 22:30:42 +00:00
|
|
|
if ( is_array( $actions_raw ) ) {
|
|
|
|
foreach ( $actions_raw as $action => $parameters ) {
|
|
|
|
$actions_output[$action] = array(
|
|
|
|
'action' => $action,
|
|
|
|
'parameters' => $parameters
|
|
|
|
);
|
|
|
|
}
|
2009-01-26 22:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return array( $af_row, $actions_output );
|
|
|
|
}
|
2009-01-28 01:26:38 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $action string
|
|
|
|
* @return String
|
|
|
|
*/
|
2009-01-28 01:26:38 +00:00
|
|
|
static function getActionDisplay( $action ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
$display = wfMessage( "abusefilter-action-$action" )->text();
|
|
|
|
$display = wfMessage( "abusefilter-action-$action", $display )->isDisabled() ? $action : $display;
|
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
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @return AbuseFilterVariableHolder|null
|
|
|
|
*/
|
2009-01-28 23:54:41 +00:00
|
|
|
public static function getVarsFromRCRow( $row ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( $row->rc_this_oldid ) {
|
2009-01-28 23:54:41 +00:00
|
|
|
// It's an edit.
|
2009-03-19 02:05:58 +00:00
|
|
|
$vars = self::getEditVarsFromRCRow( $row );
|
2009-01-28 23:54:41 +00:00
|
|
|
} elseif ( $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 );
|
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
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @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
|
|
|
|
2012-05-10 08:08:48 +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 );
|
2009-01-28 23:54:41 +00:00
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @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 ),
|
|
|
|
self::generateTitleVars( $title, 'ARTICLE' )
|
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setVar( 'ACTION', 'edit' );
|
|
|
|
$vars->setVar( 'SUMMARY', $row->rc_comment );
|
2009-03-04 02:04:48 +00:00
|
|
|
$vars->setVar( 'minor_edit', $row->rc_minor );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'new_wikitext', 'revision-text-by-id',
|
|
|
|
array( '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',
|
|
|
|
array( '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
|
|
|
/**
|
|
|
|
* @param $row
|
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2009-01-28 23:54:41 +00:00
|
|
|
public static function getMoveVarsFromRCRow( $row ) {
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars = new AbuseFilterVariableHolder;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
$params = explode( "\n", trim( $row->rc_params ) );
|
|
|
|
|
2009-01-28 23:54:41 +00:00
|
|
|
$oldTitle = Title::makeTitle( $row->rc_namespace, $row->rc_title );
|
2009-03-09 13:22:58 +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(
|
|
|
|
$vars,
|
|
|
|
AbuseFilter::generateUserVars( $user ),
|
2009-01-28 23:54:41 +00:00
|
|
|
AbuseFilter::generateTitleVars( $oldTitle, 'MOVED_FROM' ),
|
2009-02-26 12:15:14 +00:00
|
|
|
AbuseFilter::generateTitleVars( $newTitle, 'MOVED_TO' )
|
|
|
|
);
|
2009-01-28 23:54:41 +00:00
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setVar( 'SUMMARY', $row->rc_comment );
|
|
|
|
$vars->setVar( 'ACTION', 'move' );
|
2009-01-28 23:54:41 +00:00
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2011-02-10 17:25:25 +00:00
|
|
|
/**
|
2013-04-01 09:04:03 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param null|Page $page
|
2011-02-10 17:25:25 +00:00
|
|
|
* @return AbuseFilterVariableHolder
|
|
|
|
*/
|
2013-01-04 15:37:56 +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 );
|
|
|
|
}
|
|
|
|
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'edit_diff', 'diff',
|
2009-03-04 01:56:21 +00:00
|
|
|
array( 'oldtext-var' => 'old_wikitext', 'newtext-var' => 'new_wikitext' ) );
|
2009-02-26 12:15:14 +00:00
|
|
|
$vars->setLazyLoadVar( 'new_size', 'length', array( 'length-var' => 'new_wikitext' ) );
|
|
|
|
$vars->setLazyLoadVar( 'old_size', 'length', array( 'length-var' => 'old_wikitext' ) );
|
|
|
|
$vars->setLazyLoadVar( 'edit_delta', 'subtract',
|
|
|
|
array( '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',
|
|
|
|
array( 'diff-var' => 'edit_diff', 'line-prefix' => '+' ) );
|
|
|
|
$vars->setLazyLoadVar( 'removed_lines', 'diff-split',
|
|
|
|
array( 'diff-var' => 'edit_diff', 'line-prefix' => '-' ) );
|
|
|
|
|
|
|
|
// Links
|
|
|
|
$vars->setLazyLoadVar( 'added_links', 'link-diff-added',
|
|
|
|
array( 'oldlink-var' => 'old_links', 'newlink-var' => 'all_links' ) );
|
|
|
|
$vars->setLazyLoadVar( 'removed_links', 'link-diff-removed',
|
|
|
|
array( 'oldlink-var' => 'old_links', 'newlink-var' => 'all_links' ) );
|
|
|
|
$vars->setLazyLoadVar( 'new_text', 'strip-html',
|
|
|
|
array( 'html-var' => 'new_html' ) );
|
|
|
|
$vars->setLazyLoadVar( 'old_text', 'strip-html',
|
|
|
|
array( 'html-var' => 'old_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',
|
|
|
|
array(
|
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'text-var' => 'new_wikitext',
|
|
|
|
'article' => $page
|
|
|
|
) );
|
|
|
|
$vars->setLazyLoadVar( 'old_links', 'links-from-wikitext-or-database',
|
|
|
|
array(
|
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'text-var' => 'old_wikitext'
|
|
|
|
) );
|
|
|
|
$vars->setLazyLoadVar( 'new_html', 'parse-wikitext',
|
|
|
|
array(
|
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'wikitext-var' => 'new_wikitext',
|
|
|
|
'article' => $page
|
|
|
|
) );
|
|
|
|
$vars->setLazyLoadVar( 'old_html', 'parse-wikitext-nonedit',
|
|
|
|
array(
|
|
|
|
'namespace' => $title->getNamespace(),
|
|
|
|
'title' => $title->getText(),
|
|
|
|
'wikitext-var' => 'old_wikitext'
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @param $vars AbuseFilterVariableHolder
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-01-29 22:44:31 +00:00
|
|
|
public static function buildVarDumpTable( $vars ) {
|
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 .=
|
|
|
|
Xml::openElement( 'table', array( 'class' => 'mw-abuselog-details' ) ) .
|
|
|
|
Xml::openElement( 'tbody' ) .
|
2009-02-07 09:34:11 +00:00
|
|
|
"\n";
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$header =
|
2012-09-02 11:07:02 +00:00
|
|
|
Xml::element( 'th', null, wfMessage( 'abusefilter-log-details-var' )->text() ) .
|
|
|
|
Xml::element( 'th', null, wfMessage( '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' );
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
// Now, build the body of the table.
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $vars as $key => $value ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
$key = strtolower( $key );
|
|
|
|
|
|
|
|
if ( !empty( $variableMessageMappings[$key] ) ) {
|
2009-01-29 22:44:31 +00:00
|
|
|
$mapping = $variableMessageMappings[$key];
|
2012-09-02 11:07:02 +00:00
|
|
|
$keyDisplay = wfMessage( "abusefilter-edit-builder-vars-$mapping" )->parse() .
|
|
|
|
' ' . Xml::element( 'code', null, wfMessage( 'parentheses', $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
|
|
|
}
|
|
|
|
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( is_null( $value ) )
|
2009-01-30 15:40:59 +00:00
|
|
|
$value = '';
|
2009-01-29 22:44:31 +00:00
|
|
|
$value = Xml::element( 'div', array( 'class' => 'mw-abuselog-var-value' ), $value );
|
|
|
|
|
2009-10-07 13:57:06 +00:00
|
|
|
$trow =
|
2010-02-13 14:10:36 +00:00
|
|
|
Xml::tags( 'td', array( 'class' => 'mw-abuselog-var' ), $keyDisplay ) .
|
2009-02-07 09:34:11 +00:00
|
|
|
Xml::tags( 'td', array( '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',
|
2009-02-07 09:34:11 +00:00
|
|
|
array( 'class' => "mw-abuselog-details-$key mw-abuselog-value" ), $trow
|
|
|
|
) . "\n";
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$output .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
|
|
|
|
return $output;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $page
|
|
|
|
* @param $type
|
|
|
|
* @param $title Title
|
|
|
|
* @param $sk Skin
|
|
|
|
* @param $args array
|
2012-12-22 18:50:55 +00:00
|
|
|
* @param $filterWikilinks bool
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return String
|
|
|
|
*/
|
2012-12-22 18:50:55 +00:00
|
|
|
static function modifyActionText( $page, $type, $title, $sk, $args, $filterWikilinks ) {
|
2009-03-11 07:12:42 +00:00
|
|
|
list( $history_id, $filter_id ) = $args;
|
2012-12-22 18:50:55 +00:00
|
|
|
$details_title = SpecialPage::getTitleFor( 'AbuseFilter', "history/$filter_id/diff/prev/$history_id" );
|
|
|
|
if ( !$filterWikilinks ) { // Plaintext? Bug 43105
|
|
|
|
return wfMessage( 'abusefilter-log-entry-modify', '[[' . $title->getFullText() . ']]', '[[' . $details_title->getFullText() . ']]' )->text();
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
$filter_link = Linker::link( $title );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
$details_text = wfMessage( 'abusefilter-log-detailslink' )->parse();
|
2012-03-11 20:40:04 +00:00
|
|
|
$details_link = Linker::link( $details_title, $details_text );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
return wfMessage( 'abusefilter-log-entry-modify' )
|
|
|
|
->rawParams( $filter_link, $details_link )->parse();
|
2009-03-11 07:12:42 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
|
|
|
* @param $action
|
|
|
|
* @param $parameters
|
|
|
|
* @return String
|
|
|
|
*/
|
2009-03-12 05:04:39 +00:00
|
|
|
static function formatAction( $action, $parameters ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
/*
|
|
|
|
* @var $wgLang Language
|
|
|
|
*/
|
2009-03-12 05:04:39 +00:00
|
|
|
global $wgLang;
|
2010-02-13 14:10:36 +00:00
|
|
|
if ( count( $parameters ) == 0 ) {
|
2009-03-12 05:04:39 +00:00
|
|
|
$displayAction = AbuseFilter::getActionDisplay( $action );
|
|
|
|
} else {
|
|
|
|
$displayAction = AbuseFilter::getActionDisplay( $action ) .
|
2012-09-02 11:07:02 +00:00
|
|
|
wfMessage( 'colon-separator' )->escaped() .
|
|
|
|
$wgLang->semicolonList( $parameters );
|
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
|
|
|
/**
|
|
|
|
* @param $value array
|
|
|
|
* @return string
|
|
|
|
*/
|
2009-03-12 05:04:39 +00:00
|
|
|
static function formatFlags( $value ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
/*
|
|
|
|
* @var $wgLang Language
|
|
|
|
*/
|
2009-03-12 05:04:39 +00:00
|
|
|
global $wgLang;
|
|
|
|
$flags = array_filter( explode( ',', $value ) );
|
|
|
|
$flags_display = array();
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $flags as $flag ) {
|
2012-09-02 11:07:02 +00:00
|
|
|
$flags_display[] = wfMessage( "abusefilter-history-$flag" )->text();
|
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
|
|
|
/**
|
|
|
|
* @param $filterID
|
|
|
|
* @return bool|mixed|string
|
|
|
|
*/
|
2009-03-30 06:12:12 +00:00
|
|
|
static function getGlobalFilterDescription( $filterID ) {
|
|
|
|
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
|
|
|
|
2009-03-30 06:12:12 +00:00
|
|
|
$fdb = wfGetDB( DB_SLAVE, array(), $wgAbuseFilterCentralDB );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
return $fdb->selectField(
|
|
|
|
'abuse_filter',
|
|
|
|
'af_public_comments',
|
|
|
|
array( 'af_id' => $filterID ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
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
|
|
|
|
* @param $group String: Internal name of the filter group
|
|
|
|
* @return String A name for that filter group, or the input.
|
|
|
|
*/
|
|
|
|
static function nameGroup($group) {
|
|
|
|
$msg = "abusefilter-group-$group";
|
|
|
|
return wfMessage($msg)->exists() ? wfMessage($msg)->escaped() : $group;
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @param Revision $revision a valid revision
|
|
|
|
* @param $audience Integer: one of:
|
|
|
|
* 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,
|
|
|
|
* or an empty string if it can not be found
|
|
|
|
*/
|
2013-01-04 15:37:56 +00:00
|
|
|
static function revisionToString( $revision, $audience = Revision::FOR_THIS_USER ) {
|
2012-11-20 15:16:58 +00:00
|
|
|
if ( !$revision instanceof Revision ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if ( defined( 'MW_SUPPORTS_CONTENTHANDLER' ) ) {
|
|
|
|
$content = $revision->getContent( $audience );
|
2013-01-08 14:52:49 +00:00
|
|
|
if ( $content === null ) {
|
2013-01-04 15:37:56 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$result = self::contentToString( $content );
|
2012-11-20 15:16:58 +00:00
|
|
|
} else {
|
|
|
|
// For MediaWiki without contenthandler support (< 1.21)
|
|
|
|
$result = $revision->getText();
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static function contentToString( Content $content ) {
|
|
|
|
$text = null;
|
|
|
|
|
|
|
|
if ( wfRunHooks( 'AbuseFilter-contentToString', array( $content, &$text ) ) ) {
|
|
|
|
$text = $content instanceof TextContent
|
|
|
|
? $content->getNativeData()
|
|
|
|
: $content->getTextForSearchIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_string( $text ) ) {
|
|
|
|
// bug 20310
|
|
|
|
// XXX: Is this really needed? Should we rather apply PST?
|
|
|
|
$text = str_replace( "\r\n", "\n", $text );
|
|
|
|
} else {
|
|
|
|
$text = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2012-12-30 03:59:33 +00:00
|
|
|
/*
|
|
|
|
* Get the history ID of the first change to a given filter
|
|
|
|
*
|
|
|
|
* @param $filterId integer: Filter id
|
|
|
|
* @return integer|bool
|
|
|
|
*/
|
|
|
|
public static function getFirstFilterChange( $filterID ) {
|
|
|
|
static $firstChanges = array();
|
|
|
|
|
|
|
|
if ( !isset( $firstChanges[ $filterID ] ) ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$row = $dbr->selectRow(
|
|
|
|
'abuse_filter_history',
|
|
|
|
'afh_id',
|
|
|
|
array(
|
|
|
|
'afh_filter' => $filterID,
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY' => 'afh_timestamp ASC' )
|
|
|
|
);
|
|
|
|
$firstChanges[$filterID] = $row->afh_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $firstChanges[$filterID];
|
|
|
|
}
|
2008-06-27 06:18:51 +00:00
|
|
|
}
|