From d1728d62f82d2b4d60e476833d449880a452f54c Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Thu, 30 Aug 2018 17:02:23 +0200 Subject: [PATCH] Add a parameter to generate(User|Title)Vars hooks to specify context These hooks can be called either when filtering the current action, or to check an action from recentchanges. While AbuseFilter already handles well these two cases, other extensions don't and need some context. Depends on the patch below because we're changing generateTitleVars, which already has a temporary extra parameter that I don't want to mess up with. Also follow-up I72933fcc9952fc1aabf6464b2fc0b04ec39c024b for a few remaining uppercase vars. Bug: T203166 Depends-On: If5f238cddb41ef92b141e36b4f2f15fd4cc86476 Change-Id: I1983b93bbadabd24d8bf94fa7bb14594d10e731e --- hooks.txt | 2 ++ includes/AbuseFilter.php | 40 ++++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/hooks.txt b/hooks.txt index d85896eff..d2b2c5ba4 100644 --- a/hooks.txt +++ b/hooks.txt @@ -37,10 +37,12 @@ $title: Title object $vars: AbuseFilterVariableHolder $title: Title object $prefix: Variable name prefix +$RCRow: If the variables should be generated for an RC row, this is the row. False if it's for the current action being filtered. 'AbuseFilter-generateUserVars': Allows altering the variables generated for a specific user $vars: AbuseFilterVariableHolder $user: User object +$RCRow: If the variables should be generated for an RC row, this is the row. False if it's for the current action being filtered. 'AbuseFilter-generateStaticVars': Allows altering static variables, i.e. independent from page and user $vars: AbuseFilterVariableHolder diff --git a/includes/AbuseFilter.php b/includes/AbuseFilter.php index 304db33c9..3b6053720 100644 --- a/includes/AbuseFilter.php +++ b/includes/AbuseFilter.php @@ -269,9 +269,11 @@ class AbuseFilter { /** * @param User $user + * @param null|stdClass $rcRow If the variables should be generated for an RC row, this is the row. + * Null if it's for the current action being filtered. * @return AbuseFilterVariableHolder */ - public static function generateUserVars( User $user ) { + public static function generateUserVars( User $user, $rcRow = null ) { $vars = new AbuseFilterVariableHolder; $vars->setLazyLoadVar( @@ -312,7 +314,7 @@ class AbuseFilter { [ 'user' => $user, 'method' => 'isBlocked' ] ); - Hooks::run( 'AbuseFilter-generateUserVars', [ $vars, $user ] ); + Hooks::run( 'AbuseFilter-generateUserVars', [ $vars, $user, $rcRow ] ); return $vars; } @@ -404,19 +406,21 @@ class AbuseFilter { /** * @param Title|null $title * @param string $prefix + * @param null|stdClass $rcRow If the variables should be generated for an RC row, this is the row. + * Null if it's for the current action being filtered. * @return AbuseFilterVariableHolder */ - public static function generateTitleVars( $title, $prefix ) { + public static function generateTitleVars( $title, $prefix, $rcRow = null ) { $vars = new AbuseFilterVariableHolder; if ( !$title ) { return $vars; } - $vars->setVar( $prefix . '_ID', $title->getArticleID() ); - $vars->setVar( $prefix . '_NAMESPACE', $title->getNamespace() ); - $vars->setVar( $prefix . '_TITLE', $title->getText() ); - $vars->setVar( $prefix . '_PREFIXEDTITLE', $title->getPrefixedText() ); + $vars->setVar( $prefix . '_id', $title->getArticleID() ); + $vars->setVar( $prefix . '_namespace', $title->getNamespace() ); + $vars->setVar( $prefix . '_title', $title->getText() ); + $vars->setVar( $prefix . '_prefixedtitle', $title->getPrefixedText() ); global $wgRestrictionTypes; foreach ( $wgRestrictionTypes as $action ) { @@ -447,7 +451,7 @@ class AbuseFilter { 'namespace' => $title->getNamespace() ] ); - Hooks::run( 'AbuseFilter-generateTitleVars', [ $vars, $title, $prefix ] ); + Hooks::run( 'AbuseFilter-generateTitleVars', [ $vars, $title, $prefix, $rcRow ] ); return $vars; } @@ -2818,7 +2822,7 @@ class AbuseFilter { // 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 ); - $vars->addHolders( self::generateUserVars( $user ) ); + $vars->addHolders( self::generateUserVars( $user, $row ) ); } $vars->setVar( 'accountname', $name ); @@ -2842,8 +2846,8 @@ class AbuseFilter { } $vars->addHolders( - self::generateUserVars( $user ), - self::generateTitleVars( $title, 'page' ) + self::generateUserVars( $user, $row ), + self::generateTitleVars( $title, 'page', $row ) ); $vars->setVar( 'action', 'delete' ); @@ -2868,8 +2872,8 @@ class AbuseFilter { } $vars->addHolders( - self::generateUserVars( $user ), - self::generateTitleVars( $title, 'page' ) + self::generateUserVars( $user, $row ), + self::generateTitleVars( $title, 'page', $row ) ); $vars->setVar( 'action', 'upload' ); @@ -2920,8 +2924,8 @@ class AbuseFilter { } $vars->addHolders( - self::generateUserVars( $user ), - self::generateTitleVars( $title, 'page' ) + self::generateUserVars( $user, $row ), + self::generateTitleVars( $title, 'page', $row ) ); $vars->setVar( 'action', 'edit' ); @@ -2960,9 +2964,9 @@ class AbuseFilter { $newTitle = Title::newFromText( $params[0] ); $vars = AbuseFilterVariableHolder::merge( - self::generateUserVars( $user ), - self::generateTitleVars( $oldTitle, 'MOVED_FROM' ), - self::generateTitleVars( $newTitle, 'MOVED_TO' ) + self::generateUserVars( $user, $row ), + self::generateTitleVars( $oldTitle, 'moved_from', $row ), + self::generateTitleVars( $newTitle, 'moved_to', $row ) ); $vars->setVar( 'summary', CommentStore::getStore()->getComment( 'rc_comment', $row )->text );