These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Wikibase\Lib\Store; |
||
4 | |||
5 | use MediaWiki\Linker\LinkTarget; |
||
6 | use Wikibase\DataAccess\EntitySource; |
||
7 | use Wikibase\DataAccess\EntitySourceDefinitions; |
||
8 | use Wikibase\DataModel\Entity\EntityId; |
||
9 | use Wikibase\DataModel\Entity\EntityIdParser; |
||
10 | use Wikibase\DataModel\Entity\EntityIdParsingException; |
||
11 | |||
12 | /** |
||
13 | * @license GPL-2.0-or-later |
||
14 | */ |
||
15 | class EntityLinkTargetEntityIdLookup implements LinkTargetEntityIdLookup { |
||
16 | /** |
||
17 | * used e.g. for links from Commons to Wikidata in the form of d:Special:EntityPage/Q42 |
||
18 | */ |
||
19 | private const SPECIAL_ENTITY_PAGE = 'Special:EntityPage'; |
||
20 | |||
21 | /** |
||
22 | * @var EntityNamespaceLookup |
||
23 | */ |
||
24 | private $namespaceLookup; |
||
25 | |||
26 | /** |
||
27 | * @var EntityIdParser |
||
28 | */ |
||
29 | private $entityIdParser; |
||
30 | |||
31 | /** |
||
32 | * @var EntitySourceDefinitions |
||
33 | */ |
||
34 | private $entitySourceDefinitions; |
||
35 | /** |
||
36 | * @var EntitySource |
||
37 | */ |
||
38 | private $localSource; |
||
39 | |||
40 | public function __construct( |
||
41 | EntityNamespaceLookup $namespaceLookup, |
||
42 | EntityIdParser $entityIdParser, |
||
43 | EntitySourceDefinitions $entitySourceDefinitions, |
||
44 | EntitySource $localSource |
||
45 | ) { |
||
46 | $this->namespaceLookup = $namespaceLookup; |
||
47 | $this->entityIdParser = $entityIdParser; |
||
48 | $this->entitySourceDefinitions = $entitySourceDefinitions; |
||
49 | $this->localSource = $localSource; |
||
50 | } |
||
51 | |||
52 | public function getEntityId( LinkTarget $linkTarget ): ?EntityId { |
||
53 | return $linkTarget->isExternal() |
||
54 | ? $this->getEntityIdFromExternalLink( $linkTarget ) |
||
55 | : $this->getEntityIdFromLocalLink( $linkTarget ); |
||
56 | } |
||
57 | |||
58 | private function getEntityIdFromExternalLink( LinkTarget $linkTarget ): ?EntityId { |
||
59 | $potentialSpecialEntityPageParts = explode( '/', $linkTarget->getText(), 2 ); |
||
60 | if ( $potentialSpecialEntityPageParts[0] !== self::SPECIAL_ENTITY_PAGE ) { |
||
61 | return null; |
||
62 | } |
||
63 | |||
64 | $id = $this->parseEntityId( $potentialSpecialEntityPageParts[1] ); |
||
65 | if ( !$id ) { |
||
66 | return null; |
||
67 | } |
||
68 | |||
69 | $source = $this->entitySourceDefinitions->getSourceForEntityType( $id->getEntityType() ); |
||
70 | return $source && $source->getInterwikiPrefix() === $linkTarget->getInterwiki() ? $id : null; |
||
71 | } |
||
72 | |||
73 | private function getEntityIdFromLocalLink( LinkTarget $linkTarget ): ?EntityId { |
||
74 | $entityTypeForNamespace = $this->namespaceLookup->getEntityType( $linkTarget->getNamespace() ); |
||
75 | if ( !$entityTypeForNamespace ) { |
||
76 | return null; |
||
77 | } |
||
78 | |||
79 | $entityId = $this->parseEntityId( $linkTarget->getText() ); |
||
80 | return ( |
||
81 | $entityId && |
||
82 | $entityId->getEntityType() === $entityTypeForNamespace && |
||
0 ignored issues
–
show
Unused Code
Bug
introduced
by
![]() |
|||
83 | in_array( $entityId->getEntityType(), $this->localSource->getEntityTypes() ) |
||
84 | ) ? $entityId : null; |
||
85 | } |
||
86 | |||
87 | private function parseEntityId( string $id ): ?EntityId { |
||
88 | try { |
||
89 | return $this->entityIdParser->parse( $id ); |
||
90 | } catch ( EntityIdParsingException $e ) { |
||
91 | return null; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | } |
||
96 |