1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Diff\Internal; |
4
|
|
|
|
5
|
|
|
use Diff\DiffOp\Diff\Diff; |
6
|
|
|
use Diff\Patcher\ListPatcher; |
7
|
|
|
use Diff\Patcher\MapPatcher; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
10
|
|
|
use Wikibase\DataModel\SiteLinkList; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Package private. |
14
|
|
|
* |
15
|
|
|
* @license GPL-2.0-or-later |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
* @author Thiemo Kreuz |
18
|
|
|
*/ |
19
|
|
|
class SiteLinkListPatcher { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MapPatcher |
23
|
|
|
*/ |
24
|
|
|
private $patcher; |
25
|
|
|
|
26
|
|
|
public function __construct() { |
27
|
3 |
|
$this->patcher = new MapPatcher( false, new ListPatcher() ); |
28
|
3 |
|
} |
29
|
3 |
|
|
30
|
|
|
/** |
31
|
|
|
* @param SiteLinkList $siteLinks |
32
|
|
|
* @param Diff $patch |
33
|
|
|
* |
34
|
|
|
* @return SiteLinkList |
35
|
|
|
* @throws InvalidArgumentException |
36
|
|
|
*/ |
37
|
|
|
public function getPatchedSiteLinkList( SiteLinkList $siteLinks, Diff $patch ) { |
38
|
3 |
|
$baseData = $this->getSiteLinksInDiffFormat( $siteLinks ); |
39
|
3 |
|
$patchedData = $this->patcher->patch( $baseData, $patch ); |
40
|
3 |
|
|
41
|
|
|
$patchedSiteLinks = new SiteLinkList(); |
42
|
3 |
|
|
43
|
|
|
foreach ( $patchedData as $siteId => $siteLinkData ) { |
44
|
3 |
|
if ( array_key_exists( 'name', $siteLinkData ) ) { |
45
|
3 |
|
$patchedSiteLinks->addNewSiteLink( |
46
|
3 |
|
$siteId, |
47
|
3 |
|
$siteLinkData['name'], |
48
|
3 |
|
$this->getBadgesFromSiteLinkData( $siteLinkData ) |
|
|
|
|
49
|
3 |
|
); |
50
|
3 |
|
} |
51
|
3 |
|
} |
52
|
3 |
|
|
53
|
|
|
return $patchedSiteLinks; |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string[] $siteLinkData |
58
|
|
|
* |
59
|
|
|
* @return ItemId[]|null |
60
|
|
|
*/ |
61
|
|
|
private function getBadgesFromSiteLinkData( array $siteLinkData ) { |
62
|
3 |
|
if ( !array_key_exists( 'badges', $siteLinkData ) ) { |
63
|
3 |
|
return null; |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
return array_map( |
67
|
2 |
|
function( $idSerialization ) { |
68
|
|
|
return new ItemId( $idSerialization ); |
69
|
1 |
|
}, |
70
|
2 |
|
$siteLinkData['badges'] |
71
|
2 |
|
); |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
private function getSiteLinksInDiffFormat( SiteLinkList $siteLinks ) { |
75
|
3 |
|
$linksInDiffFormat = []; |
76
|
3 |
|
|
77
|
|
|
foreach ( $siteLinks->toArray() as $siteLink ) { |
78
|
|
|
$linksInDiffFormat[$siteLink->getSiteId()] = [ |
79
|
|
|
'name' => $siteLink->getPageName(), |
80
|
|
|
'badges' => array_map( |
81
|
3 |
|
function( ItemId $id ) { |
82
|
2 |
|
return $id->getSerialization(); |
83
|
2 |
|
}, |
84
|
2 |
|
$siteLink->getBadges() |
85
|
2 |
|
) |
86
|
1 |
|
]; |
87
|
2 |
|
} |
88
|
2 |
|
|
89
|
2 |
|
return $linksInDiffFormat; |
90
|
2 |
|
} |
91
|
3 |
|
|
92
|
|
|
} |
93
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.