| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
use HttpStatus; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Language\LanguageCode; |
| 8 |
use MWExceptionHandler; |
| 9 |
use stdClass; |
| 10 |
use Throwable; |
| 11 |
use Wikimedia\Message\ITextFormatter; |
| 12 |
use Wikimedia\Message\MessageValue; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ResponseFactory { |
| 18 |
private const CT_HTML = 'text/html; charset=utf-8'; |
| 19 |
private const CT_JSON = 'application/json'; |
| 20 |
|
| 21 |
|
| 22 |
private $textFormatters; |
| 23 |
|
| 24 |
|
| 25 |
private $showExceptionDetails = false; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __construct( $textFormatters ) { |
| 34 |
$this->textFormatters = $textFormatters; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function setShowExceptionDetails( bool $showExceptionDetails ): void { |
| 45 |
$this->showExceptionDetails = $showExceptionDetails; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function encodeJson( $value ) { |
| 56 |
$json = json_encode( $value, |
| 57 |
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE ); |
| 58 |
if ( $json === false ) { |
| 59 |
throw new JsonEncodingException( json_last_error_msg(), json_last_error() ); |
| 60 |
} |
| 61 |
return $json; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function create() { |
| 70 |
return new Response(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function createJson( $value, $contentType = null ) { |
| 81 |
$contentType ??= self::CT_JSON; |
| 82 |
$response = new Response( $this->encodeJson( $value ) ); |
| 83 |
$response->setHeader( 'Content-Type', $contentType ); |
| 84 |
return $response; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function createNoContent() { |
| 98 |
$response = new Response(); |
| 99 |
$response->setStatus( 204 ); |
| 100 |
return $response; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function createPermanentRedirect( $target ) { |
| 113 |
$response = $this->createRedirect( $target, 301 ); |
| 114 |
return $response; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function createLegacyTemporaryRedirect( $target ) { |
| 128 |
$response = $this->createRedirect( $target, 302 ); |
| 129 |
return $response; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function createRedirect( $target, $code ) { |
| 142 |
$response = $this->createRedirectBase( $target ); |
| 143 |
$response->setStatus( $code ); |
| 144 |
return $response; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function createTemporaryRedirect( $target ) { |
| 156 |
$response = $this->createRedirect( $target, 307 ); |
| 157 |
return $response; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function createSeeOther( $target ) { |
| 169 |
$response = $this->createRedirect( $target, 303 ); |
| 170 |
return $response; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function createNotModified() { |
| 184 |
$response = new Response(); |
| 185 |
$response->setStatus( 304 ); |
| 186 |
return $response; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function createHttpError( $errorCode, array $bodyData = [] ) { |
| 196 |
if ( $errorCode < 400 || $errorCode >= 600 ) { |
| 197 |
throw new InvalidArgumentException( 'error code must be 4xx or 5xx' ); |
| 198 |
} |
| 199 |
$response = $this->createJson( $bodyData + [ |
| 200 |
'httpCode' => $errorCode, |
| 201 |
'httpReason' => HttpStatus::getMessage( $errorCode ) |
| 202 |
] ); |
| 203 |
|
| 204 |
$response->setStatus( $errorCode ); |
| 205 |
return $response; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function createLocalizedHttpError( |
| 218 |
$errorCode, |
| 219 |
MessageValue $messageValue, |
| 220 |
array $extraData = [] |
| 221 |
) { |
| 222 |
return $this->createHttpError( |
| 223 |
$errorCode, |
| 224 |
array_merge( $extraData, $this->formatMessage( $messageValue ) ) |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function createFromException( Throwable $exception, array $extraData = [] ) { |
| 236 |
if ( $exception instanceof LocalizedHttpException ) { |
| 237 |
$response = $this->createLocalizedHttpError( |
| 238 |
$exception->getCode(), |
| 239 |
$exception->getMessageValue(), |
| 240 |
$exception->getErrorData() + $extraData + [ |
| 241 |
'errorKey' => $exception->getErrorKey(), |
| 242 |
] |
| 243 |
); |
| 244 |
} elseif ( $exception instanceof ResponseException ) { |
| 245 |
return $exception->getResponse(); |
| 246 |
} elseif ( $exception instanceof RedirectException ) { |
| 247 |
$response = $this->createRedirect( $exception->getTarget(), $exception->getCode() ); |
| 248 |
} elseif ( $exception instanceof HttpException ) { |
| 249 |
if ( in_array( $exception->getCode(), [ 204, 304 ], true ) ) { |
| 250 |
$response = $this->create(); |
| 251 |
$response->setStatus( $exception->getCode() ); |
| 252 |
} else { |
| 253 |
$response = $this->createHttpError( |
| 254 |
$exception->getCode(), |
| 255 |
array_merge( |
| 256 |
[ 'message' => $exception->getMessage() ], |
| 257 |
$exception->getErrorData() |
| 258 |
) |
| 259 |
); |
| 260 |
} |
| 261 |
} elseif ( $this->showExceptionDetails ) { |
| 262 |
$response = $this->createHttpError( 500, [ |
| 263 |
'message' => 'Error: exception of type ' . get_class( $exception ) . ': ' |
| 264 |
. $exception->getMessage(), |
| 265 |
'exception' => MWExceptionHandler::getStructuredExceptionData( |
| 266 |
$exception, |
| 267 |
MWExceptionHandler::CAUGHT_BY_OTHER |
| 268 |
) |
| 269 |
] ); |
| 270 |
|
| 271 |
|
| 272 |
} else { |
| 273 |
$response = $this->createHttpError( 500, [ |
| 274 |
'message' => 'Error: exception of type ' . get_class( $exception ), |
| 275 |
] ); |
| 276 |
} |
| 277 |
return $response; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function createFromReturnValue( $value ) { |
| 288 |
$originalValue = $value; |
| 289 |
if ( is_scalar( $value ) ) { |
| 290 |
$data = [ 'value' => $value ]; |
| 291 |
} elseif ( is_array( $value ) || $value instanceof stdClass ) { |
| 292 |
$data = $value; |
| 293 |
} else { |
| 294 |
$type = get_debug_type( $originalValue ); |
| 295 |
throw new InvalidArgumentException( __METHOD__ . ": Invalid return value type $type" ); |
| 296 |
} |
| 297 |
$response = $this->createJson( $data ); |
| 298 |
return $response; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
protected function createRedirectBase( $target ) { |
| 307 |
$response = new Response( $this->getHyperLink( $target ) ); |
| 308 |
$response->setHeader( 'Content-Type', self::CT_HTML ); |
| 309 |
$response->setHeader( 'Location', $target ); |
| 310 |
return $response; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
protected function getHyperLink( $url ) { |
| 320 |
$url = htmlspecialchars( $url, ENT_COMPAT ); |
| 321 |
return "<!doctype html><title>Redirect</title><a href=\"$url\">$url</a>"; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function formatMessage( MessageValue $messageValue ): array { |
| 335 |
if ( !$this->textFormatters ) { |
| 336 |
|
| 337 |
return []; |
| 338 |
} |
| 339 |
$translations = []; |
| 340 |
foreach ( $this->textFormatters as $formatter ) { |
| 341 |
$lang = LanguageCode::bcp47( $formatter->getLangCode() ); |
| 342 |
$messageText = $formatter->format( $messageValue ); |
| 343 |
$translations[$lang] = $messageText; |
| 344 |
} |
| 345 |
return [ 'messageTranslations' => $translations ]; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function getFormattedMessage( |
| 362 |
MessageValue $messageValue, string $preferredlang = '' |
| 363 |
): string { |
| 364 |
$strings = $this->formatMessage( $messageValue ); |
| 365 |
if ( !$strings ) { |
| 366 |
return $messageValue->getKey(); |
| 367 |
} |
| 368 |
|
| 369 |
$strings = $strings['messageTranslations']; |
| 370 |
if ( $preferredlang && array_key_exists( $preferredlang, $strings ) ) { |
| 371 |
return $strings[ $preferredlang ]; |
| 372 |
} else { |
| 373 |
return reset( $strings ); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public static function getResponseComponents(): array { |
| 388 |
return [ |
| 389 |
'responses' => [ |
| 390 |
'GenericErrorResponse' => [ |
| 391 |
'description' => 'Generic error response', |
| 392 |
'content' => [ |
| 393 |
'application/json' => [ |
| 394 |
'schema' => [ |
| 395 |
'$ref' => '#/components/schemas/GenericErrorResponseModel' |
| 396 |
] |
| 397 |
], |
| 398 |
], |
| 399 |
] |
| 400 |
], |
| 401 |
'schemas' => [ |
| 402 |
'GenericErrorResponseModel' => [ |
| 403 |
'description' => 'Generic error response body', |
| 404 |
'required' => [ 'httpCode', 'httpMessage' ], |
| 405 |
'properties' => [ |
| 406 |
'httpCode' => [ |
| 407 |
'type' => 'integer' |
| 408 |
], |
| 409 |
'httpMessage' => [ |
| 410 |
'type' => 'string' |
| 411 |
], |
| 412 |
'message' => [ |
| 413 |
'type' => 'string' |
| 414 |
], |
| 415 |
'messageTranslations' => [ |
| 416 |
'type' => 'object', |
| 417 |
'additionalProperties' => [ |
| 418 |
'type' => 'string' |
| 419 |
] |
| 420 |
], |
| 421 |
] |
| 422 |
] |
| 423 |
], |
| 424 |
]; |
| 425 |
} |
| 426 |
|
| 427 |
} |