Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.22% |
8 / 36 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| WikidataEntityLookup | |
22.22% |
8 / 36 |
|
33.33% |
2 / 6 |
136.45 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isAvailable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseEntityId | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| wikidataItemExists | |
28.57% |
2 / 7 |
|
0.00% |
0 / 1 |
6.28 | |||
| resolveAbstractLabel | |
14.29% |
2 / 14 |
|
0.00% |
0 / 1 |
28.67 | |||
| getWikidataItemForTitle | |
22.22% |
2 / 9 |
|
0.00% |
0 / 1 |
7.23 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Injectable service to interface with WikibaseClient lookups. |
| 4 | * |
| 5 | * (T428129) This is designed to isolate access to WikibaseClient lookup |
| 6 | * features conditional to whether WikibaseClient extension is available |
| 7 | * or not. This class allows us to easily test Wikibase-dependent features. |
| 8 | * |
| 9 | * @file |
| 10 | * @ingroup Extensions |
| 11 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
| 12 | * @license MIT |
| 13 | */ |
| 14 | |
| 15 | namespace MediaWiki\Extension\WikiLambda; |
| 16 | |
| 17 | use MediaWiki\Language\LanguageFallback; |
| 18 | use MediaWiki\Registration\ExtensionRegistry; |
| 19 | use OutOfBoundsException; |
| 20 | |
| 21 | /** |
| 22 | * Service for checking the existence of Wikidata entities via WikibaseClient. |
| 23 | * Injectable alternative to static WikibaseClient calls, enabling testability. |
| 24 | */ |
| 25 | class WikidataEntityLookup { |
| 26 | |
| 27 | public function __construct( private readonly LanguageFallback $languageFallback ) { |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return bool |
| 32 | */ |
| 33 | private function isAvailable(): bool { |
| 34 | return ExtensionRegistry::getInstance()->isLoaded( 'WikibaseClient' ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param string $qid |
| 39 | * @return ?\Wikibase\DataModel\Entity\EntityId |
| 40 | */ |
| 41 | private function parseEntityId( string $qid ): ?\Wikibase\DataModel\Entity\EntityId { |
| 42 | try { |
| 43 | $wbEntityParser = \Wikibase\Client\WikibaseClient::getEntityIdParser(); |
| 44 | return $wbEntityParser->parse( $qid ); |
| 45 | } catch ( \Wikibase\DataModel\Entity\EntityIdParsingException ) { |
| 46 | return null; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Check whether a Wikidata item exists via WikibaseClient. |
| 52 | * |
| 53 | * Returns true if the item exists, false if it definitively does not exist, |
| 54 | * or null if WikibaseClient is unavailable (so callers can choose to skip validation). |
| 55 | * |
| 56 | * @param string $qid The Wikidata QID (e.g. Q42) |
| 57 | * @return ?bool |
| 58 | */ |
| 59 | public function wikidataItemExists( string $qid ): ?bool { |
| 60 | if ( !$this->isAvailable() ) { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | $itemId = $this->parseEntityId( $qid ); |
| 65 | if ( $itemId === null ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $wbEntityLookup = \Wikibase\Client\WikibaseClient::getEntityLookup(); |
| 70 | return $wbEntityLookup->getEntity( $itemId ) !== null; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Resolve the label for a Wikidata entity via WikibaseClient. |
| 75 | * |
| 76 | * @param string $qid The Wikidata QID (e.g. Q715040) |
| 77 | * @param string $langCode The language code to fetch the label in |
| 78 | * @return ?string The label, or null if not found |
| 79 | */ |
| 80 | public function resolveAbstractLabel( string $qid, string $langCode ): ?string { |
| 81 | if ( !$this->isAvailable() ) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | $itemId = $this->parseEntityId( $qid ); |
| 86 | if ( $itemId === null ) { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | $wbEntityLookup = \Wikibase\Client\WikibaseClient::getStore()->getEntityLookup(); |
| 91 | $wbEntity = $wbEntityLookup->getEntity( $itemId ); |
| 92 | |
| 93 | if ( !( $wbEntity instanceof \Wikibase\DataModel\Entity\Item ) ) { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | $labels = $wbEntity->getLabels(); |
| 98 | foreach ( ZObjectUtils::getFallbackLanguageCodes( $this->languageFallback, $langCode ) as $code ) { |
| 99 | try { |
| 100 | return $labels->getByLanguage( $code )->getText(); |
| 101 | } catch ( OutOfBoundsException ) { |
| 102 | // Try next fallback |
| 103 | } |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the Wikidata Item ID linked to a client page via sitelink. |
| 110 | * |
| 111 | * Returns the Qid if found, or null if WikibaseClient is unavailable |
| 112 | * r no sitelink exists for the given title. |
| 113 | * |
| 114 | * @param string $prefixedTitle |
| 115 | * @return ?string |
| 116 | */ |
| 117 | public function getWikidataItemForTitle( string $prefixedTitle ): ?string { |
| 118 | if ( !$this->isAvailable() ) { |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | $wbSiteLinkLookup = \Wikibase\Client\WikibaseClient::getStore()->getSiteLinkLookup(); |
| 123 | $wbClientSettings = \Wikibase\Client\WikibaseClient::getSettings(); |
| 124 | |
| 125 | $clientSiteGlobalID = $wbClientSettings->getSetting( 'siteGlobalID' ); |
| 126 | $entityId = $wbSiteLinkLookup->getItemIdForLink( $clientSiteGlobalID, $prefixedTitle ); |
| 127 | |
| 128 | // No linked wikidata item; return null |
| 129 | if ( !$entityId ) { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | // Success, return default value wikidata item ID; E.g. Q42 |
| 134 | return $entityId->getSerialization(); |
| 135 | } |
| 136 | } |