1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Addwiki\Wikibase\Api\Service; |
4
|
|
|
|
5
|
|
|
use Addwiki\Mediawiki\DataModel\EditInfo; |
6
|
|
|
use Addwiki\Mediawiki\DataModel\Revision; |
7
|
|
|
use Addwiki\Wikibase\Api\WikibaseApi; |
8
|
|
|
use Deserializers\Deserializer; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Serializers\Serializer; |
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
|
|
|
protected WikibaseApi $api; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private \Deserializers\Deserializer $entityDeserializer; |
26
|
|
|
|
27
|
|
|
private Serializer $entitySerializer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param WikibaseApi $api |
31
|
|
|
* @param Deserializer $entityDeserializer |
32
|
|
|
*/ |
33
|
|
|
public function __construct( WikibaseApi $api, Deserializer $entityDeserializer, Serializer $entitySerializer ) { |
34
|
|
|
$this->api = $api; |
35
|
|
|
$this->entityDeserializer = $entityDeserializer; |
36
|
|
|
$this->entitySerializer = $entitySerializer; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @since 0.1 |
41
|
|
|
* @param Revision $revision |
42
|
|
|
* @param EditInfo|null $editInfo |
43
|
|
|
* |
44
|
|
|
* @throws RuntimeException |
45
|
|
|
* @throws InvalidArgumentException |
46
|
|
|
* @return Item|Property new version of the entity |
47
|
|
|
*/ |
48
|
|
|
public function save( Revision $revision, EditInfo $editInfo = null ): object { |
49
|
|
|
if ( !$revision->getContent()->getData() instanceof EntityDocument ) { |
50
|
|
|
throw new RuntimeException( 'Can only save Content of EntityDocuments' ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @var Item|Property $entity */ |
54
|
|
|
$entity = $revision->getContent()->getData(); |
55
|
|
|
$serialized = $this->entitySerializer->serialize( $entity ); |
56
|
|
|
|
57
|
|
|
$params = [ |
58
|
|
|
'data' => json_encode( $serialized ) |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$revId = $revision->getId(); |
62
|
|
|
if ( $revId !== null ) { |
63
|
|
|
$params['baserevid'] = $revId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$entityId = $entity->getId(); |
67
|
|
|
if ( $entityId !== null ) { |
68
|
|
|
$params['id'] = $entityId->getSerialization(); |
69
|
|
|
|
70
|
|
|
// If we are provided an empty entity, then set the clear flag |
71
|
|
|
if ( $entity->isEmpty() ) { |
72
|
|
|
$params['clear'] = true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Add more detail to the default "Cleared an entity" summary |
76
|
|
|
// Note: this is later overridden if a summary is provided in the EditInfo |
77
|
|
|
$params['summary'] = 'Edited a ' . $entity->getType(); |
78
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
$params['new'] = $entity->getType(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// If no editInfo is explicitly passed call back to the one in the revision? |
84
|
|
|
if ( $editInfo === null ) { |
85
|
|
|
$editInfo = $revision->getEditInfo(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$result = $this->api->postRequest( 'wbeditentity', $params, $editInfo ); |
89
|
|
|
return $this->entityDeserializer->deserialize( $result['entity'] ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|