MediaWiki master
ParamValidatorCallbacks.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
8use Psr\Http\Message\UploadedFileInterface;
9use UtfNormal\Validator;
12
14
15 private RequestInterface $request;
16 private Authority $authority;
17
18 public function __construct(
19 RequestInterface $request,
20 Authority $authority
21 ) {
22 $this->request = $request;
23 $this->authority = $authority;
24 }
25
31 private function getParamsFromSource( $source ) {
32 // This switch block must match Validator::KNOWN_PARAM_SOURCES
33 switch ( $source ) {
34 case 'path':
35 return $this->request->getPathParams();
36
37 case 'query':
38 return $this->request->getQueryParams();
39
40 case 'post':
41 wfDeprecatedMsg( 'The "post" source is deprecated, use "body" instead', '1.43' );
42 return $this->request->getPostParams();
43
44 case 'body':
45 return $this->request->getParsedBody() ?? [];
46
47 default:
48 throw new InvalidArgumentException( __METHOD__ . ": Invalid source '$source'" );
49 }
50 }
51
52 public function hasParam( $name, array $options ) {
53 $params = $this->getParamsFromSource( $options['source'] );
54 return isset( $params[$name] );
55 }
56
57 public function getValue( $name, $default, array $options ) {
58 $params = $this->getParamsFromSource( $options['source'] );
59 $value = $params[$name] ?? $default;
60
61 // Normalisation for body is being handled in Handler::parseBodyData
62 if ( !isset( $options['raw'] ) && $options['source'] !== 'body' ) {
63 if ( is_string( $value ) ) {
64 // Normalize value to NFC UTF-8
65 $normalizedValue = Validator::cleanUp( $value );
66 // TODO: Warn if normalization was applied
67
68 $value = $normalizedValue;
69 }
70 }
71
72 return $value;
73 }
74
75 public function hasUpload( $name, array $options ) {
76 if ( $options['source'] !== 'post' ) {
77 return false;
78 }
79 return $this->getUploadedFile( $name, $options ) !== null;
80 }
81
82 public function getUploadedFile( $name, array $options ) {
83 if ( $options['source'] !== 'post' ) {
84 return null;
85 }
86 $upload = $this->request->getUploadedFiles()[$name] ?? null;
87 return $upload instanceof UploadedFileInterface ? $upload : null;
88 }
89
90 public function recordCondition(
91 DataMessageValue $message, $name, $value, array $settings, array $options
92 ) {
93 // @todo Figure out how to handle warnings
94 }
95
96 public function useHighLimits( array $options ) {
97 return $this->authority->isAllowed( 'apihighlimits' );
98 }
99
100}
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
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