Example #1
0
 /**
  * Using the settings determine the value for the given parameter
  *
  * @param $paramName String: parameter name
  * @param $paramSettings Mixed: default value or an array of settings
  *  using PARAM_* constants.
  * @param $parseLimit Boolean: parse limit?
  * @return mixed Parameter value
  */
 protected function getParameterFromSettings($paramName, $paramSettings, $parseLimit)
 {
     // Some classes may decide to change parameter names
     $encParamName = $this->encodeParamName($paramName);
     if (!is_array($paramSettings)) {
         $default = $paramSettings;
         $multi = false;
         $type = gettype($paramSettings);
         $dupes = false;
         $deprecated = 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;
         $dupes = isset($paramSettings[self::PARAM_ALLOW_DUPLICATES]) ? $paramSettings[self::PARAM_ALLOW_DUPLICATES] : false;
         $deprecated = isset($paramSettings[self::PARAM_DEPRECATED]) ? $paramSettings[self::PARAM_DEPRECATED] : 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') {
         if (isset($default) && $default !== false) {
             // Having a default value of anything other than 'false' is pointless
             ApiBase::dieDebug(__METHOD__, "Boolean param {$encParamName}'s default is set to '{$default}'");
         }
         $value = $this->getMain()->getRequest()->getCheck($encParamName);
     } else {
         $value = $this->getMain()->getRequest()->getVal($encParamName, $default);
         if (isset($value) && $type == 'namespace') {
             $type = ApiBase::getValidNamespaces();
         }
     }
     if (isset($value) && ($multi || is_array($type))) {
         $value = $this->parseMultiValue($encParamName, $value, $multi, is_array($type) ? $type : null);
     }
     // More validation only when choices were not given
     // choices were validated in parseMultiValue()
     if (isset($value)) {
         if (!is_array($type)) {
             switch ($type) {
                 case 'NULL':
                     // nothing to do
                     break;
                 case 'string':
                     // nothing to do
                     break;
                 case 'integer':
                     // Force everything using intval() and optionally validate limits
                     $value = is_array($value) ? array_map('intval', $value) : intval($value);
                     $min = isset($paramSettings[self::PARAM_MIN]) ? $paramSettings[self::PARAM_MIN] : null;
                     $max = isset($paramSettings[self::PARAM_MAX]) ? $paramSettings[self::PARAM_MAX] : null;
                     if (!is_null($min) || !is_null($max)) {
                         $values = is_array($value) ? $value : array($value);
                         foreach ($values as &$v) {
                             $this->validateLimit($paramName, $v, $min, $max);
                         }
                     }
                     break;
                 case 'limit':
                     if (!$parseLimit) {
                         // Don't do any validation whatsoever
                         break;
                     }
                     if (!isset($paramSettings[self::PARAM_MAX]) || !isset($paramSettings[self::PARAM_MAX2])) {
                         ApiBase::dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit {$encParamName}");
                     }
                     if ($multi) {
                         ApiBase::dieDebug(__METHOD__, "Multi-values not supported for {$encParamName}");
                     }
                     $min = isset($paramSettings[self::PARAM_MIN]) ? $paramSettings[self::PARAM_MIN] : 0;
                     if ($value == 'max') {
                         $value = $this->getMain()->canApiHighLimits() ? $paramSettings[self::PARAM_MAX2] : $paramSettings[self::PARAM_MAX];
                         $this->getResult()->addValue('limits', $this->getModuleName(), $value);
                     } else {
                         $value = intval($value);
                         $this->validateLimit($paramName, $value, $min, $paramSettings[self::PARAM_MAX], $paramSettings[self::PARAM_MAX2]);
                     }
                     break;
                 case 'boolean':
                     if ($multi) {
                         ApiBase::dieDebug(__METHOD__, "Multi-values not supported for {$encParamName}");
                     }
                     break;
                 case 'timestamp':
                     if ($multi) {
                         ApiBase::dieDebug(__METHOD__, "Multi-values not supported for {$encParamName}");
                     }
                     $value = wfTimestamp(TS_UNIX, $value);
                     if ($value === 0) {
                         $this->dieUsage("Invalid value '{$value}' for timestamp parameter {$encParamName}", "badtimestamp_{$encParamName}");
                     }
                     $value = wfTimestamp(TS_MW, $value);
                     break;
                 case 'user':
                     $title = Title::makeTitleSafe(NS_USER, $value);
                     if (is_null($title)) {
                         $this->dieUsage("Invalid value for user parameter {$encParamName}", "baduser_{$encParamName}");
                     }
                     $value = $title->getText();
                     break;
                 default:
                     ApiBase::dieDebug(__METHOD__, "Param {$encParamName}'s type is unknown - {$type}");
             }
         }
         // Throw out duplicates if requested
         if (is_array($value) && !$dupes) {
             $value = array_unique($value);
         }
         // Set a warning if a deprecated parameter has been passed
         if ($deprecated && $value !== false) {
             $this->setWarning("The {$encParamName} parameter has been deprecated.");
         }
     }
     return $value;
 }
