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
;
12 * ParamValidator callbacks for the Action API
16 class ApiParamValidatorCallbacks
implements Callbacks
{
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
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 );
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() ) {
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(),
77 public function recordCondition(
78 DataMessageValue
$message, $name, $value, array $settings, array $options
80 /** @var ApiBase $module */
81 $module = $options['module'];
83 $code = $message->getCode();
85 case 'param-deprecated': // @codeCoverageIgnore
86 case 'deprecated-value': // @codeCoverageIgnore
87 if ( $code === 'param-deprecated' ) {
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(
97 array_slice( $message->getParams(), 2 ),
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}";
112 $module->addDeprecation(
119 case 'param-sensitive': // @codeCoverageIgnore
120 $module->getMain()->markParamsSensitive( $name );
133 public function useHighLimits( array $options ) {
134 return $this->apiMain
->canApiHighLimits();