MediaWiki  master
ParamValidatorCallbacks.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
8 use Psr\Http\Message\UploadedFileInterface;
11 
13 
15  private $authority;
16 
18  private $request;
19 
20  public function __construct(
23  ) {
24  $this->request = $request;
25  $this->authority = $authority;
26  }
27 
33  private function getParamsFromSource( $source ) {
34  switch ( $source ) {
35  case 'path':
36  return $this->request->getPathParams();
37 
38  case 'query':
39  return $this->request->getQueryParams();
40 
41  case 'post':
42  return $this->request->getPostParams();
43 
44  default:
45  throw new InvalidArgumentException( __METHOD__ . ": Invalid source '$source'" );
46  }
47  }
48 
49  public function hasParam( $name, array $options ) {
50  $params = $this->getParamsFromSource( $options['source'] );
51  return isset( $params[$name] );
52  }
53 
54  public function getValue( $name, $default, array $options ) {
55  $params = $this->getParamsFromSource( $options['source'] );
56  return $params[$name] ?? $default;
57  // @todo Should normalization to NFC UTF-8 be done here (much like in the
58  // action API and the rest of MW), or should it be left to handlers to
59  // do whatever normalization they need?
60  }
61 
62  public function hasUpload( $name, array $options ) {
63  if ( $options['source'] !== 'post' ) {
64  return false;
65  }
66  return $this->getUploadedFile( $name, $options ) !== null;
67  }
68 
69  public function getUploadedFile( $name, array $options ) {
70  if ( $options['source'] !== 'post' ) {
71  return null;
72  }
73  $upload = $this->request->getUploadedFiles()[$name] ?? null;
74  return $upload instanceof UploadedFileInterface ? $upload : null;
75  }
76 
77  public function recordCondition(
78  DataMessageValue $message, $name, $value, array $settings, array $options
79  ) {
80  // @todo Figure out how to handle warnings
81  }
82 
83  public function useHighLimits( array $options ) {
84  return $this->authority->isAllowed( 'apihighlimits' );
85  }
86 
87 }
MediaWiki\Rest\Validator\ParamValidatorCallbacks\getValue
getValue( $name, $default, array $options)
Fetch a value from the request.
Definition: ParamValidatorCallbacks.php:54
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
MediaWiki\Rest\Validator\ParamValidatorCallbacks\$request
RequestInterface $request
Definition: ParamValidatorCallbacks.php:18
MediaWiki\Rest\Validator\ParamValidatorCallbacks\useHighLimits
useHighLimits(array $options)
Indicate whether "high limits" should be used.
Definition: ParamValidatorCallbacks.php:83
MediaWiki\Rest\Validator\ParamValidatorCallbacks\$authority
Authority $authority
Definition: ParamValidatorCallbacks.php:15
MediaWiki\Rest\Validator\ParamValidatorCallbacks\getParamsFromSource
getParamsFromSource( $source)
Get the raw parameters from a source in the request.
Definition: ParamValidatorCallbacks.php:33
MediaWiki\Rest\Validator\ParamValidatorCallbacks
Definition: ParamValidatorCallbacks.php:12
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\Rest\Validator\ParamValidatorCallbacks\hasUpload
hasUpload( $name, array $options)
Test if a parameter exists as an upload in the request.
Definition: ParamValidatorCallbacks.php:62
MediaWiki\Rest\RequestInterface
A request interface similar to PSR-7's ServerRequestInterface.
Definition: RequestInterface.php:39
MediaWiki\Rest\Validator\ParamValidatorCallbacks\__construct
__construct(RequestInterface $request, Authority $authority)
Definition: ParamValidatorCallbacks.php:20
Wikimedia\Message\DataMessageValue
Value object representing a message for i18n with alternative machine-readable data.
Definition: DataMessageValue.php:23
MediaWiki\Rest\Validator\ParamValidatorCallbacks\hasParam
hasParam( $name, array $options)
Test if a parameter exists in the request.
Definition: ParamValidatorCallbacks.php:49
MediaWiki\Rest\Validator\ParamValidatorCallbacks\recordCondition
recordCondition(DataMessageValue $message, $name, $value, array $settings, array $options)
Record non-fatal conditions.
Definition: ParamValidatorCallbacks.php:77
MediaWiki\Rest\Validator
Definition: BodyValidator.php:3
$source
$source
Definition: mwdoc-filter.php:34
MediaWiki\Rest\Validator\ParamValidatorCallbacks\getUploadedFile
getUploadedFile( $name, array $options)
Fetch data for a file upload.
Definition: ParamValidatorCallbacks.php:69