Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DescriptionLookup | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getDescriptionForTitle | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace EntitySchema\DataAccess; |
| 6 | |
| 7 | use MediaWiki\Page\PageIdentity; |
| 8 | use Wikibase\DataModel\Term\TermFallback; |
| 9 | use Wikibase\Lib\LanguageFallbackChainFactory; |
| 10 | |
| 11 | /** |
| 12 | * Lookup for EntitySchema descriptions, with language fallbacks applied. |
| 13 | * |
| 14 | * @license GPL-2.0-or-later |
| 15 | */ |
| 16 | class DescriptionLookup { |
| 17 | |
| 18 | private FullViewSchemaDataLookup $fullViewSchemaDataLookup; |
| 19 | |
| 20 | private LanguageFallbackChainFactory $languageFallbackChainFactory; |
| 21 | |
| 22 | public function __construct( |
| 23 | FullViewSchemaDataLookup $fullViewSchemaDataLookup, |
| 24 | LanguageFallbackChainFactory $languageFallbackChainFactory |
| 25 | ) { |
| 26 | $this->fullViewSchemaDataLookup = $fullViewSchemaDataLookup; |
| 27 | $this->languageFallbackChainFactory = $languageFallbackChainFactory; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Look up the description of the EntitySchema with the given title, if any. |
| 32 | * Language fallbacks are applied based on the given language code. |
| 33 | * |
| 34 | * @param PageIdentity $title |
| 35 | * @param string $langCode |
| 36 | * @return TermFallback|null The description, or null if no description or EntitySchema was found. |
| 37 | */ |
| 38 | public function getDescriptionForTitle( PageIdentity $title, string $langCode ): ?TermFallback { |
| 39 | $schemaData = $this->fullViewSchemaDataLookup->getFullViewSchemaDataForTitle( $title ); |
| 40 | if ( $schemaData === null ) { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | $chain = $this->languageFallbackChainFactory->newFromLanguageCode( $langCode ); |
| 45 | $preferredDescription = $chain->extractPreferredValue( array_map( |
| 46 | static fn ( $nameBadge ) => $nameBadge->description, |
| 47 | $schemaData->nameBadges |
| 48 | ) ); |
| 49 | if ( $preferredDescription !== null ) { |
| 50 | return new TermFallback( |
| 51 | $langCode, |
| 52 | $preferredDescription['value'], |
| 53 | $preferredDescription['language'], |
| 54 | $preferredDescription['source'] |
| 55 | ); |
| 56 | } else { |
| 57 | return null; |
| 58 | } |
| 59 | } |
| 60 | } |