1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
<?php
/**
* Collection of Site objects.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @since 1.21
*
* @file
* @ingroup Site
*
* @license GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class SiteList extends GenericArrayObject {
/**
* Internal site identifiers pointing to their sites offset value.
*
* @since 1.21
*
* @var array of integer
*/
protected $byInternalId = array();
/**
* Global site identifiers pointing to their sites offset value.
*
* @since 1.21
*
* @var array of string
*/
protected $byGlobalId = array();
/**
* @see GenericArrayObject::getObjectType
*
* @since 1.21
*
* @return string
*/
public function getObjectType() {
return 'Site';
}
/**
* @see GenericArrayObject::preSetElement
*
* @since 1.21
*
* @param int|string $index
* @param Site $site
*
* @return boolean
*/
protected function preSetElement( $index, $site ) {
if ( $this->hasSite( $site->getGlobalId() ) ) {
$this->removeSite( $site->getGlobalId() );
}
$this->byGlobalId[$site->getGlobalId()] = $index;
$this->byInternalId[$site->getInternalId()] = $index;
return true;
}
/**
* @see ArrayObject::offsetUnset()
*
* @since 1.21
*
* @param mixed $index
*/
public function offsetUnset( $index ) {
if ( $this->offsetExists( $index ) ) {
/**
* @var Site $site
*/
$site = $this->offsetGet( $index );
unset( $this->byGlobalId[$site->getGlobalId()] );
unset( $this->byInternalId[$site->getInternalId()] );
}
parent::offsetUnset( $index );
}
/**
* Returns all the global site identifiers.
* Optionally only those belonging to the specified group.
*
* @since 1.21
*
* @return array
*/
public function getGlobalIdentifiers() {
return array_keys( $this->byGlobalId );
}
/**
* Returns if the list contains the site with the provided global site identifier.
*
* @param string $globalSiteId
*
* @return boolean
*/
public function hasSite( $globalSiteId ) {
return array_key_exists( $globalSiteId, $this->byGlobalId );
}
/**
* Returns the Site with the provided global site identifier.
* The site needs to exist, so if not sure, call hasGlobalId first.
*
* @since 1.21
*
* @param string $globalSiteId
*
* @return Site
*/
public function getSite( $globalSiteId ) {
return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
}
/**
* Removes the site with the specified global site identifier.
* The site needs to exist, so if not sure, call hasGlobalId first.
*
* @since 1.21
*
* @param string $globalSiteId
*/
public function removeSite( $globalSiteId ) {
$this->offsetUnset( $this->byGlobalId[$globalSiteId] );
}
/**
* Returns if the list contains no sites.
*
* @since 1.21
*
* @return boolean
*/
public function isEmpty() {
return $this->byGlobalId === array();
}
/**
* Returns if the list contains the site with the provided site id.
*
* @param integer $id
*
* @return boolean
*/
public function hasInternalId( $id ) {
return array_key_exists( $id, $this->byInternalId );
}
/**
* Returns the Site with the provided site id.
* The site needs to exist, so if not sure, call has first.
*
* @since 1.21
*
* @param integer $id
*
* @return Site
*/
public function getSiteByInternalId( $id ) {
return $this->offsetGet( $this->byInternalId[$id] );
}
/**
* Removes the site with the specified site id.
* The site needs to exist, so if not sure, call has first.
*
* @since 1.21
*
* @param integer $id
*/
public function removeSiteByInternalId( $id ) {
$this->offsetUnset( $this->byInternalId[$id] );
}
/**
* Sets a site in the list. If the site was not there,
* it will be added. If it was, it will be updated.
*
* @since 1.21
*
* @param Site $site
*/
public function setSite( Site $site ) {
$this[] = $site;
}
/**
* Returns the sites that are in the provided group.
*
* @since 1.21
*
* @param string $groupName
*
* @return SiteList
*/
public function getGroup( $groupName ) {
$group = new self();
/**
* @var \Site $site
*/
foreach ( $this as $site ) {
if ( $site->getGroup() === $groupName ) {
$group[] = $site;
}
}
return $group;
}
/**
* A version ID that identifies the serialization structure used by getSerializationData()
* and unserialize(). This is useful for constructing cache keys in cases where the cache relies
* on serialization for storing the SiteList.
*
* @var string A string uniquely identifying the version of the serialization structure,
* not including any sub-structures.
*/
const SERIAL_VERSION_ID = '2013-02-07';
/**
* Returns the version ID that identifies the serialization structure used by
* getSerializationData() and unserialize(), including the structure of any nested structures.
* This is useful for constructing cache keys in cases where the cache relies
* on serialization for storing the SiteList.
*
* @return string A string uniquely identifying the version of the serialization structure,
* including any sub-structures.
*/
public static function getSerialVersionId() {
return self::SERIAL_VERSION_ID . '+Site:' . Site::SERIAL_VERSION_ID;
}
/**
* @see GenericArrayObject::getSerializationData
*
* @since 1.21
*
* @return array
*/
protected function getSerializationData() {
//NOTE: When changing the structure, either implement unserialize() to handle the
// old structure too, or update SERIAL_VERSION_ID to kill any caches.
return array_merge(
parent::getSerializationData(),
array(
'internalIds' => $this->byInternalId,
'globalIds' => $this->byGlobalId,
)
);
}
/**
* @see GenericArrayObject::unserialize
*
* @since 1.21
*
* @param string $serialization
*
* @return array
*/
public function unserialize( $serialization ) {
$serializationData = parent::unserialize( $serialization );
$this->byInternalId = $serializationData['internalIds'];
$this->byGlobalId = $serializationData['globalIds'];
return $serializationData;
}
}
/**
* @deprecated
*/
class SiteArray extends SiteList {}
|