a7c196b45be2ccb0f0646c7dd3a8d5b7eaaed36a
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
{
32 * Internal site identifiers pointing to their sites offset value.
36 * @var array of integer
38 protected $byInternalId = array();
41 * Global site identifiers pointing to their sites offset value.
45 * @var array of string
47 protected $byGlobalId = array();
50 * Navigational site identifiers alias inter-language prefixes
51 * pointing to their sites offset value.
55 * @var array of string
57 protected $byNavigationId = array();
60 * @see GenericArrayObject::getObjectType
66 public function getObjectType() {
71 * @see GenericArrayObject::preSetElement
75 * @param int|string $index
80 protected function preSetElement( $index, $site ) {
81 if ( $this->hasSite( $site->getGlobalId() ) ) {
82 $this->removeSite( $site->getGlobalId() );
85 $this->byGlobalId
[$site->getGlobalId()] = $index;
86 $this->byInternalId
[$site->getInternalId()] = $index;
88 $ids = $site->getNavigationIds();
89 foreach ( $ids as $navId ) {
90 $this->byNavigationId
[$navId] = $index;
97 * @see ArrayObject::offsetUnset()
101 * @param mixed $index
103 public function offsetUnset( $index ) {
104 if ( $this->offsetExists( $index ) ) {
108 $site = $this->offsetGet( $index );
110 unset( $this->byGlobalId
[$site->getGlobalId()] );
111 unset( $this->byInternalId
[$site->getInternalId()] );
113 $ids = $site->getNavigationIds();
114 foreach ( $ids as $navId ) {
115 unset( $this->byNavigationId
[$navId] );
119 parent
::offsetUnset( $index );
123 * Returns all the global site identifiers.
124 * Optionally only those belonging to the specified group.
130 public function getGlobalIdentifiers() {
131 return array_keys( $this->byGlobalId
);
135 * Returns if the list contains the site with the provided global site identifier.
137 * @param string $globalSiteId
141 public function hasSite( $globalSiteId ) {
142 return array_key_exists( $globalSiteId, $this->byGlobalId
);
146 * Returns the Site with the provided global site identifier.
147 * The site needs to exist, so if not sure, call hasGlobalId first.
151 * @param string $globalSiteId
155 public function getSite( $globalSiteId ) {
156 return $this->offsetGet( $this->byGlobalId
[$globalSiteId] );
160 * Removes the site with the specified global site identifier.
161 * The site needs to exist, so if not sure, call hasGlobalId first.
165 * @param string $globalSiteId
167 public function removeSite( $globalSiteId ) {
168 $this->offsetUnset( $this->byGlobalId
[$globalSiteId] );
172 * Returns if the list contains no sites.
178 public function isEmpty() {
179 return $this->byGlobalId
=== array();
183 * Returns if the list contains the site with the provided site id.
189 public function hasInternalId( $id ) {
190 return array_key_exists( $id, $this->byInternalId
);
194 * Returns the Site with the provided site id.
195 * The site needs to exist, so if not sure, call has first.
203 public function getSiteByInternalId( $id ) {
204 return $this->offsetGet( $this->byInternalId
[$id] );
208 * Removes the site with the specified site id.
209 * The site needs to exist, so if not sure, call has first.
215 public function removeSiteByInternalId( $id ) {
216 $this->offsetUnset( $this->byInternalId
[$id] );
220 * Returns if the list contains the site with the provided navigational site id.
226 public function hasNavigationId( $id ) {
227 return array_key_exists( $id, $this->byNavigationId
);
231 * Returns the Site with the provided navigational site id.
232 * The site needs to exist, so if not sure, call has first.
240 public function getSiteByNavigationId( $id ) {
241 return $this->offsetGet( $this->byNavigationId
[$id] );
245 * Removes the site with the specified navigational site id.
246 * The site needs to exist, so if not sure, call has first.
252 public function removeSiteByNavigationId( $id ) {
253 $this->offsetUnset( $this->byNavigationId
[$id] );
257 * Sets a site in the list. If the site was not there,
258 * it will be added. If it was, it will be updated.
264 public function setSite( Site
$site ) {
269 * Returns the sites that are in the provided group.
273 * @param string $groupName
277 public function getGroup( $groupName ) {
283 foreach ( $this as $site ) {
284 if ( $site->getGroup() === $groupName ) {
293 * A version ID that identifies the serialization structure used by getSerializationData()
294 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
295 * on serialization for storing the SiteList.
297 * @var string A string uniquely identifying the version of the serialization structure,
298 * not including any sub-structures.
300 const SERIAL_VERSION_ID
= '2014-03-17';
303 * Returns the version ID that identifies the serialization structure used by
304 * getSerializationData() and unserialize(), including the structure of any nested structures.
305 * This is useful for constructing cache keys in cases where the cache relies
306 * on serialization for storing the SiteList.
308 * @return string A string uniquely identifying the version of the serialization structure,
309 * including any sub-structures.
311 public static function getSerialVersionId() {
312 return self
::SERIAL_VERSION_ID
. '+Site:' . Site
::SERIAL_VERSION_ID
;
316 * @see GenericArrayObject::getSerializationData
322 protected function getSerializationData() {
323 //NOTE: When changing the structure, either implement unserialize() to handle the
324 // old structure too, or update SERIAL_VERSION_ID to kill any caches.
326 parent
::getSerializationData(),
328 'internalIds' => $this->byInternalId
,
329 'globalIds' => $this->byGlobalId
,
330 'navigationIds' => $this->byNavigationId
336 * @see GenericArrayObject::unserialize
340 * @param string $serialization
344 public function unserialize( $serialization ) {
345 $serializationData = parent
::unserialize( $serialization );
347 $this->byInternalId
= $serializationData['internalIds'];
348 $this->byGlobalId
= $serializationData['globalIds'];
349 $this->byNavigationId
= $serializationData['navigationIds'];
351 return $serializationData;
359 class SiteArray
extends SiteList
{}