repo/rest-api/src/RouteHandlers/AddItemStatementRouteHandler.php
<?php declare( strict_types=1 );
namespace Wikibase\Repo\RestApi\RouteHandlers;
use Exception;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MediaWikiServices;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\RequestInterface;
use MediaWiki\Rest\Response;
use MediaWiki\Rest\ResponseInterface;
use MediaWiki\Rest\SimpleHandler;
use MediaWiki\Rest\StringStream;
use MediaWiki\Rest\Validator\Validator;
use Wikibase\Repo\RestApi\Application\Serialization\StatementSerializer;
use Wikibase\Repo\RestApi\Application\UseCases\AddItemStatement\AddItemStatement;
use Wikibase\Repo\RestApi\Application\UseCases\AddItemStatement\AddItemStatementRequest;
use Wikibase\Repo\RestApi\Application\UseCases\AddItemStatement\AddItemStatementResponse;
use Wikibase\Repo\RestApi\Application\UseCases\ItemRedirect;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\AuthenticationMiddleware;
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\BotRightCheckMiddleware;
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\MiddlewareHandler;
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\TempUserCreationResponseHeaderMiddleware;
use Wikibase\Repo\RestApi\RouteHandlers\Middleware\UserAgentCheckMiddleware;
use Wikibase\Repo\RestApi\WbRestApi;
use Wikimedia\ParamValidator\ParamValidator;
/**
* @license GPL-2.0-or-later
*/
class AddItemStatementRouteHandler extends SimpleHandler {
use AssertValidTopLevelFields;
private const ITEM_ID_PATH_PARAM = 'item_id';
private const STATEMENT_BODY_PARAM = 'statement';
private const TAGS_BODY_PARAM = 'tags';
private const BOT_BODY_PARAM = 'bot';
private const COMMENT_BODY_PARAM = 'comment';
private AddItemStatement $addItemStatement;
private StatementSerializer $statementSerializer;
private ResponseFactory $responseFactory;
private MiddlewareHandler $middlewareHandler;
public function __construct(
AddItemStatement $addItemStatement,
StatementSerializer $statementSerializer,
ResponseFactory $responseFactory,
MiddlewareHandler $middlewareHandler
) {
$this->addItemStatement = $addItemStatement;
$this->statementSerializer = $statementSerializer;
$this->responseFactory = $responseFactory;
$this->middlewareHandler = $middlewareHandler;
}
public static function factory(): Handler {
$responseFactory = new ResponseFactory();
return new self(
WbRestApi::getAddItemStatement(),
WbRestApi::getStatementSerializer(),
$responseFactory,
new MiddlewareHandler( [
WbRestApi::getUnexpectedErrorHandlerMiddleware(),
new UserAgentCheckMiddleware(),
new AuthenticationMiddleware( MediaWikiServices::getInstance()->getUserIdentityUtils() ),
new BotRightCheckMiddleware( MediaWikiServices::getInstance()->getPermissionManager(), $responseFactory ),
WbRestApi::getPreconditionMiddlewareFactory()->newPreconditionMiddleware(
fn( RequestInterface $request ): string => $request->getPathParam( self::ITEM_ID_PATH_PARAM )
),
new TempUserCreationResponseHeaderMiddleware( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ),
] )
);
}
/**
* Preconditions are checked via {@link PreconditionMiddleware}
*/
public function checkPreconditions(): ?ResponseInterface {
return null;
}
/**
* @param mixed ...$args
*/
public function run( ...$args ): Response {
return $this->middlewareHandler->run( $this, [ $this, 'runUseCase' ], $args );
}
/**
* @throws Exception
*/
public function runUseCase( string $itemId ): Response {
$jsonBody = $this->getValidatedBody();
'@phan-var array $jsonBody'; // guaranteed to be an array per getBodyParamSettings()
try {
$useCaseResponse = $this->addItemStatement->execute(
new AddItemStatementRequest(
$itemId,
$jsonBody[self::STATEMENT_BODY_PARAM],
$jsonBody[self::TAGS_BODY_PARAM],
$jsonBody[self::BOT_BODY_PARAM],
$jsonBody[self::COMMENT_BODY_PARAM],
$this->getUsername()
)
);
return $this->newSuccessHttpResponse( $useCaseResponse, $itemId );
} catch ( ItemRedirect $e ) {
return $this->responseFactory->newErrorResponse(
UseCaseError::ITEM_REDIRECTED,
"Item $itemId has been redirected to {$e->getRedirectTargetId()}.",
[ UseCaseError::CONTEXT_REDIRECT_TARGET => $e->getRedirectTargetId() ]
);
} catch ( UseCaseError $e ) {
return $this->responseFactory->newErrorResponseFromException( $e );
}
}
public function validate( Validator $restValidator ): void {
$this->assertValidTopLevelTypes( $this->getRequest()->getParsedBody(), $this->getBodyParamSettings() );
parent::validate( $restValidator );
}
public function getParamSettings(): array {
return [
self::ITEM_ID_PATH_PARAM => [
self::PARAM_SOURCE => 'path',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => true,
],
];
}
public function getBodyParamSettings(): array {
return [
self::STATEMENT_BODY_PARAM => [
self::PARAM_SOURCE => 'body',
ParamValidator::PARAM_TYPE => /* object */ 'array',
ParamValidator::PARAM_REQUIRED => true,
],
self::TAGS_BODY_PARAM => [
self::PARAM_SOURCE => 'body',
ParamValidator::PARAM_TYPE => 'array',
ParamValidator::PARAM_REQUIRED => false,
ParamValidator::PARAM_DEFAULT => [],
],
self::BOT_BODY_PARAM => [
self::PARAM_SOURCE => 'body',
ParamValidator::PARAM_TYPE => 'boolean',
ParamValidator::PARAM_REQUIRED => false,
ParamValidator::PARAM_DEFAULT => false,
],
self::COMMENT_BODY_PARAM => [
self::PARAM_SOURCE => 'body',
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false,
],
];
}
private function newSuccessHttpResponse( AddItemStatementResponse $useCaseResponse, string $itemId ): Response {
$httpResponse = $this->getResponseFactory()->create();
$httpResponse->setStatus( 201 );
$httpResponse->setHeader( 'Content-Type', 'application/json' );
$httpResponse->setHeader(
'Last-Modified',
wfTimestamp( TS_RFC2822, $useCaseResponse->getLastModified() )
);
$this->setEtagFromRevId( $httpResponse, $useCaseResponse->getRevisionId() );
$this->setLocationHeader( $httpResponse, $itemId, (string)$useCaseResponse->getStatement()->getGuid() );
$httpResponse->setBody( new StringStream( json_encode(
$this->statementSerializer->serialize( $useCaseResponse->getStatement() )
) ) );
return $httpResponse;
}
private function setEtagFromRevId( Response $httpResponse, int $revId ): void {
$httpResponse->setHeader( 'ETag', "\"$revId\"" );
}
private function setLocationHeader( Response $httpResponse, string $itemId, string $statementGuid ): void {
$newStatementUrl = $this->getRouter()->getRouteUrl(
GetItemStatementRouteHandler::ROUTE,
[
GetItemStatementRouteHandler::ITEM_ID_PATH_PARAM => $itemId,
GetItemStatementRouteHandler::STATEMENT_ID_PATH_PARAM => $statementGuid,
]
);
$httpResponse->setHeader( 'Location', $newStatementUrl );
}
private function getUsername(): ?string {
$mwUser = $this->getAuthority()->getUser();
return $mwUser->isRegistered() ? $mwUser->getName() : null;
}
}