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 * @see GenericArrayObject::getObjectType
56 public function getObjectType() {
61 * @see GenericArrayObject::preSetElement
65 * @param int|string $index
70 protected function preSetElement( $index, $site ) {
71 if ( $this->hasSite( $site->getGlobalId() ) ) {
72 $this->removeSite( $site->getGlobalId() );
75 $this->byGlobalId
[$site->getGlobalId()] = $index;
76 $this->byInternalId
[$site->getInternalId()] = $index;
82 * @see ArrayObject::offsetUnset()
88 public function offsetUnset( $index ) {
89 if ( $this->offsetExists( $index ) ) {
93 $site = $this->offsetGet( $index );
95 unset( $this->byGlobalId
[$site->getGlobalId()] );
96 unset( $this->byInternalId
[$site->getInternalId()] );
99 parent
::offsetUnset( $index );
103 * Returns all the global site identifiers.
104 * Optionally only those belonging to the specified group.
110 public function getGlobalIdentifiers() {
111 return array_keys( $this->byGlobalId
);
115 * Returns if the list contains the site with the provided global site identifier.
117 * @param string $globalSiteId
121 public function hasSite( $globalSiteId ) {
122 return array_key_exists( $globalSiteId, $this->byGlobalId
);
126 * Returns the Site with the provided global site identifier.
127 * The site needs to exist, so if not sure, call hasGlobalId first.
131 * @param string $globalSiteId
135 public function getSite( $globalSiteId ) {
136 return $this->offsetGet( $this->byGlobalId
[$globalSiteId] );
140 * Removes the site with the specified global site identifier.
141 * The site needs to exist, so if not sure, call hasGlobalId first.
145 * @param string $globalSiteId
147 public function removeSite( $globalSiteId ) {
148 $this->offsetUnset( $this->byGlobalId
[$globalSiteId] );
152 * Returns if the list contains no sites.
158 public function isEmpty() {
159 return $this->byGlobalId
=== array();
163 * Returns if the list contains the site with the provided site id.
169 public function hasInternalId( $id ) {
170 return array_key_exists( $id, $this->byInternalId
);
174 * Returns the Site with the provided site id.
175 * The site needs to exist, so if not sure, call has first.
183 public function getSiteByInternalId( $id ) {
184 return $this->offsetGet( $this->byInternalId
[$id] );
188 * Removes the site with the specified site id.
189 * The site needs to exist, so if not sure, call has first.
195 public function removeSiteByInternalId( $id ) {
196 $this->offsetUnset( $this->byInternalId
[$id] );
200 * Sets a site in the list. If the site was not there,
201 * it will be added. If it was, it will be updated.
207 public function setSite( Site
$site ) {
212 * Returns the sites that are in the provided group.
216 * @param string $groupName
220 public function getGroup( $groupName ) {
226 foreach ( $this as $site ) {
227 if ( $site->getGroup() === $groupName ) {
236 * A version ID that identifies the serialization structure used by getSerializationData()
237 * and unserialize(). This is useful for constructing cache keys in cases where the cache relies
238 * on serialization for storing the SiteList.
240 * @var string A string uniquely identifying the version of the serialization structure,
241 * not including any sub-structures.
243 const SERIAL_VERSION_ID
= '2013-02-07';
246 * Returns the version ID that identifies the serialization structure used by
247 * getSerializationData() and unserialize(), including the structure of any nested structures.
248 * This is useful for constructing cache keys in cases where the cache relies
249 * on serialization for storing the SiteList.
251 * @return string A string uniquely identifying the version of the serialization structure,
252 * including any sub-structures.
254 public static function getSerialVersionId() {
255 return self
::SERIAL_VERSION_ID
. '+Site:' . Site
::SERIAL_VERSION_ID
;
259 * @see GenericArrayObject::getSerializationData
265 protected function getSerializationData() {
266 //NOTE: When changing the structure, either implement unserialize() to handle the
267 // old structure too, or update SERIAL_VERSION_ID to kill any caches.
269 parent
::getSerializationData(),
271 'internalIds' => $this->byInternalId
,
272 'globalIds' => $this->byGlobalId
,
278 * @see GenericArrayObject::unserialize
282 * @param string $serialization
286 public function unserialize( $serialization ) {
287 $serializationData = parent
::unserialize( $serialization );
289 $this->byInternalId
= $serializationData['internalIds'];
290 $this->byGlobalId
= $serializationData['globalIds'];
292 return $serializationData;
300 class SiteArray
extends SiteList
{}