Example #2
0
 /**
  * Using the settings determine the value for the given parameter
  * @param $paramName String: parameter name
  * @param $paramSettings Mixed: default value or an array of settings using PARAM_* constants.
  */
 protected function getParameterFromSettings($paramName, $paramSettings)
 {
     // Some classes may decide to change parameter names
     $paramName = $this->encodeParamName($paramName);
     if (!is_array($paramSettings)) {
         $default = $paramSettings;
         $multi = false;
         $type = gettype($paramSettings);
     } 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;
         // 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') {
         if (isset($default) && $default !== false) {
             // Having a default value of anything other than 'false' is pointless
             ApiBase::dieDebug(__METHOD__, "Boolean param {$paramName}'s default is set to '{$default}'");
         }
         $value = $this->getMain()->getRequest()->getCheck($paramName);
     } else {
         $value = $this->getMain()->getRequest()->getVal($paramName, $default);
         if (isset($value) && $type == 'namespace') {
             $type = ApiBase::getValidNamespaces();
         }
     }
     if (isset($value) && ($multi || is_array($type))) {
         $value = $this->parseMultiValue($paramName, $value, $multi, is_array($type) ? $type : null);
     }
     // More validation only when choices were not given
     // choices were validated in parseMultiValue()
     if (isset($value)) {
         if (!is_array($type)) {
             switch ($type) {
                 case 'NULL':
                     // nothing to do
                     break;
                 case 'string':
                     // nothing to do
                     break;
                 case 'integer':
                     // Force everything using intval()
                     $value = is_array($value) ? array_map('intval', $value) : intval($value);
                     break;
                 case 'limit':
                     if (!isset($paramSettings[self::PARAM_MAX1]) || !isset($paramSettings[self::PARAM_MAX2])) {
                         ApiBase::dieDebug(__METHOD__, "MAX1 or MAX2 are not defined for the limit {$paramName}");
                     }
                     if ($multi) {
                         ApiBase::dieDebug(__METHOD__, "Multi-values not supported for {$paramName}");
                     }
                     $min = isset($paramSettings[self::PARAM_MIN]) ? $paramSettings[self::PARAM_MIN] : 0;
                     $value = intval($value);
                     $this->validateLimit($paramName, $value, $min, $paramSettings[self::PARAM_MAX1], $paramSettings[self::PARAM_MAX2]);
                     break;
                 case 'boolean':
                     if ($multi) {
                         ApiBase::dieDebug(__METHOD__, "Multi-values not supported for {$paramName}");
                     }
                     break;
                 case 'timestamp':
                     if ($multi) {
                         ApiBase::dieDebug(__METHOD__, "Multi-values not supported for {$paramName}");
                     }
                     $value = wfTimestamp(TS_UNIX, $value);
                     if ($value === 0) {
                         $this->dieUsage("Invalid value '{$value}' for timestamp parameter {$paramName}", "badtimestamp_{$paramName}");
                     }
                     $value = wfTimestamp(TS_MW, $value);
                     break;
                 default:
                     ApiBase::dieDebug(__METHOD__, "Param {$paramName}'s type is unknown - {$type}");
             }
         }
         // There should never be any duplicate values in a list
         if (is_array($value)) {
             $value = array_unique($value);
         }
     }
     return $value;
 }