Skip to content

Commit

Permalink
Merge "REST: Create PropertyAliasesRetriever interface"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Sep 20, 2023
2 parents c0bc117 + 619f35c commit 686fc65
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
15 changes: 15 additions & 0 deletions repo/rest-api/src/Domain/Services/PropertyAliasesRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Domain\Services;

use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases;

/**
* @license GPL-2.0-or-later
*/
interface PropertyAliasesRetriever {

public function getAliases( PropertyId $propertyId ): ?Aliases;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
namespace Wikibase\Repo\RestApi\Infrastructure\DataAccess;

use Wikibase\DataAccess\PrefetchingTermLookup;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Term\TermTypes;
use Wikibase\Lib\ContentLanguages;
use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases;
use Wikibase\Repo\RestApi\Domain\ReadModel\AliasesInLanguage;
use Wikibase\Repo\RestApi\Domain\Services\ItemAliasesInLanguageRetriever;
use Wikibase\Repo\RestApi\Domain\Services\ItemAliasesRetriever;
use Wikibase\Repo\RestApi\Domain\Services\PropertyAliasesRetriever;

/**
* @license GPL-2.0-or-later
*/
class PrefetchingTermLookupAliasesRetriever implements ItemAliasesRetriever, ItemAliasesInLanguageRetriever {
class PrefetchingTermLookupAliasesRetriever implements
ItemAliasesRetriever,
ItemAliasesInLanguageRetriever,
PropertyAliasesRetriever
{

private PrefetchingTermLookup $prefetchingTermLookup;
private ContentLanguages $termLanguages;
Expand All @@ -24,17 +30,17 @@ public function __construct( PrefetchingTermLookup $prefetchingTermLookup, Conte
$this->termLanguages = $termLanguages;
}

public function getAliases( ItemId $itemId ): ?Aliases {
public function getAliases( EntityId $entityId ): ?Aliases {
$this->prefetchingTermLookup->prefetchTerms(
[ $itemId ],
[ $entityId ],
[ TermTypes::TYPE_ALIAS ],
$this->termLanguages->getLanguages()
);

$aliases = new Aliases();

foreach ( $this->termLanguages->getLanguages() as $lang ) {
$prefetchedAliases = $this->prefetchingTermLookup->getPrefetchedAliases( $itemId, $lang );
$prefetchedAliases = $this->prefetchingTermLookup->getPrefetchedAliases( $entityId, $lang );

if ( $prefetchedAliases ) {
$aliases[$lang] = new AliasesInLanguage( $lang, $prefetchedAliases );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Wikibase\Repo\Tests\RestApi\Infrastructure\DataAccess;

use Generator;
use PHPUnit\Framework\TestCase;
use Wikibase\DataAccess\NullPrefetchingTermLookup;
use Wikibase\DataAccess\PrefetchingTermLookup;
use Wikibase\DataAccess\Tests\FakePrefetchingTermLookup;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Entity\NumericPropertyId;
use Wikibase\Lib\StaticContentLanguages;
use Wikibase\Repo\RestApi\Domain\ReadModel\Aliases;
use Wikibase\Repo\RestApi\Domain\ReadModel\AliasesInLanguage;
Expand All @@ -23,43 +26,56 @@ class PrefetchingTermLookupAliasesRetrieverTest extends TestCase {

private const ALL_TERM_LANGUAGES = [ 'de', 'en', 'ar' ];
private const ITEM_ID = 'Q123';
private const PROPERTY_ID = 'P123';

public function testGetAliases(): void {
$itemId = new ItemId( self::ITEM_ID );

/**
* @dataProvider provideEntityId
*/
public function testGetAliases( EntityId $entityId ): void {
$aliasesRetriever = new PrefetchingTermLookupAliasesRetriever(
new FakePrefetchingTermLookup(),
new StaticContentLanguages( self::ALL_TERM_LANGUAGES )
);

$aliases = $aliasesRetriever->getAliases( $itemId );
$aliases = $aliasesRetriever->getAliases( $entityId );

$aliasesReadModel = new Aliases(
new AliasesInLanguage( 'de', [ 'Q123 de alias 1', 'Q123 de alias 2' ] ),
new AliasesInLanguage( 'en', [ 'Q123 en alias 1', 'Q123 en alias 2' ] ),
new AliasesInLanguage( 'ar', [ 'Q123 ar alias 1', 'Q123 ar alias 2' ] ),
new AliasesInLanguage(
'de',
[ "{$entityId->getSerialization()} de alias 1", "{$entityId->getSerialization()} de alias 2" ]
),
new AliasesInLanguage(
'en',
[ "{$entityId->getSerialization()} en alias 1", "{$entityId->getSerialization()} en alias 2" ]
),
new AliasesInLanguage(
'ar',
[ "{$entityId->getSerialization()} ar alias 1", "{$entityId->getSerialization()} ar alias 2" ]
),
);

$this->assertEquals( $aliasesReadModel, $aliases );
}

public function testGetAliasesForSpecificLanguages(): void {
$itemId = new ItemId( self::ITEM_ID );
/**
* @dataProvider provideEntityId
*/
public function testGetAliasesForSpecificLanguages( EntityId $entityId ): void {
$languages = [ 'en', 'de' ];

$prefetchingTermLookup = $this->createStub( PrefetchingTermLookup::class );
$prefetchingTermLookup->method( 'getPrefetchedAliases' )
->willReturnMap( [
[ $itemId, 'en', [ 'Q123 en alias 1', 'Q123 en alias 2' ] ],
[ $itemId, 'de', false ],
[ $entityId, 'en', [ "{$entityId->getSerialization()} en alias 1", "{$entityId->getSerialization()} en alias 2" ] ],
[ $entityId, 'de', false ],
] );

$aliasesRetriever = new PrefetchingTermLookupAliasesRetriever(
$prefetchingTermLookup,
new StaticContentLanguages( $languages )
);

$aliases = $aliasesRetriever->getAliases( $itemId );
$aliases = $aliasesRetriever->getAliases( $entityId );

$this->assertCount( 1, $aliases );
$this->assertArrayHasKey( 'en', $aliases );
Expand Down Expand Up @@ -96,4 +112,9 @@ public function testGetAliasesInLanguage(): void {
);
}

public function provideEntityId(): Generator {
yield 'item id' => [ new ItemId( self::ITEM_ID ) ];
yield 'property id' => [ new NumericPropertyId( self::PROPERTY_ID ) ];
}

}

0 comments on commit 686fc65

Please sign in to comment.