r74232 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74231‎ | r74232 | r74233 >
Date:20:07, 3 October 2010
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
Followup r74230, if we make things array, we should make them not an array afterwards

Well, the more sane way, is just treat them seperately, than having to do $value = $value[0];, based on whether it was an array already

Cheers to Siebrand (was already poking it myself :))
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiBase.php
@@ -52,6 +52,7 @@
5353 const PARAM_ALLOW_DUPLICATES = 6; // Boolean, do we allow the same value to be set more than once when ISMULTI=true
5454 const PARAM_DEPRECATED = 7; // Boolean, is the parameter deprecated (will show a warning)
5555 const PARAM_REQUIRED = 8; // Boolean, is the parameter required?
 56+ const PARAM_RANGE_ENFORCE = 9; // Boolean, if MIN/MAX are set, enforce (die) these? Only applies if TYPE='integer' Use with extreme caution
5657
5758 const LIMIT_BIG1 = 500; // Fast query, std user limit
5859 const LIMIT_BIG2 = 5000; // Fast query, bot/sysop limit
@@ -676,16 +677,21 @@
677678
678679 break;
679680 case 'integer': // Force everything using intval() and optionally validate limits
 681+ $value = is_array( $value ) ? array_map( 'intval', $value ) : intval( $value );
680682
681 - $value = is_array( $value ) ? array_map( 'intval', $value ) : intval( $value );
682683 $min = isset ( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : null;
683684 $max = isset ( $paramSettings[self::PARAM_MAX] ) ? $paramSettings[self::PARAM_MAX] : null;
 685+ $enforceLimits = isset ( $paramSettings[self::PARAM_RANGE_ENFORCE] )
 686+ ? $paramSettings[self::PARAM_RANGE_ENFORCE] : false;
684687
685688 if ( !is_null( $min ) || !is_null( $max ) ) {
686 - $value = is_array( $value ) ? $value : array( $value );
687 - foreach ( $value as &$v ) {
688 - $this->validateLimit( $paramName, $v, $min, $max );
689 - }
 689+ if ( is_array( $value ) ) {
 690+ foreach ( $value as &$v ) {
 691+ $this->validateLimit( $paramName, $v, $min, $max, $enforceLimits );
 692+ }
 693+ } else {
 694+ $this->validateLimit( $paramName, $value, $min, $max, $enforceLimits );
 695+ }
690696 }
691697 break;
692698 case 'limit':
@@ -820,10 +826,13 @@
821827 * @param $min int Minimum value
822828 * @param $max int Maximum value for users
823829 * @param $botMax int Maximum value for sysops/bots
 830+ * @param $enforceLimits Boolean Whether to enforce (die) if value is outside limits
824831 */
825 - function validateLimit( $paramName, &$value, $min, $max, $botMax = null ) {
 832+ function validateLimit( $paramName, &$value, $min, $max, $botMax = null, $enforceLimits = false ) {
826833 if ( !is_null( $min ) && $value < $min ) {
827 - $this->setWarning( $this->encodeParamName( $paramName ) . " may not be less than $min (set to $value)" );
 834+
 835+ $msg = $this->encodeParamName( $paramName ) . " may not be less than $min (set to $value)";
 836+ $this->warnOrDie( $msg, $enforceLimits );
828837 $value = $min;
829838 }
830839
@@ -837,17 +846,33 @@
838847 if ( !is_null( $max ) && $value > $max ) {
839848 if ( !is_null( $botMax ) && $this->getMain()->canApiHighLimits() ) {
840849 if ( $value > $botMax ) {
841 - $this->setWarning( $this->encodeParamName( $paramName ) . " may not be over $botMax (set to $value) for bots or sysops" );
 850+ $msg = $this->encodeParamName( $paramName ) . " may not be over $botMax (set to $value) for bots or sysops";
 851+ $this->warnOrDie( $msg, $enforceLimits );
842852 $value = $botMax;
843853 }
844854 } else {
845 - $this->setWarning( $this->encodeParamName( $paramName ) . " may not be over $max (set to $value) for users" );
 855+ $msg = $this->encodeParamName( $paramName ) . " may not be over $max (set to $value) for users";
 856+ $this->warnOrDie( $msg, $enforceLimits );
846857 $value = $max;
847858 }
848859 }
849860 }
850861
851862 /**
 863+ * Adds a warning to the output, else dies
 864+ *
 865+ * @param $msg String Message to show as a warning, or error message if dying
 866+ * @param $enforceLimits Boolean Whether this is an enforce (die)
 867+ */
 868+ private function warnOrDie( $msg, $enforceLimits = false ) {
 869+ if ( $enforceLimits ) {
 870+ $this->dieUsageMsg( $msg );
 871+ } else {
 872+ $this->setWarning( $msg );
 873+ }
 874+ }
 875+
 876+ /**
852877 * Truncate an array to a certain length.
853878 * @param $arr array Array to truncate
854879 * @param $limit int Maximum length

Follow-up revisions

RevisionCommit summaryAuthorDate
r74234Minor tweaks to r74232, add a value for $botMax on calls for integer validati...reedy20:29, 3 October 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r74230First part of "*(bug 25303) API: integer parameter validation does not actual...reedy19:07, 3 October 2010

Comments

#Comment by Reedy (talk | contribs)   20:08, 3 October 2010

Note, this contains unrelated changes (well, to what was being fixed).

Going to leave in, as they're staying, and are part of the same bug

#Comment by Brion VIBBER (talk | contribs)   20:53, 3 October 2010

Bad calls to validateLimit fixed in r74234

#Comment by Catrope (talk | contribs)   09:08, 4 October 2010
+			$this->dieUsageMsg( $msg );

Should be dieUsage(). dieUsageMsg() will just thrown an unknown error here.

#Comment by Catrope (talk | contribs)   09:08, 4 October 2010

Fixed in r74234 it seems.

#Comment by Reedy (talk | contribs)   09:42, 4 October 2010

Yup, per my initial comment, it wasn't supposed to be commited as such, as it wasn't tested (hence changes in r74234)

Status & tagging log