diff --git a/repo/rest-api/routes.dev.json b/repo/rest-api/routes.dev.json index 1e3ec7217af..0d5567f13e8 100644 --- a/repo/rest-api/routes.dev.json +++ b/repo/rest-api/routes.dev.json @@ -1 +1,7 @@ -[ ] +[ + { + "path": "/wikibase/v0/entities/properties/{property_id}/aliases", + "method": "GET", + "factory": "Wikibase\\Repo\\RestApi\\RouteHandlers\\GetPropertyAliasesRouteHandler::factory" + } +] diff --git a/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliases.php b/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliases.php new file mode 100644 index 00000000000..c9a996cc092 --- /dev/null +++ b/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliases.php @@ -0,0 +1,28 @@ +propertyAliasesRetriever = $propertyAliasesRetriever; + } + + public function execute( GetPropertyAliasesRequest $request ): GetPropertyAliasesResponse { + $propertyId = new NumericPropertyId( $request->getPropertyId() ); + + return new GetPropertyAliasesResponse( + // @phan-suppress-next-line PhanTypeMismatchArgumentNullable + $this->propertyAliasesRetriever->getAliases( $propertyId ) + ); + } + +} diff --git a/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesRequest.php b/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesRequest.php new file mode 100644 index 00000000000..2c187f8e68d --- /dev/null +++ b/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesRequest.php @@ -0,0 +1,18 @@ +propertyId = $propertyId; + } + + public function getPropertyId(): string { + return $this->propertyId; + } +} diff --git a/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesResponse.php b/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesResponse.php new file mode 100644 index 00000000000..ad7ca764c89 --- /dev/null +++ b/repo/rest-api/src/Application/UseCases/GetPropertyAliases/GetPropertyAliasesResponse.php @@ -0,0 +1,22 @@ +aliases = $aliases; + } + + public function getAliases(): Aliases { + return $this->aliases; + } + +} diff --git a/repo/rest-api/src/RouteHandlers/GetPropertyAliasesRouteHandler.php b/repo/rest-api/src/RouteHandlers/GetPropertyAliasesRouteHandler.php new file mode 100644 index 00000000000..aac668a20ed --- /dev/null +++ b/repo/rest-api/src/RouteHandlers/GetPropertyAliasesRouteHandler.php @@ -0,0 +1,60 @@ +useCase = $useCase; + $this->aliasesSerializer = $aliasesSerializer; + } + + public static function factory(): self { + return new self( + WbRestApi::getGetPropertyAliases(), + new AliasesSerializer(), + ); + } + + public function run( string $propertyId ): Response { + $useCaseResponse = $this->useCase->execute( new GetPropertyAliasesRequest( $propertyId ) ); + $httpResponse = $this->getResponseFactory()->create(); + $httpResponse->setHeader( 'Content-Type', 'application/json' ); + $httpResponse->setBody( + new StringStream( json_encode( $this->aliasesSerializer->serialize( $useCaseResponse->getAliases() ) ) ) + ); + + return $httpResponse; + } + + public function getParamSettings(): array { + return [ + self::PROPERTY_ID_PATH_PARAM => [ + self::PARAM_SOURCE => 'path', + ParamValidator::PARAM_TYPE => 'string', + ParamValidator::PARAM_REQUIRED => true, + ], + ]; + } + +} diff --git a/repo/rest-api/src/WbRestApi.ServiceWiring.php b/repo/rest-api/src/WbRestApi.ServiceWiring.php index d9ae9fccc4d..4e6aa4866cd 100644 --- a/repo/rest-api/src/WbRestApi.ServiceWiring.php +++ b/repo/rest-api/src/WbRestApi.ServiceWiring.php @@ -47,6 +47,7 @@ use Wikibase\Repo\RestApi\Application\UseCases\GetLatestPropertyRevisionMetadata; use Wikibase\Repo\RestApi\Application\UseCases\GetLatestStatementSubjectRevisionMetadata; use Wikibase\Repo\RestApi\Application\UseCases\GetProperty\GetProperty; +use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases; use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabels\GetPropertyLabels; use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatement\GetPropertyStatement; use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatements\GetPropertyStatements; @@ -399,6 +400,15 @@ function ( MediaWikiServices $services ): ItemDescriptionEditRequestValidatingDe ); }, + 'WbRestApi.GetPropertyAliases' => function( MediaWikiServices $services ): GetPropertyAliases { + return new GetPropertyAliases( + new PrefetchingTermLookupAliasesRetriever( + WikibaseRepo::getPrefetchingTermLookup( $services ), + WikibaseRepo::getTermsLanguages( $services ) + ) + ); + }, + 'WbRestApi.GetPropertyLabels' => function( MediaWikiServices $services ): GetPropertyLabels { return new GetPropertyLabels( WbRestApi::getGetLatestPropertyRevisionMetadata( $services ), diff --git a/repo/rest-api/src/WbRestApi.php b/repo/rest-api/src/WbRestApi.php index ba5b2e1fda4..3ca9982b468 100644 --- a/repo/rest-api/src/WbRestApi.php +++ b/repo/rest-api/src/WbRestApi.php @@ -25,6 +25,7 @@ use Wikibase\Repo\RestApi\Application\UseCases\GetLatestPropertyRevisionMetadata; use Wikibase\Repo\RestApi\Application\UseCases\GetLatestStatementSubjectRevisionMetadata; use Wikibase\Repo\RestApi\Application\UseCases\GetProperty\GetProperty; +use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyAliases\GetPropertyAliases; use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyLabels\GetPropertyLabels; use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatement\GetPropertyStatement; use Wikibase\Repo\RestApi\Application\UseCases\GetPropertyStatements\GetPropertyStatements; @@ -284,6 +285,11 @@ public static function getGetPropertyLabels( ContainerInterface $services = null ->get( 'WbRestApi.GetPropertyLabels' ); } + public static function getGetPropertyAliases( ContainerInterface $services = null ): GetPropertyAliases { + return ( $services ?: MediaWikiServices::getInstance() ) + ->get( 'WbRestApi.GetPropertyAliases' ); + } + public static function getValidatingRequestDeserializer( ContainerInterface $services = null ): ValidatingRequestDeserializer { return ( $services ?: MediaWikiServices::getInstance() )->get( 'WbRestApi.ValidatingRequestDeserializer' ); } diff --git a/repo/rest-api/tests/mocha/api-testing/GetPropertyAliasesTest.js b/repo/rest-api/tests/mocha/api-testing/GetPropertyAliasesTest.js new file mode 100644 index 00000000000..da30e93297f --- /dev/null +++ b/repo/rest-api/tests/mocha/api-testing/GetPropertyAliasesTest.js @@ -0,0 +1,33 @@ +'use strict'; + +const { assert } = require( 'api-testing' ); +const { expect } = require( '../helpers/chaiHelper' ); +const { createEntity } = require( '../helpers/entityHelper' ); +const { newGetPropertyAliasesRequestBuilder } = require( '../helpers/RequestBuilderFactory' ); + +describe( newGetPropertyAliasesRequestBuilder().getRouteDescription(), () => { + + let propertyId; + + before( async () => { + const testProperty = await createEntity( 'property', { + aliases: { + en: [ + { language: 'en', value: 'en-alias1' }, + { language: 'en', value: 'en-alias2' } + ] + }, + datatype: 'string' + } ); + propertyId = testProperty.entity.id; + } ); + + it( 'can get the aliases of a property', async () => { + const response = await newGetPropertyAliasesRequestBuilder( propertyId ).assertValidRequest() + .makeRequest(); + + expect( response ).to.have.status( 200 ); + assert.deepEqual( response.body, { en: [ 'en-alias1', 'en-alias2' ] } ); + } ); + +} ); diff --git a/repo/rest-api/tests/mocha/helpers/RequestBuilderFactory.js b/repo/rest-api/tests/mocha/helpers/RequestBuilderFactory.js index 79ad8660290..b93f018fc34 100644 --- a/repo/rest-api/tests/mocha/helpers/RequestBuilderFactory.js +++ b/repo/rest-api/tests/mocha/helpers/RequestBuilderFactory.js @@ -21,6 +21,12 @@ module.exports = { .withPathParam( 'item_id', itemId ); }, + newGetPropertyAliasesRequestBuilder( propertyId ) { + return new RequestBuilder() + .withRoute( 'GET', '/entities/properties/{property_id}/aliases' ) + .withPathParam( 'property_id', propertyId ); + }, + newGetItemAliasesInLanguageRequestBuilder( itemId, languageCode ) { return new RequestBuilder() .withRoute( 'GET', '/entities/items/{item_id}/aliases/{language_code}' ) diff --git a/repo/rest-api/tests/phpunit/Application/UseCases/GetPropertyAliases/GetPropertyAliasesTest.php b/repo/rest-api/tests/phpunit/Application/UseCases/GetPropertyAliases/GetPropertyAliasesTest.php new file mode 100644 index 00000000000..89369fe122a --- /dev/null +++ b/repo/rest-api/tests/phpunit/Application/UseCases/GetPropertyAliases/GetPropertyAliasesTest.php @@ -0,0 +1,53 @@ +propertyAliasesRetriever = $this->createStub( PropertyAliasesRetriever::class ); + } + + public function testSuccess(): void { + $propertyId = 'P123'; + + $aliases = new Aliases( + new AliasesInLanguage( 'en', [ 'en-alias1', 'en-alias2' ] ), + new AliasesInLanguage( 'de', [ 'de-alias1', 'de-alias2' ] ) + ); + + $this->propertyAliasesRetriever = $this->createMock( PropertyAliasesRetriever::class ); + $this->propertyAliasesRetriever->expects( $this->once() ) + ->method( 'getAliases' ) + ->with( new NumericPropertyId( $propertyId ) ) + ->willReturn( $aliases ); + + $response = $this->newUseCase()->execute( new GetPropertyAliasesRequest( $propertyId ) ); + + $this->assertSame( $aliases, $response->getAliases() ); + } + + public function newUseCase(): GetPropertyAliases { + return new GetPropertyAliases( $this->propertyAliasesRetriever ); + } + +}