MediaWiki master
MediaWiki\Rest\Module\SpecBasedModule Class Reference

A Module that is based on a module definition file similar to an OpenAPI spec. More...

Inherits MediaWiki\Rest\Module\MatcherBasedModule.

Collaboration diagram for MediaWiki\Rest\Module\SpecBasedModule:

Public Member Functions

 __construct (string $definitionFile, Router $router, string $pathPrefix, ResponseFactory $responseFactory, BasicAuthorizerInterface $basicAuth, ObjectFactory $objectFactory, Validator $restValidator, ErrorReporter $errorReporter)
 
 getDefinedPaths ()
 
 getOpenApiInfo ()
 Return an array with data to be included in an OpenAPI "info" object describing this module.
 
- Public Member Functions inherited from MediaWiki\Rest\Module\MatcherBasedModule
 findHandlerMatch (string $path, string $requestMethod)
 Determines which handler to use for the given path and returns an array describing the handler and initialization context.
Parameters
string$path
string$requestMethod
Returns
array<string,mixed>
  • bool "found": Whether a match was found. If true, the handler or spec field must be set.
  • Handler handler: the Handler object to use. Either "handler" or "spec" must be given.
  • array "spec":" an object spec for use with ObjectFactory - array "config": the route config, to be passed to Handler::initContext() - string "path": the path the handler is responsible for, including placeholders for path parameters. - string[] "params": path parameters, to be passed the Request::setPathPrams() - string[] "methods": supported methods, if the path is known but the method did not match. Only meaningful if "found" is false. To be used in the Allow header of a 405 response and included in CORS pre-flight.

 
 getAllowedMethods (string $relPath)
 Get the allowed methods for a path.
 
 getCacheData ()
 Return data that can later be used to initialize a new instance of this module in a fast and efficient way.
 
 initFromCacheData (array $cacheData)
 Initialize from the given cache data if possible.
 
- Public Member Functions inherited from MediaWiki\Rest\Module\Module
 __construct (Router $router, string $pathPrefix, ResponseFactory $responseFactory, BasicAuthorizerInterface $basicAuth, ObjectFactory $objectFactory, Validator $restValidator, ErrorReporter $errorReporter)
 
 execute (string $path, RequestInterface $request)
 Find the handler for a request and execute it.
 
 getHandlerForPath (string $path, RequestInterface $request, bool $initForExecute=false)
 Create a Handler for the given path, taking into account the request method.
 
 getModuleDescription ()
 Returns fields to be included when describing this module in the discovery document.
 
 getPathPrefix ()
 
 getRouter ()
 
 setCors (CorsUtils $cors)
 
 setStats (StatsFactory $stats)
 

Protected Member Functions

 getConfigHash ()
 Get a config version hash for cache invalidation.
 
 initRoutes ()
 Initialize matchers by calling addRoute() for each known route.
 
- Protected Member Functions inherited from MediaWiki\Rest\Module\MatcherBasedModule
 addRoute ( $method, string $path, array $info)
 
 getMatchers ()
 Get an array of PathMatcher objects indexed by HTTP method.
 
- Protected Member Functions inherited from MediaWiki\Rest\Module\Module
 executeHandler (Handler $handler)
 Execute a fully-constructed handler.
 
 instantiateHandlerObject (array $spec)
 Creates a handler from the given spec, but does not initialize it.
 
 throwNoMatch (string $path, string $method, array $allowed)
 Implementations of getHandlerForPath() should call this method when they cannot handle the requested path.
 

Additional Inherited Members

- Static Public Member Functions inherited from MediaWiki\Rest\Module\Module
static loadJsonFile (string $fileName)
 Loads a module specification from a file.
 
- Public Attributes inherited from MediaWiki\Rest\Module\Module
const CACHE_CONFIG_HASH_KEY = 'CONFIG-HASH'
 
- Protected Attributes inherited from MediaWiki\Rest\Module\Module
string $pathPrefix
 
ResponseFactory $responseFactory
 

Detailed Description

A Module that is based on a module definition file similar to an OpenAPI spec.

See also
docs/rest/mwapi-1.0.json for the schema of module definition files.

Just like an OpenAPI spec, the module definition file contains a "paths" section that maps paths and HTTP methods to operations. Each operation then specifies the PHP class that will handle the request under the "handler" key. The value of the "handler" key is an object spec for use with ObjectFactory::createObject.

The following fields are supported as a shorthand notation:

  • "redirect": the route represents a redirect and will be handled by the RedirectHandler class. The redirect is specified as a JSON object that specifies the target "path", and optional the redirect "code". If a redirect is defined, the "handler" key must be omitted.

More shorthands may be added in the future.

Route definitions can contain additional fields to configure the handler. The handler can access the route definition by calling getConfig().

Access: internal
Since
1.43

Definition at line 39 of file SpecBasedModule.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Rest\Module\SpecBasedModule::__construct ( string $definitionFile,
Router $router,
string $pathPrefix,
ResponseFactory $responseFactory,
BasicAuthorizerInterface $basicAuth,
ObjectFactory $objectFactory,
Validator $restValidator,
ErrorReporter $errorReporter )

Member Function Documentation

◆ getConfigHash()

MediaWiki\Rest\Module\SpecBasedModule::getConfigHash ( )
protected

Get a config version hash for cache invalidation.

Returns
string

Reimplemented from MediaWiki\Rest\Module\MatcherBasedModule.

Definition at line 79 of file SpecBasedModule.php.

◆ getDefinedPaths()

MediaWiki\Rest\Module\SpecBasedModule::getDefinedPaths ( )
Stability: unstable
for testing
Returns
array[]

Reimplemented from MediaWiki\Rest\Module\Module.

Definition at line 144 of file SpecBasedModule.php.

◆ getOpenApiInfo()

MediaWiki\Rest\Module\SpecBasedModule::getOpenApiInfo ( )

Return an array with data to be included in an OpenAPI "info" object describing this module.

See also
https://spec.openapis.org/oas/v3.0.0#info-object
Returns
array

Reimplemented from MediaWiki\Rest\Module\Module.

Definition at line 217 of file SpecBasedModule.php.

◆ initRoutes()

MediaWiki\Rest\Module\SpecBasedModule::initRoutes ( )
protected

Initialize matchers by calling addRoute() for each known route.

Reimplemented from MediaWiki\Rest\Module\MatcherBasedModule.

Definition at line 158 of file SpecBasedModule.php.

References $path.


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