|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Lookup; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Assert\RepositoryNameAssert; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
8
|
|
|
use Wikimedia\Assert\Assert; |
|
9
|
|
|
use Wikimedia\Assert\ParameterAssertionException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Delegates lookup to the repository-specific EntityLookup |
|
13
|
|
|
* based on the name of the repository an EntityId belongs to. |
|
14
|
|
|
* This class does not strip repository prefixes of incoming |
|
15
|
|
|
* entity IDs. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 3.7 |
|
18
|
|
|
* |
|
19
|
|
|
* @license GPL-2.0+ |
|
20
|
|
|
*/ |
|
21
|
|
|
class DispatchingEntityLookup implements EntityLookup { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var EntityLookup[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $lookups; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @since 3.7 |
|
30
|
|
|
* |
|
31
|
|
|
* @param EntityLookup[] $lookups associative array with repository names (strings) as keys |
|
32
|
|
|
* and EntityLookup objects as values. |
|
33
|
|
|
* |
|
34
|
|
|
* @throws ParameterAssertionException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( array $lookups ) { |
|
37
|
|
|
Assert::parameter( |
|
38
|
|
|
!empty( $lookups ), |
|
39
|
|
|
'$lookups', |
|
40
|
|
|
'must must not be empty' |
|
41
|
|
|
); |
|
42
|
|
|
Assert::parameterElementType( EntityLookup::class, $lookups, '$lookups' ); |
|
43
|
|
|
RepositoryNameAssert::assertParameterKeysAreValidRepositoryNames( $lookups, '$lookups' ); |
|
44
|
|
|
$this->lookups = $lookups; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @see EntityLookup::getEntity |
|
49
|
|
|
* |
|
50
|
|
|
* @since 3.7 |
|
51
|
|
|
* |
|
52
|
|
|
* @param EntityId $entityId |
|
53
|
|
|
* |
|
54
|
|
|
* @return null|EntityDocument |
|
55
|
|
|
* @throws EntityLookupException |
|
56
|
|
|
* @throws UnknownForeignRepositoryException |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getEntity( EntityId $entityId ) { |
|
59
|
|
|
$lookup = $this->getLookupForEntityId( $entityId ); |
|
60
|
|
|
return $lookup->getEntity( $entityId ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @see EntityLookup::hasEntity |
|
65
|
|
|
* |
|
66
|
|
|
* @since 3.7 |
|
67
|
|
|
* |
|
68
|
|
|
* @param EntityId $entityId |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
* @throws EntityLookupException |
|
72
|
|
|
* @throws UnknownForeignRepositoryException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function hasEntity( EntityId $entityId ) { |
|
75
|
|
|
$lookup = $this->getLookupForEntityId( $entityId ); |
|
76
|
|
|
return $lookup->hasEntity( $entityId ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param EntityId $entityId |
|
81
|
|
|
* |
|
82
|
|
|
* @return EntityLookup |
|
83
|
|
|
* @throws UnknownForeignRepositoryException |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getLookupForEntityId( EntityId $entityId ) { |
|
86
|
|
|
$repo = $entityId->getRepositoryName(); |
|
87
|
|
|
if ( !isset( $this->lookups[$repo] ) ) { |
|
88
|
|
|
throw new UnknownForeignRepositoryException( $entityId->getRepositoryName() ); |
|
89
|
|
|
} |
|
90
|
|
|
return $this->lookups[$repo]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|