MediaWiki  master
ApiParamValidatorCallbacks.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use ApiMain;
6 use MediaWiki\Message\Converter as MessageConverter;
10 
17 
19  private $apiMain;
20 
23 
28  public function __construct( ApiMain $main ) {
29  $this->apiMain = $main;
30  $this->messageConverter = new MessageConverter();
31  }
32 
33  public function hasParam( $name, array $options ) {
34  return $this->apiMain->getCheck( $name );
35  }
36 
37  public function getValue( $name, $default, array $options ) {
38  $value = $this->apiMain->getVal( $name, $default );
39  $request = $this->apiMain->getRequest();
40  $rawValue = $request->getRawVal( $name );
41 
42  if ( is_string( $rawValue ) ) {
43  // Preserve U+001F for multi-values
44  if ( substr( $rawValue, 0, 1 ) === "\x1f" ) {
45  // This loses the potential checkTitleEncoding() transformation done by
46  // WebRequest for $_GET. Let's call that a feature.
47  $value = implode( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
48  }
49 
50  // Check for NFC normalization, and warn
51  if ( $rawValue !== $value ) {
52  $options['module']->handleParamNormalization( $name, $value, $rawValue );
53  }
54  }
55 
56  return $value;
57  }
58 
59  public function hasUpload( $name, array $options ) {
60  return $this->getUploadedFile( $name, $options ) !== null;
61  }
62 
63  public function getUploadedFile( $name, array $options ) {
64  $upload = $this->apiMain->getUpload( $name );
65  if ( !$upload->exists() ) {
66  return null;
67  }
68  return new UploadedFile( [
69  'error' => $upload->getError(),
70  'tmp_name' => $upload->getTempName(),
71  'size' => $upload->getSize(),
72  'name' => $upload->getName(),
73  'type' => $upload->getType(),
74  ] );
75  }
76 
77  public function recordCondition(
78  DataMessageValue $message, $name, $value, array $settings, array $options
79  ) {
81  $module = $options['module'];
82 
83  $code = $message->getCode();
84  switch ( $code ) {
85  case 'param-deprecated': // @codeCoverageIgnore
86  case 'deprecated-value': // @codeCoverageIgnore
87  if ( $code === 'param-deprecated' ) {
88  $feature = $name;
89  } else {
90  $feature = $name . '=' . $value;
91  $data = $message->getData() ?? [];
92  if ( isset( $data['💩'] ) ) {
93  // This is from an old-style Message. Strip out ParamValidator's added params.
94  unset( $data['💩'] );
95  $message = DataMessageValue::new(
96  $message->getKey(),
97  array_slice( $message->getParams(), 2 ),
98  $code,
99  $data
100  );
101  }
102  }
103 
104  $m = $module;
105  while ( !$m->isMain() ) {
106  $p = $m->getParent();
107  $mName = $m->getModuleName();
108  $mParam = $p->encodeParamName( $p->getModuleManager()->getModuleGroup( $mName ) );
109  $feature = "{$mParam}={$mName}&{$feature}";
110  $m = $p;
111  }
112  $module->addDeprecation(
113  $this->messageConverter->convertMessageValue( $message ),
114  $feature,
115  $message->getData()
116  );
117  break;
118 
119  case 'param-sensitive': // @codeCoverageIgnore
120  $module->getMain()->markParamsSensitive( $name );
121  break;
122 
123  default:
124  $module->addWarning(
125  $this->messageConverter->convertMessageValue( $message ),
126  $message->getCode(),
127  $message->getData()
128  );
129  break;
130  }
131  }
132 
133  public function useHighLimits( array $options ) {
134  return $this->apiMain->canApiHighLimits();
135  }
136 
137 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:48
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\getValue
getValue( $name, $default, array $options)
Fetch a value from the request.
Definition: ApiParamValidatorCallbacks.php:37
Wikimedia\Message\DataMessageValue\getCode
getCode()
Get the message code.
Definition: DataMessageValue.php:63
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\hasUpload
hasUpload( $name, array $options)
Test if a parameter exists as an upload in the request.
Definition: ApiParamValidatorCallbacks.php:59
Wikimedia\ParamValidator\Callbacks
Interface defining callbacks needed by ParamValidator.
Definition: Callbacks.php:21
Wikimedia\Message\MessageValue\getKey
getKey()
Get the message key.
Definition: MessageValue.php:51
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\recordCondition
recordCondition(DataMessageValue $message, $name, $value, array $settings, array $options)
Record non-fatal conditions.
Definition: ApiParamValidatorCallbacks.php:77
Wikimedia\Message\DataMessageValue\getData
getData()
Get the message's structured data.
Definition: DataMessageValue.php:71
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\__construct
__construct(ApiMain $main)
Definition: ApiParamValidatorCallbacks.php:28
Wikimedia\Message\MessageValue\getParams
getParams()
Get the parameter array.
Definition: MessageValue.php:60
Message\Converter
Converter between Message and MessageValue.
Definition: Converter.php:18
Wikimedia\Message\DataMessageValue
Value object representing a message for i18n with alternative machine-readable data.
Definition: DataMessageValue.php:23
MediaWiki\Api\Validator\ApiParamValidatorCallbacks
ParamValidator callbacks for the Action API.
Definition: ApiParamValidatorCallbacks.php:16
Wikimedia\Message\DataMessageValue\new
static new( $key, $params=[], $code=null, array $data=null)
Static constructor for easier chaining of ->params() methods.
Definition: DataMessageValue.php:55
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\$messageConverter
MessageConverter $messageConverter
Definition: ApiParamValidatorCallbacks.php:22
MediaWiki\Api\Validator
Definition: ApiParamValidator.php:3
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\useHighLimits
useHighLimits(array $options)
Indicate whether "high limits" should be used.
Definition: ApiParamValidatorCallbacks.php:133
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\getUploadedFile
getUploadedFile( $name, array $options)
Fetch data for a file upload.
Definition: ApiParamValidatorCallbacks.php:63
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\$apiMain
ApiMain $apiMain
Definition: ApiParamValidatorCallbacks.php:19
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\hasParam
hasParam( $name, array $options)
Test if a parameter exists in the request.
Definition: ApiParamValidatorCallbacks.php:33
Wikimedia\ParamValidator\Util\UploadedFile
A simple implementation of UploadedFileInterface.
Definition: UploadedFile.php:20