Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
RedirectHandler | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
execute | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Rest\Handler; |
4 | |
5 | use MediaWiki\Config\ConfigException; |
6 | use MediaWiki\Rest\Handler; |
7 | use MediaWiki\Rest\Response; |
8 | use MediaWiki\Rest\RouteDefinitionException; |
9 | |
10 | /** |
11 | * A generic redirect handler for the REST API. |
12 | * |
13 | * To declare a redirect in a route file, use the following structure: |
14 | * @code |
15 | * { |
16 | * "path": "/path/to/trigger/a/redirect/{foo}", |
17 | * "redirect": { |
18 | * "path": "/redirect/target/{foo}", |
19 | * "code": 302 |
20 | * } |
21 | * } |
22 | * @endcode |
23 | * |
24 | * It is not necessary to specify the handler class. |
25 | * The default status code is 308. |
26 | * Path parameters and query parameters will be looped through. |
27 | * |
28 | * @since 1.43 |
29 | * @package MediaWiki\Rest\Handler |
30 | */ |
31 | class RedirectHandler extends Handler { |
32 | |
33 | /** |
34 | * @return Response |
35 | * @throws ConfigException |
36 | */ |
37 | public function execute() { |
38 | $path = $this->getConfig()['redirect']['path'] ?? ''; |
39 | if ( $path === '' ) { |
40 | throw new RouteDefinitionException( 'No registered redirect for this path' ); |
41 | } |
42 | $code = $this->getConfig()['redirect']['code'] ?? 308; |
43 | $pathParams = $this->getRequest()->getPathParams(); |
44 | $queryParams = $this->getRequest()->getQueryParams(); |
45 | $locationPath = $this->getRouter()->getRoutePath( $path, $pathParams, $queryParams ); |
46 | $response = $this->getResponseFactory()->createRedirect( $locationPath, $code ); |
47 | return $response; |
48 | } |
49 | } |