1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Entity; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
use Wikibase\DataModel\SiteLink; |
8
|
|
|
use Wikibase\DataModel\SiteLinkList; |
9
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
10
|
|
|
use Wikibase\DataModel\Statement\StatementListHolder; |
11
|
|
|
use Wikibase\DataModel\Term\AliasesProvider; |
12
|
|
|
use Wikibase\DataModel\Term\AliasGroupList; |
13
|
|
|
use Wikibase\DataModel\Term\DescriptionsProvider; |
14
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
15
|
|
|
use Wikibase\DataModel\Term\FingerprintHolder; |
16
|
|
|
use Wikibase\DataModel\Term\LabelsProvider; |
17
|
|
|
use Wikibase\DataModel\Term\TermList; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Represents a single Wikibase item. |
21
|
|
|
* See https://www.mediawiki.org/wiki/Wikibase/DataModel#Items |
22
|
|
|
* |
23
|
|
|
* @since 0.1 |
24
|
|
|
* |
25
|
|
|
* @licence GNU GPL v2+ |
26
|
|
|
* @author Jeroen De Dauw < [email protected] > |
27
|
|
|
* @author Bene* < [email protected] > |
28
|
|
|
*/ |
29
|
|
|
class Item implements EntityDocument, FingerprintHolder, StatementListHolder, |
30
|
|
|
LabelsProvider, DescriptionsProvider, AliasesProvider { |
31
|
|
|
|
32
|
|
|
const ENTITY_TYPE = 'item'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ItemId|null |
36
|
|
|
*/ |
37
|
|
|
private $id; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Fingerprint |
41
|
|
|
*/ |
42
|
|
|
private $fingerprint; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var SiteLinkList |
46
|
|
|
*/ |
47
|
|
|
private $siteLinks; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var StatementList |
51
|
|
|
*/ |
52
|
|
|
private $statements; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @since 1.0 |
56
|
81 |
|
* |
57
|
|
|
* @param ItemId|null $id |
58
|
|
|
* @param Fingerprint|null $fingerprint |
59
|
|
|
* @param SiteLinkList|null $siteLinks |
60
|
|
|
* @param StatementList|null $statements |
61
|
|
|
*/ |
62
|
81 |
|
public function __construct( |
63
|
81 |
|
ItemId $id = null, |
64
|
81 |
|
Fingerprint $fingerprint = null, |
65
|
81 |
|
SiteLinkList $siteLinks = null, |
66
|
81 |
|
StatementList $statements = null |
67
|
|
|
) { |
68
|
|
|
$this->id = $id; |
69
|
|
|
$this->fingerprint = $fingerprint ?: new Fingerprint(); |
70
|
|
|
$this->siteLinks = $siteLinks ?: new SiteLinkList(); |
71
|
|
|
$this->statements = $statements ?: new StatementList(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
12 |
|
* Returns the id of the entity or null if it does not have one. |
76
|
12 |
|
* |
77
|
|
|
* @since 0.1 return type changed in 0.3 |
78
|
|
|
* |
79
|
|
|
* @return ItemId|null |
80
|
|
|
*/ |
81
|
|
|
public function getId() { |
82
|
|
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Can be integer since 0.1. |
87
|
|
|
* Can be ItemId since 0.5. |
88
|
2 |
|
* Can be null since 1.0. |
89
|
2 |
|
* |
90
|
1 |
|
* @param ItemId|int|null $id |
91
|
2 |
|
* |
92
|
1 |
|
* @throws InvalidArgumentException |
93
|
1 |
|
*/ |
94
|
|
|
public function setId( $id ) { |
95
|
|
|
if ( $id === null || $id instanceof ItemId ) { |
96
|
|
|
$this->id = $id; |
97
|
2 |
|
} elseif ( is_int( $id ) ) { |
98
|
|
|
$this->id = ItemId::newFromNumber( $id ); |
99
|
|
|
} else { |
100
|
|
|
throw new InvalidArgumentException( '$id must be an instance of ItemId, an integer,' |
101
|
|
|
. ' or null' ); |
102
|
|
|
} |
103
|
|
|
} |
104
|
58 |
|
|
105
|
58 |
|
/** |
106
|
|
|
* @since 0.7.3 |
107
|
|
|
* |
108
|
|
|
* @return Fingerprint |
109
|
|
|
*/ |
110
|
|
|
public function getFingerprint() { |
111
|
|
|
return $this->fingerprint; |
112
|
|
|
} |
113
|
3 |
|
|
114
|
3 |
|
/** |
115
|
3 |
|
* @since 0.7.3 |
116
|
|
|
* |
117
|
|
|
* @param Fingerprint $fingerprint |
118
|
|
|
*/ |
119
|
|
|
public function setFingerprint( Fingerprint $fingerprint ) { |
120
|
|
|
$this->fingerprint = $fingerprint; |
121
|
|
|
} |
122
|
|
|
|
123
|
12 |
|
/** |
124
|
12 |
|
* @see LabelsProvider::getLabels |
125
|
12 |
|
* |
126
|
|
|
* @return TermList |
127
|
|
|
*/ |
128
|
|
|
public function getLabels() { |
129
|
|
|
return $this->fingerprint->getLabels(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
11 |
|
* @see DescriptionsProvider::getDescriptions |
134
|
11 |
|
* |
135
|
11 |
|
* @return TermList |
136
|
|
|
*/ |
137
|
|
|
public function getDescriptions() { |
138
|
|
|
return $this->fingerprint->getDescriptions(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @see AliasesProvider::getAliasGroups |
143
|
31 |
|
* |
144
|
31 |
|
* @return AliasGroupList |
145
|
31 |
|
*/ |
146
|
|
|
public function getAliasGroups() { |
147
|
|
|
return $this->fingerprint->getAliasGroups(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $languageCode |
152
|
17 |
|
* @param string $value |
153
|
17 |
|
* |
154
|
|
|
* @throws InvalidArgumentException |
155
|
|
|
*/ |
156
|
|
|
public function setLabel( $languageCode, $value ) { |
157
|
|
|
$this->fingerprint->setLabel( $languageCode, $value ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $languageCode |
162
|
|
|
* @param string $value |
163
|
|
|
* |
164
|
|
|
* @throws InvalidArgumentException |
165
|
|
|
*/ |
166
|
|
|
public function setDescription( $languageCode, $value ) { |
167
|
|
|
$this->fingerprint->setDescription( $languageCode, $value ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $languageCode |
172
|
|
|
* @param string[] $aliases |
173
|
|
|
* |
174
|
|
|
* @throws InvalidArgumentException |
175
|
1 |
|
*/ |
176
|
1 |
|
public function setAliases( $languageCode, array $aliases ) { |
177
|
1 |
|
$this->fingerprint->setAliasGroup( $languageCode, $aliases ); |
178
|
1 |
|
} |
179
|
|
|
|
180
|
1 |
|
/** |
181
|
1 |
|
* @since 0.8 |
182
|
|
|
* |
183
|
|
|
* @return SiteLinkList |
184
|
|
|
*/ |
185
|
|
|
public function getSiteLinkList() { |
186
|
|
|
return $this->siteLinks; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @since 0.8 |
191
|
|
|
* |
192
|
|
|
* @param SiteLinkList $siteLinks |
193
|
|
|
*/ |
194
|
|
|
public function setSiteLinkList( SiteLinkList $siteLinks ) { |
195
|
|
|
$this->siteLinks = $siteLinks; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Adds a site link to the list of site links. |
200
|
|
|
* If there already is a site link with the site id of the provided site link, |
201
|
4 |
|
* then that one will be overridden by the provided one. |
202
|
4 |
|
* |
203
|
|
|
* @deprecated since 0.8, use getSiteLinkList()->addSiteLink() instead. |
204
|
|
|
* @since 0.6 |
205
|
|
|
* |
206
|
|
|
* @param SiteLink $siteLink |
207
|
|
|
*/ |
208
|
|
|
public function addSiteLink( SiteLink $siteLink ) { |
209
|
|
|
if ( $this->siteLinks->hasLinkWithSiteId( $siteLink->getSiteId() ) ) { |
210
|
|
|
$this->siteLinks->removeLinkWithSiteId( $siteLink->getSiteId() ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->siteLinks->addSiteLink( $siteLink ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Removes the sitelink with specified site ID if the Item has such a sitelink. |
218
|
|
|
* |
219
|
|
|
* @deprecated since 0.8, use getSiteLinkList()->removeLinkWithSiteId() instead. |
220
|
|
|
* @since 0.1 |
221
|
|
|
* |
222
|
|
|
* @param string $siteId the target site's id |
223
|
|
|
*/ |
224
|
|
|
public function removeSiteLink( $siteId ) { |
225
|
|
|
$this->siteLinks->removeLinkWithSiteId( $siteId ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @deprecated since 0.8, use getSiteLinkList() instead, |
230
|
|
|
* @since 0.6 |
231
|
|
|
* |
232
|
|
|
* @return SiteLink[] |
233
|
|
|
*/ |
234
|
|
|
public function getSiteLinks() { |
235
|
|
|
return array_values( iterator_to_array( $this->siteLinks ) ); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @deprecated since 0.8, use getSiteLinkList()->getBySiteId() instead. |
240
|
|
|
* @since 0.6 |
241
|
|
|
* |
242
|
|
|
* @param string $siteId |
243
|
|
|
* |
244
|
|
|
* @return SiteLink |
245
|
|
|
* @throws OutOfBoundsException |
246
|
|
|
*/ |
247
|
|
|
public function getSiteLink( $siteId ) { |
248
|
|
|
return $this->siteLinks->getBySiteId( $siteId ); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @deprecated since 0.8, use getSiteLinkList()->hasLinkWithSiteId() instead. |
253
|
|
|
* @since 0.4 |
254
|
|
|
* |
255
|
|
|
* @param string $siteId |
256
|
|
|
* |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function hasLinkToSite( $siteId ) { |
260
|
|
|
return $this->siteLinks->hasLinkWithSiteId( $siteId ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @deprecated since 0.8, use getSiteLinkList()->isEmpty() instead. |
265
|
|
|
* @since 0.5 |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
3 |
|
*/ |
269
|
3 |
|
public function hasSiteLinks() { |
270
|
3 |
|
return !$this->siteLinks->isEmpty(); |
271
|
3 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @deprecated since 2.5, use new Item() instead. |
275
|
|
|
* |
276
|
|
|
* @return self |
277
|
|
|
*/ |
278
|
|
|
public static function newEmpty() { |
279
|
|
|
return new self(); |
280
|
2 |
|
} |
281
|
2 |
|
|
282
|
2 |
|
/** |
283
|
2 |
|
* @see Entity::getType |
284
|
2 |
|
* |
285
|
|
|
* @since 0.1 |
286
|
|
|
* |
287
|
|
|
* @return string Returns the entity type "item". |
288
|
|
|
*/ |
289
|
|
|
public function getType() { |
290
|
|
|
return self::ENTITY_TYPE; |
291
|
7 |
|
} |
292
|
7 |
|
|
293
|
|
|
/** |
294
|
|
|
* Returns if the Item has no content. |
295
|
|
|
* Having an id set does not count as having content. |
296
|
|
|
* |
297
|
|
|
* @since 0.1 |
298
|
|
|
* |
299
|
|
|
* @return bool |
300
|
1 |
|
*/ |
301
|
1 |
|
public function isEmpty() { |
302
|
1 |
|
return $this->fingerprint->isEmpty() |
303
|
|
|
&& $this->statements->isEmpty() |
304
|
|
|
&& $this->siteLinks->isEmpty(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Removes all content from the Item. |
309
|
4 |
|
* The id is not part of the content. |
310
|
4 |
|
* |
311
|
|
|
* @since 0.1 |
312
|
|
|
*/ |
313
|
|
|
public function clear() { |
314
|
|
|
$this->fingerprint = new Fingerprint(); |
315
|
|
|
$this->siteLinks = new SiteLinkList(); |
316
|
|
|
$this->statements = new StatementList(); |
317
|
|
|
} |
318
|
1 |
|
|
319
|
1 |
|
/** |
320
|
1 |
|
* @since 1.0 |
321
|
|
|
* |
322
|
|
|
* @return StatementList |
323
|
|
|
*/ |
324
|
|
|
public function getStatements() { |
325
|
|
|
return $this->statements; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @since 1.0 |
330
|
|
|
* |
331
|
|
|
* @param StatementList $statements |
332
|
|
|
*/ |
333
|
|
|
public function setStatements( StatementList $statements ) { |
334
|
|
|
$this->statements = $statements; |
335
|
|
|
} |
336
|
18 |
|
|
337
|
18 |
|
/** |
338
|
|
|
* @see EntityDocument::equals |
339
|
|
|
* |
340
|
|
|
* @since 0.1 |
341
|
|
|
* |
342
|
18 |
|
* @param mixed $target |
343
|
18 |
|
* |
344
|
18 |
|
* @return bool |
345
|
|
|
*/ |
346
|
|
|
public function equals( $target ) { |
347
|
|
|
if ( $this === $target ) { |
348
|
|
|
return true; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
return $target instanceof self |
352
|
|
|
&& $this->fingerprint->equals( $target->fingerprint ) |
353
|
|
|
&& $this->siteLinks->equals( $target->siteLinks ) |
354
|
|
|
&& $this->statements->equals( $target->statements ); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @see EntityDocument::copy |
359
|
|
|
* |
360
|
|
|
* @since 0.1 |
361
|
|
|
* |
362
|
|
|
* @return self |
363
|
|
|
*/ |
364
|
|
|
public function copy() { |
365
|
|
|
return unserialize( serialize( $this ) ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
} |
369
|
|
|
|