File: RedirectHandler.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (49 lines) | stat: -rw-r--r-- 1,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace MediaWiki\Rest\Handler;

use MediaWiki\Config\ConfigException;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\RouteDefinitionException;

/**
 * A generic redirect handler for the REST API.
 *
 * To declare a redirect in a route file, use the following structure:
 * @code
 * {
 *     "path": "/path/to/trigger/a/redirect/{foo}",
 *     "redirect": {
 *         "path": "/redirect/target/{foo}",
 *         "code": 302
 *     }
 * }
 * @endcode
 *
 * It is not necessary to specify the handler class.
 * The default status code is 308.
 * Path parameters and query parameters will be looped through.
 *
 * @since 1.43
 * @package MediaWiki\Rest\Handler
 */
class RedirectHandler extends Handler {

	/**
	 * @return Response
	 * @throws ConfigException
	 */
	public function execute() {
		$path = $this->getConfig()['redirect']['path'] ?? '';
		if ( $path === '' ) {
			throw new RouteDefinitionException( 'No registered redirect for this path' );
		}
		$code = $this->getConfig()['redirect']['code'] ?? 308;
		$pathParams = $this->getRequest()->getPathParams();
		$queryParams = $this->getRequest()->getQueryParams();
		$locationPath = $this->getRouter()->getRoutePath( $path, $pathParams, $queryParams );
		$response = $this->getResponseFactory()->createRedirect( $locationPath, $code );
		return $response;
	}
}