Merge "Add config for serving main Page from the domain root"
[lhc/web/wiklou.git] / includes / Rest / Validator / ParamValidatorCallbacks.php
1 <?php
2
3 namespace MediaWiki\Rest\Validator;
4
5 use InvalidArgumentException;
6 use MediaWiki\Permissions\PermissionManager;
7 use MediaWiki\Rest\RequestInterface;
8 use MediaWiki\User\UserIdentity;
9 use Psr\Http\Message\UploadedFileInterface;
10 use Wikimedia\ParamValidator\Callbacks;
11 use Wikimedia\ParamValidator\ValidationException;
12
13 class ParamValidatorCallbacks implements Callbacks {
14
15 /** @var PermissionManager */
16 private $permissionManager;
17
18 /** @var RequestInterface */
19 private $request;
20
21 /** @var UserIdentity */
22 private $user;
23
24 public function __construct(
25 PermissionManager $permissionManager,
26 RequestInterface $request,
27 UserIdentity $user
28 ) {
29 $this->permissionManager = $permissionManager;
30 $this->request = $request;
31 $this->user = $user;
32 }
33
34 /**
35 * Get the raw parameters from a source in the request
36 * @param string $source 'path', 'query', or 'post'
37 * @return array
38 */
39 private function getParamsFromSource( $source ) {
40 switch ( $source ) {
41 case 'path':
42 return $this->request->getPathParams();
43
44 case 'query':
45 return $this->request->getQueryParams();
46
47 case 'post':
48 return $this->request->getPostParams();
49
50 default:
51 throw new InvalidArgumentException( __METHOD__ . ": Invalid source '$source'" );
52 }
53 }
54
55 public function hasParam( $name, array $options ) {
56 $params = $this->getParamsFromSource( $options['source'] );
57 return isset( $params[$name] );
58 }
59
60 public function getValue( $name, $default, array $options ) {
61 $params = $this->getParamsFromSource( $options['source'] );
62 return $params[$name] ?? $default;
63 // @todo Should normalization to NFC UTF-8 be done here (much like in the
64 // action API and the rest of MW), or should it be left to handlers to
65 // do whatever normalization they need?
66 }
67
68 public function hasUpload( $name, array $options ) {
69 if ( $options['source'] !== 'post' ) {
70 return false;
71 }
72 return $this->getUploadedFile( $name, $options ) !== null;
73 }
74
75 public function getUploadedFile( $name, array $options ) {
76 if ( $options['source'] !== 'post' ) {
77 return null;
78 }
79 $upload = $this->request->getUploadedFiles()[$name] ?? null;
80 return $upload instanceof UploadedFileInterface ? $upload : null;
81 }
82
83 public function recordCondition( ValidationException $condition, array $options ) {
84 // @todo Figure out how to handle warnings
85 }
86
87 public function useHighLimits( array $options ) {
88 return $this->permissionManager->userHasRight( $this->user, 'apihighlimits' );
89 }
90
91 }