1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Lookup; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
7
|
|
|
use Wikibase\DataModel\Term\DescriptionsProvider; |
8
|
|
|
use Wikibase\DataModel\Term\LabelsProvider; |
9
|
|
|
use Wikibase\DataModel\Term\TermList; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @since 1.1 |
13
|
|
|
* |
14
|
|
|
* @license GPL-2.0+ |
15
|
|
|
* @author Katie Filbert < [email protected] > |
16
|
|
|
* @author Addshore |
17
|
|
|
*/ |
18
|
|
|
class EntityRetrievingTermLookup implements TermLookup { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EntityLookup |
22
|
|
|
*/ |
23
|
|
|
private $entityLookup; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TermList[] Labels in all languages, indexed by entity ID serialization. |
27
|
|
|
*/ |
28
|
|
|
private $labels; |
29
|
|
|
|
30
|
|
|
/** |
31
|
14 |
|
* @var TermList[] Descriptions in all languages, indexed by entity ID serialization. |
32
|
14 |
|
*/ |
33
|
14 |
|
private $descriptions; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param EntityLookup $entityLookup |
37
|
|
|
*/ |
38
|
|
|
public function __construct( EntityLookup $entityLookup ) { |
39
|
|
|
$this->entityLookup = $entityLookup; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @see TermLookup::getLabel() |
44
|
4 |
|
* |
45
|
4 |
|
* @param EntityId $entityId |
46
|
|
|
* @param string $languageCode |
47
|
2 |
|
* |
48
|
1 |
|
* @return string|null |
49
|
|
|
* @throws TermLookupException |
50
|
|
|
*/ |
51
|
1 |
|
public function getLabel( EntityId $entityId, $languageCode ) { |
52
|
|
|
$labels = $this->getAllLabels( $entityId, [ $languageCode ] ); |
53
|
|
|
|
54
|
|
|
if ( $labels->hasTermForLanguage( $languageCode ) ) { |
55
|
|
|
return $labels->getByLanguage( $languageCode )->getText(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see TermLookup::getLabels() |
63
|
3 |
|
* |
64
|
3 |
|
* @param EntityId $entityId |
65
|
3 |
|
* @param string[] $languages |
66
|
|
|
* |
67
|
3 |
|
* @throws TermLookupException |
68
|
|
|
* @return string[] |
69
|
|
|
*/ |
70
|
|
|
public function getLabels( EntityId $entityId, array $languages ) { |
71
|
|
|
$labels = $this->getAllLabels( $entityId, $languages )->toTextArray(); |
72
|
|
|
|
73
|
|
|
return array_intersect_key( $labels, array_flip( $languages ) ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @see TermLookup::getDescription() |
78
|
|
|
* |
79
|
4 |
|
* @param EntityId $entityId |
80
|
4 |
|
* @param string $languageCode |
81
|
|
|
* |
82
|
2 |
|
* @throws TermLookupException |
83
|
1 |
|
* @return string|null |
84
|
|
|
*/ |
85
|
|
|
public function getDescription( EntityId $entityId, $languageCode ) { |
86
|
1 |
|
$descriptions = $this->getAllDescriptions( $entityId, [ $languageCode ] ); |
87
|
|
|
|
88
|
|
|
if ( $descriptions->hasTermForLanguage( $languageCode ) ) { |
89
|
|
|
return $descriptions->getByLanguage( $languageCode )->getText(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @see TermLookup::getDescriptions() |
97
|
|
|
* |
98
|
3 |
|
* @param EntityId $entityId |
99
|
3 |
|
* @param string[] $languages |
100
|
3 |
|
* |
101
|
|
|
* @throws TermLookupException |
102
|
3 |
|
* @return string[] |
103
|
|
|
*/ |
104
|
|
|
public function getDescriptions( EntityId $entityId, array $languages ) { |
105
|
|
|
$descriptions = $this->getAllDescriptions( $entityId, $languages )->toTextArray(); |
106
|
|
|
|
107
|
|
|
return array_intersect_key( $descriptions, array_flip( $languages ) ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param EntityId $entityId |
112
|
14 |
|
* @param string[] $languageCodes Not used for filtering but in thrown exceptions. |
113
|
14 |
|
* |
114
|
|
|
* @throws TermLookupException |
115
|
14 |
|
* @return TermList |
116
|
14 |
|
*/ |
117
|
10 |
|
private function getAllLabels( EntityId $entityId, array $languageCodes ) { |
118
|
|
|
$id = $entityId->getSerialization(); |
119
|
10 |
|
|
120
|
|
|
if ( !isset( $this->labels[$id] ) ) { |
121
|
|
|
$entity = $this->fetchEntity( $entityId, $languageCodes ); |
122
|
|
|
$this->labels[$id] = $entity instanceof LabelsProvider |
123
|
|
|
? $entity->getLabels() |
124
|
|
|
: new TermList(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->labels[$id]; |
128
|
|
|
} |
129
|
14 |
|
|
130
|
|
|
/** |
131
|
14 |
|
* @param EntityId $entityId |
132
|
14 |
|
* @param string[] $languageCodes Not used for filtering but in thrown exceptions. |
133
|
2 |
|
* |
134
|
|
|
* @throws TermLookupException |
135
|
|
|
* @return TermList |
136
|
12 |
|
*/ |
137
|
2 |
|
private function getAllDescriptions( EntityId $entityId, array $languageCodes ) { |
138
|
|
|
$id = $entityId->getSerialization(); |
139
|
|
|
|
140
|
10 |
|
if ( !isset( $this->descriptions[$id] ) ) { |
141
|
|
|
$entity = $this->fetchEntity( $entityId, $languageCodes ); |
142
|
|
|
$this->descriptions[$id] = $entity instanceof DescriptionsProvider |
143
|
|
|
? $entity->getDescriptions() |
144
|
|
|
: new TermList(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->descriptions[$id]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param EntityId $entityId |
152
|
|
|
* @param string[] $languages Not used for filtering but in thrown exceptions. |
153
|
|
|
* |
154
|
|
|
* @throws TermLookupException |
155
|
|
|
* @return EntityDocument |
156
|
|
|
*/ |
157
|
|
|
private function fetchEntity( EntityId $entityId, array $languages ) { |
158
|
|
|
try { |
159
|
|
|
$entity = $this->entityLookup->getEntity( $entityId ); |
160
|
|
|
} catch ( EntityLookupException $ex ) { |
161
|
|
|
throw new TermLookupException( $entityId, $languages, 'The entity could not be loaded', $ex ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ( $entity === null ) { |
165
|
|
|
throw new TermLookupException( $entityId, $languages, 'The entity could not be loaded' ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $entity; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|