|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Statement; |
|
4
|
|
|
|
|
5
|
|
|
use Comparable; |
|
6
|
|
|
use Hashable; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
9
|
|
|
use Wikibase\DataModel\Facet\FacetContainer; |
|
10
|
|
|
use Wikibase\DataModel\Facet\NoSuchFacetException; |
|
11
|
|
|
use Wikibase\DataModel\Internal\FacetManager; |
|
12
|
|
|
use Wikibase\DataModel\PropertyIdProvider; |
|
13
|
|
|
use Wikibase\DataModel\Reference; |
|
14
|
|
|
use Wikibase\DataModel\ReferenceList; |
|
15
|
|
|
use Wikibase\DataModel\Snak\Snak; |
|
16
|
|
|
use Wikibase\DataModel\Snak\SnakList; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class representing a Wikibase statement. |
|
20
|
|
|
* See https://www.mediawiki.org/wiki/Wikibase/DataModel#Statements |
|
21
|
|
|
* |
|
22
|
|
|
* @since 0.1 |
|
23
|
|
|
* |
|
24
|
|
|
* @licence GNU GPL v2+ |
|
25
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
26
|
|
|
* @author Bene* < [email protected] > |
|
27
|
|
|
* @author Daniel Kinzler |
|
28
|
|
|
*/ |
|
29
|
|
|
class Statement implements Hashable, Comparable, PropertyIdProvider, FacetContainer { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Rank enum. Higher values are more preferred. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 2.0 |
|
35
|
|
|
*/ |
|
36
|
|
|
const RANK_PREFERRED = 2; |
|
37
|
|
|
const RANK_NORMAL = 1; |
|
38
|
|
|
const RANK_DEPRECATED = 0; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string|null |
|
42
|
|
|
*/ |
|
43
|
|
|
private $guid = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Snak |
|
47
|
|
|
*/ |
|
48
|
|
|
private $mainSnak; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The property value snaks making up the qualifiers for this statement. |
|
52
|
|
|
* |
|
53
|
|
|
* @var SnakList |
|
54
|
|
|
*/ |
|
55
|
|
|
private $qualifiers; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var ReferenceList |
|
59
|
|
|
*/ |
|
60
|
|
|
private $references; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var integer, element of the Statement::RANK_ enum |
|
64
|
|
|
*/ |
|
65
|
|
|
private $rank = self::RANK_NORMAL; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var FacetManager |
|
69
|
|
|
*/ |
|
70
|
|
|
private $facetManager; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @since 2.0 |
|
74
|
|
|
* |
|
75
|
|
|
* @param Snak $mainSnak |
|
76
|
|
|
* @param SnakList|null $qualifiers |
|
77
|
|
|
* @param ReferenceList|null $references |
|
78
|
|
|
* @param string|null $guid |
|
79
|
|
|
*/ |
|
80
|
23 |
|
public function __construct( |
|
81
|
|
|
Snak $mainSnak, |
|
82
|
|
|
SnakList $qualifiers = null, |
|
83
|
|
|
ReferenceList $references = null, |
|
84
|
|
|
$guid = null |
|
85
|
|
|
) { |
|
86
|
23 |
|
$this->mainSnak = $mainSnak; |
|
87
|
23 |
|
$this->qualifiers = $qualifiers ?: new SnakList(); |
|
88
|
23 |
|
$this->references = $references ?: new ReferenceList(); |
|
89
|
23 |
|
$this->setGuid( $guid ); |
|
90
|
19 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the GUID of this statement. |
|
94
|
|
|
* |
|
95
|
|
|
* @since 0.2 |
|
96
|
|
|
* |
|
97
|
|
|
* @return string|null |
|
98
|
|
|
*/ |
|
99
|
11 |
|
public function getGuid() { |
|
100
|
11 |
|
return $this->guid; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Sets the GUID of this statement. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 0.2 |
|
107
|
|
|
* |
|
108
|
|
|
* @param string|null $guid |
|
109
|
|
|
* |
|
110
|
|
|
* @throws InvalidArgumentException |
|
111
|
|
|
*/ |
|
112
|
31 |
|
public function setGuid( $guid ) { |
|
113
|
31 |
|
if ( !is_string( $guid ) && $guid !== null ) { |
|
114
|
8 |
|
throw new InvalidArgumentException( '$guid must be a string or null' ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
27 |
|
$this->guid = $guid; |
|
118
|
27 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns the main value snak of this statement. |
|
122
|
|
|
* |
|
123
|
|
|
* @since 0.1 |
|
124
|
|
|
* |
|
125
|
|
|
* @return Snak |
|
126
|
|
|
*/ |
|
127
|
9 |
|
public function getMainSnak() { |
|
128
|
9 |
|
return $this->mainSnak; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Sets the main value snak of this statement. |
|
133
|
|
|
* |
|
134
|
|
|
* @since 0.1 |
|
135
|
|
|
* |
|
136
|
|
|
* @param Snak $mainSnak |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setMainSnak( Snak $mainSnak ) { |
|
139
|
|
|
$this->mainSnak = $mainSnak; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the property value snaks making up the qualifiers for this statement. |
|
144
|
|
|
* |
|
145
|
|
|
* @since 0.1 |
|
146
|
|
|
* |
|
147
|
|
|
* @return SnakList |
|
148
|
|
|
*/ |
|
149
|
8 |
|
public function getQualifiers() { |
|
150
|
8 |
|
return $this->qualifiers; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets the property value snaks making up the qualifiers for this statement. |
|
155
|
|
|
* |
|
156
|
|
|
* @since 0.1 |
|
157
|
|
|
* |
|
158
|
|
|
* @param SnakList $propertySnaks |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setQualifiers( SnakList $propertySnaks ) { |
|
161
|
|
|
$this->qualifiers = $propertySnaks; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the references attached to this statement. |
|
166
|
|
|
* |
|
167
|
|
|
* @since 0.1 |
|
168
|
|
|
* |
|
169
|
|
|
* @return ReferenceList |
|
170
|
|
|
*/ |
|
171
|
23 |
|
public function getReferences() { |
|
172
|
23 |
|
return $this->references; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Sets the references attached to this statement. |
|
177
|
|
|
* |
|
178
|
|
|
* @since 0.1 |
|
179
|
|
|
* |
|
180
|
|
|
* @param ReferenceList $references |
|
181
|
|
|
*/ |
|
182
|
4 |
|
public function setReferences( ReferenceList $references ) { |
|
183
|
4 |
|
$this->references = $references; |
|
184
|
4 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @since 2.0 |
|
188
|
|
|
* |
|
189
|
|
|
* @param Snak[]|Snak $snaks |
|
190
|
|
|
* @param Snak [$snak2,...] |
|
191
|
|
|
* |
|
192
|
|
|
* @throws InvalidArgumentException |
|
193
|
|
|
*/ |
|
194
|
8 |
|
public function addNewReference( $snaks = array() /*...*/ ) { |
|
195
|
8 |
|
if ( $snaks instanceof Snak ) { |
|
196
|
4 |
|
$snaks = func_get_args(); |
|
197
|
4 |
|
} |
|
198
|
|
|
|
|
199
|
8 |
|
$this->references->addNewReference( $snaks ); |
|
200
|
8 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sets the rank of the statement. |
|
204
|
|
|
* The rank is an element of the Statement::RANK_ enum. |
|
205
|
|
|
* |
|
206
|
|
|
* @since 0.1 |
|
207
|
|
|
* |
|
208
|
|
|
* @param integer $rank |
|
209
|
|
|
* @throws InvalidArgumentException |
|
210
|
|
|
*/ |
|
211
|
9 |
|
public function setRank( $rank ) { |
|
212
|
9 |
|
$ranks = array( self::RANK_DEPRECATED, self::RANK_NORMAL, self::RANK_PREFERRED ); |
|
213
|
|
|
|
|
214
|
9 |
|
if ( !in_array( $rank, $ranks, true ) ) { |
|
215
|
4 |
|
throw new InvalidArgumentException( 'Invalid rank specified for statement: ' . var_export( $rank, true ) ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
5 |
|
$this->rank = $rank; |
|
219
|
5 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @since 0.1 |
|
223
|
|
|
* |
|
224
|
|
|
* @return integer |
|
225
|
|
|
*/ |
|
226
|
8 |
|
public function getRank() { |
|
227
|
8 |
|
return $this->rank; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @see Hashable::getHash |
|
232
|
|
|
* |
|
233
|
|
|
* @since 0.1 |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
5 |
|
public function getHash() { |
|
238
|
5 |
|
return sha1( implode( |
|
239
|
5 |
|
'|', |
|
240
|
|
|
array( |
|
241
|
5 |
|
sha1( $this->mainSnak->getHash() . $this->qualifiers->getHash() ), |
|
242
|
5 |
|
$this->rank, |
|
243
|
5 |
|
$this->references->getValueHash(), |
|
244
|
|
|
) |
|
245
|
5 |
|
) ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Returns the id of the property of the main snak. |
|
250
|
|
|
* Short for ->getMainSnak()->getPropertyId() |
|
251
|
|
|
* |
|
252
|
|
|
* @see PropertyIdProvider::getPropertyId |
|
253
|
|
|
* |
|
254
|
|
|
* @since 0.2 |
|
255
|
|
|
* |
|
256
|
|
|
* @return PropertyId |
|
257
|
|
|
*/ |
|
258
|
4 |
|
public function getPropertyId() { |
|
259
|
4 |
|
return $this->getMainSnak()->getPropertyId(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Returns a list of all Snaks on this statement. This includes the main snak and all snaks |
|
264
|
|
|
* from qualifiers and references. |
|
265
|
|
|
* |
|
266
|
|
|
* This is a convenience method for use in code that needs to operate on all snaks, e.g. |
|
267
|
|
|
* to find all referenced Entities. |
|
268
|
|
|
* |
|
269
|
|
|
* @return Snak[] |
|
270
|
|
|
*/ |
|
271
|
4 |
|
public function getAllSnaks() { |
|
272
|
4 |
|
$snaks = array( $this->mainSnak ); |
|
273
|
|
|
|
|
274
|
4 |
|
foreach ( $this->qualifiers as $qualifier ) { |
|
275
|
|
|
$snaks[] = $qualifier; |
|
276
|
4 |
|
} |
|
277
|
|
|
|
|
278
|
|
|
/* @var Reference $reference */ |
|
279
|
4 |
|
foreach ( $this->getReferences() as $reference ) { |
|
280
|
1 |
|
foreach ( $reference->getSnaks() as $referenceSnak ) { |
|
281
|
1 |
|
$snaks[] = $referenceSnak; |
|
282
|
1 |
|
} |
|
283
|
4 |
|
} |
|
284
|
|
|
|
|
285
|
4 |
|
return $snaks; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @see Comparable::equals |
|
290
|
|
|
* |
|
291
|
|
|
* @since 0.7.4 |
|
292
|
|
|
* |
|
293
|
|
|
* @param mixed $target |
|
294
|
|
|
* |
|
295
|
|
|
* @return bool |
|
296
|
|
|
*/ |
|
297
|
12 |
|
public function equals( $target ) { |
|
298
|
12 |
|
if ( $this === $target ) { |
|
299
|
1 |
|
return true; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return $target instanceof self |
|
303
|
12 |
|
&& $this->guid === $target->guid |
|
304
|
12 |
|
&& $this->rank === $target->rank |
|
305
|
12 |
|
&& $this->mainSnak->equals( $target->mainSnak ) |
|
306
|
12 |
|
&& $this->qualifiers->equals( $target->qualifiers ) |
|
307
|
12 |
|
&& $this->references->equals( $target->references ); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @param string $name |
|
312
|
|
|
* |
|
313
|
|
|
* @return boolean |
|
314
|
|
|
*/ |
|
315
|
4 |
|
public function hasFacet( $name ) { |
|
316
|
4 |
|
return $this->facetManager && $this->facetManager->hasFacet( $name ); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @return string[] |
|
321
|
|
|
*/ |
|
322
|
4 |
|
public function listFacets() { |
|
323
|
4 |
|
return $this->facetManager ? $this->facetManager->listFacets() : array(); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @param string $name |
|
328
|
|
|
* @param string|null $type The desired type |
|
329
|
|
|
* |
|
330
|
|
|
* @return object |
|
331
|
|
|
*/ |
|
332
|
8 |
|
public function getFacet( $name, $type = null ) { |
|
333
|
8 |
|
if ( !$this->facetManager ) { |
|
334
|
|
|
throw new NoSuchFacetException( $name ); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
8 |
|
return $this->facetManager->getFacet( $name, $type ); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @param string $name |
|
342
|
|
|
* @param object $facetObject |
|
343
|
|
|
*/ |
|
344
|
16 |
|
public function addFacet( $name, $facetObject ) { |
|
345
|
16 |
|
if ( !$this->facetManager ) { |
|
346
|
16 |
|
$this->facetManager = new FacetManager(); |
|
347
|
16 |
|
} |
|
348
|
|
|
|
|
349
|
16 |
|
$this->facetManager->addFacet( $name, $facetObject ); |
|
350
|
16 |
|
} |
|
351
|
|
|
|
|
352
|
|
|
} |
|
353
|
|
|
|