Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ApiRestHelper | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| getStatusForErrorMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResponseBodySchema | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| getResponseBodySchemaFileName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Api; |
| 4 | |
| 5 | use RuntimeException; |
| 6 | |
| 7 | /** |
| 8 | * Helper for action API modules that support the REST paradigm. |
| 9 | * This is used as glue by the ActionModuleBasedHandler in the REST framework |
| 10 | * to help with translating an action API result to a REST response. |
| 11 | * |
| 12 | * @unstable since 1.47 |
| 13 | */ |
| 14 | abstract class ApiRestHelper { |
| 15 | |
| 16 | /** |
| 17 | * Returns the HTTP status code that should be returned to the client along |
| 18 | * with the given message. If 0 is returned, it is left to |
| 19 | * ActionModuleBasedHandler to determine the status code. This will generally |
| 20 | * result in status 400 being returned, except for some well known kinds |
| 21 | * of errors such as 'badtoken' (401) or 'ratelimited' (429). |
| 22 | * |
| 23 | * @stable to override |
| 24 | */ |
| 25 | public function getStatusForErrorMessage( IApiMessage $msg ): int { |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Returns an OpenAPI Schema Object specification structure as an associative array. |
| 31 | * @see \MediaWiki\Rest\Handler::getResponseBodySchema() for details. |
| 32 | * |
| 33 | * @note This method does not take a $method parameter like its pendant in |
| 34 | * Handler, because action API modules don't vary behavior on method. |
| 35 | * REST Handlers can (though probably shouldn't either). |
| 36 | * |
| 37 | * @stable to override |
| 38 | * @return ?array |
| 39 | */ |
| 40 | public function getResponseBodySchema(): ?array { |
| 41 | $fileName = $this->getResponseBodySchemaFileName(); |
| 42 | |
| 43 | if ( $fileName === null ) { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | // NOTE: Below is the same code as in Module::loadJsonFile, but we |
| 48 | // don't want to depend on that here. |
| 49 | $json = file_get_contents( $fileName ); |
| 50 | if ( $json === false ) { |
| 51 | // XXX: What kind of exception should we throw? The equivalent code in |
| 52 | // Handler uses ModuleConfigurationException, but we shouldn't depend |
| 53 | // on code from the REST framework here. |
| 54 | throw new RuntimeException( "Failed to load file `$fileName`" ); |
| 55 | } |
| 56 | |
| 57 | $spec = json_decode( $json, true ); |
| 58 | |
| 59 | if ( !is_array( $spec ) ) { |
| 60 | throw new RuntimeException( |
| 61 | "Failed to parse `$fileName` as a JSON object" |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | return $spec; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the absolute path of a JSON file containing an OpenAPI Schema |
| 70 | * Object specification structure describing the response body. |
| 71 | * |
| 72 | * @see \MediaWiki\Rest\Handler::getResponseBodySchema() for details. |
| 73 | * @return ?string |
| 74 | */ |
| 75 | public function getResponseBodySchemaFileName(): ?string { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | } |