Merge "Add missing wfProfileOut()"
[mediawiki.git] / includes / site / Sites.php
blob56d567ea814d5fe9f13a76a21be8268a40af065c
1 <?php
3 /**
4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList) and takes care
6 * of retrieving and caching site information when appropriate.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @since 1.21
25 * @file
26 * @ingroup Site
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 class Sites {
33 /**
34 * @since 1.21
35 * @var SiteList|false
37 protected $sites = false;
39 /**
40 * Constructor.
42 * @since 1.21
44 protected function __construct() {}
46 /**
47 * Returns an instance of Sites.
49 * @since 1.21
51 * @return Sites
53 public static function singleton() {
54 static $instance = false;
56 if ( $instance === false ) {
57 $instance = new static();
60 return $instance;
63 /**
64 * Factory for creating new site objects.
66 * @since 1.21
68 * @param string|false $globalId
70 * @return Site
72 public static function newSite( $globalId = false ) {
73 /**
74 * @var Site $site
76 $site = SitesTable::singleton()->newRow( array(), true );
78 if ( $globalId !== false ) {
79 $site->setGlobalId( $globalId );
82 return $site;
85 /**
86 * Returns a list of all sites. By default this site is
87 * fetched from the cache, which can be changed to loading
88 * the list from the database using the $useCache parameter.
90 * @since 1.21
92 * @param string $source either 'cache' or 'recache'
94 * @return SiteList
96 public function getSites( $source = 'cache' ) {
97 if ( $source === 'cache' ) {
98 if ( $this->sites === false ) {
99 $cache = wfGetMainCache();
100 $sites = $cache->get( 'sites-cache' );
102 if ( is_object( $sites ) ) {
103 $this->sites = $sites;
105 else {
106 $this->loadSites();
110 else {
111 $this->loadSites();
114 return $this->sites;
118 * Returns a list of sites in the given group. Calling getGroup() on any of
119 * the sites in the resulting SiteList shall return $group.
121 * @since 1.21
123 * @param string $group th group to get.
125 * @return SiteList
127 public function getSiteGroup( $group ) {
128 $sites = self::getSites();
130 $siteGroup = new SiteArray();
132 /* @var Site $site */
133 foreach ( $sites as $site ) {
134 if ( $site->getGroup() == $group ) {
135 $siteGroup->append( $site );
139 return $siteGroup;
143 * Fetches the site from the database and loads them into the sites field.
145 * @since 1.21
147 protected function loadSites() {
148 $this->sites = new SiteArray( SitesTable::singleton()->select() );
150 // Batch load the local site identifiers.
151 $dbr = wfGetDB( SitesTable::singleton()->getReadDb() );
153 $ids = $dbr->select(
154 'site_identifiers',
155 array(
156 'si_site',
157 'si_type',
158 'si_key',
160 array(),
161 __METHOD__
164 foreach ( $ids as $id ) {
165 if ( $this->sites->hasInternalId( $id->si_site ) ) {
166 $site = $this->sites->getSiteByInternalId( $id->si_site );
167 $site->addLocalId( $id->si_type, $id->si_key );
168 $this->sites->setSite( $site );
172 $cache = wfGetMainCache();
173 $cache->set( 'sites-cache', $this->sites );
177 * Returns the site with provided global id, or false if there is no such site.
179 * @since 1.21
181 * @param string $globalId
182 * @param string $source
184 * @return Site|false
186 public function getSite( $globalId, $source = 'cache' ) {
187 if ( $source === 'cache' && $this->sites !== false ) {
188 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : false;
191 return SitesTable::singleton()->selectRow( null, array( 'global_key' => $globalId ) );