|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataAccess; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityRedirect; |
|
7
|
|
|
use Wikibase\DataModel\Entity\Property; |
|
8
|
|
|
use Wikibase\Lib\Interactors\DispatchingTermSearchInteractorFactory; |
|
9
|
|
|
use Wikibase\Lib\Store\EntityNamespaceLookup; |
|
10
|
|
|
use Wikibase\Lib\Store\EntityRevision; |
|
11
|
|
|
use Wikibase\Lib\Store\EntityStoreWatcher; |
|
12
|
|
|
use Wikimedia\Assert\Assert; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* TODO this has been introduced into data-access with a couple of points that still bind to |
|
16
|
|
|
* wikibase lib: |
|
17
|
|
|
* - Wikibase\Lib\Store\EntityRevision; (could already be moved to data-access) |
|
18
|
|
|
* - Wikibase\Lib\Store\EntityStoreWatcher; (only binds to EntityRevision within lib) |
|
19
|
|
|
* |
|
20
|
|
|
* @license GPL-2.0-or-later |
|
21
|
|
|
*/ |
|
22
|
|
|
class MultipleEntitySourceServices implements WikibaseServices, EntityStoreWatcher { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntitySourceDefinitions |
|
26
|
|
|
*/ |
|
27
|
|
|
private $entitySourceDefinitions; |
|
28
|
|
|
|
|
29
|
|
|
private $genericServices; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var SingleEntitySourceServices[] indexed by source name |
|
33
|
|
|
*/ |
|
34
|
|
|
private $singleSourceServices; |
|
35
|
|
|
|
|
36
|
|
|
private $entityRevisionLookup = null; |
|
37
|
|
|
|
|
38
|
|
|
private $termSearchInteractorFactory = null; |
|
39
|
|
|
|
|
40
|
|
|
private $prefetchingTermLookup = null; |
|
41
|
|
|
|
|
42
|
|
|
private $entityPrefetcher = null; |
|
43
|
|
|
|
|
44
|
|
|
private $entityNamespaceLookup = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param EntitySourceDefinitions $entitySourceDefinitions |
|
48
|
|
|
* @param GenericServices $genericServices |
|
49
|
|
|
* @param SingleEntitySourceServices[] $singleSourceServices indexed by source name |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
EntitySourceDefinitions $entitySourceDefinitions, |
|
53
|
|
|
GenericServices $genericServices, |
|
54
|
|
|
array $singleSourceServices |
|
55
|
|
|
) { |
|
56
|
|
|
Assert::parameterElementType( SingleEntitySourceServices::class, $singleSourceServices, '$singleSourceServices' ); |
|
57
|
|
|
$this->entitySourceDefinitions = $entitySourceDefinitions; |
|
58
|
|
|
$this->genericServices = $genericServices; |
|
59
|
|
|
$this->singleSourceServices = $singleSourceServices; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getEntityRevisionLookup() { |
|
63
|
|
|
if ( $this->entityRevisionLookup === null ) { |
|
64
|
|
|
$lookupsPerType = []; |
|
65
|
|
|
|
|
66
|
|
|
/** @var EntitySource $source */ |
|
67
|
|
|
foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) { |
|
|
|
|
|
|
68
|
|
|
$lookupsPerType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getEntityRevisionLookup(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->entityRevisionLookup = new ByTypeDispatchingEntityRevisionLookup( $lookupsPerType ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->entityRevisionLookup; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getTermSearchInteractorFactory() { |
|
78
|
|
|
if ( $this->termSearchInteractorFactory === null ) { |
|
79
|
|
|
$factoriesByType = []; |
|
80
|
|
|
|
|
81
|
|
|
/** @var EntitySource $source */ |
|
82
|
|
|
foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) { |
|
|
|
|
|
|
83
|
|
|
$factoriesByType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getTermSearchInteractorFactory(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->termSearchInteractorFactory = new DispatchingTermSearchInteractorFactory( $factoriesByType ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->termSearchInteractorFactory; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getPrefetchingTermLookup() { |
|
93
|
|
|
if ( $this->prefetchingTermLookup === null ) { |
|
94
|
|
|
$lookupsByType = []; |
|
95
|
|
|
|
|
96
|
|
|
/** @var EntitySource $source */ |
|
97
|
|
|
foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) { |
|
|
|
|
|
|
98
|
|
|
$lookupsByType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getPrefetchingTermLookup(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->prefetchingTermLookup = new ByTypeDispatchingPrefetchingTermLookup( $lookupsByType ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->prefetchingTermLookup; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getEntityPrefetcher() { |
|
108
|
|
|
if ( $this->entityPrefetcher === null ) { |
|
109
|
|
|
$prefetchersByType = []; |
|
110
|
|
|
|
|
111
|
|
|
/** @var EntitySource $source */ |
|
112
|
|
|
foreach ( $this->entitySourceDefinitions->getEntityTypeToSourceMapping() as $entityType => $source ) { |
|
|
|
|
|
|
113
|
|
|
$prefetchersByType[$entityType] = $this->singleSourceServices[$source->getSourceName()]->getEntityPrefetcher(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->entityPrefetcher = new ByTypeDispatchingEntityPrefetcher( $prefetchersByType ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->entityPrefetcher; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getPropertyInfoLookup() { |
|
123
|
|
|
$source = $this->entitySourceDefinitions->getSourceForEntityType( Property::ENTITY_TYPE ); |
|
124
|
|
|
if ( $source === null ) { |
|
125
|
|
|
throw new \LogicException( 'No entity source provides properties!' ); |
|
126
|
|
|
} |
|
127
|
|
|
return $this->singleSourceServices[$source->getSourceName()]->getPropertyInfoLookup(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getEntityStoreWatcher() { |
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function entityUpdated( EntityRevision $entityRevision ) { |
|
135
|
|
|
$source = $this->entitySourceDefinitions->getSourceForEntityType( $entityRevision->getEntity()->getType() ); |
|
136
|
|
|
if ( $source !== null ) { |
|
137
|
|
|
$this->singleSourceServices[$source->getSourceName()]->entityUpdated( $entityRevision ); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function redirectUpdated( EntityRedirect $entityRedirect, $revisionId ) { |
|
142
|
|
|
$source = $this->entitySourceDefinitions->getSourceForEntityType( $entityRedirect->getEntityId()->getEntityType() ); |
|
143
|
|
|
if ( $source !== null ) { |
|
144
|
|
|
$this->singleSourceServices[$source->getSourceName()]->redirectUpdated( $entityRedirect, $revisionId ); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function entityDeleted( EntityId $entityId ) { |
|
149
|
|
|
$source = $this->entitySourceDefinitions->getSourceForEntityType( $entityId->getEntityType() ); |
|
150
|
|
|
if ( $source !== null ) { |
|
151
|
|
|
$this->singleSourceServices[$source->getSourceName()]->entityDeleted( $entityId ); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getEntityNamespaceLookup(): EntityNamespaceLookup { |
|
156
|
|
|
if ( $this->entityNamespaceLookup === null ) { |
|
157
|
|
|
$this->entityNamespaceLookup = array_reduce( $this->singleSourceServices, function ( $nsLookup, $service ){ |
|
158
|
|
|
return $nsLookup->merge( $service->getEntityNamespaceLookup() ); |
|
159
|
|
|
}, new EntityNamespaceLookup( [], [] ) ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->entityNamespaceLookup; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function getFullEntitySerializer() { |
|
166
|
|
|
return $this->genericServices->getFullEntitySerializer(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function getCompactEntitySerializer() { |
|
170
|
|
|
return $this->genericServices->getCompactEntitySerializer(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getStorageEntitySerializer() { |
|
174
|
|
|
return $this->genericServices->getStorageEntitySerializer(); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function getBaseDataModelSerializerFactory() { |
|
178
|
|
|
return $this->genericServices->getBaseDataModelSerializerFactory(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function getCompactBaseDataModelSerializerFactory() { |
|
182
|
|
|
return $this->genericServices->getCompactBaseDataModelSerializerFactory(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function getLanguageFallbackChainFactory() { |
|
186
|
|
|
return $this->genericServices->getLanguageFallbackChainFactory(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function getStringNormalizer() { |
|
190
|
|
|
return $this->genericServices->getStringNormalizer(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function getTermBuffer() { |
|
194
|
|
|
return $this->getPrefetchingTermLookup(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.