2019-08-21 19:53:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Api\Validator;
|
|
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
|
use ApiMain;
|
|
|
|
|
use ApiMessage;
|
|
|
|
|
use ApiUsageException;
|
|
|
|
|
use MediaWiki\Message\Converter as MessageConverter;
|
|
|
|
|
use MediaWiki\ParamValidator\TypeDef\NamespaceDef;
|
|
|
|
|
use MediaWiki\ParamValidator\TypeDef\TagsDef;
|
2020-11-03 03:12:00 +00:00
|
|
|
use MediaWiki\ParamValidator\TypeDef\TitleDef;
|
2019-08-21 19:53:53 +00:00
|
|
|
use MediaWiki\ParamValidator\TypeDef\UserDef;
|
|
|
|
|
use Message;
|
|
|
|
|
use Wikimedia\Message\DataMessageValue;
|
|
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
use Wikimedia\ObjectFactory;
|
|
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\EnumDef;
|
2020-04-01 01:14:12 +00:00
|
|
|
use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
|
2019-08-21 19:53:53 +00:00
|
|
|
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\LimitDef;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\PasswordDef;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\PresenceBooleanDef;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\StringDef;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\TimestampDef;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\UploadDef;
|
|
|
|
|
use Wikimedia\ParamValidator\ValidationException;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This wraps a bunch of the API-specific parameter validation logic.
|
|
|
|
|
*
|
|
|
|
|
* It's intended to be used in ApiMain by composition.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.35
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiParamValidator {
|
|
|
|
|
|
|
|
|
|
/** @var ParamValidator */
|
|
|
|
|
private $paramValidator;
|
|
|
|
|
|
|
|
|
|
/** @var MessageConverter */
|
|
|
|
|
private $messageConverter;
|
|
|
|
|
|
|
|
|
|
/** Type defs for ParamValidator */
|
|
|
|
|
private const TYPE_DEFS = [
|
|
|
|
|
'boolean' => [ 'class' => PresenceBooleanDef::class ],
|
|
|
|
|
'enum' => [ 'class' => EnumDef::class ],
|
2020-04-01 01:14:12 +00:00
|
|
|
'expiry' => [ 'class' => ExpiryDef::class ],
|
2019-08-21 19:53:53 +00:00
|
|
|
'integer' => [ 'class' => IntegerDef::class ],
|
|
|
|
|
'limit' => [ 'class' => LimitDef::class ],
|
|
|
|
|
'namespace' => [
|
|
|
|
|
'class' => NamespaceDef::class,
|
|
|
|
|
'services' => [ 'NamespaceInfo' ],
|
|
|
|
|
],
|
|
|
|
|
'NULL' => [
|
|
|
|
|
'class' => StringDef::class,
|
|
|
|
|
'args' => [ [
|
|
|
|
|
'allowEmptyWhenRequired' => true,
|
|
|
|
|
] ],
|
|
|
|
|
],
|
|
|
|
|
'password' => [ 'class' => PasswordDef::class ],
|
2020-12-15 23:56:01 +00:00
|
|
|
// Unlike 'string', the 'raw' type will not be subject to Unicode
|
|
|
|
|
// NFC normalization.
|
|
|
|
|
'raw' => [ 'class' => StringDef::class ],
|
2019-08-21 19:53:53 +00:00
|
|
|
'string' => [ 'class' => StringDef::class ],
|
|
|
|
|
'submodule' => [ 'class' => SubmoduleDef::class ],
|
|
|
|
|
'tags' => [ 'class' => TagsDef::class ],
|
|
|
|
|
'text' => [ 'class' => StringDef::class ],
|
|
|
|
|
'timestamp' => [
|
|
|
|
|
'class' => TimestampDef::class,
|
|
|
|
|
'args' => [ [
|
|
|
|
|
'defaultFormat' => TS_MW,
|
|
|
|
|
] ],
|
|
|
|
|
],
|
2020-11-03 03:12:00 +00:00
|
|
|
'title' => [
|
|
|
|
|
'class' => TitleDef::class,
|
|
|
|
|
'services' => [ 'TitleFactory' ],
|
|
|
|
|
],
|
2020-07-28 20:24:49 +00:00
|
|
|
'user' => [
|
|
|
|
|
'class' => UserDef::class,
|
UserDef: no need to return full User objects
Maintain the existing behavior for creating based on a user
id or name that does not correspond to an existing user:
* if creating a User object based on the id 0, User::loadFromId()
will load the defaults with the name being set to false, and
User::getName() will convert that false to the ip of the current
request. We probably don't want UserDef to be doing that, but
this behavior should be changed separately - at the moment
we are just matching the existing behavior (see the prior commit
which adds tests to confirm the behavior isn't changing)
* if creating a User object based on an id that cannot be found
in the database, User::loadFromDatabase() will load the same
defaults, *except* that it will set the user name to "Unknown user"
* if creating a User object based on a name, User::getId() will,
if the id isn't already known, return 0 if the name matches
UserNameUtils::isIP() or ExternalUserNames::isExternal(). However,
the User object is currently only created based on a name after
passing an ExternalUserNames check, and the creation with
RIGOR_VALID prevents it from being an ip. Thus, the existing
code will call User::load(), which will check the database or
fallback to 0 if the user does not exist.
When creating a User from an id that does not exist, User::getId()
will return that id, until after the object tries to fully load,
after which it'll return 0 for the id. Since we cannot replicate
this with UserIdentity (switching the id to 0 only after getName()
is called for the first time) we set the id to always be 0, which
is more accurate. This is the only change in behavior.
We will remove the use of the context ip in a follow-up that will
switch to using "Unknown user" instead.
Accordingly, we will use a UserIdentityLookup as well as
manual construction of UserIdentityValue objects to match this
behavior.
Bug: T288311
Change-Id: Ida80c5d04d721fafa8d66f656dbd346c6cf643eb
2021-07-06 19:56:09 +00:00
|
|
|
'services' => [ 'UserIdentityLookup', 'TitleFactory', 'UserNameUtils' ]
|
2020-07-28 20:24:49 +00:00
|
|
|
],
|
2019-08-21 19:53:53 +00:00
|
|
|
'upload' => [ 'class' => UploadDef::class ],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
* @param ApiMain $main
|
|
|
|
|
* @param ObjectFactory $objectFactory
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( ApiMain $main, ObjectFactory $objectFactory ) {
|
|
|
|
|
$this->paramValidator = new ParamValidator(
|
|
|
|
|
new ApiParamValidatorCallbacks( $main ),
|
|
|
|
|
$objectFactory,
|
|
|
|
|
[
|
|
|
|
|
'typeDefs' => self::TYPE_DEFS,
|
|
|
|
|
'ismultiLimits' => [ ApiBase::LIMIT_SML1, ApiBase::LIMIT_SML2 ],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$this->messageConverter = new MessageConverter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List known type names
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function knownTypes(): array {
|
2019-08-21 19:53:53 +00:00
|
|
|
return $this->paramValidator->knownTypes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-15 21:08:43 +00:00
|
|
|
* Map deprecated styles for messages for ParamValidator
|
|
|
|
|
* @param array $settings
|
2019-08-21 19:53:53 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function mapDeprecatedSettingsMessages( array $settings ): array {
|
2019-08-21 19:53:53 +00:00
|
|
|
if ( isset( $settings[EnumDef::PARAM_DEPRECATED_VALUES] ) ) {
|
|
|
|
|
foreach ( $settings[EnumDef::PARAM_DEPRECATED_VALUES] as &$v ) {
|
|
|
|
|
if ( $v === null || $v === true || $v instanceof MessageValue ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert the message specification to a DataMessageValue. Flag in the data
|
|
|
|
|
// that it was so converted, so ApiParamValidatorCallbacks::recordCondition() can
|
|
|
|
|
// take that into account.
|
|
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument
|
|
|
|
|
$msg = $this->messageConverter->convertMessage( ApiMessage::create( $v ) );
|
|
|
|
|
$v = DataMessageValue::new(
|
|
|
|
|
$msg->getKey(),
|
|
|
|
|
$msg->getParams(),
|
|
|
|
|
'bogus',
|
|
|
|
|
[ '💩' => 'back-compat' ]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
unset( $v );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $settings;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 21:08:43 +00:00
|
|
|
/**
|
|
|
|
|
* Adjust certain settings where ParamValidator differs from historical Action API behavior
|
|
|
|
|
* @param array|mixed $settings
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function normalizeSettings( $settings ): array {
|
2020-01-15 21:08:43 +00:00
|
|
|
if ( is_array( $settings ) ) {
|
|
|
|
|
if ( !isset( $settings[ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES] ) ) {
|
|
|
|
|
$settings[ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $settings[IntegerDef::PARAM_IGNORE_RANGE] ) ) {
|
|
|
|
|
$settings[IntegerDef::PARAM_IGNORE_RANGE] = empty( $settings[ApiBase::PARAM_RANGE_ENFORCE] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$settings = $this->mapDeprecatedSettingsMessages( $settings );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->paramValidator->normalizeSettings( $settings );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check an API settings message
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param array &$ret
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
private function checkSettingsMessage( ApiBase $module, string $key, $value, array &$ret ): void {
|
2020-01-15 21:08:43 +00:00
|
|
|
$msg = ApiBase::makeMessage( $value, $module );
|
|
|
|
|
if ( $msg instanceof Message ) {
|
|
|
|
|
$ret['messages'][] = $this->messageConverter->convertMessage( $msg );
|
|
|
|
|
} else {
|
|
|
|
|
$ret['issues'][] = "Message specification for $key is not valid";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check settings for the Action API.
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param array $params All module params to test
|
|
|
|
|
* @param string $name Parameter to test
|
|
|
|
|
* @param array $options Options array
|
|
|
|
|
* @return array As for ParamValidator::checkSettings()
|
|
|
|
|
*/
|
|
|
|
|
public function checkSettings(
|
|
|
|
|
ApiBase $module, array $params, string $name, array $options
|
2021-07-22 03:11:47 +00:00
|
|
|
): array {
|
2020-01-15 21:08:43 +00:00
|
|
|
$options['module'] = $module;
|
|
|
|
|
$settings = $params[$name];
|
|
|
|
|
if ( is_array( $settings ) ) {
|
|
|
|
|
$settings = $this->mapDeprecatedSettingsMessages( $settings );
|
|
|
|
|
}
|
|
|
|
|
$ret = $this->paramValidator->checkSettings(
|
|
|
|
|
$module->encodeParamName( $name ), $settings, $options
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$ret['allowedKeys'] = array_merge( $ret['allowedKeys'], [
|
|
|
|
|
ApiBase::PARAM_RANGE_ENFORCE, ApiBase::PARAM_HELP_MSG, ApiBase::PARAM_HELP_MSG_APPEND,
|
|
|
|
|
ApiBase::PARAM_HELP_MSG_INFO, ApiBase::PARAM_HELP_MSG_PER_VALUE, ApiBase::PARAM_TEMPLATE_VARS,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
if ( !is_array( $settings ) ) {
|
|
|
|
|
$settings = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( array_key_exists( ApiBase::PARAM_VALUE_LINKS, $settings ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_VALUE_LINKS]
|
|
|
|
|
= 'PARAM_VALUE_LINKS was deprecated in MediaWiki 1.35';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_bool( $settings[ApiBase::PARAM_RANGE_ENFORCE] ?? false ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_RANGE_ENFORCE] = 'PARAM_RANGE_ENFORCE must be boolean, got '
|
|
|
|
|
. gettype( $settings[ApiBase::PARAM_RANGE_ENFORCE] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $settings[ApiBase::PARAM_HELP_MSG] ) ) {
|
|
|
|
|
$this->checkSettingsMessage(
|
|
|
|
|
$module, 'PARAM_HELP_MSG', $settings[ApiBase::PARAM_HELP_MSG], $ret
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
|
|
|
|
|
if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_HELP_MSG_APPEND] = 'PARAM_HELP_MSG_APPEND must be an array, got '
|
|
|
|
|
. gettype( $settings[ApiBase::PARAM_HELP_MSG_APPEND] );
|
|
|
|
|
} else {
|
|
|
|
|
foreach ( $settings[ApiBase::PARAM_HELP_MSG_APPEND] as $k => $v ) {
|
|
|
|
|
$this->checkSettingsMessage( $module, "PARAM_HELP_MSG_APPEND[$k]", $v, $ret );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
|
|
|
|
|
if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_INFO] ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_HELP_MSG_INFO] = 'PARAM_HELP_MSG_INFO must be an array, got '
|
|
|
|
|
. gettype( $settings[ApiBase::PARAM_HELP_MSG_INFO] );
|
|
|
|
|
} else {
|
|
|
|
|
$path = $module->getModulePath();
|
|
|
|
|
foreach ( $settings[ApiBase::PARAM_HELP_MSG_INFO] as $k => $v ) {
|
|
|
|
|
if ( !is_array( $v ) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_HELP_MSG_INFO[$k] must be an array, got " . gettype( $v );
|
|
|
|
|
} elseif ( !is_string( $v[0] ) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_HELP_MSG_INFO[$k][0] must be a string, got " . gettype( $v[0] );
|
|
|
|
|
} else {
|
|
|
|
|
$v[0] = "apihelp-{$path}-paraminfo-{$v[0]}";
|
|
|
|
|
$this->checkSettingsMessage( $module, "PARAM_HELP_MSG_INFO[$k]", $v, $ret );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
|
|
|
|
|
if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_HELP_MSG_PER_VALUE] = 'PARAM_HELP_MSG_PER_VALUE must be an array,'
|
2021-09-03 22:52:31 +00:00
|
|
|
. ' got ' . gettype( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] );
|
2020-01-15 21:08:43 +00:00
|
|
|
} elseif ( !is_array( $settings[ParamValidator::PARAM_TYPE] ?? '' ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_HELP_MSG_PER_VALUE] = 'PARAM_HELP_MSG_PER_VALUE can only be used '
|
|
|
|
|
. 'with PARAM_TYPE as an array';
|
|
|
|
|
} else {
|
|
|
|
|
$values = array_map( 'strval', $settings[ParamValidator::PARAM_TYPE] );
|
|
|
|
|
foreach ( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] as $k => $v ) {
|
|
|
|
|
if ( !in_array( (string)$k, $values, true ) ) {
|
|
|
|
|
// Or should this be allowed?
|
|
|
|
|
$ret['issues'][] = "PARAM_HELP_MSG_PER_VALUE contains \"$k\", which is not in PARAM_TYPE.";
|
|
|
|
|
}
|
|
|
|
|
$this->checkSettingsMessage( $module, "PARAM_HELP_MSG_PER_VALUE[$k]", $v, $ret );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $settings[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
|
|
|
|
|
if ( !is_array( $settings[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_TEMPLATE_VARS] = 'PARAM_TEMPLATE_VARS must be an array,'
|
2021-09-03 22:52:31 +00:00
|
|
|
. ' got ' . gettype( $settings[ApiBase::PARAM_TEMPLATE_VARS] );
|
2020-01-15 21:08:43 +00:00
|
|
|
} elseif ( $settings[ApiBase::PARAM_TEMPLATE_VARS] === [] ) {
|
|
|
|
|
$ret['issues'][ApiBase::PARAM_TEMPLATE_VARS] = 'PARAM_TEMPLATE_VARS cannot be the empty array';
|
|
|
|
|
} else {
|
|
|
|
|
foreach ( $settings[ApiBase::PARAM_TEMPLATE_VARS] as $key => $target ) {
|
|
|
|
|
if ( !preg_match( '/^[^{}]+$/', $key ) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_TEMPLATE_VARS keys may not contain '{' or '}', got \"$key\"";
|
|
|
|
|
} elseif ( strpos( $name, '{' . $key . '}' ) === false ) {
|
|
|
|
|
$ret['issues'][] = "Parameter name must contain PARAM_TEMPLATE_VARS key {{$key}}";
|
|
|
|
|
}
|
|
|
|
|
if ( !is_string( $target ) && !is_int( $target ) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_TEMPLATE_VARS[$key] has invalid target type " . gettype( $target );
|
|
|
|
|
} elseif ( !isset( $params[$target] ) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_TEMPLATE_VARS[$key] target parameter \"$target\" does not exist";
|
|
|
|
|
} else {
|
|
|
|
|
$settings2 = $params[$target];
|
|
|
|
|
if ( empty( $settings2[ParamValidator::PARAM_ISMULTI] ) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_TEMPLATE_VARS[$key] target parameter \"$target\" must have "
|
|
|
|
|
. 'PARAM_ISMULTI = true';
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $settings2[ApiBase::PARAM_TEMPLATE_VARS] ) ) {
|
|
|
|
|
if ( $target === $name ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_TEMPLATE_VARS[$key] cannot target the parameter itself";
|
|
|
|
|
}
|
|
|
|
|
if ( array_diff(
|
|
|
|
|
$settings2[ApiBase::PARAM_TEMPLATE_VARS],
|
|
|
|
|
$settings[ApiBase::PARAM_TEMPLATE_VARS]
|
|
|
|
|
) ) {
|
|
|
|
|
$ret['issues'][] = "PARAM_TEMPLATE_VARS[$key]: Target's "
|
|
|
|
|
. 'PARAM_TEMPLATE_VARS must be a subset of the original';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$keys = implode( '|', array_map(
|
2021-02-10 22:31:02 +00:00
|
|
|
static function ( $key ) {
|
2020-01-15 21:08:43 +00:00
|
|
|
return preg_quote( $key, '/' );
|
|
|
|
|
},
|
|
|
|
|
array_keys( $settings[ApiBase::PARAM_TEMPLATE_VARS] )
|
|
|
|
|
) );
|
|
|
|
|
if ( !preg_match( '/^(?>[^{}]+|\{(?:' . $keys . ')\})+$/', $name ) ) {
|
|
|
|
|
$ret['issues'][] = "Parameter name may not contain '{' or '}' other than '
|
|
|
|
|
. 'as defined by PARAM_TEMPLATE_VARS";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ( !preg_match( '/^[^{}]+$/', $name ) ) {
|
|
|
|
|
$ret['issues'][] = "Parameter name may not contain '{' or '}' without PARAM_TEMPLATE_VARS";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 19:53:53 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a ValidationException to an ApiUsageException
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param ValidationException $ex
|
|
|
|
|
* @throws ApiUsageException always
|
2021-09-04 01:42:33 +00:00
|
|
|
* @return never
|
2019-08-21 19:53:53 +00:00
|
|
|
*/
|
2020-12-18 20:48:04 +00:00
|
|
|
private function convertValidationException( ApiBase $module, ValidationException $ex ) {
|
2019-08-21 19:53:53 +00:00
|
|
|
$mv = $ex->getFailureMessage();
|
|
|
|
|
throw ApiUsageException::newWithMessage(
|
|
|
|
|
$module,
|
|
|
|
|
$this->messageConverter->convertMessageValue( $mv ),
|
|
|
|
|
$mv->getCode(),
|
|
|
|
|
$mv->getData(),
|
|
|
|
|
0,
|
|
|
|
|
$ex
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get and validate a value
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param string $name Parameter name, unprefixed
|
|
|
|
|
* @param array|mixed $settings Default value or an array of settings
|
|
|
|
|
* using PARAM_* constants.
|
|
|
|
|
* @param array $options Options array
|
|
|
|
|
* @return mixed Validated parameter value
|
|
|
|
|
* @throws ApiUsageException if the value is invalid
|
|
|
|
|
*/
|
|
|
|
|
public function getValue( ApiBase $module, string $name, $settings, array $options = [] ) {
|
|
|
|
|
$options['module'] = $module;
|
|
|
|
|
$name = $module->encodeParamName( $name );
|
|
|
|
|
$settings = $this->normalizeSettings( $settings );
|
|
|
|
|
try {
|
|
|
|
|
return $this->paramValidator->getValue( $name, $settings, $options );
|
|
|
|
|
} catch ( ValidationException $ex ) {
|
|
|
|
|
$this->convertValidationException( $module, $ex );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Valiate a parameter value using a settings array
|
|
|
|
|
*
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param string $name Parameter name, unprefixed
|
|
|
|
|
* @param mixed $value Parameter value
|
|
|
|
|
* @param array|mixed $settings Default value or an array of settings
|
|
|
|
|
* using PARAM_* constants.
|
|
|
|
|
* @param array $options Options array
|
|
|
|
|
* @return mixed Validated parameter value(s)
|
|
|
|
|
* @throws ApiUsageException if the value is invalid
|
|
|
|
|
*/
|
|
|
|
|
public function validateValue(
|
|
|
|
|
ApiBase $module, string $name, $value, $settings, array $options = []
|
|
|
|
|
) {
|
|
|
|
|
$options['module'] = $module;
|
|
|
|
|
$name = $module->encodeParamName( $name );
|
|
|
|
|
$settings = $this->normalizeSettings( $settings );
|
|
|
|
|
try {
|
|
|
|
|
return $this->paramValidator->validateValue( $name, $value, $settings, $options );
|
|
|
|
|
} catch ( ValidationException $ex ) {
|
|
|
|
|
$this->convertValidationException( $module, $ex );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Describe parameter settings in a machine-readable format.
|
|
|
|
|
*
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param string $name Parameter name.
|
|
|
|
|
* @param array|mixed $settings Default value or an array of settings
|
|
|
|
|
* using PARAM_* constants.
|
|
|
|
|
* @param array $options Options array.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getParamInfo( ApiBase $module, string $name, $settings, array $options ): array {
|
2019-08-21 19:53:53 +00:00
|
|
|
$options['module'] = $module;
|
|
|
|
|
$name = $module->encodeParamName( $name );
|
|
|
|
|
return $this->paramValidator->getParamInfo( $name, $settings, $options );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Describe parameter settings in human-readable format
|
|
|
|
|
*
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
* @param string $name Parameter name being described.
|
|
|
|
|
* @param array|mixed $settings Default value or an array of settings
|
|
|
|
|
* using PARAM_* constants.
|
|
|
|
|
* @param array $options Options array.
|
|
|
|
|
* @return Message[]
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function getHelpInfo( ApiBase $module, string $name, $settings, array $options ): array {
|
2019-08-21 19:53:53 +00:00
|
|
|
$options['module'] = $module;
|
|
|
|
|
$name = $module->encodeParamName( $name );
|
|
|
|
|
|
|
|
|
|
$ret = $this->paramValidator->getHelpInfo( $name, $settings, $options );
|
|
|
|
|
foreach ( $ret as &$m ) {
|
|
|
|
|
$k = $m->getKey();
|
|
|
|
|
$m = $this->messageConverter->convertMessageValue( $m );
|
|
|
|
|
if ( substr( $k, 0, 20 ) === 'paramvalidator-help-' ) {
|
|
|
|
|
$m = new Message(
|
|
|
|
|
[ 'api-help-param-' . substr( $k, 20 ), $k ],
|
|
|
|
|
$m->getParams()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'@phan-var Message[] $ret'; // The above loop converts it
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|