2019-05-09 01:36:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Rest;
|
|
|
|
|
|
|
|
|
|
abstract class Handler {
|
2019-06-22 23:08:07 +00:00
|
|
|
/** @var Router */
|
|
|
|
|
private $router;
|
|
|
|
|
|
2019-05-09 01:36:18 +00:00
|
|
|
/** @var RequestInterface */
|
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
|
|
/** @var ResponseFactory */
|
|
|
|
|
private $responseFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialise with dependencies from the Router. This is called after construction.
|
2019-06-22 23:08:07 +00:00
|
|
|
* @internal
|
2019-05-09 01:36:18 +00:00
|
|
|
*/
|
2019-06-22 23:08:07 +00:00
|
|
|
public function init( Router $router, RequestInterface $request, array $config,
|
2019-05-09 01:36:18 +00:00
|
|
|
ResponseFactory $responseFactory
|
|
|
|
|
) {
|
2019-06-22 23:08:07 +00:00
|
|
|
$this->router = $router;
|
2019-05-09 01:36:18 +00:00
|
|
|
$this->request = $request;
|
|
|
|
|
$this->config = $config;
|
|
|
|
|
$this->responseFactory = $responseFactory;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-22 23:08:07 +00:00
|
|
|
/**
|
|
|
|
|
* Get the Router. The return type declaration causes it to raise
|
|
|
|
|
* a fatal error if init() has not yet been called.
|
|
|
|
|
*/
|
|
|
|
|
protected function getRouter(): Router {
|
|
|
|
|
return $this->router;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 01:36:18 +00:00
|
|
|
/**
|
|
|
|
|
* Get the current request. The return type declaration causes it to raise
|
|
|
|
|
* a fatal error if init() has not yet been called.
|
|
|
|
|
*
|
|
|
|
|
* @return RequestInterface
|
|
|
|
|
*/
|
|
|
|
|
public function getRequest(): RequestInterface {
|
|
|
|
|
return $this->request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the configuration array for the current route. The return type
|
|
|
|
|
* declaration causes it to raise a fatal error if init() has not
|
|
|
|
|
* been called.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig(): array {
|
|
|
|
|
return $this->config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the ResponseFactory which can be used to generate Response objects.
|
|
|
|
|
* This will raise a fatal error if init() has not been
|
|
|
|
|
* called.
|
|
|
|
|
*
|
|
|
|
|
* @return ResponseFactory
|
|
|
|
|
*/
|
|
|
|
|
public function getResponseFactory(): ResponseFactory {
|
|
|
|
|
return $this->responseFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The subclass should override this to provide the maximum last modified
|
|
|
|
|
* timestamp for the current request. This is called before execute() in
|
|
|
|
|
* order to decide whether to send a 304.
|
|
|
|
|
*
|
|
|
|
|
* The timestamp can be in any format accepted by ConvertibleTimestamp, or
|
|
|
|
|
* null to indicate that the timestamp is unknown.
|
|
|
|
|
*
|
|
|
|
|
* @return bool|string|int|float|\DateTime|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getLastModified() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The subclass should override this to provide an ETag for the current
|
|
|
|
|
* request. This is called before execute() in order to decide whether to
|
|
|
|
|
* send a 304.
|
|
|
|
|
*
|
|
|
|
|
* See RFC 7232 § 2.3 for semantics.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
protected function getETag() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 02:33:35 +00:00
|
|
|
/**
|
|
|
|
|
* Indicates whether this route requires read rights.
|
|
|
|
|
*
|
|
|
|
|
* The handler should override this if it does not need to read from the
|
|
|
|
|
* wiki. This is uncommon, but may be useful for login and other account
|
|
|
|
|
* management APIs.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function needsReadAccess() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates whether this route requires write access.
|
|
|
|
|
*
|
|
|
|
|
* The handler should override this if the route does not need to write to
|
|
|
|
|
* the database.
|
|
|
|
|
*
|
|
|
|
|
* This should return true for routes that may require synchronous database writes.
|
|
|
|
|
* Modules that do not need such writes should also not rely on master database access,
|
|
|
|
|
* since only read queries are needed and each master DB is a single point of failure.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function needsWriteAccess() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 01:36:18 +00:00
|
|
|
/**
|
|
|
|
|
* Execute the handler. This is called after parameter validation. The
|
|
|
|
|
* return value can either be a Response or any type accepted by
|
|
|
|
|
* ResponseFactory::createFromReturnValue().
|
|
|
|
|
*
|
|
|
|
|
* To automatically construct an error response, execute() should throw a
|
|
|
|
|
* RestException. Such exceptions will not be logged like a normal exception.
|
|
|
|
|
*
|
|
|
|
|
* If execute() throws any other kind of exception, the exception will be
|
|
|
|
|
* logged and a generic 500 error page will be shown.
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
abstract public function execute();
|
|
|
|
|
}
|