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
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
37 protected $sites = null;
44 protected function __construct() {}
47 * Returns an instance of Sites.
53 public static function singleton() {
54 static $instance = false;
56 if ( $instance === false ) {
57 $instance = new static();
64 * Factory for creating new site objects.
68 * @param string|boolean false $globalId
72 public static function newSite( $globalId = false ) {
76 $site = SitesTable
::singleton()->newRow( array(), true );
78 if ( $globalId !== false ) {
79 $site->setGlobalId( $globalId );
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.
92 * @param string $source either 'cache' or 'recache'
96 public function getSites( $source = 'cache' ) {
97 if ( $source === 'cache' ) {
98 if ( $this->sites
=== null ) {
99 $cache = wfGetMainCache();
100 $sites = $cache->get( wfMemcKey( 'SiteList' ) );
102 if ( is_object( $sites ) && isset( $sites->cacheVersion
) && $sites->cacheVersion
=== SiteArray
::CACHE_VERSION
) {
103 $this->sites
= $sites;
117 * Returns a list of sites in the given group. Calling getGroup() on any of
118 * the sites in the resulting SiteList shall return $group.
122 * @param string $group th group to get.
126 public function getSiteGroup( $group ) {
127 $sites = self
::getSites();
129 $siteGroup = new SiteArray();
131 /* @var Site $site */
132 foreach ( $sites as $site ) {
133 if ( $site->getGroup() == $group ) {
134 $siteGroup->append( $site );
142 * Fetches the site from the database and loads them into the sites field.
146 protected function loadSites() {
147 $this->sites
= new SiteArray( SitesTable
::singleton()->select() );
149 // Batch load the local site identifiers.
150 $dbr = wfGetDB( SitesTable
::singleton()->getReadDb() );
163 foreach ( $ids as $id ) {
164 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
165 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
166 $site->addLocalId( $id->si_type
, $id->si_key
);
167 $this->sites
->setSite( $site );
171 $cache = wfGetMainCache();
172 $cache->set( wfMemcKey( 'SiteList' ), $this->sites
);
176 * Returns the site with provided global id, or false if there is no such site.
180 * @param string $globalId
181 * @param string $source
185 public function getSite( $globalId, $source = 'cache' ) {
186 $sites = $this->getSites( $source );
188 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : false;