Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / site / SiteList.php
blob2d9f22dd6ed17d22e33d608e31bbc71d4a0ed26d
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 {
30 /**
31 * Internal site identifiers pointing to their sites offset value.
33 * @since 1.21
35 * @var array Array of integer
37 protected $byInternalId = array();
39 /**
40 * Global site identifiers pointing to their sites offset value.
42 * @since 1.21
44 * @var array Array of string
46 protected $byGlobalId = array();
48 /**
49 * Navigational site identifiers alias inter-language prefixes
50 * pointing to their sites offset value.
52 * @since 1.23
54 * @var array Array of string
56 protected $byNavigationId = array();
58 /**
59 * @see GenericArrayObject::getObjectType
61 * @since 1.21
63 * @return string
65 public function getObjectType() {
66 return 'Site';
69 /**
70 * @see GenericArrayObject::preSetElement
72 * @since 1.21
74 * @param int|string $index
75 * @param Site $site
77 * @return bool
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;
92 return true;
95 /**
96 * @see ArrayObject::offsetUnset()
98 * @since 1.21
100 * @param mixed $index
102 public function offsetUnset( $index ) {
103 if ( $this->offsetExists( $index ) ) {
105 * @var Site $site
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.
125 * @since 1.21
127 * @return array
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
138 * @return bool
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.
148 * @since 1.21
150 * @param string $globalSiteId
152 * @return Site
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.
162 * @since 1.21
164 * @param string $globalSiteId
166 public function removeSite( $globalSiteId ) {
167 $this->offsetUnset( $this->byGlobalId[$globalSiteId] );
171 * Returns if the list contains no sites.
173 * @since 1.21
175 * @return bool
177 public function isEmpty() {
178 return $this->byGlobalId === array();
182 * Returns if the list contains the site with the provided site id.
184 * @param int $id
186 * @return bool
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.
196 * @since 1.21
198 * @param int $id
200 * @return Site
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.
210 * @since 1.21
212 * @param int $id
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.
221 * @param string $id
223 * @return bool
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.
233 * @since 1.23
235 * @param string $id
237 * @return Site
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.
247 * @since 1.23
249 * @param string $id
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.
259 * @since 1.21
261 * @param Site $site
263 public function setSite( Site $site ) {
264 $this[] = $site;
268 * Returns the sites that are in the provided group.
270 * @since 1.21
272 * @param string $groupName
274 * @return SiteList
276 public function getGroup( $groupName ) {
277 $group = new self();
280 * @var Site $site
282 foreach ( $this as $site ) {
283 if ( $site->getGroup() === $groupName ) {
284 $group[] = $site;
288 return $group;
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
317 * @since 1.21
319 * @return array
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 array(
327 'internalIds' => $this->byInternalId,
328 'globalIds' => $this->byGlobalId,
329 'navigationIds' => $this->byNavigationId
335 * @see GenericArrayObject::unserialize
337 * @since 1.21
339 * @param string $serialization
341 * @return array
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 {