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 = false;
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|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
=== false ) {
99 $cache = wfGetMainCache();
100 $sites = $cache->get( 'sites-cache' );
102 if ( is_object( $sites ) ) {
103 $this->sites
= $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.
123 * @param string $group th group to get.
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 );
143 * Fetches the site from the database and loads them into the sites field.
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() );
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.
181 * @param string $globalId
182 * @param string $source
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 ) );