MediaWiki master
ApiParamValidatorCallbacks.php
Go to the documentation of this file.
1<?php
2
4
10
17
19 private $apiMain;
20
25 public function __construct( ApiMain $main ) {
26 $this->apiMain = $main;
27 }
28
30 public function hasParam( $name, array $options ) {
31 return $this->apiMain->getCheck( $name );
32 }
33
35 public function getValue( $name, $default, array $options ) {
36 $value = $this->apiMain->getVal( $name, $default );
37 $request = $this->apiMain->getRequest();
38 $rawValue = $request->getRawVal( $name );
39
40 if ( $options['raw'] ?? false ) {
41 // Bypass NFC normalization
42 return $rawValue;
43 }
44 if ( is_string( $rawValue ) ) {
45 // Preserve U+001F for multi-values
46 if ( str_starts_with( $rawValue, "\x1f" ) ) {
47 // This loses the potential checkTitleEncoding() transformation done by
48 // WebRequest for $_GET. Let's call that a feature.
49 $value = implode( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
50 }
51
52 // Check for NFC normalization, and warn
53 if ( $rawValue !== $value ) {
54 $options['module']->handleParamNormalization( $name, $value, $rawValue );
55 }
56 }
57
58 return $value;
59 }
60
62 public function hasUpload( $name, array $options ) {
63 return $this->getUploadedFile( $name, $options ) !== null;
64 }
65
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
82 public function recordCondition(
83 DataMessageValue $message, $name, $value, array $settings, array $options
84 ) {
86 $module = $options['module'];
87
88 $code = $message->getCode();
89 switch ( $code ) {
90 case 'param-deprecated': // @codeCoverageIgnore
91 case 'deprecated-value': // @codeCoverageIgnore
92 if ( $code === 'param-deprecated' ) {
93 $feature = $name;
94 } else {
95 $feature = $name . '=' . $value;
96 $data = $message->getData() ?? [];
97 if ( isset( $data['💩'] ) ) {
98 // This is from an old-style Message. Strip out ParamValidator's added params.
99 unset( $data['💩'] );
100 $message = DataMessageValue::new(
101 $message->getKey(),
102 array_slice( $message->getParams(), 2 ),
103 $code,
104 $data
105 );
106 }
107 }
108
109 $m = $module;
110 while ( !$m->isMain() ) {
111 $p = $m->getParent();
112 $mName = $m->getModuleName();
113 $mParam = $p->encodeParamName( $p->getModuleManager()->getModuleGroup( $mName ) );
114 $feature = "{$mParam}={$mName}&{$feature}";
115 $m = $p;
116 }
117 $module->addDeprecation(
118 $message,
119 $feature,
120 $message->getData()
121 );
122 break;
123
124 case 'param-sensitive': // @codeCoverageIgnore
125 $module->getMain()->markParamsSensitive( $name );
126 break;
127
128 default:
129 $module->addWarning(
130 $message,
131 $message->getCode(),
132 $message->getData()
133 );
134 break;
135 }
136 }
137
139 public function useHighLimits( array $options ) {
140 return $this->apiMain->canApiHighLimits();
141 }
142
143}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
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.UploadedFileInterface|null Uploaded file, or null if there is no file fo...
getValue( $name, $default, array $options)
Fetch a value from the request.Return $default for file-upload parameters.string|string[]|mixed A str...
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.bool True if present, false if absent. Return false for fil...
useHighLimits(array $options)
Indicate whether "high limits" should be used.Some settings have multiple limits, one for "normal" us...
hasUpload( $name, array $options)
Test if a parameter exists as an upload in the request.bool True if present, false if absent.
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