Debian Codegraph @

includes/api/Validator/ApiParamValidatorCallbacks.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Api\Validator;
4
5 use MediaWiki\Api\ApiBase;
6 use MediaWiki\Api\ApiMain;
7 use MediaWiki\Message\Converter as MessageConverter;
8 use Wikimedia\Message\DataMessageValue;
9 use Wikimedia\ParamValidator\Callbacks;
10 use Wikimedia\ParamValidator\Util\UploadedFile;
11
12 /**
13 * ParamValidator callbacks for the Action API
14 * @since 1.35
15 * @ingroup API
16 */
17 class ApiParamValidatorCallbacks implements Callbacks {
18
19 /** @var ApiMain */
20 private $apiMain;
21
22 /** @var MessageConverter */
23 private $messageConverter;
24
25 /**
26 * @internal
27 * @param ApiMain $main
28 */
29 public function __construct( ApiMain $main ) {
30 $this->apiMain = $main;
31 $this->messageConverter = new MessageConverter();
32 }
33
34 public function hasParam( $name, array $options ) {
35 return $this->apiMain->getCheck( $name );
36 }
37
38 public function getValue( $name, $default, array $options ) {
39 $value = $this->apiMain->getVal( $name, $default );
40 $request = $this->apiMain->getRequest();
41 $rawValue = $request->getRawVal( $name );
42
43 if ( $options['raw'] ?? false ) {
44 // Bypass NFC normalization
45 return $rawValue;
46 }
47 if ( is_string( $rawValue ) ) {
48 // Preserve U+001F for multi-values
49 if ( substr( $rawValue, 0, 1 ) === "\x1f" ) {
50 // This loses the potential checkTitleEncoding() transformation done by
51 // WebRequest for $_GET. Let's call that a feature.
52 $value = implode( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
53 }
54
55 // Check for NFC normalization, and warn
56 if ( $rawValue !== $value ) {
57 $options['module']->handleParamNormalization( $name, $value, $rawValue );
58 }
59 }
60
61 return $value;
62 }
63
64 public function hasUpload( $name, array $options ) {
65 return $this->getUploadedFile( $name, $options ) !== null;
66 }
67
68 public function getUploadedFile( $name, array $options ) {
69 $upload = $this->apiMain->getUpload( $name );
70 if ( !$upload->exists() ) {
71 return null;
72 }
73 return new UploadedFile( [
74 'error' => $upload->getError(),
75 'tmp_name' => $upload->getTempName(),
76 'size' => $upload->getSize(),
77 'name' => $upload->getName(),
78 'type' => $upload->getType(),
79 ] );
80 }
81
82 public function recordCondition(
83 DataMessageValue $message, $name, $value, array $settings, array $options
84 ) {
85 /** @var ApiBase $module */
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 $this->messageConverter->convertMessageValue( $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 $this->messageConverter->convertMessageValue( $message ),
131 $message->getCode(),
132 $message->getData()
133 );
134 break;
135 }
136 }
137
138 public function useHighLimits( array $options ) {
139 return $this->apiMain->canApiHighLimits();
140 }
141
142 }