3 namespace MediaWiki\Rest
;
5 abstract class Handler
{
9 /** @var RequestInterface */
15 /** @var ResponseFactory */
16 private $responseFactory;
19 * Initialise with dependencies from the Router. This is called after construction.
22 public function init( Router
$router, RequestInterface
$request, array $config,
23 ResponseFactory
$responseFactory
25 $this->router
= $router;
26 $this->request
= $request;
27 $this->config
= $config;
28 $this->responseFactory
= $responseFactory;
32 * Get the Router. The return type declaration causes it to raise
33 * a fatal error if init() has not yet been called.
35 protected function getRouter(): Router
{
40 * Get the current request. The return type declaration causes it to raise
41 * a fatal error if init() has not yet been called.
43 * @return RequestInterface
45 public function getRequest(): RequestInterface
{
46 return $this->request
;
50 * Get the configuration array for the current route. The return type
51 * declaration causes it to raise a fatal error if init() has not
56 public function getConfig(): array {
61 * Get the ResponseFactory which can be used to generate Response objects.
62 * This will raise a fatal error if init() has not been
65 * @return ResponseFactory
67 public function getResponseFactory(): ResponseFactory
{
68 return $this->responseFactory
;
72 * The subclass should override this to provide the maximum last modified
73 * timestamp for the current request. This is called before execute() in
74 * order to decide whether to send a 304.
76 * The timestamp can be in any format accepted by ConvertibleTimestamp, or
77 * null to indicate that the timestamp is unknown.
79 * @return bool|string|int|float|\DateTime|null
81 protected function getLastModified() {
86 * The subclass should override this to provide an ETag for the current
87 * request. This is called before execute() in order to decide whether to
90 * See RFC 7232 ยง 2.3 for semantics.
94 protected function getETag() {
99 * Indicates whether this route requires read rights.
101 * The handler should override this if it does not need to read from the
102 * wiki. This is uncommon, but may be useful for login and other account
107 public function needsReadAccess() {
112 * Indicates whether this route requires write access.
114 * The handler should override this if the route does not need to write to
117 * This should return true for routes that may require synchronous database writes.
118 * Modules that do not need such writes should also not rely on master database access,
119 * since only read queries are needed and each master DB is a single point of failure.
123 public function needsWriteAccess() {
128 * Execute the handler. This is called after parameter validation. The
129 * return value can either be a Response or any type accepted by
130 * ResponseFactory::createFromReturnValue().
132 * To automatically construct an error response, execute() should throw a
133 * RestException. Such exceptions will not be logged like a normal exception.
135 * If execute() throws any other kind of exception, the exception will be
136 * logged and a generic 500 error page will be shown.
140 abstract public function execute();