Merge "CologneBlue rewrite: rewrite bottomLinks()"
[lhc/web/wiklou.git] / includes / site / Sites.php
1 <?php
2
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.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @since 1.21
24 *
25 * @file
26 * @ingroup Site
27 *
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 */
31 class Sites {
32
33 /**
34 * @since 1.21
35 * @var SiteList|false
36 */
37 protected $sites = false;
38
39 /**
40 * Constructor.
41 *
42 * @since 1.21
43 */
44 protected function __construct() {}
45
46 /**
47 * Returns an instance of Sites.
48 *
49 * @since 1.21
50 *
51 * @return Sites
52 */
53 public static function singleton() {
54 static $instance = false;
55
56 if ( $instance === false ) {
57 $instance = new static();
58 }
59
60 return $instance;
61 }
62
63 /**
64 * Factory for creating new site objects.
65 *
66 * @since 1.21
67 *
68 * @param string|false $globalId
69 *
70 * @return Site
71 */
72 public static function newSite( $globalId = false ) {
73 /**
74 * @var Site $site
75 */
76 $site = SitesTable::singleton()->newRow( array(), true );
77
78 if ( $globalId !== false ) {
79 $site->setGlobalId( $globalId );
80 }
81
82 return $site;
83 }
84
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.
89 *
90 * @since 1.21
91 *
92 * @param string $source either 'cache' or 'recache'
93 *
94 * @return SiteList
95 */
96 public function getSites( $source = 'cache' ) {
97 if ( $source === 'cache' ) {
98 if ( $this->sites === false ) {
99 $cache = wfGetMainCache();
100 $sites = $cache->get( 'sites-cache' );
101
102 if ( is_object( $sites ) ) {
103 $this->sites = $sites;
104 }
105 else {
106 $this->loadSites();
107 }
108 }
109 }
110 else {
111 $this->loadSites();
112 }
113
114 return $this->sites;
115 }
116
117 /**
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.
120 *
121 * @since 1.21
122 *
123 * @param string $group th group to get.
124 *
125 * @return SiteList
126 */
127 public function getSiteGroup( $group ) {
128 $sites = self::getSites();
129
130 $siteGroup = new SiteArray();
131
132 /* @var Site $site */
133 foreach ( $sites as $site ) {
134 if ( $site->getGroup() == $group ) {
135 $siteGroup->append( $site );
136 }
137 }
138
139 return $siteGroup;
140 }
141
142 /**
143 * Fetches the site from the database and loads them into the sites field.
144 *
145 * @since 1.21
146 */
147 protected function loadSites() {
148 $this->sites = new SiteArray( SitesTable::singleton()->select() );
149
150 // Batch load the local site identifiers.
151 $dbr = wfGetDB( SitesTable::singleton()->getReadDb() );
152
153 $ids = $dbr->select(
154 'site_identifiers',
155 array(
156 'si_site',
157 'si_type',
158 'si_key',
159 ),
160 array(),
161 __METHOD__
162 );
163
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 );
169 }
170 }
171
172 $cache = wfGetMainCache();
173 $cache->set( 'sites-cache', $this->sites );
174 }
175
176 /**
177 * Returns the site with provided global id, or false if there is no such site.
178 *
179 * @since 1.21
180 *
181 * @param string $globalId
182 * @param string $source
183 *
184 * @return Site|false
185 */
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;
189 }
190
191 return SitesTable::singleton()->selectRow( null, array( 'global_key' => $globalId ) );
192 }
193
194 }