Merge "rdbms: make transaction rounds apply DBO_TRX to DB_REPLICA connections"
[mediawiki.git] / includes / api / Validator / ApiParamValidatorCallbacks.php
blob5be4464eced6fbfe90f598a5261370dabb5350a8
1 <?php
3 namespace MediaWiki\Api\Validator;
5 use MediaWiki\Api\ApiBase;
6 use MediaWiki\Api\ApiMain;
7 use Wikimedia\Message\DataMessageValue;
8 use Wikimedia\ParamValidator\Callbacks;
9 use Wikimedia\ParamValidator\Util\UploadedFile;
11 /**
12 * ParamValidator callbacks for the Action API
13 * @since 1.35
14 * @ingroup API
16 class ApiParamValidatorCallbacks implements Callbacks {
18 /** @var ApiMain */
19 private $apiMain;
21 /**
22 * @internal
23 * @param ApiMain $main
25 public function __construct( ApiMain $main ) {
26 $this->apiMain = $main;
29 public function hasParam( $name, array $options ) {
30 return $this->apiMain->getCheck( $name );
33 public function getValue( $name, $default, array $options ) {
34 $value = $this->apiMain->getVal( $name, $default );
35 $request = $this->apiMain->getRequest();
36 $rawValue = $request->getRawVal( $name );
38 if ( $options['raw'] ?? false ) {
39 // Bypass NFC normalization
40 return $rawValue;
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 ) ) );
50 // Check for NFC normalization, and warn
51 if ( $rawValue !== $value ) {
52 $options['module']->handleParamNormalization( $name, $value, $rawValue );
56 return $value;
59 public function hasUpload( $name, array $options ) {
60 return $this->getUploadedFile( $name, $options ) !== null;
63 public function getUploadedFile( $name, array $options ) {
64 $upload = $this->apiMain->getUpload( $name );
65 if ( !$upload->exists() ) {
66 return null;
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 ] );
77 public function recordCondition(
78 DataMessageValue $message, $name, $value, array $settings, array $options
79 ) {
80 /** @var ApiBase $module */
81 $module = $options['module'];
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
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;
112 $module->addDeprecation(
113 $message,
114 $feature,
115 $message->getData()
117 break;
119 case 'param-sensitive': // @codeCoverageIgnore
120 $module->getMain()->markParamsSensitive( $name );
121 break;
123 default:
124 $module->addWarning(
125 $message,
126 $message->getCode(),
127 $message->getData()
129 break;
133 public function useHighLimits( array $options ) {
134 return $this->apiMain->canApiHighLimits();