1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Api\Service; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Mediawiki\DataModel\EditInfo; |
7
|
|
|
use Serializers\Serializer; |
8
|
|
|
use UnexpectedValueException; |
9
|
|
|
use Wikibase\Api\WikibaseApi; |
10
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
11
|
|
|
use Wikibase\DataModel\Entity\Item; |
12
|
|
|
use Wikibase\DataModel\Entity\Property; |
13
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
14
|
|
|
use Wikibase\DataModel\Snak\Snak; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @access private |
18
|
|
|
* |
19
|
|
|
* @author Addshore |
20
|
|
|
*/ |
21
|
|
|
class StatementCreator { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var WikibaseApi |
25
|
|
|
*/ |
26
|
|
|
private $api; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Deserializer |
30
|
|
|
*/ |
31
|
|
|
private $dataValueSerializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param WikibaseApi $api |
35
|
|
|
* @param Serializer $dataValueSerializer |
36
|
|
|
*/ |
37
|
|
|
public function __construct( WikibaseApi $api, Serializer $dataValueSerializer ) { |
38
|
|
|
$this->api = $api; |
39
|
|
|
$this->dataValueSerializer = $dataValueSerializer; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @since 0.2 |
44
|
|
|
* |
45
|
|
|
* @param Snak $mainSnak |
46
|
|
|
* @param EntityId|Item|Property|string $target |
47
|
|
|
* @param EditInfo|null $editInfo |
48
|
|
|
* |
49
|
|
|
* @return string the GUID of the claim |
50
|
|
|
* @throws UnexpectedValueException |
51
|
|
|
*/ |
52
|
|
|
public function create( Snak $mainSnak, $target, EditInfo $editInfo = null ) { |
53
|
|
|
if ( is_string( $target ) ) { |
54
|
|
|
$entityId = $target; |
55
|
|
|
} elseif ( $target instanceof EntityId ) { |
56
|
|
|
$entityId = $target->getSerialization(); |
57
|
|
|
} elseif ( $target instanceof Item || $target instanceof Property ) { |
58
|
|
|
$entityId = $target->getId()->getSerialization(); |
59
|
|
|
} else { |
60
|
|
|
throw new UnexpectedValueException( '$target needs to be an EntityId, Entity or string' ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$params = [ |
64
|
|
|
'entity' => $entityId, |
65
|
|
|
'snaktype' => $mainSnak->getType(), |
66
|
|
|
'property' => $mainSnak->getPropertyId()->getSerialization(), |
67
|
|
|
]; |
68
|
|
|
if ( $mainSnak instanceof PropertyValueSnak ) { |
69
|
|
|
$serializedDataValue = $this->dataValueSerializer->serialize( $mainSnak->getDataValue() ); |
70
|
|
|
$params['value'] = json_encode( $serializedDataValue['value'] ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$result = $this->api->postRequest( 'wbcreateclaim', $params, $editInfo ); |
74
|
|
|
return $result['claim']['id']; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..