MediaWiki  master
Handler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
10 
11 abstract class Handler {
12 
17  public const PARAM_SOURCE = 'rest-param-source';
18 
20  private $router;
21 
23  private $request;
24 
26  private $config;
27 
30 
33 
35  private $validatedBody;
36 
39 
41  private $hookContainer;
42 
44  private $hookRunner;
45 
55  final public function init( Router $router, RequestInterface $request, array $config,
57  ) {
58  $this->router = $router;
59  $this->request = $request;
60  $this->config = $config;
61  $this->responseFactory = $responseFactory;
62  $this->hookContainer = $hookContainer;
63  $this->hookRunner = new HookRunner( $hookContainer );
64  $this->postInitSetup();
65  }
66 
72  protected function getRouter(): Router {
73  return $this->router;
74  }
75 
82  public function getRequest(): RequestInterface {
83  return $this->request;
84  }
85 
93  public function getConfig(): array {
94  return $this->config;
95  }
96 
104  public function getResponseFactory(): ResponseFactory {
105  return $this->responseFactory;
106  }
107 
116  public function validate( Validator $restValidator ) {
117  $validatedParams = $restValidator->validateParams( $this->getParamSettings() );
118  $validatedBody = $restValidator->validateBody( $this->request, $this );
119  $this->validatedParams = $validatedParams;
120  $this->validatedBody = $validatedBody;
121  }
122 
132  protected function getConditionalHeaderUtil() {
133  if ( $this->conditionalHeaderUtil === null ) {
134  $this->conditionalHeaderUtil = new ConditionalHeaderUtil;
135  $this->conditionalHeaderUtil->setValidators(
136  $this->getETag(),
137  $this->getLastModified(),
138  $this->hasRepresentation() );
139  }
141  }
142 
149  public function checkPreconditions() {
150  $status = $this->getConditionalHeaderUtil()->checkPreconditions( $this->getRequest() );
151  if ( $status ) {
152  $response = $this->getResponseFactory()->create();
153  $response->setStatus( $status );
154  return $response;
155  } else {
156  return null;
157  }
158  }
159 
168  $this->getConditionalHeaderUtil()->applyResponseHeaders( $response );
169  }
170 
180  public function getParamSettings() {
181  return [];
182  }
183 
189  public function getBodyValidator( $contentType ) {
190  return new NullBodyValidator();
191  }
192 
200  public function getValidatedParams() {
201  if ( $this->validatedParams === null ) {
202  throw new \RuntimeException( 'getValidatedParams() called before validate()' );
203  }
204  return $this->validatedParams;
205  }
206 
212  public function getValidatedBody() {
213  return $this->validatedBody;
214  }
215 
222  protected function getHookContainer() {
223  return $this->hookContainer;
224  }
225 
234  protected function getHookRunner() {
235  return $this->hookRunner;
236  }
237 
248  protected function getLastModified() {
249  return null;
250  }
251 
263  protected function getETag() {
264  return null;
265  }
266 
274  protected function hasRepresentation() {
275  return null;
276  }
277 
287  public function needsReadAccess() {
288  return true;
289  }
290 
303  public function needsWriteAccess() {
304  return true;
305  }
306 
311  protected function postInitSetup() {
312  }
313 
327  abstract public function execute();
328 }
MediaWiki\Rest\Handler\init
init(Router $router, RequestInterface $request, array $config, ResponseFactory $responseFactory, HookContainer $hookContainer)
Initialise with dependencies from the Router.
Definition: Handler.php:55
MediaWiki\Rest\ResponseFactory
Generates standardized response objects.
Definition: ResponseFactory.php:17
MediaWiki\Rest\Handler\$hookContainer
HookContainer $hookContainer
Definition: Handler.php:41
MediaWiki\Rest\Validator\Validator
Wrapper for ParamValidator.
Definition: Validator.php:31
MediaWiki\Rest\Handler\getResponseFactory
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:104
$response
$response
Definition: opensearch_desc.php:44
MediaWiki\Rest\Handler\getParamSettings
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: Handler.php:180
MediaWiki\Rest\Handler\getETag
getETag()
The subclass should override this to provide an ETag for the current request.
Definition: Handler.php:263
MediaWiki\Rest\Handler\$router
Router $router
Definition: Handler.php:20
MediaWiki\Rest\Handler\$request
RequestInterface $request
Definition: Handler.php:23
MediaWiki\Rest\Handler\PARAM_SOURCE
const PARAM_SOURCE
(string) ParamValidator constant to specify the source of the parameter.
Definition: Handler.php:17
MediaWiki\Rest\Handler\applyConditionalResponseHeaders
applyConditionalResponseHeaders(ResponseInterface $response)
Modify the response, adding Last-Modified and ETag headers as indicated the values previously returne...
Definition: Handler.php:167
MediaWiki\Rest\Handler\postInitSetup
postInitSetup()
The handler can override this to do any necessary setup after init() is called to inject the dependen...
Definition: Handler.php:311
MediaWiki\Rest\Handler\validate
validate(Validator $restValidator)
Validate the request parameters/attributes and body.
Definition: Handler.php:116
MediaWiki\Rest\Handler\execute
execute()
Execute the handler.
MediaWiki\Rest\Handler
Definition: Handler.php:11
MediaWiki\Rest\Validator\BodyValidator
Interface for validating a request body.
Definition: BodyValidator.php:11
MediaWiki\Rest\Handler\$validatedBody
mixed $validatedBody
Definition: Handler.php:35
MediaWiki\Rest\Router
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition: Router.php:21
MediaWiki\Rest\Validator\Validator\validateParams
validateParams(array $paramSettings)
Validate parameters.
Definition: Validator.php:96
MediaWiki\Rest\Handler\getHookContainer
getHookContainer()
Get a HookContainer, for running extension hooks or for hook metadata.
Definition: Handler.php:222
MediaWiki\Rest\Handler\checkPreconditions
checkPreconditions()
Check the conditional request headers and generate a response if appropriate.
Definition: Handler.php:149
MediaWiki\Rest\Handler\getValidatedBody
getValidatedBody()
Fetch the validated body.
Definition: Handler.php:212
MediaWiki\Rest\Handler\getValidatedParams
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:200
MediaWiki\Rest
MediaWiki\Rest\Handler\getConfig
getConfig()
Get the configuration array for the current route.
Definition: Handler.php:93
MediaWiki\Rest\Handler\getHookRunner
getHookRunner()
Get a HookRunner for running core hooks.
Definition: Handler.php:234
MediaWiki\Rest\Handler\$config
array $config
Definition: Handler.php:26
MediaWiki\Rest\ResponseInterface
An interface similar to PSR-7's ResponseInterface, the primary difference being that it is mutable.
Definition: ResponseInterface.php:39
MediaWiki\Rest\Handler\$conditionalHeaderUtil
ConditionalHeaderUtil $conditionalHeaderUtil
Definition: Handler.php:38
MediaWiki\Rest\Validator\NullBodyValidator
Do-nothing body validator.
Definition: NullBodyValidator.php:10
MediaWiki\Rest\Handler\needsWriteAccess
needsWriteAccess()
Indicates whether this route requires write access.
Definition: Handler.php:303
MediaWiki\Rest\Handler\$responseFactory
ResponseFactory $responseFactory
Definition: Handler.php:29
MediaWiki\Rest\ConditionalHeaderUtil
Definition: ConditionalHeaderUtil.php:9
MediaWiki\Rest\RequestInterface
A request interface similar to PSR-7's ServerRequestInterface.
Definition: RequestInterface.php:39
MediaWiki\Rest\Handler\getRequest
getRequest()
Get the current request.
Definition: Handler.php:82
MediaWiki\Rest\Handler\$hookRunner
HookRunner $hookRunner
Definition: Handler.php:44
MediaWiki\Rest\Handler\hasRepresentation
hasRepresentation()
The subclass should override this to indicate whether the resource exists.
Definition: Handler.php:274
MediaWiki\Rest\Validator\Validator\validateBody
validateBody(RequestInterface $request, Handler $handler)
Validate the body of a request.
Definition: Validator.php:128
MediaWiki\Rest\Handler\getLastModified
getLastModified()
The subclass should override this to provide the maximum last modified timestamp for the current requ...
Definition: Handler.php:248
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:44
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:23
MediaWiki\Rest\Handler\getRouter
getRouter()
Get the Router.
Definition: Handler.php:72
MediaWiki\Rest\Handler\getConditionalHeaderUtil
getConditionalHeaderUtil()
Get a ConditionalHeaderUtil object.
Definition: Handler.php:132
MediaWiki\Rest\Handler\$validatedParams
array null $validatedParams
Definition: Handler.php:32
MediaWiki\Rest\Handler\needsReadAccess
needsReadAccess()
Indicates whether this route requires read rights.
Definition: Handler.php:287
MediaWiki\Rest\Handler\getBodyValidator
getBodyValidator( $contentType)
Fetch the BodyValidator.
Definition: Handler.php:189
MediaWiki\Rest\ConditionalHeaderUtil\setValidators
setValidators( $eTag, $lastModified, $hasRepresentation)
Initialize the object with information about the requested resource.
Definition: ConditionalHeaderUtil.php:29