3 namespace MediaWiki\Rest;
6 use InvalidArgumentException;
18 private const CT_PLAIN =
'text/plain; charset=utf-8';
19 private const CT_HTML =
'text/html; charset=utf-8';
20 private const CT_JSON =
'application/json';
52 $json = json_encode( $value,
53 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE );
54 if ( $json ===
false ) {
76 public function createJson( $value, $contentType =
null ) {
77 $contentType = $contentType ?? self::CT_JSON;
78 $response =
new Response( $this->encodeJson( $value ) );
79 $response->setHeader(
'Content-Type', $contentType );
95 $response->setStatus( 204 );
109 $response = $this->createRedirectBase( $target );
110 $response->setStatus( 301 );
125 $response = $this->createRedirectBase( $target );
126 $response->setStatus( 302 );
139 $response = $this->createRedirectBase( $target );
140 $response->setStatus( 307 );
153 $response = $this->createRedirectBase( $target );
154 $response->setStatus( 303 );
170 $response->setStatus( 304 );
182 if ( $errorCode < 400 || $errorCode >= 600 ) {
183 throw new InvalidArgumentException(
'error code must be 4xx or 5xx' );
185 $response = $this->createJson( $bodyData + [
186 'httpCode' => $errorCode,
190 $response->setStatus( $errorCode );
206 array $extraData = []
208 return $this->createHttpError(
210 array_merge( $extraData, $this->formatMessage( $messageValue ) )
221 $response = $this->createLocalizedHttpError(
222 $exception->getCode(),
223 $exception->getMessageValue(),
224 (array)$exception->getErrorData()
227 return $exception->getResponse();
229 $response = $this->createRedirectBase( $exception->getTarget() );
230 $response->setStatus( $exception->getCode() );
232 if ( in_array( $exception->getCode(), [ 204, 304 ],
true ) ) {
233 $response = $this->create();
234 $response->setStatus( $exception->getCode() );
236 $response = $this->createHttpError(
237 $exception->getCode(),
239 [
'message' => $exception->getMessage() ],
240 (array)$exception->getErrorData()
245 $response = $this->createHttpError( 500, [
246 'message' =>
'Error: exception of type ' . get_class( $exception ) .
': '
247 . $exception->getMessage(),
250 MWExceptionHandler::CAUGHT_BY_OTHER,
251 $this->sendExceptionBacktrace
268 $originalValue = $value;
269 if ( is_scalar( $value ) ) {
270 $data = [
'value' => $value ];
271 } elseif ( is_array( $value ) || $value instanceof stdClass ) {
274 $type = gettype( $originalValue );
275 if (
$type ===
'object' ) {
276 $type = get_class( $originalValue );
278 throw new InvalidArgumentException( __METHOD__ .
": Invalid return value type $type" );
280 $response = $this->createJson( $data );
290 $response =
new Response( $this->getHyperLink( $target ) );
291 $response->setHeader(
'Content-Type', self::CT_HTML );
292 $response->setHeader(
'Location', $target );
303 $url = htmlspecialchars( $url, ENT_COMPAT );
304 return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>";
308 if ( !$this->textFormatters ) {
313 foreach ( $this->textFormatters as $formatter ) {
315 $messageText = $formatter->format( $messageValue );
316 $translations[
$lang] = $messageText;
318 return [
'messageTranslations' => $translations ];
static getMessage( $code)
Get the message associated with an HTTP response status code.
Methods for dealing with language codes.
static bcp47( $code)
Get the normalised IETF language tag See unit test for examples.
Handler class for MWExceptions.
static getStructuredExceptionData(Throwable $e, $catcher=self::CAUGHT_BY_OTHER, ?bool $includeBacktrace=null)
Get a structured representation of a Throwable.
if(!isset( $args[0])) $lang