Make Sites::singleton() actually return a singleton.
[lhc/web/wiklou.git] / includes / site / SiteSQLStore.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 SiteSQLStore implements SiteStore {
32
33 /**
34 * @since 1.21
35 *
36 * @var SiteList|null
37 */
38 protected $sites = null;
39
40 /**
41 * @var ORMTable
42 */
43 protected $sitesTable;
44
45 /**
46 * @var string|null
47 */
48 private $cacheKey = null;
49
50 /**
51 * @since 1.21
52 *
53 * @param ORMTable|null $sitesTable
54 *
55 * @return SiteStore
56 */
57 public static function newInstance( ORMTable $sitesTable = null ) {
58 return new static( $sitesTable );
59 }
60
61 /**
62 * Constructor.
63 *
64 * @since 1.21
65 *
66 * @param ORMTable|null $sitesTable
67 */
68 protected function __construct( ORMTable $sitesTable = null ) {
69 if ( $sitesTable === null ) {
70 $sitesTable = $this->newSitesTable();
71 }
72
73 $this->sitesTable = $sitesTable;
74 }
75
76 /**
77 * Constructs a cache key to use for caching the list of sites.
78 *
79 * This includes the concrete class name of the site list as well as a version identifier
80 * for the list's serialization, to avoid problems when unserializing site lists serialized
81 * by an older version, e.g. when reading from a cache.
82 *
83 * The cache key also includes information about where the sites were loaded from, e.g.
84 * the name of a database table.
85 *
86 * @see SiteList::getSerialVersionId
87 *
88 * @return String The cache key.
89 */
90 protected function getCacheKey() {
91 wfProfileIn( __METHOD__ );
92
93 if ( $this->cacheKey === null ) {
94 $type = 'SiteList#' . SiteList::getSerialVersionId();
95 $source = $this->sitesTable->getName();
96
97 if ( $this->sitesTable->getTargetWiki() !== false ) {
98 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
99 }
100
101 $this->cacheKey = wfMemcKey( "$source/$type" );
102 }
103
104 wfProfileOut( __METHOD__ );
105 return $this->cacheKey;
106 }
107
108 /**
109 * @see SiteStore::getSites
110 *
111 * @since 1.21
112 *
113 * @param string $source either 'cache' or 'recache'
114 *
115 * @return SiteList
116 */
117 public function getSites( $source = 'cache' ) {
118 wfProfileIn( __METHOD__ );
119
120 if ( $source === 'cache' ) {
121 if ( $this->sites === null ) {
122 $cache = wfGetMainCache();
123 $sites = $cache->get( $this->getCacheKey() );
124
125 if ( is_object( $sites ) ) {
126 $this->sites = $sites;
127 } else {
128 $this->loadSites();
129 }
130 }
131 }
132 else {
133 $this->loadSites();
134 }
135
136 wfProfileOut( __METHOD__ );
137 return $this->sites;
138 }
139
140 /**
141 * Returns a new Site object constructed from the provided ORMRow.
142 *
143 * @since 1.21
144 *
145 * @param ORMRow $siteRow
146 *
147 * @return Site
148 */
149 protected function siteFromRow( ORMRow $siteRow ) {
150 wfProfileIn( __METHOD__ );
151
152 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
153
154 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
155
156 $site->setInternalId( $siteRow->getField( 'id' ) );
157
158 if ( $siteRow->hasField( 'forward' ) ) {
159 $site->setForward( $siteRow->getField( 'forward' ) );
160 }
161
162 if ( $siteRow->hasField( 'group' ) ) {
163 $site->setGroup( $siteRow->getField( 'group' ) );
164 }
165
166 if ( $siteRow->hasField( 'language' ) ) {
167 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ? null : $siteRow->getField( 'language' ) );
168 }
169
170 if ( $siteRow->hasField( 'source' ) ) {
171 $site->setSource( $siteRow->getField( 'source' ) );
172 }
173
174 if ( $siteRow->hasField( 'data' ) ) {
175 $site->setExtraData( $siteRow->getField( 'data' ) );
176 }
177
178 if ( $siteRow->hasField( 'config' ) ) {
179 $site->setExtraConfig( $siteRow->getField( 'config' ) );
180 }
181
182 wfProfileOut( __METHOD__ );
183 return $site;
184 }
185
186 /**
187 * Fetches the site from the database and loads them into the sites field.
188 *
189 * @since 1.21
190 */
191 protected function loadSites() {
192 wfProfileIn( __METHOD__ );
193
194 $this->sites = new SiteList();
195
196 foreach ( $this->sitesTable->select() as $siteRow ) {
197 $this->sites[] = $this->siteFromRow( $siteRow );
198 }
199
200 // Batch load the local site identifiers.
201 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
202 'site_identifiers',
203 array(
204 'si_site',
205 'si_type',
206 'si_key',
207 ),
208 array(),
209 __METHOD__
210 );
211
212 foreach ( $ids as $id ) {
213 if ( $this->sites->hasInternalId( $id->si_site ) ) {
214 $site = $this->sites->getSiteByInternalId( $id->si_site );
215 $site->addLocalId( $id->si_type, $id->si_key );
216 $this->sites->setSite( $site );
217 }
218 }
219
220 $cache = wfGetMainCache();
221 $cache->set( $this->getCacheKey(), $this->sites );
222
223 wfProfileOut( __METHOD__ );
224 }
225
226 /**
227 * @see SiteStore::getSite
228 *
229 * @since 1.21
230 *
231 * @param string $globalId
232 * @param string $source
233 *
234 * @return Site|null
235 */
236 public function getSite( $globalId, $source = 'cache' ) {
237 wfProfileIn( __METHOD__ );
238
239 $sites = $this->getSites( $source );
240
241 wfProfileOut( __METHOD__ );
242 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
243 }
244
245 /**
246 * @see SiteStore::saveSite
247 *
248 * @since 1.21
249 *
250 * @param Site $site
251 *
252 * @return boolean Success indicator
253 */
254 public function saveSite( Site $site ) {
255 return $this->saveSites( array( $site ) );
256 }
257
258 /**
259 * @see SiteStore::saveSites
260 *
261 * @since 1.21
262 *
263 * @param Site[] $sites
264 *
265 * @return boolean Success indicator
266 */
267 public function saveSites( array $sites ) {
268 wfProfileIn( __METHOD__ );
269
270 if ( empty( $sites ) ) {
271 wfProfileOut( __METHOD__ );
272 return true;
273 }
274
275 $dbw = $this->sitesTable->getWriteDbConnection();
276
277 $trx = $dbw->trxLevel();
278
279 if ( $trx == 0 ) {
280 $dbw->begin( __METHOD__ );
281 }
282
283 $success = true;
284
285 $internalIds = array();
286 $localIds = array();
287
288 foreach ( $sites as $site ) {
289 $fields = array(
290 // Site data
291 'global_key' => $site->getGlobalId(), // TODO: check not null
292 'type' => $site->getType(),
293 'group' => $site->getGroup(),
294 'source' => $site->getSource(),
295 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
296 'protocol' => $site->getProtocol(),
297 'domain' => strrev( $site->getDomain() ) . '.',
298 'data' => $site->getExtraData(),
299
300 // Site config
301 'forward' => $site->shouldForward(),
302 'config' => $site->getExtraConfig(),
303 );
304
305 if ( $site->getInternalId() !== null ) {
306 $fields['id'] = $site->getInternalId();
307 $internalIds[] = $site->getInternalId();
308 }
309
310 $siteRow = new ORMRow( $this->sitesTable, $fields );
311 $success = $siteRow->save( __METHOD__ ) && $success;
312
313 foreach ( $site->getLocalIds() as $idType => $ids ) {
314 foreach ( $ids as $id ) {
315 $localIds[] = array( $siteRow->getId(), $idType, $id );
316 }
317 }
318 }
319
320 if ( $internalIds !== array() ) {
321 $dbw->delete(
322 'site_identifiers',
323 array( 'si_site' => $internalIds ),
324 __METHOD__
325 );
326 }
327
328 foreach ( $localIds as $localId ) {
329 $dbw->insert(
330 'site_identifiers',
331 array(
332 'si_site' => $localId[0],
333 'si_type' => $localId[1],
334 'si_key' => $localId[2],
335 ),
336 __METHOD__
337 );
338 }
339
340 if ( $trx == 0 ) {
341 $dbw->commit( __METHOD__ );
342 }
343
344 // purge cache
345 $this->reset();
346
347 wfProfileOut( __METHOD__ );
348 return $success;
349 }
350
351 /**
352 * Purges the internal and external cache of the site list, forcing the list
353 * of sites to be re-read from the database.
354 *
355 * @since 1.21
356 */
357 public function reset() {
358 wfProfileIn( __METHOD__ );
359 // purge cache
360 $cache = wfGetMainCache();
361 $cache->delete( $this->getCacheKey() );
362 $this->sites = null;
363
364 wfProfileOut( __METHOD__ );
365 }
366
367 /**
368 * Clears the list of sites stored in the database.
369 *
370 * @see SiteStore::clear()
371 *
372 * @return bool success
373 */
374 public function clear() {
375 wfProfileIn( __METHOD__ );
376 $dbw = $this->sitesTable->getWriteDbConnection();
377
378 $trx = $dbw->trxLevel();
379
380 if ( $trx == 0 ) {
381 $dbw->begin( __METHOD__ );
382 }
383
384 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
385 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
386
387 if ( $trx == 0 ) {
388 $dbw->commit( __METHOD__ );
389 }
390
391 $this->reset();
392
393 wfProfileOut( __METHOD__ );
394 return $ok;
395 }
396
397 /**
398 * @since 1.21
399 *
400 * @return ORMTable
401 */
402 protected function newSitesTable() {
403 return new ORMTable(
404 'sites',
405 array(
406 'id' => 'id',
407
408 // Site data
409 'global_key' => 'str',
410 'type' => 'str',
411 'group' => 'str',
412 'source' => 'str',
413 'language' => 'str',
414 'protocol' => 'str',
415 'domain' => 'str',
416 'data' => 'array',
417
418 // Site config
419 'forward' => 'bool',
420 'config' => 'array',
421 ),
422 array(
423 'type' => Site::TYPE_UNKNOWN,
424 'group' => Site::GROUP_NONE,
425 'source' => Site::SOURCE_LOCAL,
426 'data' => array(),
427
428 'forward' => false,
429 'config' => array(),
430 'language' => '',
431 ),
432 'ORMRow',
433 'site_'
434 );
435 }
436
437 }
438
439 /**
440 * @deprecated
441 */
442 class Sites extends SiteSQLStore {
443
444 /**
445 * Factory for creating new site objects.
446 *
447 * @since 1.21
448 * @deprecated
449 *
450 * @param string|boolean false $globalId
451 *
452 * @return Site
453 */
454 public static function newSite( $globalId = false ) {
455 $site = new Site();
456
457 if ( $globalId !== false ) {
458 $site->setGlobalId( $globalId );
459 }
460
461 return $site;
462 }
463
464 /**
465 * @deprecated
466 * @return SiteStore
467 */
468 public static function singleton() {
469 static $singleton;
470
471 if ( $singleton === null ) {
472 $singleton = new static();
473 }
474
475 return $singleton;
476 }
477
478 /**
479 * @deprecated
480 * @return SiteList
481 */
482 public function getSiteGroup( $group ) {
483 return $this->getSites()->getGroup( $group );
484 }
485
486 }