MediaWiki master
MediaWiki\Rest\Router Class Reference

The REST router is responsible for gathering module configuration, matching an input path against the defined modules, and constructing and executing the relevant module for a request. More...

Public Member Functions

 __construct (private readonly ModuleManager $moduleManager, private readonly array $extraRoutes, private readonly ServiceOptions $options, private readonly BagOStuff $cacheBag, private readonly array $textFormatters, private readonly bool $showExceptionDetails, private readonly BasicAuthorizerInterface $basicAuth, private readonly Authority $authority, private readonly ObjectFactory $objectFactory, private readonly Validator $restValidator, private ErrorReporter $errorReporter, private readonly HookContainer $hookContainer, private readonly Session $session, private readonly UrlUtils $urlUtils, private readonly JsonLocalizer $jsonLocalizer,)
 execute (RequestInterface $request)
 getModule (string $name)
 Returns an uninitialized module by name.
 getModuleBaseUrl (string $moduleId)
 Gets the absolute base URL for a given module ID.
 getModuleForPath (string $fullPath)
 Returns an uninitialized module by full path.
 getModuleForRequest (RequestInterface $request, ?string $name)
 Returns a module suitable for handling the given request.
 getModuleIds ()
 getModuleManager ()
 getModuleResponseFactoryByName (string $moduleName, RequestInterface $request)
 Like getModuleResponseFactory(), but for the module with the given name: the ResponseFactory that module would use for this request, with the error format selected by the module's errorSchemaVersion or, if the request asks for it, the RESTBase-compatible format (see isRestbaseCompatEnabled()).
 getModuleSpecUrl (string $moduleId)
 Gets the absolute OpenAPI specification URL for a given module ID.
 getPrivateRouteUrl (string $pathWithModulePrefix, array $pathParams=[], array $queryParams=[])
 getRoutePath (string $pathWithModulePrefix, array $pathParams=[], array $queryParams=[])
 getRouteUrl (string $pathWithModulePrefix, array $pathParams=[], array $queryParams=[])
 isRestbaseCompatEnabled (RequestInterface $request)
 prepareHandler (Handler $handler)
 Prepare the handler by injecting relevant service objects and state into $handler.
 setCors (CorsUtils $cors)
 setStats (StatsFactory $stats)

Static Public Member Functions

static substPathParams (string $route, array $pathParams)
 Substitute parameters into a template string, following the requirements for path parameters: Spaces are encoded as %20 (not +) and slashes are encoded as %2F, other characters with special meaning are encoded as they would be for query parameters.

Public Attributes

const CONSTRUCTOR_OPTIONS
const DEFAULT_ERROR_SCHEMA = '1.0'
const ROUTE_MODULE_SPEC = '/specs/v0/module/{module}'

Detailed Description

The REST router is responsible for gathering module configuration, matching an input path against the defined modules, and constructing and executing the relevant module for a request.

Definition at line 34 of file Router.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Rest\Router::__construct ( private readonly ModuleManager $moduleManager,
private readonly array $extraRoutes,
private readonly ServiceOptions $options,
private readonly BagOStuff $cacheBag,
private readonly array $textFormatters,
private readonly bool $showExceptionDetails,
private readonly BasicAuthorizerInterface $basicAuth,
private readonly Authority $authority,
private readonly ObjectFactory $objectFactory,
private readonly Validator $restValidator,
private ErrorReporter $errorReporter,
private readonly HookContainer $hookContainer,
private readonly Session $session,
private readonly UrlUtils $urlUtils,
private readonly JsonLocalizer $jsonLocalizer )
Parameters
ModuleManager$moduleManager
array[]$extraRoutes
ServiceOptions$options
BagOStuff$cacheBagA cache in which to store the matcher trees
array$textFormatters
bool$showExceptionDetails
BasicAuthorizerInterface$basicAuth
Authority$authority
ObjectFactory$objectFactory
Validator$restValidator
ErrorReporter$errorReporter
HookContainer$hookContainer
Session$session
UrlUtils$urlUtils
JsonLocalizer$jsonLocalizerUsed to localize the spec of the modules it instantiates.
Access: internal

Definition at line 111 of file Router.php.

References MediaWiki\MainConfigNames\CanonicalServer, MediaWiki\MainConfigNames\InternalServer, MediaWiki\MainConfigNames\RestPath, and MediaWiki\MainConfigNames\ScriptPath.

Member Function Documentation

◆ execute()

MediaWiki\Rest\Router::execute ( RequestInterface $request)

Definition at line 569 of file Router.php.

References $request, and try.

◆ getModule()

MediaWiki\Rest\Router::getModule ( string $name)

Returns an uninitialized module by name.

Note
To get a module that can be used for handling a request, use getModuleForRequest() instead.

Definition at line 422 of file Router.php.

References if.

Referenced by getModuleForPath().

◆ getModuleBaseUrl()

MediaWiki\Rest\Router::getModuleBaseUrl ( string $moduleId)

Gets the absolute base URL for a given module ID.

For external modules, UrlUtils expands any relative URL. For local modules, the route URL is generated from the module ID.

Parameters
string$moduleIdThe module ID
Returns
string|null The absolute base URL, or null if the module is unresolvable
Exceptions
UnexpectedValueExceptionIf an external module has no base URL configured
Since
1.47

Definition at line 493 of file Router.php.

◆ getModuleForPath()

MediaWiki\Rest\Router::getModuleForPath ( string $fullPath)

Returns an uninitialized module by full path.

Deprecated
since 1.47, use getModuleForRequest() instead.

