Merge "Typo fix"
[mediawiki.git] / includes / site / SiteList.php
blobb0d1f95b0d310ad195a6fdc3ad2340dcec5abff3
1 <?php
3 /**
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
21 * @since 1.21
23 * @file
24 * @ingroup Site
26 * @license GNU GPL v2+
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 class SiteList extends GenericArrayObject {
31 /**
32 * Internal site identifiers pointing to their sites offset value.
34 * @since 1.21
36 * @var array of integer
38 protected $byInternalId = array();
40 /**
41 * Global site identifiers pointing to their sites offset value.
43 * @since 1.21
45 * @var array of string
47 protected $byGlobalId = array();
49 /**
50 * @see GenericArrayObject::getObjectType
52 * @since 1.21
54 * @return string
56 public function getObjectType() {
57 return 'Site';
60 /**
61 * @see GenericArrayObject::preSetElement
63 * @since 1.21
65 * @param int|string $index
66 * @param Site $site
68 * @return boolean
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;
78 return true;
81 /**
82 * @see ArrayObject::offsetUnset()
84 * @since 1.21
86 * @param mixed $index
88 public function offsetUnset( $index ) {
89 if ( $this->offsetExists( $index ) ) {
90 /**
91 * @var Site $site
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.
106 * @since 1.21
108 * @return array
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
119 * @return boolean
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.
129 * @since 1.21
131 * @param string $globalSiteId
133 * @return Site
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.
143 * @since 1.21
145 * @param string $globalSiteId
147 public function removeSite( $globalSiteId ) {
148 $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
152 * Returns if the list contains no sites.
154 * @since 1.21
156 * @return boolean
158 public function isEmpty() {
159 return $this->byGlobalId === array();
163 * Returns if the list contains the site with the provided site id.
165 * @param integer $id
167 * @return boolean
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.
177 * @since 1.21
179 * @param integer $id
181 * @return Site
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.
191 * @since 1.21
193 * @param integer $id
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.
203 * @since 1.21
205 * @param Site $site
207 public function setSite( Site $site ) {
208 $this[] = $site;
212 * Returns the sites that are in the provided group.
214 * @since 1.21
216 * @param string $groupName
218 * @return SiteList
220 public function getGroup( $groupName ) {
221 $group = new self();
224 * @var \Site $site
226 foreach ( $this as $site ) {
227 if ( $site->getGroup() === $groupName ) {
228 $group[] = $site;
232 return $group;
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
261 * @since 1.21
263 * @return array
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.
268 return array_merge(
269 parent::getSerializationData(),
270 array(
271 'internalIds' => $this->byInternalId,
272 'globalIds' => $this->byGlobalId,
278 * @see GenericArrayObject::unserialize
280 * @since 1.21
282 * @param string $serialization
284 * @return array
286 public function unserialize( $serialization ) {
287 $serializationData = parent::unserialize( $serialization );
289 $this->byInternalId = $serializationData['internalIds'];
290 $this->byGlobalId = $serializationData['globalIds'];
292 return $serializationData;
298 * @deprecated
300 class SiteArray extends SiteList {}