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