Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| GetLexemeRouteHandler | |
0.00% |
0 / 49 |
|
0.00% |
0 / 9 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| factory | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| runUseCase | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| newSuccessHttpResponse | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| newRedirectHttpResponse | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| getParamSettings | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| needsWriteAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| checkPreconditions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Wikibase\Lexeme\MediaWiki\RestApi; |
| 6 | |
| 7 | use MediaWiki\MediaWikiServices; |
| 8 | use MediaWiki\Rest\Handler; |
| 9 | use MediaWiki\Rest\RequestInterface; |
| 10 | use MediaWiki\Rest\Response; |
| 11 | use MediaWiki\Rest\ResponseInterface; |
| 12 | use MediaWiki\Rest\SimpleHandler; |
| 13 | use Wikibase\Lexeme\Interactors\GetLexeme\GetLexeme; |
| 14 | use Wikibase\Lexeme\Interactors\GetLexeme\GetLexemeRequest; |
| 15 | use Wikibase\Lexeme\Interactors\GetLexeme\GetLexemeResponse; |
| 16 | use Wikibase\Lexeme\Interactors\GetLexeme\LexemeRedirect; |
| 17 | use Wikibase\Lexeme\Interactors\UseCaseError; |
| 18 | use Wikibase\Lexeme\Presentation\RestSerialization\LexemeSerializer; |
| 19 | use Wikibase\Lexeme\WikibaseLexemeServices; |
| 20 | use Wikibase\Repo\RestApi\Middleware\AuthenticationMiddleware; |
| 21 | use Wikibase\Repo\RestApi\Middleware\MiddlewareHandler; |
| 22 | use Wikibase\Repo\RestApi\Middleware\UserAgentCheckMiddleware; |
| 23 | use Wikimedia\ParamValidator\ParamValidator; |
| 24 | |
| 25 | /** |
| 26 | * @license GPL-2.0-or-later |
| 27 | */ |
| 28 | class GetLexemeRouteHandler extends SimpleHandler { |
| 29 | |
| 30 | public const ROUTE = '/wikibaselexeme/v0/entities/lexemes/{lexeme_id}'; |
| 31 | public const LEXEME_ID_PATH_PARAM = 'lexeme_id'; |
| 32 | |
| 33 | public function __construct( |
| 34 | private GetLexeme $getLexeme, |
| 35 | private MiddlewareHandler $middlewareHandler, |
| 36 | private LexemeSerializer $lexemeSerializer, |
| 37 | private ResponseFactory $responseFactory, |
| 38 | ) { |
| 39 | } |
| 40 | |
| 41 | public static function factory(): Handler { |
| 42 | return new self( |
| 43 | WikibaseLexemeServices::getGetLexeme(), |
| 44 | new MiddlewareHandler( [ |
| 45 | WikibaseLexemeServices::getUnexpectedErrorHandlerMiddleware(), |
| 46 | new UserAgentCheckMiddleware(), |
| 47 | new AuthenticationMiddleware( MediaWikiServices::getInstance()->getUserIdentityUtils() ), |
| 48 | WikibaseLexemeServices::getPreconditionMiddlewareFactory()->newPreconditionMiddleware( |
| 49 | fn ( RequestInterface $request ): string => $request->getPathParam( self::LEXEME_ID_PATH_PARAM ) |
| 50 | ), |
| 51 | ] ), |
| 52 | WikibaseLexemeServices::getLexemeSerializer(), |
| 53 | new ResponseFactory(), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public function run( string $lexemeId ): Response { |
| 58 | return $this->middlewareHandler->run( $this, fn () => $this->runUseCase( $lexemeId ) ); |
| 59 | } |
| 60 | |
| 61 | public function runUseCase( string $lexemeId ): Response { |
| 62 | try { |
| 63 | return $this->newSuccessHttpResponse( |
| 64 | $this->getLexeme->execute( new GetLexemeRequest( $lexemeId ) ) |
| 65 | ); |
| 66 | } catch ( LexemeRedirect $e ) { |
| 67 | return $this->newRedirectHttpResponse( $e ); |
| 68 | } catch ( UseCaseError $e ) { |
| 69 | return $this->responseFactory->newErrorResponseFromException( $e ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private function newSuccessHttpResponse( GetLexemeResponse $useCaseResponse ): Response { |
| 74 | return $this->responseFactory->newSuccessResponse( |
| 75 | json_encode( |
| 76 | $this->lexemeSerializer->serialize( $useCaseResponse->lexeme ), |
| 77 | JSON_UNESCAPED_SLASHES |
| 78 | ), |
| 79 | $useCaseResponse->revisionId, |
| 80 | $useCaseResponse->lastModified, |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | private function newRedirectHttpResponse( LexemeRedirect $e ): Response { |
| 85 | $httpResponse = $this->getResponseFactory()->create(); |
| 86 | $httpResponse->setHeader( |
| 87 | 'Location', |
| 88 | $this->getRouteUrl( |
| 89 | [ self::LEXEME_ID_PATH_PARAM => $e->redirectTarget->getSerialization() ], |
| 90 | $this->getRequest()->getQueryParams(), |
| 91 | ) |
| 92 | ); |
| 93 | $httpResponse->setStatus( 308 ); |
| 94 | |
| 95 | return $httpResponse; |
| 96 | } |
| 97 | |
| 98 | public function getParamSettings(): array { |
| 99 | return [ |
| 100 | self::LEXEME_ID_PATH_PARAM => [ |
| 101 | self::PARAM_SOURCE => 'path', |
| 102 | ParamValidator::PARAM_TYPE => 'string', |
| 103 | ParamValidator::PARAM_REQUIRED => true, |
| 104 | ], |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | public function needsWriteAccess(): bool { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Preconditions are checked via {@link PreconditionMiddleware} |
| 114 | */ |
| 115 | public function checkPreconditions(): ?ResponseInterface { |
| 116 | return null; |
| 117 | } |
| 118 | |
| 119 | } |