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 >
31 class SiteSQLStore
implements SiteStore
{
37 protected $sites = null;
42 protected $sitesTable;
47 private $cacheKey = null;
52 private $cacheTimeout = 3600;
57 * @param ORMTable|null $sitesTable
61 public static function newInstance( ORMTable
$sitesTable = null ) {
62 return new static( $sitesTable );
70 * @param ORMTable|null $sitesTable
72 protected function __construct( ORMTable
$sitesTable = null ) {
73 if ( $sitesTable === null ) {
74 $sitesTable = $this->newSitesTable();
77 $this->sitesTable
= $sitesTable;
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
117 * @param string $source Either 'cache' or 'recache'
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;
140 wfProfileOut( __METHOD__
);
145 * Returns a new Site object constructed from the provided ORMRow.
149 * @param ORMRow $siteRow
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' ) === ''
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__
);
194 * Get a new ORMRow from a Site object
202 protected function getRowFromSite( Site
$site ) {
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(),
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.
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(
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
271 * @param string $globalId
272 * @param string $source
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
292 * @return bool Success indicator
294 public function saveSite( Site
$site ) {
295 return $this->saveSites( array( $site ) );
299 * @see SiteStore::saveSites
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__
);
315 $dbw = $this->sitesTable
->getWriteDbConnection();
317 $trx = $dbw->trxLevel();
320 $dbw->begin( __METHOD__
);
325 $internalIds = 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() ) {
346 array( 'si_site' => $internalIds ),
351 foreach ( $localIds as $localId ) {
355 'si_site' => $localId[0],
356 'si_type' => $localId[1],
357 'si_key' => $localId[2],
364 $dbw->commit( __METHOD__
);
370 wfProfileOut( __METHOD__
);
375 * Purges the internal and external cache of the site list, forcing the list
376 * of sites to be re-read from the database.
380 public function reset() {
381 wfProfileIn( __METHOD__
);
383 $cache = wfGetMainCache();
384 $cache->delete( $this->getCacheKey() );
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();
404 $dbw->begin( __METHOD__
);
407 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
408 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
411 $dbw->commit( __METHOD__
);
416 wfProfileOut( __METHOD__
);
425 protected function newSitesTable() {
432 'global_key' => 'str',
446 'type' => Site
::TYPE_UNKNOWN
,
447 'group' => Site
::GROUP_NONE
,
448 'source' => Site
::SOURCE_LOCAL
,
463 * @deprecated since 1.21
465 class Sites
extends SiteSQLStore
{
468 * Factory for creating new site objects.
471 * @deprecated since 1.21
473 * @param string|bool $globalId
477 public static function newSite( $globalId = false ) {
480 if ( $globalId !== false ) {
481 $site->setGlobalId( $globalId );
488 * @deprecated since 1.21
491 public static function singleton() {
494 if ( $singleton === null ) {
495 $singleton = new static();
502 * @deprecated since 1.21
503 * @param string $group
506 public function getSiteGroup( $group ) {
507 return $this->getSites()->getGroup( $group );