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 wfProfileIn( __METHOD__
);
93 if ( $this->cacheKey
=== null ) {
94 $type = 'SiteList#' . SiteList
::getSerialVersionId();
95 $source = $this->sitesTable
->getName();
97 if ( $this->sitesTable
->getTargetWiki() !== false ) {
98 $source = $this->sitesTable
->getTargetWiki() . '.' . $source;
101 $this->cacheKey
= wfMemcKey( "$source/$type" );
104 wfProfileOut( __METHOD__
);
105 return $this->cacheKey
;
109 * @see SiteStore::getSites
113 * @param string $source either 'cache' or 'recache'
117 public function getSites( $source = 'cache' ) {
118 wfProfileIn( __METHOD__
);
120 if ( $source === 'cache' ) {
121 if ( $this->sites
=== null ) {
122 $cache = wfGetMainCache();
123 $sites = $cache->get( $this->getCacheKey() );
125 if ( is_object( $sites ) ) {
126 $this->sites
= $sites;
136 wfProfileOut( __METHOD__
);
141 * Returns a new Site object constructed from the provided ORMRow.
145 * @param ORMRow $siteRow
149 protected function siteFromRow( ORMRow
$siteRow ) {
150 wfProfileIn( __METHOD__
);
152 $site = Site
::newForType( $siteRow->getField( 'type', Site
::TYPE_UNKNOWN
) );
154 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
156 $site->setInternalId( $siteRow->getField( 'id' ) );
158 if ( $siteRow->hasField( 'forward' ) ) {
159 $site->setForward( $siteRow->getField( 'forward' ) );
162 if ( $siteRow->hasField( 'group' ) ) {
163 $site->setGroup( $siteRow->getField( 'group' ) );
166 if ( $siteRow->hasField( 'language' ) ) {
167 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ?
null : $siteRow->getField( 'language' ) );
170 if ( $siteRow->hasField( 'source' ) ) {
171 $site->setSource( $siteRow->getField( 'source' ) );
174 if ( $siteRow->hasField( 'data' ) ) {
175 $site->setExtraData( $siteRow->getField( 'data' ) );
178 if ( $siteRow->hasField( 'config' ) ) {
179 $site->setExtraConfig( $siteRow->getField( 'config' ) );
182 wfProfileOut( __METHOD__
);
187 * Fetches the site from the database and loads them into the sites field.
191 protected function loadSites() {
192 wfProfileIn( __METHOD__
);
194 $this->sites
= new SiteList();
196 foreach ( $this->sitesTable
->select() as $siteRow ) {
197 $this->sites
[] = $this->siteFromRow( $siteRow );
200 // Batch load the local site identifiers.
201 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
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 );
220 $cache = wfGetMainCache();
221 $cache->set( $this->getCacheKey(), $this->sites
);
223 wfProfileOut( __METHOD__
);
227 * @see SiteStore::getSite
231 * @param string $globalId
232 * @param string $source
236 public function getSite( $globalId, $source = 'cache' ) {
237 wfProfileIn( __METHOD__
);
239 $sites = $this->getSites( $source );
241 wfProfileOut( __METHOD__
);
242 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
246 * @see SiteStore::saveSite
252 * @return boolean Success indicator
254 public function saveSite( Site
$site ) {
255 return $this->saveSites( array( $site ) );
259 * @see SiteStore::saveSites
263 * @param Site[] $sites
265 * @return boolean Success indicator
267 public function saveSites( array $sites ) {
268 wfProfileIn( __METHOD__
);
270 if ( empty( $sites ) ) {
271 wfProfileOut( __METHOD__
);
275 $dbw = $this->sitesTable
->getWriteDbConnection();
277 $trx = $dbw->trxLevel();
280 $dbw->begin( __METHOD__
);
285 $internalIds = array();
288 foreach ( $sites as $site ) {
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(),
301 'forward' => $site->shouldForward(),
302 'config' => $site->getExtraConfig(),
305 if ( $site->getInternalId() !== null ) {
306 $fields['id'] = $site->getInternalId();
307 $internalIds[] = $site->getInternalId();
310 $siteRow = new ORMRow( $this->sitesTable
, $fields );
311 $success = $siteRow->save( __METHOD__
) && $success;
313 foreach ( $site->getLocalIds() as $idType => $ids ) {
314 foreach ( $ids as $id ) {
315 $localIds[] = array( $siteRow->getId(), $idType, $id );
320 if ( $internalIds !== array() ) {
323 array( 'si_site' => $internalIds ),
328 foreach ( $localIds as $localId ) {
332 'si_site' => $localId[0],
333 'si_type' => $localId[1],
334 'si_key' => $localId[2],
341 $dbw->commit( __METHOD__
);
347 wfProfileOut( __METHOD__
);
352 * Purges the internal and external cache of the site list, forcing the list
353 * of sites to be re-read from the database.
357 public function reset() {
358 wfProfileIn( __METHOD__
);
360 $cache = wfGetMainCache();
361 $cache->delete( $this->getCacheKey() );
364 wfProfileOut( __METHOD__
);
368 * Clears the list of sites stored in the database.
370 * @see SiteStore::clear()
372 * @return bool success
374 public function clear() {
375 wfProfileIn( __METHOD__
);
376 $dbw = $this->sitesTable
->getWriteDbConnection();
378 $trx = $dbw->trxLevel();
381 $dbw->begin( __METHOD__
);
384 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
385 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
388 $dbw->commit( __METHOD__
);
393 wfProfileOut( __METHOD__
);
402 protected function newSitesTable() {
409 'global_key' => 'str',
423 'type' => Site
::TYPE_UNKNOWN
,
424 'group' => Site
::GROUP_NONE
,
425 'source' => Site
::SOURCE_LOCAL
,
442 class Sites
extends SiteSQLStore
{
445 * Factory for creating new site objects.
450 * @param string|boolean false $globalId
454 public static function newSite( $globalId = false ) {
457 if ( $globalId !== false ) {
458 $site->setGlobalId( $globalId );
468 public static function singleton() {
471 if ( $singleton === null ) {
472 $singleton = new static();
482 public function getSiteGroup( $group ) {
483 return $this->getSites()->getGroup( $group );