|
MediaWiki master
|
Generates standardized response objects. More...
Public Member Functions | |
| __construct (array $textFormatters, ?ErrorFormatter $errorFormatter=null,) | |
| create () | |
| Create an unspecified response. | |
| createFromException (Throwable $exception, array $extraData=[]) | |
| Turn a throwable into a JSON error response. | |
| createFromReturnValue ( $value) | |
| Create a JSON response from an arbitrary value. | |
| createHttpError ( $errorCode, array $bodyData=[]) | |
| Create a HTTP 4xx or 5xx response. | |
| createJson ( $value, $contentType=null) | |
| Create a successful JSON response. | |
| createLegacyTemporaryRedirect ( $target) | |
| Creates a temporary (302) redirect. | |
| createLocalizedHttpError ( $errorCode, MessageSpecifier $messageValue, array $extraData=[]) | |
| Create an HTTP 4xx or 5xx response with error message localisation. | |
| createNoContent () | |
| Create a 204 (No Content) response, used to indicate that an operation which does not return anything (e.g. | |
| createNotModified () | |
| Create a 304 (Not Modified) response, used when the client has an up-to-date cached response. | |
| createPermanentRedirect ( $target) | |
| Creates a permanent (301) redirect. | |
| createRedirect ( $target, $code) | |
| Creates a redirect specifying the code. | |
| createSeeOther ( $target) | |
| Creates a See Other (303) redirect. | |
| createTemporaryRedirect ( $target) | |
| Creates a temporary (307) redirect. | |
| encodeJson ( $value) | |
| Encode a stdClass object or array to a JSON string. | |
| formatMessage (MessageSpecifier $messageValue) | |
| Tries to return the formatted string(s) for a message object using the response factory's text formatters. | |
| getFormattedMessage (MessageSpecifier $messageValue, string $preferredlang='') | |
| Tries to return one formatted string for a message object. | |
| getLangCodes () | |
| Returns an array of all language codes supported by this instance's text formatters, in fallback order. | |
| getResponseComponents () | |
| Returns OpenAPI schema response components object, providing information about the structure of some standard responses, for use in path specs. | |
Protected Member Functions | |
| createRedirectBase ( $target) | |
| Create a redirect response with type / response code unspecified. | |
| getHyperLink ( $url) | |
| Returns a minimal HTML document that links to the given URL, as suggested by RFC 7231 for 3xx responses. | |
Generates standardized response objects.
Definition at line 14 of file ResponseFactory.php.
| MediaWiki\Rest\ResponseFactory::__construct | ( | array | $textFormatters, |
| ?ErrorFormatter | $errorFormatter = null ) |
| ITextFormatter[] | $textFormatters | Only used to build a default ErrorFormatter when $errorFormatter is omitted; ignored otherwise. If there is a relative preference among the input text formatters, the formatters should be ordered from most to least preferred. |
| ErrorFormatter | null | $errorFormatter | Defaults to the legacy error shape if omitted, for backwards compatibility with callers constructing ResponseFactory directly. |
Definition at line 28 of file ResponseFactory.php.
| MediaWiki\Rest\ResponseFactory::create | ( | ) |
Create an unspecified response.
It is the caller's responsibility to set specifics like response code, content type etc.
Definition at line 56 of file ResponseFactory.php.
Referenced by createFromException().
| MediaWiki\Rest\ResponseFactory::createFromException | ( | Throwable | $exception, |
| array | $extraData = [] ) |
Turn a throwable into a JSON error response.
| Throwable | $exception | |
| array | $extraData | if present, used to generate a RESTbase-style response |
Definition at line 262 of file ResponseFactory.php.
References create(), createRedirect(), and MediaWiki\Rest\ResponseException\getResponse().
| MediaWiki\Rest\ResponseFactory::createFromReturnValue | ( | $value | ) |
Create a JSON response from an arbitrary value.
This is a fallback; it's preferable to use createJson() instead.
| mixed | $value | A structure containing only scalars, arrays and stdClass objects |
| InvalidArgumentException | When $value cannot be reasonably represented as JSON |
Definition at line 292 of file ResponseFactory.php.
References createJson().
| MediaWiki\Rest\ResponseFactory::createHttpError | ( | $errorCode, | |
| array | $bodyData = [] ) |
Create a HTTP 4xx or 5xx response.
| int | $errorCode | HTTP error code |
| array | $bodyData | An array of data to be included in the JSON response |
Definition at line 195 of file ResponseFactory.php.
| MediaWiki\Rest\ResponseFactory::createJson | ( | $value, | |
| $contentType = null ) |
Create a successful JSON response.
| array | stdClass | \\JsonSerializable | $value | JSON value |
| string | null | $contentType | HTTP content type (should be 'application/json+...') or null for plain 'application/json' |
Definition at line 67 of file ResponseFactory.php.
References encodeJson().
Referenced by createFromReturnValue().
| MediaWiki\Rest\ResponseFactory::createLegacyTemporaryRedirect | ( | $target | ) |
Creates a temporary (302) redirect.
HTTP 302 was underspecified and has been superseded by 303 (when the redirected request should be a GET, regardless of what the current request is) and 307 (when the method should not be changed), but might still be needed for HTTP 1.0 clients or to match legacy behavior.
| string | $target | Redirect target (an absolute URL) |
Definition at line 114 of file ResponseFactory.php.
References createRedirect().
| MediaWiki\Rest\ResponseFactory::createLocalizedHttpError | ( | $errorCode, | |
| MessageSpecifier | $messageValue, | ||
| array | $extraData = [] ) |
Create an HTTP 4xx or 5xx response with error message localisation.
| int | $errorCode | |
| MessageSpecifier | $messageValue | Prior to MediaWiki 1.47 this had to be a MessageValue |
| array | $extraData | An array of additional data to be included in the JSON response |
Definition at line 244 of file ResponseFactory.php.
| MediaWiki\Rest\ResponseFactory::createNoContent | ( | ) |
Create a 204 (No Content) response, used to indicate that an operation which does not return anything (e.g.
a PUT request) was successful.
Headers are generally interpreted to refer to the target of the operation. E.g. if this was a PUT request, the caller of this method might want to add an ETag header describing the created resource.
Definition at line 84 of file ResponseFactory.php.
| MediaWiki\Rest\ResponseFactory::createNotModified | ( | ) |
Create a 304 (Not Modified) response, used when the client has an up-to-date cached response.
Per RFC 7232 the response should contain all Cache-Control, Content-Location, Date, ETag, Expires, and Vary headers that would have been sent with the 200 OK answer if the requesting client did not have a valid cached response. This is the responsibility of the caller of this method.
Definition at line 170 of file ResponseFactory.php.
| MediaWiki\Rest\ResponseFactory::createPermanentRedirect | ( | $target | ) |
Creates a permanent (301) redirect.
This indicates that the caller of the API should update their indexes and call the new URL in the future. 301 redirects tend to get cached and are hard to undo. Client behavior for methods other than GET/HEAD is not well-defined and this type of response should be avoided in such cases.
| string | $target | Redirect target (an absolute URL) |
Definition at line 99 of file ResponseFactory.php.
References createRedirect().
| MediaWiki\Rest\ResponseFactory::createRedirect | ( | $target, | |
| $code ) |
Creates a redirect specifying the code.
This indicates that the operation the client was trying to perform can temporarily be achieved by using a different URL. Clients will preserve the request method when retrying the request with the new URL.
| string | $target | Redirect target |
| int | $code | Status code |
Definition at line 128 of file ResponseFactory.php.
References createRedirectBase().
Referenced by createFromException(), createLegacyTemporaryRedirect(), createPermanentRedirect(), createSeeOther(), and createTemporaryRedirect().
|
protected |
Create a redirect response with type / response code unspecified.
| string | $target | Redirect target (an absolute URL) |
Definition at line 311 of file ResponseFactory.php.
References getHyperLink().
Referenced by createRedirect().
| MediaWiki\Rest\ResponseFactory::createSeeOther | ( | $target | ) |
Creates a See Other (303) redirect.
This indicates that the target resource might be of interest to the client, without necessarily implying that it is the same resource. The client will always use GET (or HEAD) when following the redirection. Useful for GET-after-POST.
| string | $target | Redirect target (an absolute URL) |
Definition at line 155 of file ResponseFactory.php.
References createRedirect().
| MediaWiki\Rest\ResponseFactory::createTemporaryRedirect | ( | $target | ) |
Creates a temporary (307) redirect.
This indicates that the operation the client was trying to perform can temporarily be achieved by using a different URL. Clients will preserve the request method when retrying the request with the new URL.
| string | $target | Redirect target (an absolute URL) |
Definition at line 142 of file ResponseFactory.php.
References createRedirect().
| MediaWiki\Rest\ResponseFactory::encodeJson | ( | $value | ) |
Encode a stdClass object or array to a JSON string.
| array | stdClass | \\JsonSerializable | $value |
| JsonEncodingException |
Definition at line 42 of file ResponseFactory.php.
Referenced by createJson().
| MediaWiki\Rest\ResponseFactory::formatMessage | ( | MessageSpecifier | $messageValue | ) |
Tries to return the formatted string(s) for a message object using the response factory's text formatters.
The returned array will either be empty (if there are no text formatters), or have exactly one key, "messageTranslations", whose value is an array of formatted strings, keyed by the associated language code.
| MessageSpecifier | $messageValue | The message object to format. Prior to MediaWiki 1.47 this had to be a MessageValue. |
Definition at line 350 of file ResponseFactory.php.
References formatMessage(), and return.
Referenced by formatMessage(), and getFormattedMessage().
| MediaWiki\Rest\ResponseFactory::getFormattedMessage | ( | MessageSpecifier | $messageValue, |
| string | $preferredlang = '' ) |
Tries to return one formatted string for a message object.
Return value will be: 1) the formatted string for $preferredLang, if $preferredLang is supplied and the formatted string for that language is available. 2) the first available formatted string, if any are available. 3) the message key string, if no formatted strings are available. Callers who need more specific control should call formatMessage() instead.
| MessageSpecifier | $messageValue | The message object to format. Prior to MediaWiki 1.47 this had to be a MessageValue |
| string | $preferredlang | preferred language for the formatted string, if available |
Definition at line 368 of file ResponseFactory.php.
References formatMessage(), and Wikimedia\Message\MessageSpecifier\getKey().
|
protected |
Returns a minimal HTML document that links to the given URL, as suggested by RFC 7231 for 3xx responses.
| string | $url | An absolute URL |
Definition at line 324 of file ResponseFactory.php.
References $url.
Referenced by createRedirectBase().
| MediaWiki\Rest\ResponseFactory::getLangCodes | ( | ) |
Returns an array of all language codes supported by this instance's text formatters, in fallback order.
Useful for constructing cache keys.
Definition at line 335 of file ResponseFactory.php.
References getLangCodes(), and return.
Referenced by getLangCodes().
| MediaWiki\Rest\ResponseFactory::getResponseComponents | ( | ) |
Returns OpenAPI schema response components object, providing information about the structure of some standard responses, for use in path specs.
GenericErrorResponseModel is the schema of this factory's error format, see ErrorFormatter::getOpenApiSchema().
Definition at line 398 of file ResponseFactory.php.
References return.