From eff4580a6fe238e7de0df7f12fad670e5cd2293b Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sun, 24 Feb 2019 15:55:19 +0100 Subject: [PATCH] Add new method: AbuseFilterVariableHolder::newFromArray Instead of duplicating code in several files. Depends-On: I2eab2e50356eeb5224446ee2d0df9c787ae95b80 Change-Id: Iafc142d2e5ba7aa0fb0d3265fa05cace27679738 --- includes/AbuseFilter.php | 16 ++-------------- includes/AbuseFilterVariableHolder.php | 14 ++++++++++++++ includes/api/ApiAbuseFilterCheckMatch.php | 5 +---- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/includes/AbuseFilter.php b/includes/AbuseFilter.php index 7dfba6c83..bb45839a7 100644 --- a/includes/AbuseFilter.php +++ b/includes/AbuseFilter.php @@ -870,16 +870,7 @@ class AbuseFilter { // Backward compatibility if ( substr( $stored_dump, 0, strlen( 'stored-text:' ) ) !== 'stored-text:' ) { $data = unserialize( $stored_dump ); - if ( is_array( $data ) ) { - $vh = new AbuseFilterVariableHolder; - foreach ( $data as $name => $value ) { - $vh->setVar( $name, $value ); - } - - return $vh; - } else { - return $data; - } + return is_array( $data ) ? AbuseFilterVariableHolder::newFromArray( $data ) : $data; } $text_id = substr( $stored_dump, strlen( 'stored-text:' ) ); @@ -912,10 +903,7 @@ class AbuseFilter { if ( in_array( 'nativeDataArray', $flags ) ) { $vars = $obj; - $obj = new AbuseFilterVariableHolder(); - foreach ( $vars as $key => $value ) { - $obj->setVar( $key, $value ); - } + $obj = AbuseFilterVariableHolder::newFromArray( $vars ); // If old variable names are used, make sure to keep them if ( count( array_intersect_key( self::getDeprecatedVariables(), $obj->getVars() ) ) !== 0 ) { $obj->mVarsVersion = 1; diff --git a/includes/AbuseFilterVariableHolder.php b/includes/AbuseFilterVariableHolder.php index 0b468431a..ad3a71894 100644 --- a/includes/AbuseFilterVariableHolder.php +++ b/includes/AbuseFilterVariableHolder.php @@ -13,6 +13,20 @@ class AbuseFilterVariableHolder { */ public $mVarsVersion = 2; + /** + * Utility function to translate an array with shape [ varname => value ] into a self instance + * + * @param array $vars + * @return AbuseFilterVariableHolder + */ + public static function newFromArray( array $vars ) : AbuseFilterVariableHolder { + $ret = new self; + foreach ( $vars as $var => $value ) { + $ret->setVar( $var, $value ); + } + return $ret; + } + /** * @param string $variable * @param mixed $datum diff --git a/includes/api/ApiAbuseFilterCheckMatch.php b/includes/api/ApiAbuseFilterCheckMatch.php index 816bf6119..a6bd53e69 100644 --- a/includes/api/ApiAbuseFilterCheckMatch.php +++ b/includes/api/ApiAbuseFilterCheckMatch.php @@ -15,11 +15,8 @@ class ApiAbuseFilterCheckMatch extends ApiBase { $vars = null; if ( $params['vars'] ) { - $vars = new AbuseFilterVariableHolder; $pairs = FormatJson::decode( $params['vars'], true ); - foreach ( $pairs as $name => $value ) { - $vars->setVar( $name, $value ); - } + $vars = AbuseFilterVariableHolder::newFromArray( $pairs ); } elseif ( $params['rcid'] ) { $dbr = wfGetDB( DB_REPLICA ); $rcQuery = RecentChange::getQueryInfo();