3 namespace MediaWiki\Rest\Validator
;
5 use InvalidArgumentException
;
6 use MediaWiki\Rest\RequestInterface
;
7 use Psr\Http\Message\UploadedFileInterface
;
9 use Wikimedia\ParamValidator\Callbacks
;
10 use Wikimedia\ParamValidator\ValidationException
;
12 class ParamValidatorCallbacks
implements Callbacks
{
14 /** @var RequestInterface */
20 public function __construct( RequestInterface
$request, User
$user ) {
21 $this->request
= $request;
26 * Get the raw parameters from a source in the request
27 * @param string $source 'path', 'query', or 'post'
30 private function getParamsFromSource( $source ) {
33 return $this->request
->getPathParams();
36 return $this->request
->getQueryParams();
39 return $this->request
->getPostParams();
42 throw new InvalidArgumentException( __METHOD__
. ": Invalid source '$source'" );
46 public function hasParam( $name, array $options ) {
47 $params = $this->getParamsFromSource( $options['source'] );
48 return isset( $params[$name] );
51 public function getValue( $name, $default, array $options ) {
52 $params = $this->getParamsFromSource( $options['source'] );
53 return $params[$name] ??
$default;
54 // @todo Should normalization to NFC UTF-8 be done here (much like in the
55 // action API and the rest of MW), or should it be left to handlers to
56 // do whatever normalization they need?
59 public function hasUpload( $name, array $options ) {
60 if ( $options['source'] !== 'post' ) {
63 return $this->getUploadedFile( $name, $options ) !== null;
66 public function getUploadedFile( $name, array $options ) {
67 if ( $options['source'] !== 'post' ) {
70 $upload = $this->request
->getUploadedFiles()[$name] ??
null;
71 return $upload instanceof UploadedFileInterface ?
$upload : null;
74 public function recordCondition( ValidationException
$condition, array $options ) {
75 // @todo Figure out how to handle warnings
78 public function useHighLimits( array $options ) {
79 return $this->user
->isAllowed( 'apihighlimits' );