|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Diff; |
|
4
|
|
|
|
|
5
|
|
|
use Diff\Differ\MapDiffer; |
|
6
|
|
|
use Diff\DiffOp\DiffOp; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
9
|
|
|
use Wikibase\DataModel\Entity\Property; |
|
10
|
|
|
use Wikibase\DataModel\Statement\StatementList; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @since 1.0 |
|
14
|
|
|
* |
|
15
|
|
|
* @license GPL-2.0-or-later |
|
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
17
|
|
|
*/ |
|
18
|
|
|
class PropertyDiffer implements EntityDifferStrategy { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var MapDiffer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $recursiveMapDiffer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var StatementListDiffer |
|
27
|
|
|
*/ |
|
28
|
|
|
private $statementListDiffer; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct() { |
|
31
|
3 |
|
$this->recursiveMapDiffer = new MapDiffer( true ); |
|
32
|
3 |
|
$this->statementListDiffer = new StatementListDiffer(); |
|
33
|
3 |
|
} |
|
34
|
3 |
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $entityType |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function canDiffEntityType( $entityType ) { |
|
41
|
|
|
return $entityType === 'property'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param EntityDocument $from |
|
46
|
|
|
* @param EntityDocument $to |
|
47
|
|
|
* |
|
48
|
|
|
* @return EntityDiff |
|
49
|
|
|
* @throws InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function diffEntities( EntityDocument $from, EntityDocument $to ) { |
|
52
|
|
|
$fromProperty = $this->assertIsPropertyAndCast( $from ); |
|
53
|
|
|
$toProperty = $this->assertIsPropertyAndCast( $to ); |
|
54
|
|
|
|
|
55
|
|
|
return $this->diffProperties( $fromProperty, $toProperty ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function assertIsPropertyAndCast( EntityDocument $property ): Property { |
|
59
|
2 |
|
if ( !( $property instanceof Property ) ) { |
|
60
|
2 |
|
throw new InvalidArgumentException( '$property must be an instance of Property' ); |
|
61
|
|
|
} |
|
62
|
|
|
return $property; |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function diffProperties( Property $from, Property $to ): EntityDiff { |
|
66
|
1 |
|
$diffOps = $this->diffPropertyArrays( |
|
67
|
1 |
|
$this->toDiffArray( $from ), |
|
68
|
1 |
|
$this->toDiffArray( $to ) |
|
69
|
1 |
|
); |
|
70
|
|
|
|
|
71
|
1 |
|
$diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() ); |
|
72
|
|
|
|
|
73
|
1 |
|
return new EntityDiff( $diffOps ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param array[] $from |
|
78
|
|
|
* @param array[] $to |
|
79
|
|
|
* |
|
80
|
|
|
* @return DiffOp[] |
|
81
|
|
|
*/ |
|
82
|
3 |
|
private function diffPropertyArrays( array $from, array $to ) { |
|
83
|
3 |
|
return $this->recursiveMapDiffer->doDiff( $from, $to ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Property $property |
|
88
|
|
|
* |
|
89
|
|
|
* @return array[] |
|
90
|
|
|
*/ |
|
91
|
3 |
|
private function toDiffArray( Property $property ) { |
|
92
|
3 |
|
$array = []; |
|
93
|
|
|
|
|
94
|
3 |
|
$array['aliases'] = $property->getAliasGroups()->toTextArray(); |
|
95
|
3 |
|
$array['label'] = $property->getLabels()->toTextArray(); |
|
96
|
3 |
|
$array['description'] = $property->getDescriptions()->toTextArray(); |
|
97
|
|
|
|
|
98
|
3 |
|
return $array; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param EntityDocument $entity |
|
103
|
|
|
* |
|
104
|
|
|
* @return EntityDiff |
|
105
|
|
|
* @throws InvalidArgumentException |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function getConstructionDiff( EntityDocument $entity ) { |
|
108
|
1 |
|
$property = $this->assertIsPropertyAndCast( $entity ); |
|
109
|
|
|
|
|
110
|
|
|
$diffOps = $this->diffPropertyArrays( [], $this->toDiffArray( $property ) ); |
|
111
|
1 |
|
$diffOps['claim'] = $this->statementListDiffer->getDiff( new StatementList(), $property->getStatements() ); |
|
112
|
1 |
|
|
|
113
|
|
|
return new EntityDiff( $diffOps ); |
|
114
|
1 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param EntityDocument $entity |
|
118
|
|
|
* |
|
119
|
|
|
* @return EntityDiff |
|
120
|
|
|
* @throws InvalidArgumentException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getDestructionDiff( EntityDocument $entity ) { |
|
123
|
1 |
|
$property = $this->assertIsPropertyAndCast( $entity ); |
|
124
|
1 |
|
|
|
125
|
|
|
$diffOps = $this->diffPropertyArrays( $this->toDiffArray( $property ), [] ); |
|
126
|
|
|
$diffOps['claim'] = $this->statementListDiffer->getDiff( $property->getStatements(), new StatementList() ); |
|
127
|
1 |
|
|
|
128
|
1 |
|
return new EntityDiff( $diffOps ); |
|
129
|
|
|
} |
|
130
|
1 |
|
|
|
131
|
|
|
} |
|
132
|
|
|
|