r23358 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23357‎ | r23358 | r23359 >
Date:05:44, 25 June 2007
Author:yurik
Status:old
Tags:
Comment:
API: Fixed error codes encoding per bug 10308.
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryRevisions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQueryRevisions.php
@@ -128,7 +128,7 @@
129129 // must manually initialize unset limit
130130 if (is_null($limit))
131131 $limit = 10;
132 - $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
 132+ $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
133133
134134 // There is only one ID, use it
135135 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
Index: trunk/phase3/includes/api/ApiBase.php
@@ -336,7 +336,7 @@
337337 protected function getParameterFromSettings($paramName, $paramSettings) {
338338
339339 // Some classes may decide to change parameter names
340 - $paramName = $this->encodeParamName($paramName);
 340+ $encParamName = $this->encodeParamName($paramName);
341341
342342 if (!is_array($paramSettings)) {
343343 $default = $paramSettings;
@@ -359,19 +359,19 @@
360360 if ($type == 'boolean') {
361361 if (isset ($default) && $default !== false) {
362362 // Having a default value of anything other than 'false' is pointless
363 - ApiBase :: dieDebug(__METHOD__, "Boolean param $paramName's default is set to '$default'");
 363+ ApiBase :: dieDebug(__METHOD__, "Boolean param $encParamName's default is set to '$default'");
364364 }
365365
366 - $value = $this->getMain()->getRequest()->getCheck($paramName);
 366+ $value = $this->getMain()->getRequest()->getCheck($encParamName);
367367 } else {
368 - $value = $this->getMain()->getRequest()->getVal($paramName, $default);
 368+ $value = $this->getMain()->getRequest()->getVal($encParamName, $default);
369369
370370 if (isset ($value) && $type == 'namespace')
371371 $type = ApiBase :: getValidNamespaces();
372372 }
373373
374374 if (isset ($value) && ($multi || is_array($type)))
375 - $value = $this->parseMultiValue($paramName, $value, $multi, is_array($type) ? $type : null);
 375+ $value = $this->parseMultiValue($encParamName, $value, $multi, is_array($type) ? $type : null);
376376
377377 // More validation only when choices were not given
378378 // choices were validated in parseMultiValue()
@@ -385,51 +385,45 @@
386386 case 'integer' : // Force everything using intval() and optionally validate limits
387387
388388 $value = is_array($value) ? array_map('intval', $value) : intval($value);
389 - $checkMin = isset ($paramSettings[self :: PARAM_MIN]);
390 - $checkMax = isset ($paramSettings[self :: PARAM_MAX]);
 389+ $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : null;
 390+ $max = isset ($paramSettings[self :: PARAM_MAX]) ? $paramSettings[self :: PARAM_MAX] : null;
391391
392 - if ($checkMin || $checkMax) {
393 - $min = $checkMin ? $paramSettings[self :: PARAM_MIN] : false;
394 - $max = $checkMax ? $paramSettings[self :: PARAM_MAX] : false;
395 -
 392+ if (!is_null($min) || !is_null($max)) {
396393 $values = is_array($value) ? $value : array($value);
397394 foreach ($values as $v) {
398 - if ($checkMin && $v < $min)
399 - $this->dieUsage("$paramName may not be less than $min (set to $v)", $paramName);
400 - if ($checkMax && $v > $max)
401 - $this->dieUsage("$paramName may not be over $max (set to $v)", $paramName);
 395+ $this->validateLimit($paramName, $v, $min, $max);
402396 }
403397 }
404398 break;
405399 case 'limit' :
406400 if (!isset ($paramSettings[self :: PARAM_MAX]) || !isset ($paramSettings[self :: PARAM_MAX2]))
407 - ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $paramName");
 401+ ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $encParamName");
408402 if ($multi)
409 - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName");
 403+ ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
410404 $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0;
411405 $value = intval($value);
412406 $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2]);
413407 break;
414408 case 'boolean' :
415409 if ($multi)
416 - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName");
 410+ ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
417411 break;
418412 case 'timestamp' :
419413 if ($multi)
420 - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName");
 414+ ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
421415 $value = wfTimestamp(TS_UNIX, $value);
422416 if ($value === 0)
423 - $this->dieUsage("Invalid value '$value' for timestamp parameter $paramName", "badtimestamp_{$paramName}");
 417+ $this->dieUsage("Invalid value '$value' for timestamp parameter $encParamName", "badtimestamp_{$encParamName}");
424418 $value = wfTimestamp(TS_MW, $value);
425419 break;
426420 case 'user' :
427421 $title = Title::makeTitleSafe( NS_USER, $value );
428422 if ( is_null( $title ) )
429 - $this->dieUsage("Invalid value $user for user parameter $paramName", "baduser_{$paramName}");
 423+ $this->dieUsage("Invalid value $user for user parameter $encParamName", "baduser_{$encParamName}");
430424 $value = $title->getText();
431425 break;
432426 default :
433 - ApiBase :: dieDebug(__METHOD__, "Param $paramName's type is unknown - $type");
 427+ ApiBase :: dieDebug(__METHOD__, "Param $encParamName's type is unknown - $type");
434428 }
435429 }
436430
@@ -470,19 +464,22 @@
471465 /**
472466 * Validate the value against the minimum and user/bot maximum limits. Prints usage info on failure.
473467 */
474 - function validateLimit($varname, $value, $min, $max, $botMax) {
475 - if ($value < $min) {
476 - $this->dieUsage("$varname may not be less than $min (set to $value)", $varname);
 468+ function validateLimit($paramName, $value, $min, $max, $botMax = null) {
 469+ if (!is_null($min) && $value < $min) {
 470+ $this->dieUsage($this->encodeParamName($paramName) . " may not be less than $min (set to $value)", $paramName);
477471 }
478472
479 - if ($this->getMain()->isBot() || $this->getMain()->isSysop()) {
480 - if ($value > $botMax) {
481 - $this->dieUsage("$varname may not be over $botMax (set to $value) for bots or sysops", $varname);
 473+ // Optimization: do not check user's bot status unless really needed -- skips db query
 474+ // assumes $botMax >= $max
 475+ if (!is_null($max) && $value > $max) {
 476+ if (!is_null($botMax) && ($this->getMain()->isBot() || $this->getMain()->isSysop())) {
 477+ if ($value > $botMax) {
 478+ $this->dieUsage($this->encodeParamName($paramName) . " may not be over $botMax (set to $value) for bots or sysops", $paramName);
 479+ }
 480+ } else {
 481+ $this->dieUsage($this->encodeParamName($paramName) . " may not be over $max (set to $value) for users", $paramName);
482482 }
483483 }
484 - elseif ($value > $max) {
485 - $this->dieUsage("$varname may not be over $max (set to $value) for users", $varname);
486 - }
487484 }
488485
489486 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r23407Merged revisions 23203-23405 via svnmerge from...david23:00, 25 June 2007

Status & tagging log