4 * Collection of Site objects.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
26 * @license GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 class SiteList
extends GenericArrayObject
{
31 * Internal site identifiers pointing to their sites offset value.
35 * @var array Array of integer
37 protected $byInternalId = array();
40 * Global site identifiers pointing to their sites offset value.
44 * @var array Array of string
46 protected $byGlobalId = array();
49 * Navigational site identifiers alias inter-language prefixes
50 * pointing to their sites offset value.
54 * @var array Array of string
56 protected $byNavigationId = array();
59 * @see GenericArrayObject::getObjectType
65 public function getObjectType() {
70 * @see GenericArrayObject::preSetElement
74 * @param int|string $index
79 protected function preSetElement( $index, $site ) {
80 if ( $this->hasSite( $site->getGlobalId() ) ) {
81 $this->removeSite( $site->getGlobalId() );
84 $this->byGlobalId
[$site->getGlobalId()] = $index;
85 $this->byInternalId
[$site->getInternalId()] = $index;
87 $ids = $site->getNavigationIds();
88 foreach ( $ids as $navId ) {
89 $this->byNavigationId
[$navId] = $index;
96 * @see ArrayObject::offsetUnset()
100 * @param mixed $index
102 public function offsetUnset( $index ) {
103 if ( $this->offsetExists( $index ) ) {
107 $site = $this->offsetGet( $index );
109 unset( $this->byGlobalId
[$site->getGlobalId()] );
110 unset( $this->byInternalId
[$site->getInternalId()] );
112 $ids = $site->getNavigationIds();
113 foreach ( $ids as $navId ) {
114 unset( $this->byNavigationId
[$navId] );
118 parent
::offsetUnset( $index );
122 * Returns all the global site identifiers.
123 * Optionally only those belonging to the specified group.
129 public function getGlobalIdentifiers() {
130 return array_keys( $this->byGlobalId
);
134 * Returns if the list contains the site with the provided global site identifier.
136 * @param string $globalSiteId
140 public function hasSite( $globalSiteId ) {
141 return array_key_exists( $globalSiteId, $this->byGlobalId
);
145 * Returns the Site with the provided global site identifier.
146 * The site needs to exist, so if not sure, call hasGlobalId first.
150 * @param string $globalSiteId
154 public function getSite( $globalSiteId ) {
155 return $this->offsetGet( $this->byGlobalId
[$globalSiteId] );
159 * Removes the site with the specified global site identifier.
160 * The site needs to exist, so if not sure, call hasGlobalId first.
164 * @param string $globalSiteId
166 public function removeSite( $globalSiteId ) {
167 $this->offsetUnset( $this->byGlobalId
[$globalSiteId] );
171 * Returns if the list contains no sites.
177 public function isEmpty() {
178 return $this->byGlobalId
=== array();
182 * Returns if the list contains the site with the provided site id.
188 public function hasInternalId( $id ) {
189 return array_key_exists( $id, $this->byInternalId
);
193 * Returns the Site with the provided site id.
194 * The site needs to exist, so if not sure, call has first.
202 public function getSiteByInternalId( $id ) {
203 return $this->offsetGet( $this->byInternalId
[$id] );
207 * Removes the site with the specified site id.
208 * The site needs to exist, so if not sure, call has first.
214 public function removeSiteByInternalId( $id ) {
215 $this->offsetUnset( $this->byInternalId
[$id] );
219 * Returns if the list contains the site with the provided navigational site id.
225 public function hasNavigationId( $id ) {
226 return array_key_exists( $id, $this->byNavigationId
);
230 * Returns the Site with the provided navigational site id.
231 * The site needs to exist, so if not sure, call has first.
239 public function getSiteByNavigationId( $id ) {
240 return $this->offsetGet( $this->byNavigationId
[$id] );
244 * Removes the site with the specified navigational site id.
245 * The site needs to exist, so if not sure, call has first.
251 public function removeSiteByNavigationId( $id ) {
252 $this->offsetUnset( $this->byNavigationId
[$id] );
256 * Sets a site in the list. If the site was not there,
257 * it will be added. If it was, it will be updated.
263 public function setSite( Site
$site ) {
268 * Returns the sites that are in the provided group.
272 * @param string $groupName
276 public function getGroup( $groupName ) {
282 foreach ( $this as $site ) {
283 if ( $site->getGroup() === $groupName ) {
292 * A version ID that identifies the serialization structure used by getSerializationData()
293 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
294 * on serialization for storing the SiteList.
296 * @var string A string uniquely identifying the version of the serialization structure,
297 * not including any sub-structures.
299 const SERIAL_VERSION_ID
= '2014-03-17';
302 * Returns the version ID that identifies the serialization structure used by
303 * getSerializationData() and unserialize(), including the structure of any nested structures.
304 * This is useful for constructing cache keys in cases where the cache relies
305 * on serialization for storing the SiteList.
307 * @return string A string uniquely identifying the version of the serialization structure,
308 * including any sub-structures.
310 public static function getSerialVersionId() {
311 return self
::SERIAL_VERSION_ID
. '+Site:' . Site
::SERIAL_VERSION_ID
;
315 * @see GenericArrayObject::getSerializationData
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.
325 parent
::getSerializationData(),
327 'internalIds' => $this->byInternalId
,
328 'globalIds' => $this->byGlobalId
,
329 'navigationIds' => $this->byNavigationId
335 * @see GenericArrayObject::unserialize
339 * @param string $serialization
343 public function unserialize( $serialization ) {
344 $serializationData = parent
::unserialize( $serialization );
346 $this->byInternalId
= $serializationData['internalIds'];
347 $this->byGlobalId
= $serializationData['globalIds'];
348 $this->byNavigationId
= $serializationData['navigationIds'];
350 return $serializationData;
355 * @deprecated since 1.21
357 class SiteArray
extends SiteList
{