1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
<?php
namespace MediaWiki\Api\Validator;
use MediaWiki\Api\ApiBase;
use MediaWiki\Api\ApiMain;
use MediaWiki\Message\Converter as MessageConverter;
use Wikimedia\Message\DataMessageValue;
use Wikimedia\ParamValidator\Callbacks;
use Wikimedia\ParamValidator\Util\UploadedFile;
/**
* ParamValidator callbacks for the Action API
* @since 1.35
* @ingroup API
*/
class ApiParamValidatorCallbacks implements Callbacks {
/** @var ApiMain */
private $apiMain;
/** @var MessageConverter */
private $messageConverter;
/**
* @internal
* @param ApiMain $main
*/
public function __construct( ApiMain $main ) {
$this->apiMain = $main;
$this->messageConverter = new MessageConverter();
}
public function hasParam( $name, array $options ) {
return $this->apiMain->getCheck( $name );
}
public function getValue( $name, $default, array $options ) {
$value = $this->apiMain->getVal( $name, $default );
$request = $this->apiMain->getRequest();
$rawValue = $request->getRawVal( $name );
if ( $options['raw'] ?? false ) {
// Bypass NFC normalization
return $rawValue;
}
if ( is_string( $rawValue ) ) {
// Preserve U+001F for multi-values
if ( substr( $rawValue, 0, 1 ) === "\x1f" ) {
// This loses the potential checkTitleEncoding() transformation done by
// WebRequest for $_GET. Let's call that a feature.
$value = implode( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
}
// Check for NFC normalization, and warn
if ( $rawValue !== $value ) {
$options['module']->handleParamNormalization( $name, $value, $rawValue );
}
}
return $value;
}
public function hasUpload( $name, array $options ) {
return $this->getUploadedFile( $name, $options ) !== null;
}
public function getUploadedFile( $name, array $options ) {
$upload = $this->apiMain->getUpload( $name );
if ( !$upload->exists() ) {
return null;
}
return new UploadedFile( [
'error' => $upload->getError(),
'tmp_name' => $upload->getTempName(),
'size' => $upload->getSize(),
'name' => $upload->getName(),
'type' => $upload->getType(),
] );
}
public function recordCondition(
DataMessageValue $message, $name, $value, array $settings, array $options
) {
/** @var ApiBase $module */
$module = $options['module'];
$code = $message->getCode();
switch ( $code ) {
case 'param-deprecated': // @codeCoverageIgnore
case 'deprecated-value': // @codeCoverageIgnore
if ( $code === 'param-deprecated' ) {
$feature = $name;
} else {
$feature = $name . '=' . $value;
$data = $message->getData() ?? [];
if ( isset( $data['💩'] ) ) {
// This is from an old-style Message. Strip out ParamValidator's added params.
unset( $data['💩'] );
$message = DataMessageValue::new(
$message->getKey(),
array_slice( $message->getParams(), 2 ),
$code,
$data
);
}
}
$m = $module;
while ( !$m->isMain() ) {
$p = $m->getParent();
$mName = $m->getModuleName();
$mParam = $p->encodeParamName( $p->getModuleManager()->getModuleGroup( $mName ) );
$feature = "{$mParam}={$mName}&{$feature}";
$m = $p;
}
$module->addDeprecation(
$this->messageConverter->convertMessageValue( $message ),
$feature,
$message->getData()
);
break;
case 'param-sensitive': // @codeCoverageIgnore
$module->getMain()->markParamsSensitive( $name );
break;
default:
$module->addWarning(
$this->messageConverter->convertMessageValue( $message ),
$message->getCode(),
$message->getData()
);
break;
}
}
public function useHighLimits( array $options ) {
return $this->apiMain->canApiHighLimits();
}
}
|