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
{
38 protected $sites = null;
43 protected $sitesTable;
48 private $cacheKey = null;
53 * @param ORMTable|null $sitesTable
57 public static function newInstance( ORMTable
$sitesTable = null ) {
58 return new static( $sitesTable );
66 * @param ORMTable|null $sitesTable
68 protected function __construct( ORMTable
$sitesTable = null ) {
69 if ( $sitesTable === null ) {
70 $sitesTable = $this->newSitesTable();
73 $this->sitesTable
= $sitesTable;
77 * Constructs a cache key to use for caching the list of sites.
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.
83 * The cache key also includes information about where the sites were loaded from, e.g.
84 * the name of a database table.
86 * @see SiteList::getSerialVersionId
88 * @return String The cache key.
90 protected function getCacheKey() {
91 if ( $this->cacheKey
=== null ) {
92 $type = 'SiteList#' . SiteList
::getSerialVersionId();
93 $source = $this->sitesTable
->getName();
95 if ( $this->sitesTable
->getTargetWiki() !== false ) {
96 $source = $this->sitesTable
->getTargetWiki() . '.' . $source;
99 $this->cacheKey
= wfMemcKey( "$source/$type" );
102 return $this->cacheKey
;
106 * @see SiteStore::getSites
110 * @param string $source either 'cache' or 'recache'
114 public function getSites( $source = 'cache' ) {
115 if ( $source === 'cache' ) {
116 if ( $this->sites
=== null ) {
117 $cache = wfGetMainCache();
118 $sites = $cache->get( $this->getCacheKey() );
120 if ( is_object( $sites ) ) {
121 $this->sites
= $sites;
135 * Returns a new Site object constructed from the provided ORMRow.
139 * @param ORMRow $siteRow
143 protected function siteFromRow( ORMRow
$siteRow ) {
144 $site = Site
::newForType( $siteRow->getField( 'type', Site
::TYPE_UNKNOWN
) );
146 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
148 if ( $siteRow->hasField( 'forward' ) ) {
149 $site->setForward( $siteRow->getField( 'forward' ) );
152 if ( $siteRow->hasField( 'group' ) ) {
153 $site->setGroup( $siteRow->getField( 'group' ) );
156 if ( $siteRow->hasField( 'language' ) ) {
157 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ?
null : $siteRow->getField( 'language' ) );
160 if ( $siteRow->hasField( 'source' ) ) {
161 $site->setSource( $siteRow->getField( 'source' ) );
164 if ( $siteRow->hasField( 'data' ) ) {
165 $site->setExtraData( $siteRow->getField( 'data' ) );
168 if ( $siteRow->hasField( 'config' ) ) {
169 $site->setExtraConfig( $siteRow->getField( 'config' ) );
176 * Fetches the site from the database and loads them into the sites field.
180 protected function loadSites() {
181 $this->sites
= new SiteList();
183 foreach ( $this->sitesTable
->select() as $siteRow ) {
184 $this->sites
[] = $this->siteFromRow( $siteRow );
187 // Batch load the local site identifiers.
188 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
199 foreach ( $ids as $id ) {
200 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
201 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
202 $site->addLocalId( $id->si_type
, $id->si_key
);
203 $this->sites
->setSite( $site );
207 $cache = wfGetMainCache();
208 $cache->set( $this->getCacheKey(), $this->sites
);
212 * @see SiteStore::getSite
216 * @param string $globalId
217 * @param string $source
221 public function getSite( $globalId, $source = 'cache' ) {
222 $sites = $this->getSites( $source );
224 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
228 * @see SiteStore::saveSite
234 * @return boolean Success indicator
236 public function saveSite( Site
$site ) {
237 return $this->saveSites( array( $site ) );
241 * @see SiteStore::saveSites
245 * @param Site[] $sites
247 * @return boolean Success indicator
249 public function saveSites( array $sites ) {
250 if ( empty( $sites ) ) {
254 $dbw = $this->sitesTable
->getWriteDbConnection();
256 $trx = $dbw->trxLevel();
259 $dbw->begin( __METHOD__
);
264 $internalIds = array();
267 foreach ( $sites as $site ) {
270 'global_key' => $site->getGlobalId(), // TODO: check not null
271 'type' => $site->getType(),
272 'group' => $site->getGroup(),
273 'source' => $site->getSource(),
274 'language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
275 'protocol' => $site->getProtocol(),
276 'domain' => strrev( $site->getDomain() ) . '.',
277 'data' => $site->getExtraData(),
280 'forward' => $site->shouldForward(),
281 'config' => $site->getExtraConfig(),
284 if ( $site->getInternalId() !== null ) {
285 $fields['id'] = $site->getInternalId();
286 $internalIds[] = $site->getInternalId();
289 $siteRow = new ORMRow( $this->sitesTable
, $fields );
290 $success = $siteRow->save( __METHOD__
) && $success;
292 foreach ( $site->getLocalIds() as $idType => $ids ) {
293 foreach ( $ids as $id ) {
294 $localIds[] = array( $siteRow->getId(), $idType, $id );
299 if ( $internalIds !== array() ) {
302 array( 'si_site' => $internalIds ),
307 foreach ( $localIds as $localId ) {
311 'si_site' => $localId[0],
312 'si_type' => $localId[1],
313 'si_key' => $localId[2],
320 $dbw->commit( __METHOD__
);
330 * Purges the internal and external cache of the site list, forcing the list
331 * of sites to be re-read from the database.
335 public function reset() {
337 $cache = wfGetMainCache();
338 $cache->delete( $this->getCacheKey() );
343 * Clears the list of sites stored in the database.
345 * @see SiteStore::clear()
347 * @return bool success
349 public function clear() {
350 $dbw = $this->sitesTable
->getWriteDbConnection();
352 $trx = $dbw->trxLevel();
355 $dbw->begin( __METHOD__
);
358 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
359 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
362 $dbw->commit( __METHOD__
);
375 protected function newSitesTable() {
382 'global_key' => 'str',
396 'type' => Site
::TYPE_UNKNOWN
,
397 'group' => Site
::GROUP_NONE
,
398 'source' => Site
::SOURCE_LOCAL
,
415 class Sites
extends SiteSQLStore
{
418 * Factory for creating new site objects.
423 * @param string|boolean false $globalId
427 public static function newSite( $globalId = false ) {
430 if ( $globalId !== false ) {
431 $site->setGlobalId( $globalId );
441 public static function singleton() {
449 public function getSiteGroup( $group ) {
450 return $this->getSites()->getGroup( $group );