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 ( $options['raw'] ?? false ) {
43  // Bypass NFC normalization
44  return $rawValue;
45  }
46  if ( is_string( $rawValue ) ) {
47  // Preserve U+001F for multi-values
48  if ( substr( $rawValue, 0, 1 ) === "\x1f" ) {
49  // This loses the potential checkTitleEncoding() transformation done by
50  // WebRequest for $_GET. Let's call that a feature.
51  $value = implode( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
52  }
53 
54  // Check for NFC normalization, and warn
55  if ( $rawValue !== $value ) {
56  $options['module']->handleParamNormalization( $name, $value, $rawValue );
57  }
58  }
59 
60  return $value;
61  }
62 
63  public function hasUpload( $name, array $options ) {
64  return $this->getUploadedFile( $name, $options ) !== null;
65  }
66 
67  public function getUploadedFile( $name, array $options ) {
68  $upload = $this->apiMain->getUpload( $name );
69  if ( !$upload->exists() ) {
70  return null;
71  }
72  return new UploadedFile( [
73  'error' => $upload->getError(),
74  'tmp_name' => $upload->getTempName(),
75  'size' => $upload->getSize(),
76  'name' => $upload->getName(),
77  'type' => $upload->getType(),
78  ] );
79  }
80 
81  public function recordCondition(
82  DataMessageValue $message, $name, $value, array $settings, array $options
83  ) {
85  $module = $options['module'];
86 
87  $code = $message->getCode();
88  switch ( $code ) {
89  case 'param-deprecated': // @codeCoverageIgnore
90  case 'deprecated-value': // @codeCoverageIgnore
91  if ( $code === 'param-deprecated' ) {
92  $feature = $name;
93  } else {
94  $feature = $name . '=' . $value;
95  $data = $message->getData() ?? [];
96  if ( isset( $data['💩'] ) ) {
97  // This is from an old-style Message. Strip out ParamValidator's added params.
98  unset( $data['💩'] );
99  $message = DataMessageValue::new(
100  $message->getKey(),
101  array_slice( $message->getParams(), 2 ),
102  $code,
103  $data
104  );
105  }
106  }
107 
108  $m = $module;
109  while ( !$m->isMain() ) {
110  $p = $m->getParent();
111  $mName = $m->getModuleName();
112  $mParam = $p->encodeParamName( $p->getModuleManager()->getModuleGroup( $mName ) );
113  $feature = "{$mParam}={$mName}&{$feature}";
114  $m = $p;
115  }
116  $module->addDeprecation(
117  $this->messageConverter->convertMessageValue( $message ),
118  $feature,
119  $message->getData()
120  );
121  break;
122 
123  case 'param-sensitive': // @codeCoverageIgnore
124  $module->getMain()->markParamsSensitive( $name );
125  break;
126 
127  default:
128  $module->addWarning(
129  $this->messageConverter->convertMessageValue( $message ),
130  $message->getCode(),
131  $message->getData()
132  );
133  break;
134  }
135  }
136 
137  public function useHighLimits( array $options ) {
138  return $this->apiMain->canApiHighLimits();
139  }
140 
141 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
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:63
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:81
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:137
MediaWiki\Api\Validator\ApiParamValidatorCallbacks\getUploadedFile
getUploadedFile( $name, array $options)
Fetch data for a file upload.
Definition: ApiParamValidatorCallbacks.php:67
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