Definition at line 373 of file Router.php.

References getModule(), and wfDeprecated().

◆ getModuleForRequest()

MediaWiki\Rest\Router::getModuleForRequest ( RequestInterface $request,
?string $name )

Returns a module suitable for handling the given request.

Parameters
RequestInterface$request
string | null$nameThe module name, if known. Will be derived from $request if not given.
Returns
Module|null
Since
since 1.47

Definition at line 390 of file Router.php.

References $request, and if.

◆ getModuleIds()

MediaWiki\Rest\Router::getModuleIds ( )
Returns
string[]

Definition at line 365 of file Router.php.

References return.

◆ getModuleManager()

MediaWiki\Rest\Router::getModuleManager ( )
Returns
ModuleManager

Definition at line 358 of file Router.php.

References return.

◆ getModuleResponseFactoryByName()

MediaWiki\Rest\Router::getModuleResponseFactoryByName ( string $moduleName,
RequestInterface $request )

Like getModuleResponseFactory(), but for the module with the given name: the ResponseFactory that module would use for this request, with the error format selected by the module's errorSchemaVersion or, if the request asks for it, the RESTBase-compatible format (see isRestbaseCompatEnabled()).

Access: internal
For use by ModuleSpecHandler

Definition at line 743 of file Router.php.

References $request, and return.

◆ getModuleSpecUrl()

MediaWiki\Rest\Router::getModuleSpecUrl ( string $moduleId)

Gets the absolute OpenAPI specification URL for a given module ID.

For external modules, UrlUtils expands the spec URL. For local modules, returns the default route URL defined by self::ROUTE_MODULE_SPEC.

Parameters
string$moduleIdThe module ID
Returns
string|null The absolute spec URL, or null if the module is unresolvable
Exceptions
UnexpectedValueExceptionIf an external module has no spec URL configured
Since
1.47

Definition at line 523 of file Router.php.

◆ getPrivateRouteUrl()

MediaWiki\Rest\Router::getPrivateRouteUrl ( string $pathWithModulePrefix,
array $pathParams = [],
array $queryParams = [] )

Definition at line 474 of file Router.php.

References getRoutePath(), and return.

◆ getRoutePath()

MediaWiki\Rest\Router::getRoutePath ( string $pathWithModulePrefix,
array $pathParams = [],
array $queryParams = [] )
Since
1.42
Todo
This should be called getRelativeRouteUrl() since query parameters are included

Definition at line 456 of file Router.php.

References $path, substPathParams(), and wfAppendQuery().

Referenced by getPrivateRouteUrl(), and getRouteUrl().

◆ getRouteUrl()

MediaWiki\Rest\Router::getRouteUrl ( string $pathWithModulePrefix,
array $pathParams = [],
array $queryParams = [] )

Definition at line 466 of file Router.php.

References getRoutePath(), and return.

◆ isRestbaseCompatEnabled()

MediaWiki\Rest\Router::isRestbaseCompatEnabled ( RequestInterface $request)
Access: internal
Returns
bool

Definition at line 701 of file Router.php.

References $request, and return.

◆ prepareHandler()

MediaWiki\Rest\Router::prepareHandler ( Handler $handler)

Prepare the handler by injecting relevant service objects and state into $handler.

Access: internal

Definition at line 635 of file Router.php.

References MediaWiki\Rest\Handler\initServices(), and MediaWiki\Rest\Handler\initSession().

◆ setCors()

MediaWiki\Rest\Router::setCors ( CorsUtils $cors)

Definition at line 646 of file Router.php.

◆ setStats()

MediaWiki\Rest\Router::setStats ( StatsFactory $stats)
Access: internal
Parameters
StatsFactory$stats
Returns
self

Definition at line 659 of file Router.php.

◆ substPathParams()

MediaWiki\Rest\Router::substPathParams ( string $route,
array $pathParams )
static

Substitute parameters into a template string, following the requirements for path parameters: Spaces are encoded as %20 (not +) and slashes are encoded as %2F, other characters with special meaning are encoded as they would be for query parameters.

Parameters
string$routeA path with {placeholders}
array$pathParams
Returns
string
Since
1.47 (was protected/internal before that)

Definition at line 559 of file Router.php.

Referenced by getRoutePath().

Member Data Documentation

◆ CONSTRUCTOR_OPTIONS

const MediaWiki\Rest\Router::CONSTRUCTOR_OPTIONS
Initial value:
= [
]
const EmergencyContact
Name constant for the EmergencyContact setting, for use with Config\get().
const CanonicalServer
Name constant for the CanonicalServer setting, for use with Config\get().
const Sitename
Name constant for the Sitename setting, for use with Config\get().
const ScriptPath
Name constant for the ScriptPath setting, for use with Config\get().
const RestPath
Name constant for the RestPath setting, for use with Config\get().
const InternalServer
Name constant for the InternalServer setting, for use with Config\get().
const RestTermsOfServiceUrl
Name constant for the RestTermsOfServiceUrl setting, for use with Config\get().
Access: internal

Definition at line 83 of file Router.php.

Referenced by MediaWiki\Rest\EntryPoint\createRouter().

◆ DEFAULT_ERROR_SCHEMA

const MediaWiki\Rest\Router::DEFAULT_ERROR_SCHEMA = '1.0'

Definition at line 39 of file Router.php.

◆ ROUTE_MODULE_SPEC

const MediaWiki\Rest\Router::ROUTE_MODULE_SPEC = '/specs/v0/module/{module}'

Definition at line 35 of file Router.php.


The documentation for this class was generated from the following file: