Debian Codegraph @

includes/site/SiteList.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Site;
22
23 use ArrayObject;
24 use InvalidArgumentException;
25
26 /**
27 * Array-like collection of Site objects.
28 *
29 * This uses ArrayObject to intercept additions and deletions for purposes
30 * such as additional indexing, and to enforce that values are restricted
31 * to Site objects only.
32 *
33 * @since 1.21
34 * @ingroup Site
35 * @author Jeroen De Dauw < [email protected] >
36 */
37 class SiteList extends ArrayObject {
38 /**
39 * @see SiteList::getNewOffset()
40 * @since 1.20
41 * @var int
42 */
43 protected $indexOffset = 0;
44
45 /**
46 * Internal site identifiers pointing to their sites offset value.
47 *
48 * @since 1.21
49 *
50 * @var array Maps int identifiers to local ArrayObject keys
51 */
52 protected $byInternalId = [];
53
54 /**
55 * Global site identifiers pointing to their sites offset value.
56 *
57 * @since 1.21
58 *
59 * @var array Maps string identifiers to local ArrayObject keys
60 */
61 protected $byGlobalId = [];
62
63 /**
64 * Navigational site identifiers alias inter-language prefixes
65 * pointing to their sites offset value.
66 *
67 * @since 1.23
68 *
69 * @var array Maps string identifiers to local ArrayObject keys
70 */
71 protected $byNavigationId = [];
72
73 /**
74 * @see Overrides ArrayObject::__construct https://www.php.net/manual/en/arrayobject.construct.php
75 * @since 1.20
76 * @param null|array $input
77 * @param int $flags
78 * @param string $iterator_class
79 */
80 public function __construct( $input = null, $flags = 0, $iterator_class = 'ArrayIterator' ) {
81 parent::__construct( [], $flags, $iterator_class );
82
83 if ( $input !== null ) {
84 foreach ( $input as $offset => $value ) {
85 $this->offsetSet( $offset, $value );
86 }
87 }
88 }
89
90 /**
91 * @see Overrides ArrayObject::append
92 * @since 1.20
93 * @param mixed $value
94 */
95 public function append( $value ): void {
96 $this->setElement( null, $value );
97 }
98
99 /**
100 * @since 1.20
101 * @see Overrides ArrayObject::offsetSet()
102 * @param mixed $index
103 * @param mixed $value
104 */
105 public function offsetSet( $index, $value ): void {
106 $this->setElement( $index, $value );
107 }
108
109 /**
110 * Returns if the provided value has the same type as the elements
111 * that can be added to this ArrayObject.
112 *
113 * @since 1.20
114 * @param mixed $value
115 * @return bool
116 */
117 protected function hasValidType( $value ) {
118 $class = $this->getObjectType();
119 return $value instanceof $class;
120 }
121
122 /**
123 * The class or interface type that array elements must match.
124 *
125 * @since 1.21
126 * @return string
127 */
128 public function getObjectType() {
129 return Site::class;
130 }
131
132 /**
133 * Find a new offset for when appending an element.
134 *
135 * @since 1.20
136 * @return int
137 */
138 protected function getNewOffset() {
139 while ( $this->offsetExists( $this->indexOffset ) ) {
140 $this->indexOffset++;
141 }
142
143 return $this->indexOffset;
144 }
145
146 /**
147 * Actually set the element and enforce type checking and offset resolving.
148 *
149 * @since 1.20
150 * @param int|string|null $index
151 * @param Site $site
152 */
153 protected function setElement( $index, $site ) {
154 if ( !$this->hasValidType( $site ) ) {
155 throw new InvalidArgumentException(
156 'Can only add ' . $this->getObjectType() . ' implementing objects to ' . static::class . '.'
157 );
158 }
159
160 $index ??= $this->getNewOffset();
161
162 if ( $this->hasSite( $site->getGlobalId() ) ) {
163 $this->removeSite( $site->getGlobalId() );
164 }
165
166 $this->byGlobalId[$site->getGlobalId() ?? ''] = $index;
167 $internalId = $site->getInternalId();
168 if ( $internalId !== null ) {
169 $this->byInternalId[$internalId] = $index;
170 }
171
172 $ids = $site->getNavigationIds();
173 foreach ( $ids as $navId ) {
174 $this->byNavigationId[$navId] = $index;
175 }
176
177 parent::offsetSet( $index, $site );
178 }
179
180 /**
181 * @see ArrayObject::offsetUnset()
182 *
183 * @since 1.21
184 *
185 * @param mixed $index
186 */
187 public function offsetUnset( $index ): void {
188 if ( $this->offsetExists( $index ) ) {
189 /**
190 * @var Site $site
191 */
192 $site = $this->offsetGet( $index );
193
194 unset( $this->byGlobalId[$site->getGlobalId()] );
195 $internalId = $site->getInternalId();
196 if ( $internalId !== null ) {
197 unset( $this->byInternalId[$internalId] );
198 }
199
200 $ids = $site->getNavigationIds();
201 foreach ( $ids as $navId ) {
202 unset( $this->byNavigationId[$navId] );
203 }
204 }
205
206 parent::offsetUnset( $index );
207 }
208
209 /**
210 * Returns all the global site identifiers.
211 * Optionally only those belonging to the specified group.
212 *
213 * @since 1.21
214 *
215 * @return array
216 */
217 public function getGlobalIdentifiers() {
218 return array_keys( $this->byGlobalId );
219 }
220
221 /**
222 * Returns if the list contains the site with the provided global site identifier.
223 *
224 * @param string $globalSiteId
225 *
226 * @return bool
227 */
228 public function hasSite( $globalSiteId ) {
229 return array_key_exists( $globalSiteId ?? '', $this->byGlobalId );
230 }
231
232 /**
233 * Returns the Site with the provided global site identifier.
234 * The site needs to exist, so if not sure, call hasGlobalId first.
235 *
236 * @since 1.21
237 *
238 * @param string $globalSiteId
239 *
240 * @return Site
241 */
242 public function getSite( $globalSiteId ) {
243 return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
244 }
245
246 /**
247 * Removes the site with the specified global site identifier.
248 * The site needs to exist, so if not sure, call hasGlobalId first.
249 *
250 * @since 1.21
251 *
252 * @param string $globalSiteId
253 */
254 public function removeSite( $globalSiteId ) {
255 $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
256 }
257
258 /**
259 * Whether the list contains no sites.
260 *
261 * @since 1.21
262 * @return bool
263 */
264 public function isEmpty() {
265 return $this->byGlobalId === [];
266 }
267
268 /**
269 * Returns if the list contains the site with the provided site id.
270 *
271 * @param int $id
272 *
273 * @return bool
274 */
275 public function hasInternalId( $id ) {
276 return array_key_exists( $id, $this->byInternalId );
277 }
278
279 /**
280 * Returns the Site with the provided site id.
281 * The site needs to exist, so if not sure, call has first.
282 *
283 * @since 1.21
284 *
285 * @param int $id
286 *
287 * @return Site
288 */
289 public function getSiteByInternalId( $id ) {
290 return $this->offsetGet( $this->byInternalId[$id] );
291 }
292
293 /**
294 * Removes the site with the specified site id.
295 * The site needs to exist, so if not sure, call has first.
296 *
297 * @since 1.21
298 *
299 * @param int $id
300 */
301 public function removeSiteByInternalId( $id ) {
302 $this->offsetUnset( $this->byInternalId[$id] );
303 }
304
305 /**
306 * Returns if the list contains the site with the provided navigational site id.
307 *
308 * @param string $id
309 *
310 * @return bool
311 */
312 public function hasNavigationId( $id ) {
313 return array_key_exists( $id, $this->byNavigationId );
314 }
315
316 /**
317 * Returns the Site with the provided navigational site id.
318 * The site needs to exist, so if not sure, call has first.
319 *
320 * @since 1.23
321 *
322 * @param string $id
323 *
324 * @return Site
325 */
326 public function getSiteByNavigationId( $id ) {
327 return $this->offsetGet( $this->byNavigationId[$id] );
328 }
329
330 /**
331 * Removes the site with the specified navigational site id.
332 * The site needs to exist, so if not sure, call has first.
333 *
334 * @since 1.23
335 *
336 * @param string $id
337 */
338 public function removeSiteByNavigationId( $id ) {
339 $this->offsetUnset( $this->byNavigationId[$id] );
340 }
341
342 /**
343 * Sets a site in the list. If the site was not there,
344 * it will be added. If it was, it will be updated.
345 *
346 * @since 1.21
347 *
348 * @param Site $site
349 */
350 public function setSite( Site $site ) {
351 $this[] = $site;
352 }
353
354 /**
355 * Returns the sites that are in the provided group.
356 *
357 * @since 1.21
358 *
359 * @param string $groupName
360 *
361 * @return SiteList
362 */
363 public function getGroup( $groupName ) {
364 $group = new self();
365
366 /**
367 * @var Site $site
368 */
369 foreach ( $this as $site ) {
370 if ( $site->getGroup() === $groupName ) {
371 $group[] = $site;
372 }
373 }
374
375 return $group;
376 }
377
378 /**
379 * A version ID that identifies the serialization structure used by __serialize()
380 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
381 * on serialization for storing the SiteList.
382 *
383 * @var string A string uniquely identifying the version of the serialization structure,
384 * not including any sub-structures.
385 */
386 private const SERIAL_VERSION_ID = '2014-03-17';
387
388 /**
389 * Returns the version ID that identifies the serialization structure used by
390 * __serialize() and unserialize(), including the structure of any nested structures.
391 * This is useful for constructing cache keys in cases where the cache relies
392 * on serialization for storing the SiteList.
393 *
394 * @return string A string uniquely identifying the version of the serialization structure,
395 * including any sub-structures.
396 */
397 public static function getSerialVersionId() {
398 return self::SERIAL_VERSION_ID . '+Site:' . Site::SERIAL_VERSION_ID;
399 }
400
401 /**
402 * @see Overrides Serializable::serialize
403 * @since 1.38
404 * @return array
405 */
406 public function __serialize(): array {
407 // Data that should go into serialization calls.
408 //
409 // NOTE: When changing the structure, either implement unserialize() to handle the
410 // old structure too, or update SERIAL_VERSION_ID to kill any caches.
411 return [
412 'data' => $this->getArrayCopy(),
413 'index' => $this->indexOffset,
414 'internalIds' => $this->byInternalId,
415 'globalIds' => $this->byGlobalId,
416 'navigationIds' => $this->byNavigationId,
417 ];
418 }
419
420 /**
421 * @see Overrides Serializable::unserialize
422 * @since 1.38
423 * @param array $serializationData
424 */
425 public function __unserialize( $serializationData ): void {
426 foreach ( $serializationData['data'] as $offset => $value ) {
427 // Just set the element, bypassing checks and offset resolving,
428 // as these elements have already gone through this.
429 parent::offsetSet( $offset, $value );
430 }
431
432 $this->indexOffset = $serializationData['index'];
433
434 $this->byInternalId = $serializationData['internalIds'];
435 $this->byGlobalId = $serializationData['globalIds'];
436 $this->byNavigationId = $serializationData['navigationIds'];
437 }
438 }
439
440 /** @deprecated class alias since 1.42 */
441 class_alias( SiteList::class, 'SiteList' );