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 public function __construct(
16 private readonly RequestInterface $request,
17 private readonly Authority $authority,
18 ) {
19 }
20
26 private function getParamsFromSource( $source ) {
27 // This switch block must match Validator::KNOWN_PARAM_SOURCES
28 switch ( $source ) {
29 case 'path':
30 return $this->request->getPathParams();
31
32 case 'query':
33 return $this->request->getQueryParams();
34
35 case 'post':
36 wfDeprecatedMsg( 'The "post" source is deprecated, use "body" instead', '1.43' );
37 return $this->request->getPostParams();
38
39 case 'body':
40 return $this->request->getParsedBody() ?? [];
41
42 case 'header':
43 return $this->request->getHeaders() ?? [];
44
45 default:
46 throw new InvalidArgumentException( __METHOD__ . ": Invalid source '$source'" );
47 }
48 }
49
51 public function hasParam( $name, array $options ) {
52 $params = $this->getParamsFromSource( $options['source'] );
53 return isset( $params[$name] );
54 }
55
57 public function getValue( $name, $default, array $options ) {
58 $params = $this->getParamsFromSource( $options['source'] );
59 $value = $params[$name] ?? $default;
60 if (
61 $options['source'] === 'header' &&
62 $options['type'] === 'string' &&
63 isset( $params[$name] )
64 ) {
65 $value = implode( ', ', $value );
66 }
67
68 // Normalisation for body is being handled in Handler::parseBodyData
69 if ( !isset( $options['raw'] ) && $options['source'] !== 'body' ) {
70 if ( is_string( $value ) ) {
71 // Normalize value to NFC UTF-8
72 $normalizedValue = Validator::cleanUp( $value );
73 // TODO: Warn if normalization was applied
74
75 $value = $normalizedValue;
76 }
77 }
78
79 return $value;
80 }
81
83 public function hasUpload( $name, array $options ) {
84 if ( $options['source'] !== 'post' ) {
85 return false;
86 }
87 return $this->getUploadedFile( $name, $options ) !== null;
88 }
89
91 public function getUploadedFile( $name, array $options ) {
92 if ( $options['source'] !== 'post' ) {
93 return null;
94 }
95 $upload = $this->request->getUploadedFiles()[$name] ?? null;
96 return $upload instanceof UploadedFileInterface ? $upload : null;
97 }
98
100 public function recordCondition(
101 DataMessageValue $message, $name, $value, array $settings, array $options
102 ) {
103 // @todo Figure out how to handle warnings
104 }
105
107 public function useHighLimits( array $options ) {
108 return $this->authority->isAllowed( 'apihighlimits' );
109 }
110
111}
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
getValue( $name, $default, array $options)
Fetch a value from the request.Return $default for file-upload parameters.string|string[]|mixed A str...
hasParam( $name, array $options)
Test if a parameter exists in the request.bool True if present, false if absent. Return false for fil...
useHighLimits(array $options)
Indicate whether "high limits" should be used.Some settings have multiple limits, one for "normal" us...
__construct(private readonly RequestInterface $request, private readonly Authority $authority,)
recordCondition(DataMessageValue $message, $name, $value, array $settings, array $options)
Record non-fatal conditions.
getUploadedFile( $name, array $options)
Fetch data for a file upload.UploadedFileInterface|null Uploaded file, or null if there is no file fo...
hasUpload( $name, array $options)
Test if a parameter exists as an upload in the request.bool True if present, false if absent.
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:23
A request interface similar to PSR-7's ServerRequestInterface.
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21
$source