Index: trunk/phase3/includes/api/ApiQueryRevisions.php |
— | — | @@ -128,7 +128,7 @@ |
129 | 129 | // must manually initialize unset limit |
130 | 130 | if (is_null($limit)) |
131 | 131 | $limit = 10; |
132 | | - $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax); |
| 132 | + $this->validateLimit('limit', $limit, 1, $userMax, $botMax); |
133 | 133 | |
134 | 134 | // There is only one ID, use it |
135 | 135 | $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles()))); |
Index: trunk/phase3/includes/api/ApiBase.php |
— | — | @@ -336,7 +336,7 @@ |
337 | 337 | protected function getParameterFromSettings($paramName, $paramSettings) { |
338 | 338 | |
339 | 339 | // Some classes may decide to change parameter names |
340 | | - $paramName = $this->encodeParamName($paramName); |
| 340 | + $encParamName = $this->encodeParamName($paramName); |
341 | 341 | |
342 | 342 | if (!is_array($paramSettings)) { |
343 | 343 | $default = $paramSettings; |
— | — | @@ -359,19 +359,19 @@ |
360 | 360 | if ($type == 'boolean') { |
361 | 361 | if (isset ($default) && $default !== false) { |
362 | 362 | // 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'"); |
364 | 364 | } |
365 | 365 | |
366 | | - $value = $this->getMain()->getRequest()->getCheck($paramName); |
| 366 | + $value = $this->getMain()->getRequest()->getCheck($encParamName); |
367 | 367 | } else { |
368 | | - $value = $this->getMain()->getRequest()->getVal($paramName, $default); |
| 368 | + $value = $this->getMain()->getRequest()->getVal($encParamName, $default); |
369 | 369 | |
370 | 370 | if (isset ($value) && $type == 'namespace') |
371 | 371 | $type = ApiBase :: getValidNamespaces(); |
372 | 372 | } |
373 | 373 | |
374 | 374 | 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); |
376 | 376 | |
377 | 377 | // More validation only when choices were not given |
378 | 378 | // choices were validated in parseMultiValue() |
— | — | @@ -385,51 +385,45 @@ |
386 | 386 | case 'integer' : // Force everything using intval() and optionally validate limits |
387 | 387 | |
388 | 388 | $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; |
391 | 391 | |
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)) { |
396 | 393 | $values = is_array($value) ? $value : array($value); |
397 | 394 | 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); |
402 | 396 | } |
403 | 397 | } |
404 | 398 | break; |
405 | 399 | case 'limit' : |
406 | 400 | 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"); |
408 | 402 | if ($multi) |
409 | | - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); |
| 403 | + ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName"); |
410 | 404 | $min = isset ($paramSettings[self :: PARAM_MIN]) ? $paramSettings[self :: PARAM_MIN] : 0; |
411 | 405 | $value = intval($value); |
412 | 406 | $this->validateLimit($paramName, $value, $min, $paramSettings[self :: PARAM_MAX], $paramSettings[self :: PARAM_MAX2]); |
413 | 407 | break; |
414 | 408 | case 'boolean' : |
415 | 409 | if ($multi) |
416 | | - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); |
| 410 | + ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName"); |
417 | 411 | break; |
418 | 412 | case 'timestamp' : |
419 | 413 | if ($multi) |
420 | | - ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $paramName"); |
| 414 | + ApiBase :: dieDebug(__METHOD__, "Multi-values not supported for $encParamName"); |
421 | 415 | $value = wfTimestamp(TS_UNIX, $value); |
422 | 416 | 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}"); |
424 | 418 | $value = wfTimestamp(TS_MW, $value); |
425 | 419 | break; |
426 | 420 | case 'user' : |
427 | 421 | $title = Title::makeTitleSafe( NS_USER, $value ); |
428 | 422 | 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}"); |
430 | 424 | $value = $title->getText(); |
431 | 425 | break; |
432 | 426 | default : |
433 | | - ApiBase :: dieDebug(__METHOD__, "Param $paramName's type is unknown - $type"); |
| 427 | + ApiBase :: dieDebug(__METHOD__, "Param $encParamName's type is unknown - $type"); |
434 | 428 | } |
435 | 429 | } |
436 | 430 | |
— | — | @@ -470,19 +464,22 @@ |
471 | 465 | /** |
472 | 466 | * Validate the value against the minimum and user/bot maximum limits. Prints usage info on failure. |
473 | 467 | */ |
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); |
477 | 471 | } |
478 | 472 | |
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); |
482 | 482 | } |
483 | 483 | } |
484 | | - elseif ($value > $max) { |
485 | | - $this->dieUsage("$varname may not be over $max (set to $value) for users", $varname); |
486 | | - } |
487 | 484 | } |
488 | 485 | |
489 | 486 | /** |