With the introduction of a REST API into MediaWiki core, we're going to want to share parameter validation logic rather than having similar code in both the Action API and the REST API. This abstracts out parameter validation logic as a library. There will be at least two follow-up patches: * One to add calls in the REST API, plus the interface for the REST API to do body validation. Should be reasonably straightforward. * One to adjust the Action API to use this. That'll be much less straightforward, as the Action API needs some MediaWiki-specific types (which the REST API might use too in the future) and needs to override the defaults on some of the library's checks (to maintain back-compat). Bug: T142080 Bug: T223239 Change-Id: I5c0cc3a8d686ace97596df5832c450a6a50f902c Depends-On: Iea05dc439688871c574c639e617765ae88a75ff7
79 lines
1.8 KiB
PHP
79 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Wikimedia\ParamValidator;
|
|
|
|
use Wikimedia\ParamValidator\Util\UploadedFile;
|
|
|
|
/**
|
|
* Simple Callbacks implementation for $_GET/$_POST/$_FILES data
|
|
*
|
|
* Options array keys used by this class:
|
|
* - 'useHighLimits': (bool) Return value from useHighLimits()
|
|
*
|
|
* @since 1.34
|
|
*/
|
|
class SimpleCallbacks implements Callbacks {
|
|
|
|
/** @var (string|string[])[] $_GET/$_POST data */
|
|
private $params;
|
|
|
|
/** @var (array|UploadedFile)[] $_FILES data or UploadedFile instances */
|
|
private $files;
|
|
|
|
/** @var array Any recorded conditions */
|
|
private $conditions = [];
|
|
|
|
/**
|
|
* @param (string|string[])[] $params Data from $_POST + $_GET
|
|
* @param array[] $files Data from $_FILES
|
|
*/
|
|
public function __construct( array $params, array $files = [] ) {
|
|
$this->params = $params;
|
|
$this->files = $files;
|
|
}
|
|
|
|
public function hasParam( $name, array $options ) {
|
|
return isset( $this->params[$name] );
|
|
}
|
|
|
|
public function getValue( $name, $default, array $options ) {
|
|
return $this->params[$name] ?? $default;
|
|
}
|
|
|
|
public function hasUpload( $name, array $options ) {
|
|
return isset( $this->files[$name] );
|
|
}
|
|
|
|
public function getUploadedFile( $name, array $options ) {
|
|
$file = $this->files[$name] ?? null;
|
|
if ( $file && !$file instanceof UploadedFile ) {
|
|
$file = new UploadedFile( $file );
|
|
$this->files[$name] = $file;
|
|
}
|
|
return $file;
|
|
}
|
|
|
|
public function recordCondition( ValidationException $condition, array $options ) {
|
|
$this->conditions[] = $condition;
|
|
}
|
|
|
|
/**
|
|
* Fetch any recorded conditions
|
|
* @return array[]
|
|
*/
|
|
public function getRecordedConditions() {
|
|
return $this->conditions;
|
|
}
|
|
|
|
/**
|
|
* Clear any recorded conditions
|
|
*/
|
|
public function clearRecordedConditions() {
|
|
$this->conditions = [];
|
|
}
|
|
|
|
public function useHighLimits( array $options ) {
|
|
return !empty( $options['useHighLimits'] );
|
|
}
|
|
|
|
}
|