From 435d16e003ad51bf47084969f5e3ecebda41cec9 Mon Sep 17 00:00:00 2001 From: umherirrender Date: Sat, 9 Mar 2013 22:14:03 +0100 Subject: [PATCH] API param validation: Add wrong value to error message on user params It is always nice to get the wrong value back to know it. Refactor a bit to avoid the array wrapping and unwrapping for non-multi params. Adjust another variable, add doc comments, removed a empty line and reorder a condition for easy reading. Change-Id: Ia91aa5908b82ad1209dc7da1139f91e1f2b45fac --- includes/api/ApiBase.php | 46 +++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index b82aadf80c..3577193ce0 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -965,7 +965,6 @@ abstract class ApiBase extends ContextSource { if ( $required && $value === '' ) { $this->dieUsageMsg( array( 'missingparam', $paramName ) ); } - break; case 'integer': // Force everything using intval() and optionally validate limits $min = isset ( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : null; @@ -1022,20 +1021,12 @@ abstract class ApiBase extends ContextSource { } break; case 'user': - if ( !is_array( $value ) ) { - $value = array( $value ); - } - - foreach ( $value as $key => $val ) { - $title = Title::makeTitleSafe( NS_USER, $val ); - if ( is_null( $title ) ) { - $this->dieUsage( "Invalid value for user parameter $encParamName", "baduser_{$encParamName}" ); + if ( is_array( $value ) ) { + foreach ( $value as $key => $val ) { + $value[$key] = $this->validateUser( $val, $encParamName ); } - $value[$key] = $title->getText(); - } - - if ( !$multi ) { - $value = $value[0]; + } else { + $value = $this->validateUser( $value, $encParamName ); } break; case 'upload': // nothing to do @@ -1046,7 +1037,7 @@ abstract class ApiBase extends ContextSource { } // Throw out duplicates if requested - if ( is_array( $value ) && !$dupes ) { + if ( !$dupes && is_array( $value ) ) { $value = array_unique( $value ); } @@ -1158,18 +1149,33 @@ abstract class ApiBase extends ContextSource { } /** - * @param $value string - * @param $paramName string - * @return string + * Validate and normalize of parameters of type 'timestamp' + * @param $value string Parameter value + * @param $encParamName string Parameter name + * @return string Validated and normalized parameter */ - function validateTimestamp( $value, $paramName ) { + function validateTimestamp( $value, $encParamName ) { $unixTimestamp = wfTimestamp( TS_UNIX, $value ); if ( $unixTimestamp === false ) { - $this->dieUsage( "Invalid value '$value' for timestamp parameter $paramName", "badtimestamp_{$paramName}" ); + $this->dieUsage( "Invalid value '$value' for timestamp parameter $encParamName", "badtimestamp_{$encParamName}" ); } return wfTimestamp( TS_MW, $unixTimestamp ); } + /** + * Validate and normalize of parameters of type 'user' + * @param $value string Parameter value + * @param $encParamName string Parameter value + * @return string Validated and normalized parameter + */ + private function validateUser( $value, $encParamName ) { + $title = Title::makeTitleSafe( NS_USER, $value ); + if ( $title === null ) { + $this->dieUsage( "Invalid value '$value' for user parameter $encParamName", "baduser_{$encParamName}" ); + } + return $title->getText(); + } + /** * Adds a warning to the output, else dies * -- 2.20.1