1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Diff; |
4
|
|
|
|
5
|
|
|
use Diff\Differ\MapDiffer; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
8
|
|
|
use Wikibase\DataModel\Entity\Item; |
9
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
10
|
|
|
use Wikibase\DataModel\Services\Diff\Internal\StatementListDiffer; |
11
|
|
|
use Wikibase\DataModel\SiteLinkList; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @since 1.0 |
15
|
|
|
* |
16
|
|
|
* @licence GNU GPL v2+ |
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
18
|
|
|
*/ |
19
|
|
|
class ItemDiffer implements EntityDifferStrategy { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MapDiffer |
23
|
|
|
*/ |
24
|
|
|
private $recursiveMapDiffer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var StatementListDiffer |
28
|
|
|
*/ |
29
|
|
|
private $statementListDiffer; |
30
|
|
|
|
31
|
6 |
|
public function __construct() { |
32
|
6 |
|
$this->recursiveMapDiffer = new MapDiffer( true ); |
33
|
6 |
|
$this->statementListDiffer = new StatementListDiffer(); |
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $entityType |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function canDiffEntityType( $entityType ) { |
42
|
|
|
return $entityType === 'item'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param EntityDocument $from |
47
|
|
|
* @param EntityDocument $to |
48
|
|
|
* |
49
|
|
|
* @return ItemDiff |
50
|
|
|
* @throws InvalidArgumentException |
51
|
|
|
*/ |
52
|
4 |
|
public function diffEntities( EntityDocument $from, EntityDocument $to ) { |
53
|
4 |
|
$this->assertIsItem( $from ); |
54
|
4 |
|
$this->assertIsItem( $to ); |
55
|
|
|
|
56
|
4 |
|
return $this->diffItems( $from, $to ); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
private function assertIsItem( EntityDocument $item ) { |
60
|
4 |
|
if ( !( $item instanceof Item ) ) { |
61
|
|
|
throw new InvalidArgumentException( '$item must be an instance of Item' ); |
62
|
|
|
} |
63
|
4 |
|
} |
64
|
|
|
|
65
|
6 |
|
public function diffItems( Item $from, Item $to ) { |
66
|
6 |
|
$diffOps = $this->recursiveMapDiffer->doDiff( |
67
|
6 |
|
$this->toDiffArray( $from ), |
68
|
6 |
|
$this->toDiffArray( $to ) |
69
|
6 |
|
); |
70
|
|
|
|
71
|
6 |
|
$diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() ); |
72
|
|
|
|
73
|
6 |
|
return new ItemDiff( $diffOps ); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
private function toDiffArray( Item $item ) { |
77
|
6 |
|
$array = array(); |
78
|
|
|
|
79
|
6 |
|
$array['aliases'] = $item->getFingerprint()->getAliasGroups()->toTextArray(); |
80
|
6 |
|
$array['label'] = $item->getFingerprint()->getLabels()->toTextArray(); |
81
|
6 |
|
$array['description'] = $item->getFingerprint()->getDescriptions()->toTextArray(); |
82
|
6 |
|
$array['links'] = $this->getSiteLinksInDiffFormat( $item->getSiteLinkList() ); |
83
|
|
|
|
84
|
6 |
|
return $array; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
private function getSiteLinksInDiffFormat( SiteLinkList $siteLinks ) { |
88
|
6 |
|
$linksInDiffFormat = array(); |
89
|
|
|
|
90
|
|
|
foreach ( $siteLinks->toArray() as $siteLink ) { |
91
|
|
|
$linksInDiffFormat[$siteLink->getSiteId()] = array( |
92
|
|
|
'name' => $siteLink->getPageName(), |
93
|
6 |
|
'badges' => array_map( |
94
|
1 |
|
function( ItemId $id ) { |
95
|
1 |
|
return $id->getSerialization(); |
96
|
1 |
|
}, |
97
|
1 |
|
$siteLink->getBadges() |
98
|
|
|
) |
99
|
1 |
|
); |
100
|
1 |
|
} |
101
|
1 |
|
|
102
|
1 |
|
return $linksInDiffFormat; |
103
|
6 |
|
} |
104
|
|
|
|
105
|
6 |
|
/** |
106
|
|
|
* @param EntityDocument $entity |
107
|
|
|
* |
108
|
|
|
* @return ItemDiff |
109
|
|
|
* @throws InvalidArgumentException |
110
|
|
|
*/ |
111
|
|
|
public function getConstructionDiff( EntityDocument $entity ) { |
112
|
|
|
$this->assertIsItem( $entity ); |
113
|
|
|
return $this->diffEntities( new Item(), $entity ); |
114
|
2 |
|
} |
115
|
2 |
|
|
116
|
2 |
|
/** |
117
|
|
|
* @param EntityDocument $entity |
118
|
|
|
* |
119
|
|
|
* @return ItemDiff |
120
|
|
|
* @throws InvalidArgumentException |
121
|
|
|
*/ |
122
|
|
|
public function getDestructionDiff( EntityDocument $entity ) { |
123
|
|
|
$this->assertIsItem( $entity ); |
124
|
|
|
return $this->diffEntities( $entity, new Item() ); |
125
|
1 |
|
} |
126
|
1 |
|
|
127
|
|
|
} |
128
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.