-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show item label instead of Q-IDs if possible (#146)
* Show item label instead of Q-IDs if possible * Fix various issues raised by CI * Add missing argument to constructor function * Add missing use statement * Fix setLabel language code * Fix language code not being string * Use Wikibase MockRepo for test * Set test to true for now * Fix merge issues * Add FacetLabelBuilder class and use existing class for label lookup * Fix some of the lint issues * Fix incorrect type for getFacetLabel * Refactor getPropertyDataTypeId * Drop public methods from FacetLabelBuilder * Fix getFacetLabelBuilder scope * Fix tests * Fix tests * Add test * Refactor label handling with new FacetValueFormatter * Fix lint issue * Remove unused todo --------- Co-authored-by: Morne Alberts <morne@malberts.net>
- Loading branch information
1 parent
9bf61cb
commit 570710a
Showing
9 changed files
with
146 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Presentation; | ||
|
||
use Wikibase\DataModel\Entity\ItemId; | ||
use Wikibase\DataModel\Entity\PropertyId; | ||
use Wikibase\DataModel\Services\Lookup\LabelLookup; | ||
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; | ||
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookupException; | ||
|
||
class FacetValueFormatter { | ||
|
||
/** @var array<string, ?string> */ | ||
private array $propertyIdToType = []; | ||
|
||
public function __construct( | ||
private readonly PropertyDataTypeLookup $dataTypeLookup, | ||
private readonly LabelLookup $labelLookup | ||
) { | ||
} | ||
|
||
public function getLabel( string $value, PropertyId $propertyId ): string { | ||
$dataTypeId = $this->getPropertyDataTypeId( $propertyId ); | ||
|
||
if ( $dataTypeId === null || $dataTypeId !== 'wikibase-item' ) { | ||
return $value; | ||
} | ||
|
||
return $this->labelLookup->getLabel( new ItemId( $value ) )?->getText() ?? $value; | ||
} | ||
|
||
private function getPropertyDataTypeId( PropertyId $propertyId ): ?string { | ||
$key = $propertyId->getSerialization(); | ||
|
||
if ( !array_key_exists( $key, $this->propertyIdToType ) ) { | ||
$this->propertyIdToType[$key] = $this->getDataTypeIdForProperty( $propertyId ); | ||
} | ||
|
||
return $this->propertyIdToType[$key]; | ||
} | ||
|
||
private function getDataTypeIdForProperty( PropertyId $propertyId ): ?string { | ||
try { | ||
return $this->dataTypeLookup->getDataTypeIdForProperty( $propertyId ); | ||
} catch ( PropertyDataTypeLookupException ) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<span class="wikibase-faceted-search__facet-label-text">{{label}}</span><span class="wikibase-faceted-search__facet-label-count">{{count}}</span> | ||
<span class="wikibase-faceted-search__facet-label-text">{{formattedValue}}</span><span class="wikibase-faceted-search__facet-label-count">{{count}}</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Tests\Presentation; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Presentation\FacetValueFormatter; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Tests\TestDoubles\StubLabelLookup; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Tests\TestDoubles\StubPropertyDataTypeLookup; | ||
use Wikibase\DataModel\Entity\NumericPropertyId; | ||
|
||
/** | ||
* @covers \ProfessionalWiki\WikibaseFacetedSearch\Presentation\FacetValueFormatter | ||
*/ | ||
class FacetValueFormatterTest extends TestCase { | ||
|
||
public function testGetLabelWithWikibaseItemDataType(): void { | ||
$valueFormatter = $this->newFacetValueFormatter(); | ||
|
||
$this->assertSame( 'Q100', $valueFormatter->getLabel( 'Q100', new NumericPropertyId( 'P123' ) ) ); | ||
} | ||
|
||
public function testGetLabelWithNonWikibaseItemDataType(): void { | ||
$valueFormatter = $this->newFacetValueFormatter( new StubPropertyDataTypeLookup( 'not-wikibase-item' ) ); | ||
|
||
$this->assertSame( 'unimportant', $valueFormatter->getLabel( 'unimportant', new NumericPropertyId( 'P123' ) ) ); | ||
} | ||
|
||
public function testGetLabelWithNullDataType(): void { | ||
$valueFormatter = $this->newFacetValueFormatter( new StubPropertyDataTypeLookup( null ) ); | ||
|
||
$this->assertSame( 'unimportant', $valueFormatter->getLabel( 'unimportant', new NumericPropertyId( 'P123' ) ) ); | ||
} | ||
|
||
private function newFacetValueFormatter( ?StubPropertyDataTypeLookup $dataTypeLookup = null, ?StubLabelLookup $labelLookup = null ): FacetValueFormatter { | ||
return new FacetValueFormatter( | ||
$dataTypeLookup ?? new StubPropertyDataTypeLookup( 'wikibase-item' ), | ||
$labelLookup ?? new StubLabelLookup( null ) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Tests\TestDoubles; | ||
|
||
use Wikibase\DataModel\Entity\PropertyId; | ||
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; | ||
|
||
class StubPropertyDataTypeLookup implements PropertyDataTypeLookup { | ||
|
||
public function __construct( | ||
private readonly ?string $dataType | ||
) { | ||
} | ||
|
||
public function getDataTypeIdForProperty( PropertyId $propertyId ): ?string { | ||
return $this->dataType; | ||
} | ||
|
||
} |