MediaWiki fundraising/REL1_35
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
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}
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:47
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