r55652 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55651‎ | r55652 | r55653 >
Date:16:37, 28 August 2009
Author:catrope
Status:ok
Tags:
Comment:
API: BREAKING CHANGE: (bug 20426) Instead of throwing an error when a limit is set too high/low, throw a warning and continue with the limit set to the maximum/minimum
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiBase.php
@@ -439,18 +439,18 @@
440440 /**
441441 * Using getAllowedParams(), this function makes an array of the values
442442 * provided by the user, with key being the name of the variable, and
443 - * value - validated value from user or default. limit=max will not be
444 - * parsed if $parseMaxLimit is set to false; use this when the max
 443+ * value - validated value from user or default. limits will not be
 444+ * parsed if $parseLimit is set to false; use this when the max
445445 * limit is not definitive yet, e.g. when getting revisions.
446 - * @param $parseMaxLimit bool
 446+ * @param $parseLimit bool
447447 * @return array
448448 */
449 - public function extractRequestParams($parseMaxLimit = true) {
 449+ public function extractRequestParams($parseLimit = true) {
450450 $params = $this->getFinalParams();
451451 $results = array ();
452452
453453 foreach ($params as $paramName => $paramSettings)
454 - $results[$paramName] = $this->getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit);
 454+ $results[$paramName] = $this->getParameterFromSettings($paramName, $paramSettings, $parseLimit);
455455
456456 return $results;
457457 }
@@ -458,13 +458,13 @@
459459 /**
460460 * Get a value for the given parameter
461461 * @param $paramName string Parameter name
462 - * @param $parseMaxLimit bool see extractRequestParams()
 462+ * @param $parseLimit bool see extractRequestParams()
463463 * @return mixed Parameter value
464464 */
465 - protected function getParameter($paramName, $parseMaxLimit = true) {
 465+ protected function getParameter($paramName, $parseLimit = true) {
466466 $params = $this->getFinalParams();
467467 $paramSettings = $params[$paramName];
468 - return $this->getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit);
 468+ return $this->getParameterFromSettings($paramName, $paramSettings, $parseLimit);
469469 }
470470
471471 /**
@@ -510,10 +510,10 @@
511511 * @param $paramName String: parameter name
512512 * @param $paramSettings Mixed: default value or an array of settings
513513 * using PARAM_* constants.
514 - * @param $parseMaxLimit Boolean: parse limit when max is given?
 514+ * @param $parseLimit Boolean: parse limit?
515515 * @return mixed Parameter value
516516 */
517 - protected function getParameterFromSettings($paramName, $paramSettings, $parseMaxLimit) {
 517+ protected function getParameterFromSettings($paramName, $paramSettings, $parseLimit) {
518518
519519 // Some classes may decide to change parameter names
520520 $encParamName = $this->encodeParamName($paramName);
@@ -572,23 +572,23 @@
573573
574574 if (!is_null($min) || !is_null($max)) {
575575 $values = is_array($value) ? $value : array($value);
576 - foreach ($values as $v) {
 576+ foreach ($values as &$v) {
577577 $this->validateLimit($paramName, $v, $min, $max);
578578 }
579579 }
580580 break;
581581 case 'limit' :
 582+ if ( !$parseLimit )
 583+ // Don't do any validation whatsoever
 584+ break;
582585 if (!isset ($paramSettings[self :: PARAM_MAX]) || !isset ($paramSettings[self :: PARAM_MAX2]))
583586 ApiBase :: dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit $encParamName");
584587 if ($multi)
585588 ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName");
586589 $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0;
587590 if( $value == 'max' ) {
588 - if( $parseMaxLimit ) {
589591 $value = $this->getMain()->canApiHighLimits() ? $paramSettings[self :: PARAM_MAX2] : $paramSettings[self :: PARAM_MAX];
590592 $this->getResult()->addValue( 'limits', $this->getModuleName(), $value );
591 - $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2]);
592 - }
593593 }
594594 else {
595595 $value = intval($value);
@@ -681,9 +681,10 @@
682682 * @param $max int Maximum value for users
683683 * @param $botMax int Maximum value for sysops/bots
684684 */
685 - function validateLimit($paramName, $value, $min, $max, $botMax = null) {
 685+ function validateLimit($paramName, &$value, $min, $max, $botMax = null) {
686686 if (!is_null($min) && $value < $min) {
687 - $this->dieUsage($this->encodeParamName($paramName) . " may not be less than $min (set to $value)", $paramName);
 687+ $this->setWarning($this->encodeParamName($paramName) . " may not be less than $min (set to $value)");
 688+ $value = $min;
688689 }
689690
690691 // Minimum is always validated, whereas maximum is checked only if not running in internal call mode
@@ -695,10 +696,12 @@
696697 if (!is_null($max) && $value > $max) {
697698 if (!is_null($botMax) && $this->getMain()->canApiHighLimits()) {
698699 if ($value > $botMax) {
699 - $this->dieUsage($this->encodeParamName($paramName) . " may not be over $botMax (set to $value) for bots or sysops", $paramName);
 700+ $this->setWarning($this->encodeParamName($paramName) . " may not be over $botMax (set to $value) for bots or sysops");
 701+ $value = $botMax;
700702 }
701703 } else {
702 - $this->dieUsage($this->encodeParamName($paramName) . " may not be over $max (set to $value) for users", $paramName);
 704+ $this->setWarning($this->encodeParamName($paramName) . " may not be over $max (set to $value) for users");
 705+ $value = $max;
703706 }
704707 }
705708 }

Status & tagging log