Completed
Push — master ( 951284...b5c57e )
by
unknown
06:36 queued 11s
created

getEntityIdFromLocalLink()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
cc 5
nc 7
nop 1
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entityTypeForNamespace of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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
The strict comparison === seems to always evaluate to false as the types of $entityId->getEntityType() (string) and $entityTypeForNamespace (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
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