1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel; |
4
|
|
|
|
5
|
|
|
use Comparable; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
8
|
|
|
use Wikibase\DataModel\Entity\ItemIdSet; |
9
|
|
|
use Wikibase\DataModel\Facet\FacetContainer; |
10
|
|
|
use Wikibase\DataModel\Facet\NoSuchFacetException; |
11
|
|
|
use Wikibase\DataModel\Internal\FacetManager; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Immutable value object representing a link to a page on another site. |
15
|
|
|
* |
16
|
|
|
* A set of badges, represented as ItemId objects, acts as flags |
17
|
|
|
* describing attributes of the linked to page. |
18
|
|
|
* |
19
|
|
|
* @since 0.4 |
20
|
|
|
* |
21
|
|
|
* @licence GNU GPL v2+ |
22
|
|
|
* @author Jeroen De Dauw < [email protected] > |
23
|
|
|
* @author Michał Łazowik |
24
|
|
|
* @author Thiemo Mättig |
25
|
|
|
* @author Daniel Kinzler |
26
|
|
|
*/ |
27
|
|
|
class SiteLink implements Comparable, FacetContainer { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $siteId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $pageName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ItemIdSet |
41
|
|
|
*/ |
42
|
|
|
private $badges; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var FacetManager |
46
|
|
|
*/ |
47
|
|
|
private $facetManager; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $siteId |
51
|
|
|
* @param string $pageName |
52
|
|
|
* @param ItemIdSet|ItemId[]|null $badges |
53
|
|
|
* |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
*/ |
56
|
34 |
|
public function __construct( $siteId, $pageName, $badges = null ) { |
57
|
34 |
|
if ( !is_string( $siteId ) || $siteId === '' ) { |
58
|
5 |
|
throw new InvalidArgumentException( '$siteId must be a non-empty string' ); |
59
|
|
|
} |
60
|
|
|
|
61
|
29 |
|
if ( !is_string( $pageName ) || $pageName === '' ) { |
62
|
5 |
|
throw new InvalidArgumentException( '$pageName must be a non-empty string' ); |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
$this->siteId = $siteId; |
66
|
24 |
|
$this->pageName = $pageName; |
67
|
24 |
|
$this->setBadges( $badges ); |
68
|
16 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ItemIdSet|ItemId[]|null $badges |
72
|
|
|
* |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
*/ |
75
|
24 |
|
private function setBadges( $badges ) { |
76
|
24 |
|
if ( $badges === null ) { |
77
|
12 |
|
$badges = new ItemIdSet(); |
78
|
24 |
|
} elseif ( is_array( $badges ) ) { |
79
|
7 |
|
$badges = new ItemIdSet( $badges ); |
80
|
8 |
|
} elseif ( !( $badges instanceof ItemIdSet ) ) { |
81
|
4 |
|
throw new InvalidArgumentException( |
82
|
|
|
'$badges must be an instance of ItemIdSet, an array of instances of ItemId, or null' |
83
|
4 |
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
16 |
|
$this->badges = $badges; |
87
|
16 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @since 0.4 |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
3 |
|
public function getSiteId() { |
95
|
3 |
|
return $this->siteId; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @since 0.4 |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
3 |
|
public function getPageName() { |
104
|
3 |
|
return $this->pageName; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Badges are not order dependent. |
109
|
|
|
* |
110
|
|
|
* @since 0.5 |
111
|
|
|
* |
112
|
|
|
* @return ItemId[] |
113
|
|
|
*/ |
114
|
5 |
|
public function getBadges() { |
115
|
5 |
|
return array_values( iterator_to_array( $this->badges ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @see Comparable::equals |
120
|
|
|
* |
121
|
|
|
* @since 0.7.4 |
122
|
|
|
* |
123
|
|
|
* @param mixed $target |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
7 |
|
public function equals( $target ) { |
128
|
7 |
|
if ( $this === $target ) { |
129
|
3 |
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $target instanceof self |
133
|
7 |
|
&& $this->siteId === $target->siteId |
134
|
7 |
|
&& $this->pageName === $target->pageName |
135
|
7 |
|
&& $this->badges->equals( $target->badges ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $name |
140
|
|
|
* |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
1 |
|
public function hasFacet( $name ) { |
144
|
1 |
|
return $this->facetManager && $this->facetManager->hasFacet( $name ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string[] |
149
|
|
|
*/ |
150
|
1 |
|
public function listFacets() { |
151
|
1 |
|
return $this->facetManager ? $this->facetManager->listFacets() : array(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $name |
156
|
|
|
* @param string|null $type The desired type |
157
|
|
|
* |
158
|
|
|
* @return object |
159
|
|
|
*/ |
160
|
2 |
|
public function getFacet( $name, $type = null ) { |
161
|
2 |
|
if ( !$this->facetManager ) { |
162
|
|
|
throw new NoSuchFacetException( $name ); |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
return $this->facetManager->getFacet( $name, $type ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $name |
170
|
|
|
* @param object $facetObject |
171
|
|
|
*/ |
172
|
4 |
|
public function addFacet( $name, $facetObject ) { |
173
|
4 |
|
if ( !$this->facetManager ) { |
174
|
4 |
|
$this->facetManager = new FacetManager(); |
175
|
4 |
|
} |
176
|
|
|
|
177
|
4 |
|
$this->facetManager->addFacet( $name, $facetObject ); |
178
|
4 |
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|