Merge "Localisation updates from https://translatewiki.net."
[mediawiki.git] / includes / site / SiteSQLStore.php
blob665940786f4520c982e71a8a7e581de84f377a71
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 SiteSQLStore implements SiteStore {
32 /**
33 * @since 1.21
35 * @var SiteList|null
37 protected $sites = null;
39 /**
40 * @var ORMTable
42 protected $sitesTable;
44 /**
45 * @var string|null
47 private $cacheKey = null;
49 /**
50 * @var int
52 private $cacheTimeout = 3600;
54 /**
55 * @since 1.21
57 * @param ORMTable|null $sitesTable
59 * @return SiteStore
61 public static function newInstance( ORMTable $sitesTable = null ) {
62 return new static( $sitesTable );
65 /**
66 * Constructor.
68 * @since 1.21
70 * @param ORMTable|null $sitesTable
72 protected function __construct( ORMTable $sitesTable = null ) {
73 if ( $sitesTable === null ) {
74 $sitesTable = $this->newSitesTable();
77 $this->sitesTable = $sitesTable;
80 /**
81 * Constructs a cache key to use for caching the list of sites.
83 * This includes the concrete class name of the site list as well as a version identifier
84 * for the list's serialization, to avoid problems when unserializing site lists serialized
85 * by an older version, e.g. when reading from a cache.
87 * The cache key also includes information about where the sites were loaded from, e.g.
88 * the name of a database table.
90 * @see SiteList::getSerialVersionId
92 * @return string The cache key.
94 protected function getCacheKey() {
95 wfProfileIn( __METHOD__ );
97 if ( $this->cacheKey === null ) {
98 $type = 'SiteList#' . SiteList::getSerialVersionId();
99 $source = $this->sitesTable->getName();
101 if ( $this->sitesTable->getTargetWiki() !== false ) {
102 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
105 $this->cacheKey = wfMemcKey( "$source/$type" );
108 wfProfileOut( __METHOD__ );
109 return $this->cacheKey;
113 * @see SiteStore::getSites
115 * @since 1.21
117 * @param string $source Either 'cache' or 'recache'
119 * @return SiteList
121 public function getSites( $source = 'cache' ) {
122 wfProfileIn( __METHOD__ );
124 if ( $source === 'cache' ) {
125 if ( $this->sites === null ) {
126 $cache = wfGetMainCache();
127 $sites = $cache->get( $this->getCacheKey() );
129 if ( is_object( $sites ) ) {
130 $this->sites = $sites;
131 } else {
132 $this->loadSites();
136 else {
137 $this->loadSites();
140 wfProfileOut( __METHOD__ );
141 return $this->sites;
145 * Returns a new Site object constructed from the provided ORMRow.
147 * @since 1.21
149 * @param ORMRow $siteRow
151 * @return Site
153 protected function siteFromRow( ORMRow $siteRow ) {
154 wfProfileIn( __METHOD__ );
156 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
158 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
160 $site->setInternalId( $siteRow->getField( 'id' ) );
162 if ( $siteRow->hasField( 'forward' ) ) {
163 $site->setForward( $siteRow->getField( 'forward' ) );
166 if ( $siteRow->hasField( 'group' ) ) {
167 $site->setGroup( $siteRow->getField( 'group' ) );
170 if ( $siteRow->hasField( 'language' ) ) {
171 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
172 ? null
173 : $siteRow->getField( 'language' )
177 if ( $siteRow->hasField( 'source' ) ) {
178 $site->setSource( $siteRow->getField( 'source' ) );
181 if ( $siteRow->hasField( 'data' ) ) {
182 $site->setExtraData( $siteRow->getField( 'data' ) );
185 if ( $siteRow->hasField( 'config' ) ) {
186 $site->setExtraConfig( $siteRow->getField( 'config' ) );
189 wfProfileOut( __METHOD__ );
190 return $site;
194 * Get a new ORMRow from a Site object
196 * @since 1.22
198 * @param Site $site
200 * @return ORMRow
202 protected function getRowFromSite( Site $site ) {
203 $fields = array(
204 // Site data
205 'global_key' => $site->getGlobalId(), // TODO: check not null
206 'type' => $site->getType(),
207 'group' => $site->getGroup(),
208 'source' => $site->getSource(),
209 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
210 'protocol' => $site->getProtocol(),
211 'domain' => strrev( $site->getDomain() ) . '.',
212 'data' => $site->getExtraData(),
214 // Site config
215 'forward' => $site->shouldForward(),
216 'config' => $site->getExtraConfig(),
219 if ( $site->getInternalId() !== null ) {
220 $fields['id'] = $site->getInternalId();
223 return new ORMRow( $this->sitesTable, $fields );
227 * Fetches the site from the database and loads them into the sites field.
229 * @since 1.21
231 protected function loadSites() {
232 wfProfileIn( __METHOD__ );
234 $this->sites = new SiteList();
236 foreach ( $this->sitesTable->select() as $siteRow ) {
237 $this->sites[] = $this->siteFromRow( $siteRow );
240 // Batch load the local site identifiers.
241 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
242 'site_identifiers',
243 array(
244 'si_site',
245 'si_type',
246 'si_key',
248 array(),
249 __METHOD__
252 foreach ( $ids as $id ) {
253 if ( $this->sites->hasInternalId( $id->si_site ) ) {
254 $site = $this->sites->getSiteByInternalId( $id->si_site );
255 $site->addLocalId( $id->si_type, $id->si_key );
256 $this->sites->setSite( $site );
260 $cache = wfGetMainCache();
261 $cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
263 wfProfileOut( __METHOD__ );
267 * @see SiteStore::getSite
269 * @since 1.21
271 * @param string $globalId
272 * @param string $source
274 * @return Site|null
276 public function getSite( $globalId, $source = 'cache' ) {
277 wfProfileIn( __METHOD__ );
279 $sites = $this->getSites( $source );
281 wfProfileOut( __METHOD__ );
282 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
286 * @see SiteStore::saveSite
288 * @since 1.21
290 * @param Site $site
292 * @return bool Success indicator
294 public function saveSite( Site $site ) {
295 return $this->saveSites( array( $site ) );
299 * @see SiteStore::saveSites
301 * @since 1.21
303 * @param Site[] $sites
305 * @return bool Success indicator
307 public function saveSites( array $sites ) {
308 wfProfileIn( __METHOD__ );
310 if ( empty( $sites ) ) {
311 wfProfileOut( __METHOD__ );
312 return true;
315 $dbw = $this->sitesTable->getWriteDbConnection();
317 $trx = $dbw->trxLevel();
319 if ( $trx == 0 ) {
320 $dbw->begin( __METHOD__ );
323 $success = true;
325 $internalIds = array();
326 $localIds = array();
328 foreach ( $sites as $site ) {
329 if ( $site->getInternalId() !== null ) {
330 $internalIds[] = $site->getInternalId();
333 $siteRow = $this->getRowFromSite( $site );
334 $success = $siteRow->save( __METHOD__ ) && $success;
336 foreach ( $site->getLocalIds() as $idType => $ids ) {
337 foreach ( $ids as $id ) {
338 $localIds[] = array( $siteRow->getId(), $idType, $id );
343 if ( $internalIds !== array() ) {
344 $dbw->delete(
345 'site_identifiers',
346 array( 'si_site' => $internalIds ),
347 __METHOD__
351 foreach ( $localIds as $localId ) {
352 $dbw->insert(
353 'site_identifiers',
354 array(
355 'si_site' => $localId[0],
356 'si_type' => $localId[1],
357 'si_key' => $localId[2],
359 __METHOD__
363 if ( $trx == 0 ) {
364 $dbw->commit( __METHOD__ );
367 // purge cache
368 $this->reset();
370 wfProfileOut( __METHOD__ );
371 return $success;
375 * Purges the internal and external cache of the site list, forcing the list
376 * of sites to be re-read from the database.
378 * @since 1.21
380 public function reset() {
381 wfProfileIn( __METHOD__ );
382 // purge cache
383 $cache = wfGetMainCache();
384 $cache->delete( $this->getCacheKey() );
385 $this->sites = null;
387 wfProfileOut( __METHOD__ );
391 * Clears the list of sites stored in the database.
393 * @see SiteStore::clear()
395 * @return bool Success
397 public function clear() {
398 wfProfileIn( __METHOD__ );
399 $dbw = $this->sitesTable->getWriteDbConnection();
401 $trx = $dbw->trxLevel();
403 if ( $trx == 0 ) {
404 $dbw->begin( __METHOD__ );
407 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
408 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
410 if ( $trx == 0 ) {
411 $dbw->commit( __METHOD__ );
414 $this->reset();
416 wfProfileOut( __METHOD__ );
417 return $ok;
421 * @since 1.21
423 * @return ORMTable
425 protected function newSitesTable() {
426 return new ORMTable(
427 'sites',
428 array(
429 'id' => 'id',
431 // Site data
432 'global_key' => 'str',
433 'type' => 'str',
434 'group' => 'str',
435 'source' => 'str',
436 'language' => 'str',
437 'protocol' => 'str',
438 'domain' => 'str',
439 'data' => 'array',
441 // Site config
442 'forward' => 'bool',
443 'config' => 'array',
445 array(
446 'type' => Site::TYPE_UNKNOWN,
447 'group' => Site::GROUP_NONE,
448 'source' => Site::SOURCE_LOCAL,
449 'data' => array(),
451 'forward' => false,
452 'config' => array(),
453 'language' => '',
455 'ORMRow',
456 'site_'
463 * @deprecated since 1.21
465 class Sites extends SiteSQLStore {
468 * Factory for creating new site objects.
470 * @since 1.21
471 * @deprecated since 1.21
473 * @param string|bool $globalId
475 * @return Site
477 public static function newSite( $globalId = false ) {
478 $site = new Site();
480 if ( $globalId !== false ) {
481 $site->setGlobalId( $globalId );
484 return $site;
488 * @deprecated since 1.21
489 * @return SiteStore
491 public static function singleton() {
492 static $singleton;
494 if ( $singleton === null ) {
495 $singleton = new static();
498 return $singleton;
502 * @deprecated since 1.21
503 * @param string $group
504 * @return SiteList
506 public function getSiteGroup( $group ) {
507 return $this->getSites()->getGroup( $group );