1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Api\Service; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Mediawiki\DataModel\EditInfo; |
8
|
|
|
use Mediawiki\DataModel\Revision; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use Serializers\Serializer; |
11
|
|
|
use Wikibase\Api\WikibaseApi; |
12
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
13
|
|
|
use Wikibase\DataModel\Entity\Item; |
14
|
|
|
use Wikibase\DataModel\Entity\Property; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @access private |
18
|
|
|
* |
19
|
|
|
* @author Addshore |
20
|
|
|
*/ |
21
|
|
|
class RevisionSaver { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var WikibaseApi |
25
|
|
|
*/ |
26
|
|
|
protected $api; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Deserializer |
30
|
|
|
*/ |
31
|
|
|
private $entityDeserializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Serializer |
35
|
|
|
*/ |
36
|
|
|
private $entitySerializer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param WikibaseApi $api |
40
|
|
|
* @param Deserializer $entityDeserializer |
41
|
|
|
*/ |
42
|
|
|
public function __construct( WikibaseApi $api, Deserializer $entityDeserializer, Serializer $entitySerializer ) { |
43
|
|
|
$this->api = $api; |
44
|
|
|
$this->entityDeserializer = $entityDeserializer; |
45
|
|
|
$this->entitySerializer = $entitySerializer; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @since 0.1 |
50
|
|
|
* @param Revision $revision |
51
|
|
|
* @param EditInfo|null $editInfo |
52
|
|
|
* |
53
|
|
|
* @throws RuntimeException |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
* @return Item|Property new version of the entity |
56
|
|
|
*/ |
57
|
|
|
public function save( Revision $revision, EditInfo $editInfo = null ) { |
58
|
|
|
if ( !$revision->getContent()->getData() instanceof EntityDocument ) { |
59
|
|
|
throw new RuntimeException( 'Can only save Content of EntityDocuments' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @var Item|Property $entity */ |
63
|
|
|
$entity = $revision->getContent()->getData(); |
64
|
|
|
$serialized = $this->entitySerializer->serialize( $entity ); |
65
|
|
|
|
66
|
|
|
$params = [ |
67
|
|
|
'data' => json_encode( $serialized ) |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$revId = $revision->getId(); |
71
|
|
|
if ( $revId !== null ) { |
72
|
|
|
$params['baserevid'] = $revId; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$entityId = $entity->getId(); |
76
|
|
|
if ( $entityId !== null ) { |
77
|
|
|
$params['id'] = $entityId->getSerialization(); |
78
|
|
|
|
79
|
|
|
// Always clear so that removing elements is possible |
80
|
|
|
$params['clear'] = 'true'; |
81
|
|
|
// Add more detail to the default "Cleared an entity" summary |
82
|
|
|
// Note: this is later overridden if a summary is provided in the EditInfo |
83
|
|
|
$params['summary'] = 'Edited a ' . $entity->getType(); |
84
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
$params['new'] = $entity->getType(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// If no editInfo is explicitly passed call back to the one in the revision? |
90
|
|
|
if ( $editInfo === null ) { |
91
|
|
|
$editInfo = $revision->getEditInfo(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ( $editInfo->getBot() ) { |
95
|
|
|
$params['bot'] = true; |
96
|
|
|
} |
97
|
|
|
if ( $editInfo->getMinor() ) { |
98
|
|
|
$params['minor'] = true; |
99
|
|
|
} |
100
|
|
|
$summary = $editInfo->getSummary(); |
101
|
|
|
if ( !empty( $summary ) ) { |
102
|
|
|
$params['summary'] = $summary; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$result = $this->api->postRequest( 'wbeditentity', $params, $editInfo ); |
106
|
|
|
return $this->entityDeserializer->deserialize( $result['entity'] ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|