* value - validated value from user or default. limits will not be
* parsed if $parseLimit is set to false; use this when the max
* limit is not definitive yet, e.g. when getting revisions.
- * @param bool $parseLimit True by default
+ * @param bool|array $options If a boolean, uses that as the value for 'parseLimit'
+ * - parseLimit: (bool, default true) Whether to parse the 'max' value for limit types
+ * - safeMode: (bool, default false) If true, avoid throwing for parameter validation errors.
+ * Returned parameter values might be ApiUsageException instances.
* @return array
*/
- public function extractRequestParams( $parseLimit = true ) {
+ public function extractRequestParams( $options = [] ) {
+ if ( is_bool( $options ) ) {
+ $options = [ 'parseLimit' => $options ];
+ }
+ $options += [
+ 'parseLimit' => true,
+ 'safeMode' => false,
+ ];
+
+ $parseLimit = (bool)$options['parseLimit'];
+
// Cache parameters, for performance and to avoid T26564.
if ( !isset( $this->mParamCache[$parseLimit] ) ) {
$params = $this->getFinalParams() ?: [];
if ( isset( $paramSettings[self::PARAM_TEMPLATE_VARS] ) ) {
$toProcess[] = [ $paramName, $paramSettings[self::PARAM_TEMPLATE_VARS], $paramSettings ];
} else {
- $results[$paramName] = $this->getParameterFromSettings(
- $paramName, $paramSettings, $parseLimit
- );
+ try {
+ $results[$paramName] = $this->getParameterFromSettings(
+ $paramName, $paramSettings, $parseLimit
+ );
+ } catch ( ApiUsageException $ex ) {
+ $results[$paramName] = $ex;
+ }
}
}
$newName = str_replace( $placeholder, $value, $name );
if ( !$targets ) {
- $results[$newName] = $this->getParameterFromSettings( $newName, $settings, $parseLimit );
+ try {
+ $results[$newName] = $this->getParameterFromSettings( $newName, $settings, $parseLimit );
+ } catch ( ApiUsageException $ex ) {
+ $results[$newName] = $ex;
+ }
} else {
$newTargets = [];
foreach ( $targets as $k => $v ) {
$this->mParamCache[$parseLimit] = $results;
}
+ $ret = $this->mParamCache[$parseLimit];
+ if ( !$options['safeMode'] ) {
+ foreach ( $ret as $v ) {
+ if ( $v instanceof ApiUsageException ) {
+ throw $v;
+ }
+ }
+ }
+
return $this->mParamCache[$parseLimit];
}
* @return mixed Parameter value
*/
protected function getParameter( $paramName, $parseLimit = true ) {
- return $this->extractRequestParams( $parseLimit )[$paramName];
+ $ret = $this->extractRequestParams( [
+ 'parseLimit' => $parseLimit,
+ 'safeMode' => true,
+ ] )[$paramName];
+ if ( $ret instanceof ApiUsageException ) {
+ throw $ret;
+ }
+ return $ret;
}
/**
];
}
- $default = isset( $paramSettings[self::PARAM_DFLT] )
- ? $paramSettings[self::PARAM_DFLT]
- : null;
- $multi = isset( $paramSettings[self::PARAM_ISMULTI] )
- ? $paramSettings[self::PARAM_ISMULTI]
- : false;
- $multiLimit1 = isset( $paramSettings[self::PARAM_ISMULTI_LIMIT1] )
- ? $paramSettings[self::PARAM_ISMULTI_LIMIT1]
- : null;
- $multiLimit2 = isset( $paramSettings[self::PARAM_ISMULTI_LIMIT2] )
- ? $paramSettings[self::PARAM_ISMULTI_LIMIT2]
- : null;
- $type = isset( $paramSettings[self::PARAM_TYPE] )
- ? $paramSettings[self::PARAM_TYPE]
- : null;
- $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] )
- ? $paramSettings[self::PARAM_ALLOW_DUPLICATES]
- : false;
- $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] )
- ? $paramSettings[self::PARAM_DEPRECATED]
- : false;
- $deprecatedValues = isset( $paramSettings[self::PARAM_DEPRECATED_VALUES] )
- ? $paramSettings[self::PARAM_DEPRECATED_VALUES]
- : [];
- $required = isset( $paramSettings[self::PARAM_REQUIRED] )
- ? $paramSettings[self::PARAM_REQUIRED]
- : false;
- $allowAll = isset( $paramSettings[self::PARAM_ALL] )
- ? $paramSettings[self::PARAM_ALL]
- : false;
+ $default = $paramSettings[self::PARAM_DFLT] ?? null;
+ $multi = $paramSettings[self::PARAM_ISMULTI] ?? false;
+ $multiLimit1 = $paramSettings[self::PARAM_ISMULTI_LIMIT1] ?? null;
+ $multiLimit2 = $paramSettings[self::PARAM_ISMULTI_LIMIT2] ?? null;
+ $type = $paramSettings[self::PARAM_TYPE] ?? null;
+ $dupes = $paramSettings[self::PARAM_ALLOW_DUPLICATES] ?? false;
+ $deprecated = $paramSettings[self::PARAM_DEPRECATED] ?? false;
+ $deprecatedValues = $paramSettings[self::PARAM_DEPRECATED_VALUES] ?? [];
+ $required = $paramSettings[self::PARAM_REQUIRED] ?? false;
+ $allowAll = $paramSettings[self::PARAM_ALL] ?? false;
// When type is not given, and no choices, the type is the same as $default
if ( !isset( $type ) ) {
}
break;
case 'integer': // Force everything using intval() and optionally validate limits
- $min = isset( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : null;
- $max = isset( $paramSettings[self::PARAM_MAX] ) ? $paramSettings[self::PARAM_MAX] : null;
- $enforceLimits = isset( $paramSettings[self::PARAM_RANGE_ENFORCE] )
- ? $paramSettings[self::PARAM_RANGE_ENFORCE] : false;
+ $min = $paramSettings[self::PARAM_MIN] ?? null;
+ $max = $paramSettings[self::PARAM_MAX] ?? null;
+ $enforceLimits = $paramSettings[self::PARAM_RANGE_ENFORCE] ?? false;
if ( is_array( $value ) ) {
$value = array_map( 'intval', $value );
if ( $multi ) {
self::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" );
}
- $min = isset( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : 0;
+ $min = $paramSettings[self::PARAM_MIN] ?? 0;
if ( $value == 'max' ) {
$value = $this->getMain()->canApiHighLimits()
? $paramSettings[self::PARAM_MAX2]
return $allowedValues;
}
- if ( self::truncateArray( $valuesList, $sizeLimit ) ) {
- $this->addDeprecation(
- [ 'apiwarn-toomanyvalues', $valueName, $sizeLimit ],
- "too-many-$valueName-for-{$this->getModulePath()}"
+ if ( count( $valuesList ) > $sizeLimit ) {
+ $this->dieWithError(
+ [ 'apierror-toomanyvalues', $valueName, $sizeLimit ],
+ "too-many-$valueName"
);
}
return $value;
}
- $titleObj = Title::makeTitleSafe( NS_USER, $value );
-
- if ( $titleObj ) {
- $value = $titleObj->getText();
+ $name = User::getCanonicalName( $value, 'valid' );
+ if ( $name !== false ) {
+ return $name;
}
if (
- !User::isValidUserName( $value ) &&
// We allow ranges as well, for blocks.
- !IP::isIPAddress( $value ) &&
+ IP::isIPAddress( $value ) ||
// See comment for User::isIP. We don't just call that function
// here because it also returns true for things like
// 300.300.300.300 that are neither valid usernames nor valid IP
// addresses.
- !preg_match(
+ preg_match(
'/^' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.xxx$/',
$value
)
) {
- $this->dieWithError(
- [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
- "baduser_{$encParamName}"
- );
+ return IP::sanitizeIP( $value );
}
- return $value;
+ $this->dieWithError(
+ [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
+ "baduser_{$encParamName}"
+ );
}
/**@}*/
WatchAction::doWatchOrUnwatch( $value, $titleObj, $this->getUser() );
}
- /**
- * Truncate an array to a certain length.
- * @param array &$arr Array to truncate
- * @param int $limit Maximum length
- * @return bool True if the array was truncated, false otherwise
- */
- public static function truncateArray( &$arr, $limit ) {
- $modified = false;
- while ( count( $arr ) > $limit ) {
- array_pop( $arr );
- $modified = true;
- }
-
- return $modified;
- }
-
/**
* Gets the user for whom to get the watchlist
*
'api-help-param-token',
$this->needsToken(),
],
- ] + ( isset( $params['token'] ) ? $params['token'] : [] );
+ ] + ( $params['token'] ?? [] );
}
// Avoid PHP 7.1 warning of passing $this by reference
$settings = [];
}
- $d = isset( $desc[$param] ) ? $desc[$param] : '';
+ $d = $desc[$param] ?? '';
if ( is_array( $d ) ) {
// Special handling for prop parameters
$d = array_map( function ( $line ) {
}
$valueMsgs = $settings[self::PARAM_HELP_MSG_PER_VALUE];
- $deprecatedValues = isset( $settings[self::PARAM_DEPRECATED_VALUES] )
- ? $settings[self::PARAM_DEPRECATED_VALUES]
- : [];
+ $deprecatedValues = $settings[self::PARAM_DEPRECATED_VALUES] ?? [];
foreach ( $settings[self::PARAM_TYPE] as $value ) {
if ( isset( $valueMsgs[$value] ) ) {
if ( !$msg instanceof IApiMessage ) {
$key = $msg->getKey();
$params = $msg->getParams();
- array_unshift( $params, isset( self::$messageMap[$key] ) ? self::$messageMap[$key] : $key );
+ array_unshift( $params, self::$messageMap[$key] ?? $key );
$msg = ApiMessage::create( $params );
}
] ];
}
+ /**
+ * Truncate an array to a certain length.
+ * @deprecated since 1.32, no replacement
+ * @param array &$arr Array to truncate
+ * @param int $limit Maximum length
+ * @return bool True if the array was truncated, false otherwise
+ */
+ public static function truncateArray( &$arr, $limit ) {
+ wfDeprecated( __METHOD__, '1.32' );
+ $modified = false;
+ while ( count( $arr ) > $limit ) {
+ array_pop( $arr );
+ $modified = true;
+ }
+
+ return $modified;
+ }
+
/**@}*/
}