MediaWiki  master
SiteList.php
Go to the documentation of this file.
1 <?php
2 
37  protected $byInternalId = [];
38 
46  protected $byGlobalId = [];
47 
56  protected $byNavigationId = [];
57 
65  public function getObjectType() {
66  return Site::class;
67  }
68 
79  protected function preSetElement( $index, $site ) {
80  if ( $this->hasSite( $site->getGlobalId() ) ) {
81  $this->removeSite( $site->getGlobalId() );
82  }
83 
84  $this->byGlobalId[$site->getGlobalId()] = $index;
85  $this->byInternalId[$site->getInternalId()] = $index;
86 
87  $ids = $site->getNavigationIds();
88  foreach ( $ids as $navId ) {
89  $this->byNavigationId[$navId] = $index;
90  }
91 
92  return true;
93  }
94 
102  public function offsetUnset( $index ) {
103  if ( $this->offsetExists( $index ) ) {
107  $site = $this->offsetGet( $index );
108 
109  unset( $this->byGlobalId[$site->getGlobalId()] );
110  unset( $this->byInternalId[$site->getInternalId()] );
111 
112  $ids = $site->getNavigationIds();
113  foreach ( $ids as $navId ) {
114  unset( $this->byNavigationId[$navId] );
115  }
116  }
117 
118  parent::offsetUnset( $index );
119  }
120 
129  public function getGlobalIdentifiers() {
130  return array_keys( $this->byGlobalId );
131  }
132 
140  public function hasSite( $globalSiteId ) {
141  return array_key_exists( $globalSiteId, $this->byGlobalId );
142  }
143 
154  public function getSite( $globalSiteId ) {
155  return $this->offsetGet( $this->byGlobalId[$globalSiteId] );
156  }
157 
166  public function removeSite( $globalSiteId ) {
167  $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
168  }
169 
177  public function isEmpty() {
178  return $this->byGlobalId === [];
179  }
180 
188  public function hasInternalId( $id ) {
189  return array_key_exists( $id, $this->byInternalId );
190  }
191 
202  public function getSiteByInternalId( $id ) {
203  return $this->offsetGet( $this->byInternalId[$id] );
204  }
205 
214  public function removeSiteByInternalId( $id ) {
215  $this->offsetUnset( $this->byInternalId[$id] );
216  }
217 
225  public function hasNavigationId( $id ) {
226  return array_key_exists( $id, $this->byNavigationId );
227  }
228 
239  public function getSiteByNavigationId( $id ) {
240  return $this->offsetGet( $this->byNavigationId[$id] );
241  }
242 
251  public function removeSiteByNavigationId( $id ) {
252  $this->offsetUnset( $this->byNavigationId[$id] );
253  }
254 
263  public function setSite( Site $site ) {
264  $this[] = $site;
265  }
266 
276  public function getGroup( $groupName ) {
277  $group = new self();
278 
282  foreach ( $this as $site ) {
283  if ( $site->getGroup() === $groupName ) {
284  $group[] = $site;
285  }
286  }
287 
288  return $group;
289  }
290 
299  private const SERIAL_VERSION_ID = '2014-03-17';
300 
310  public static function getSerialVersionId() {
311  return self::SERIAL_VERSION_ID . '+Site:' . Site::SERIAL_VERSION_ID;
312  }
313 
321  protected function getSerializationData() {
322  // NOTE: When changing the structure, either implement unserialize() to handle the
323  // old structure too, or update SERIAL_VERSION_ID to kill any caches.
324  return array_merge(
325  parent::getSerializationData(),
326  [
327  'internalIds' => $this->byInternalId,
328  'globalIds' => $this->byGlobalId,
329  'navigationIds' => $this->byNavigationId
330  ]
331  );
332  }
333 
343  public function unserialize( $serialization ) {
344  $serializationData = parent::unserialize( $serialization );
345 
346  $this->byInternalId = $serializationData['internalIds'];
347  $this->byGlobalId = $serializationData['globalIds'];
348  $this->byNavigationId = $serializationData['navigationIds'];
349 
350  return $serializationData;
351  }
352 }
SiteList\$byGlobalId
array $byGlobalId
Global site identifiers pointing to their sites offset value.
Definition: SiteList.php:46
SiteList\getSerialVersionId
static getSerialVersionId()
Returns the version ID that identifies the serialization structure used by getSerializationData() and...
Definition: SiteList.php:310
SiteList\getGroup
getGroup( $groupName)
Returns the sites that are in the provided group.
Definition: SiteList.php:276
GenericArrayObject
Definition: GenericArrayObject.php:35
SiteList\hasSite
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition: SiteList.php:140
SiteList\getObjectType
getObjectType()
Definition: SiteList.php:65
SiteList\hasInternalId
hasInternalId( $id)
Returns if the list contains the site with the provided site id.
Definition: SiteList.php:188
SiteList\getSiteByNavigationId
getSiteByNavigationId( $id)
Returns the Site with the provided navigational site id.
Definition: SiteList.php:239
SiteList\offsetUnset
offsetUnset( $index)
Definition: SiteList.php:102
SiteList
Definition: SiteList.php:29
SiteList\preSetElement
preSetElement( $index, $site)
Definition: SiteList.php:79
Site
Definition: Site.php:31
SiteList\getSerializationData
getSerializationData()
Definition: SiteList.php:321
SiteList\isEmpty
isEmpty()
Returns if the list contains no sites.
Definition: SiteList.php:177
SiteList\removeSite
removeSite( $globalSiteId)
Removes the site with the specified global site identifier.
Definition: SiteList.php:166
SiteList\setSite
setSite(Site $site)
Sets a site in the list.
Definition: SiteList.php:263
SiteList\unserialize
unserialize( $serialization)
Definition: SiteList.php:343
SiteList\removeSiteByNavigationId
removeSiteByNavigationId( $id)
Removes the site with the specified navigational site id.
Definition: SiteList.php:251
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
SiteList\getSiteByInternalId
getSiteByInternalId( $id)
Returns the Site with the provided site id.
Definition: SiteList.php:202
SiteList\getGlobalIdentifiers
getGlobalIdentifiers()
Returns all the global site identifiers.
Definition: SiteList.php:129
SiteList\hasNavigationId
hasNavigationId( $id)
Returns if the list contains the site with the provided navigational site id.
Definition: SiteList.php:225
SiteList\$byNavigationId
array $byNavigationId
Navigational site identifiers alias inter-language prefixes pointing to their sites offset value.
Definition: SiteList.php:56
SiteList\getSite
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition: SiteList.php:154
SiteList\removeSiteByInternalId
removeSiteByInternalId( $id)
Removes the site with the specified site id.
Definition: SiteList.php:214
SiteList\$byInternalId
array $byInternalId
Internal site identifiers pointing to their sites offset value.
Definition: SiteList.php:37