Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.33% |
19 / 30 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ParamValidatorCallbacks | |
63.33% |
19 / 30 |
|
37.50% |
3 / 8 |
36.80 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getParamsFromSource | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
6 | |||
| hasParam | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getValue | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| hasUpload | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getUploadedFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| recordCondition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| useHighLimits | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest\Validator; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use MediaWiki\Permissions\Authority; |
| 7 | use MediaWiki\Rest\RequestInterface; |
| 8 | use Psr\Http\Message\UploadedFileInterface; |
| 9 | use UtfNormal\Validator; |
| 10 | use Wikimedia\Message\DataMessageValue; |
| 11 | use Wikimedia\ParamValidator\Callbacks; |
| 12 | |
| 13 | class ParamValidatorCallbacks implements Callbacks { |
| 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 | |
| 26 | /** |
| 27 | * Get the raw parameters from a source in the request |
| 28 | * @param string $source 'path', 'query', or 'post' |
| 29 | * @return array |
| 30 | */ |
| 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 | /** @inheritDoc */ |
| 53 | public function hasParam( $name, array $options ) { |
| 54 | $params = $this->getParamsFromSource( $options['source'] ); |
| 55 | return isset( $params[$name] ); |
| 56 | } |
| 57 | |
| 58 | /** @inheritDoc */ |
| 59 | public function getValue( $name, $default, array $options ) { |
| 60 | $params = $this->getParamsFromSource( $options['source'] ); |
| 61 | $value = $params[$name] ?? $default; |
| 62 | |
| 63 | // Normalisation for body is being handled in Handler::parseBodyData |
| 64 | if ( !isset( $options['raw'] ) && $options['source'] !== 'body' ) { |
| 65 | if ( is_string( $value ) ) { |
| 66 | // Normalize value to NFC UTF-8 |
| 67 | $normalizedValue = Validator::cleanUp( $value ); |
| 68 | // TODO: Warn if normalization was applied |
| 69 | |
| 70 | $value = $normalizedValue; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $value; |
| 75 | } |
| 76 | |
| 77 | /** @inheritDoc */ |
| 78 | public function hasUpload( $name, array $options ) { |
| 79 | if ( $options['source'] !== 'post' ) { |
| 80 | return false; |
| 81 | } |
| 82 | return $this->getUploadedFile( $name, $options ) !== null; |
| 83 | } |
| 84 | |
| 85 | /** @inheritDoc */ |
| 86 | public function getUploadedFile( $name, array $options ) { |
| 87 | if ( $options['source'] !== 'post' ) { |
| 88 | return null; |
| 89 | } |
| 90 | $upload = $this->request->getUploadedFiles()[$name] ?? null; |
| 91 | return $upload instanceof UploadedFileInterface ? $upload : null; |
| 92 | } |
| 93 | |
| 94 | /** @inheritDoc */ |
| 95 | public function recordCondition( |
| 96 | DataMessageValue $message, $name, $value, array $settings, array $options |
| 97 | ) { |
| 98 | // @todo Figure out how to handle warnings |
| 99 | } |
| 100 | |
| 101 | /** @inheritDoc */ |
| 102 | public function useHighLimits( array $options ) { |
| 103 | return $this->authority->isAllowed( 'apihighlimits' ); |
| 104 | } |
| 105 | |
| 106 | } |