MediaWiki master
ApiParamValidatorCallbacks.php
Go to the documentation of this file.
1<?php
2
4
5use ApiMain;
6use MediaWiki\Message\Converter as MessageConverter;
10
17
19 private $apiMain;
20
22 private $messageConverter;
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}
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:65
getUploadedFile( $name, array $options)
Fetch data for a file upload.
getValue( $name, $default, array $options)
Fetch a value from the request.
recordCondition(DataMessageValue $message, $name, $value, array $settings, array $options)
Record non-fatal conditions.
hasParam( $name, array $options)
Test if a parameter exists in the request.
useHighLimits(array $options)
Indicate whether "high limits" should be used.
hasUpload( $name, array $options)
Test if a parameter exists as an upload in the request.
Converter between Message and MessageValue.
Definition Converter.php:18
Value object representing a message for i18n with alternative machine-readable data.
getData()
Get the message's structured data.
getKey()
Get the message key.
getParams()
Get the parameter array.
A simple implementation of UploadedFileInterface.
Interface defining callbacks needed by ParamValidator.
Definition Callbacks.php:21