MediaWiki  master
ParamValidatorCallbacks.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
9 use Psr\Http\Message\UploadedFileInterface;
12 
14 
17 
19  private $request;
20 
22  private $user;
23 
24  public function __construct(
28  ) {
29  $this->permissionManager = $permissionManager;
30  $this->request = $request;
31  $this->user = $user;
32  }
33 
39  private function getParamsFromSource( $source ) {
40  switch ( $source ) {
41  case 'path':
42  return $this->request->getPathParams();
43 
44  case 'query':
45  return $this->request->getQueryParams();
46 
47  case 'post':
48  return $this->request->getPostParams();
49 
50  default:
51  throw new InvalidArgumentException( __METHOD__ . ": Invalid source '$source'" );
52  }
53  }
54 
55  public function hasParam( $name, array $options ) {
56  $params = $this->getParamsFromSource( $options['source'] );
57  return isset( $params[$name] );
58  }
59 
60  public function getValue( $name, $default, array $options ) {
61  $params = $this->getParamsFromSource( $options['source'] );
62  return $params[$name] ?? $default;
63  // @todo Should normalization to NFC UTF-8 be done here (much like in the
64  // action API and the rest of MW), or should it be left to handlers to
65  // do whatever normalization they need?
66  }
67 
68  public function hasUpload( $name, array $options ) {
69  if ( $options['source'] !== 'post' ) {
70  return false;
71  }
72  return $this->getUploadedFile( $name, $options ) !== null;
73  }
74 
75  public function getUploadedFile( $name, array $options ) {
76  if ( $options['source'] !== 'post' ) {
77  return null;
78  }
79  $upload = $this->request->getUploadedFiles()[$name] ?? null;
80  return $upload instanceof UploadedFileInterface ? $upload : null;
81  }
82 
83  public function recordCondition(
84  DataMessageValue $message, $name, $value, array $settings, array $options
85  ) {
86  // @todo Figure out how to handle warnings
87  }
88 
89  public function useHighLimits( array $options ) {
90  return $this->permissionManager->userHasRight( $this->user, 'apihighlimits' );
91  }
92 
93 }
MediaWiki\Rest\Validator\ParamValidatorCallbacks\getValue
getValue( $name, $default, array $options)
Fetch a value from the request.
Definition: ParamValidatorCallbacks.php:60
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
MediaWiki\Rest\Validator\ParamValidatorCallbacks\$request
RequestInterface $request
Definition: ParamValidatorCallbacks.php:19
MediaWiki\Rest\Validator\ParamValidatorCallbacks\useHighLimits
useHighLimits(array $options)
Indicate whether "high limits" should be used.
Definition: ParamValidatorCallbacks.php:89
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\Rest\Validator\ParamValidatorCallbacks\getParamsFromSource
getParamsFromSource( $source)
Get the raw parameters from a source in the request.
Definition: ParamValidatorCallbacks.php:39
MediaWiki\Rest\Validator\ParamValidatorCallbacks
Definition: ParamValidatorCallbacks.php:13
MediaWiki\Rest\Validator\ParamValidatorCallbacks\$permissionManager
PermissionManager $permissionManager
Definition: ParamValidatorCallbacks.php:16
MediaWiki\Rest\Validator\ParamValidatorCallbacks\$user
UserIdentity $user
Definition: ParamValidatorCallbacks.php:22
MediaWiki\Rest\Validator\ParamValidatorCallbacks\hasUpload
hasUpload( $name, array $options)
Test if a parameter exists as an upload in the request.
Definition: ParamValidatorCallbacks.php:68
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:51
MediaWiki\Rest\RequestInterface
A request interface similar to PSR-7's ServerRequestInterface.
Definition: RequestInterface.php:39
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:55
MediaWiki\Rest\Validator\ParamValidatorCallbacks\recordCondition
recordCondition(DataMessageValue $message, $name, $value, array $settings, array $options)
Record non-fatal conditions.
Definition: ParamValidatorCallbacks.php:83
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:75
MediaWiki\Rest\Validator\ParamValidatorCallbacks\__construct
__construct(PermissionManager $permissionManager, RequestInterface $request, UserIdentity $user)
Definition: ParamValidatorCallbacks.php:24