Umherirrender has uploaded a new change for review.
https://gerrit.wikimedia.org/r/53795
Change subject: Api: Refactor handling of missing PARAM_TYPE
......................................................................
Api: Refactor handling of missing PARAM_TYPE
In makeHelpMsgParameters a missing PARAM_TYPE or a null default is
treated as 'string', in getParameterFromSettings it is 'NULL'.
This makes problems in getParameterFromSettings, when a PARAM_REQUIRED
has no PARAM_TYPE. Than it is a 'NULL', which means no validation. But
no default or a default of null means 'string' and than the empty string
check is missing for required string params.
Adding a new method to handle this at one place.
Change-Id: I562eb5af1b2acb6cd2ac83d77bf9e56445cff992
---
M includes/api/ApiBase.php
1 file changed, 88 insertions(+), 92 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/95/53795/1
diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php
index ee6f2c0..49948f8 100644
--- a/includes/api/ApiBase.php
+++ b/includes/api/ApiBase.php
@@ -391,18 +391,6 @@
);
}
- //handle missing type
- if ( !isset(
$paramSettings[ApiBase::PARAM_TYPE] ) ) {
- $dflt = isset(
$paramSettings[ApiBase::PARAM_DFLT] ) ? $paramSettings[ApiBase::PARAM_DFLT] :
null;
- if ( is_bool( $dflt ) ) {
-
$paramSettings[ApiBase::PARAM_TYPE] = 'boolean';
- } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
-
$paramSettings[ApiBase::PARAM_TYPE] = 'string';
- } elseif ( is_int( $dflt ) ) {
-
$paramSettings[ApiBase::PARAM_TYPE] = 'integer';
- }
- }
-
if ( isset(
$paramSettings[self::PARAM_DEPRECATED] ) &&
$paramSettings[self::PARAM_DEPRECATED] ) {
$desc = "DEPRECATED! $desc";
}
@@ -411,79 +399,76 @@
$desc .= $paramPrefix . "This parameter
is required";
}
- $type = isset( $paramSettings[self::PARAM_TYPE]
) ? $paramSettings[self::PARAM_TYPE] : null;
- if ( isset( $type ) ) {
- $hintPipeSeparated = true;
- $multi = isset(
$paramSettings[self::PARAM_ISMULTI] ) ? $paramSettings[self::PARAM_ISMULTI] :
false;
- if ( $multi ) {
- $prompt = 'Values (separate
with \'|\'): ';
- } else {
- $prompt = 'One value: ';
- }
+ $type = $this->getParamTypeFromSettings(
$paramSettings );
+ $hintPipeSeparated = true;
+ $multi = isset(
$paramSettings[self::PARAM_ISMULTI] ) ? $paramSettings[self::PARAM_ISMULTI] :
false;
+ if ( $multi ) {
+ $prompt = 'Values (separate with
\'|\'): ';
+ } else {
+ $prompt = 'One value: ';
+ }
- if ( is_array( $type ) ) {
- $choices = array();
- $nothingPrompt = '';
- foreach ( $type as $t ) {
- if ( $t === '' ) {
- $nothingPrompt
= 'Can be empty, or ';
- } else {
- $choices[] = $t;
+ if ( is_array( $type ) ) {
+ $choices = array();
+ $nothingPrompt = '';
+ foreach ( $type as $t ) {
+ if ( $t === '' ) {
+ $nothingPrompt = 'Can
be empty, or ';
+ } else {
+ $choices[] = $t;
+ }
+ }
+ $desc .= $paramPrefix . $nothingPrompt
. $prompt;
+ $choicesstring = implode( ', ',
$choices );
+ $desc .= wordwrap( $choicesstring, 100,
$descWordwrap );
+ $hintPipeSeparated = false;
+ } else {
+ switch ( $type ) {
+ case 'namespace':
+ // Special handling
because namespaces are type-limited, yet they are not given
+ $desc .= $paramPrefix .
$prompt;
+ $desc .= wordwrap(
implode( ', ', MWNamespace::getValidNamespaces() ),
+ 100,
$descWordwrap );
+ $hintPipeSeparated =
false;
+ break;
+ case 'limit':
+ $desc .= $paramPrefix .
"No more than {$paramSettings[self :: PARAM_MAX]}";
+ if ( isset(
$paramSettings[self::PARAM_MAX2] ) ) {
+ $desc .= "
({$paramSettings[self::PARAM_MAX2]} for bots)";
}
- }
- $desc .= $paramPrefix .
$nothingPrompt . $prompt;
- $choicesstring = implode( ', ',
$choices );
- $desc .= wordwrap(
$choicesstring, 100, $descWordwrap );
- $hintPipeSeparated = false;
- } else {
- switch ( $type ) {
- case 'namespace':
- // Special
handling because namespaces are type-limited, yet they are not given
- $desc .=
$paramPrefix . $prompt;
- $desc .=
wordwrap( implode( ', ', MWNamespace::getValidNamespaces() ),
- 100,
$descWordwrap );
-
$hintPipeSeparated = false;
- break;
- case 'limit':
- $desc .=
$paramPrefix . "No more than {$paramSettings[self :: PARAM_MAX]}";
- if ( isset(
$paramSettings[self::PARAM_MAX2] ) ) {
- $desc
.= " ({$paramSettings[self::PARAM_MAX2]} for bots)";
+ $desc .= ' allowed';
+ break;
+ case 'integer':
+ $s = $multi ? 's' : '';
+ $hasMin = isset(
$paramSettings[self::PARAM_MIN] );
+ $hasMax = isset(
$paramSettings[self::PARAM_MAX] );
+ if ( $hasMin || $hasMax
) {
+ if ( !$hasMax )
{
+
$intRangeStr = "The value$s must be no less than
{$paramSettings[self::PARAM_MIN]}";
+ } elseif (
!$hasMin ) {
+
$intRangeStr = "The value$s must be no more than
{$paramSettings[self::PARAM_MAX]}";
+ } else {
+
$intRangeStr = "The value$s must be between {$paramSettings[self::PARAM_MIN]}
and {$paramSettings[self::PARAM_MAX]}";
}
- $desc .= '
allowed';
- break;
- case 'integer':
- $s = $multi ?
's' : '';
- $hasMin =
isset( $paramSettings[self::PARAM_MIN] );
- $hasMax =
isset( $paramSettings[self::PARAM_MAX] );
- if ( $hasMin ||
$hasMax ) {
- if (
!$hasMax ) {
-
$intRangeStr = "The value$s must be no less than
{$paramSettings[self::PARAM_MIN]}";
- }
elseif ( !$hasMin ) {
-
$intRangeStr = "The value$s must be no more than
{$paramSettings[self::PARAM_MAX]}";
- } else {
-
$intRangeStr = "The value$s must be between {$paramSettings[self::PARAM_MIN]}
and {$paramSettings[self::PARAM_MAX]}";
- }
+ $desc .=
$paramPrefix . $intRangeStr;
+ }
+ break;
+ case 'upload':
+ $desc .= $paramPrefix .
"Must be posted as a file upload using multipart/form-data";
+ break;
+ }
+ }
- $desc
.= $paramPrefix . $intRangeStr;
- }
- break;
- case 'upload':
- $desc .=
$paramPrefix . "Must be posted as a file upload using multipart/form-data";
- break;
- }
+ if ( $multi ) {
+ if ( $hintPipeSeparated ) {
+ $desc .= $paramPrefix .
"Separate values with '|'";
}
- if ( $multi ) {
- if ( $hintPipeSeparated ) {
- $desc .= $paramPrefix .
"Separate values with '|'";
- }
-
- $isArray = is_array( $type );
- if ( !$isArray
- || $isArray &&
count( $type ) > self::LIMIT_SML1 ) {
- $desc .= $paramPrefix .
"Maximum number of values " .
-
self::LIMIT_SML1 . " (" . self::LIMIT_SML2 . " for bots)";
- }
+ $isArray = is_array( $type );
+ if ( !$isArray
+ || $isArray && count(
$type ) > self::LIMIT_SML1 ) {
+ $desc .= $paramPrefix .
"Maximum number of values " .
+
self::LIMIT_SML1 . " (" . self::LIMIT_SML2 . " for bots)";
}
}
@@ -890,26 +875,17 @@
if ( !is_array( $paramSettings ) ) {
$default = $paramSettings;
$multi = false;
- $type = gettype( $paramSettings );
+ $type = $this->getParamTypeFromSettings( array(
self::PARAM_DFLT => $paramSettings ) );
$dupes = false;
$deprecated = false;
$required = false;
} else {
$default = isset( $paramSettings[self::PARAM_DFLT] ) ?
$paramSettings[self::PARAM_DFLT] : null;
$multi = isset( $paramSettings[self::PARAM_ISMULTI] ) ?
$paramSettings[self::PARAM_ISMULTI] : false;
- $type = isset( $paramSettings[self::PARAM_TYPE] ) ?
$paramSettings[self::PARAM_TYPE] : null;
+ $type = $this->getParamTypeFromSettings( $paramSettings
);
$dupes = isset(
$paramSettings[self::PARAM_ALLOW_DUPLICATES] ) ?
$paramSettings[self::PARAM_ALLOW_DUPLICATES] : false;
$deprecated = isset(
$paramSettings[self::PARAM_DEPRECATED] ) ?
$paramSettings[self::PARAM_DEPRECATED] : false;
$required = isset( $paramSettings[self::PARAM_REQUIRED]
) ? $paramSettings[self::PARAM_REQUIRED] : false;
-
- // When type is not given, and no choices, the type is
the same as $default
- if ( !isset( $type ) ) {
- if ( isset( $default ) ) {
- $type = gettype( $default );
- } else {
- $type = 'NULL'; // allow everything
- }
- }
}
if ( $type == 'boolean' ) {
@@ -959,8 +935,6 @@
if ( isset( $value ) ) {
if ( !is_array( $type ) ) {
switch ( $type ) {
- case 'NULL': // nothing to do
- break;
case 'string':
if ( $required && $value === ''
) {
$this->dieUsageMsg(
array( 'missingparam', $paramName ) );
@@ -1053,6 +1027,28 @@
}
/**
+ * get the internal param type from a paramSettings array
+ * This method handles the shortcut, when no PARAM_TYPE is set
+ * @return string type
+ */
+ private function getParamTypeFromSettings( $paramSettings ) {
+ if ( isset( $paramSettings[self::PARAM_TYPE] ) ) {
+ return $paramSettings[self::PARAM_TYPE];
+ }
+
+ //handle missing type
+ $dflt = isset( $paramSettings[ApiBase::PARAM_DFLT] ) ?
$paramSettings[ApiBase::PARAM_DFLT] : null;
+ if ( is_bool( $dflt ) ) {
+ return 'boolean';
+ }
+ if ( is_int( $dflt ) ) {
+ return 'integer';
+ }
+ // treat all the rest as string
+ return 'string';
+ }
+
+ /**
* Return an array of values that were given in a 'a|b|c' notation,
* after it optionally validates them against the list allowed values.
*
--
To view, visit https://gerrit.wikimedia.org/r/53795
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I562eb5af1b2acb6cd2ac83d77bf9e56445cff992
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits