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