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