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;
62 * @param ORMTable|null $sitesTable
63 * @param BagOStuff|null $cache
67 public static function newInstance( ORMTable
$sitesTable = null, BagOStuff
$cache = null ) {
68 if ( $cache === null ) {
69 $cache = wfGetMainCache();
72 return new static( $cache, $sitesTable );
80 * @param BagOStuff $cache
81 * @param ORMTable|null $sitesTable
83 protected function __construct( BagOStuff
$cache, ORMTable
$sitesTable = null ) {
84 if ( $sitesTable === null ) {
85 $sitesTable = $this->newSitesTable();
88 $this->cache
= $cache;
89 $this->sitesTable
= $sitesTable;
93 * Constructs a cache key to use for caching the list of sites.
95 * This includes the concrete class name of the site list as well as a version identifier
96 * for the list's serialization, to avoid problems when unserializing site lists serialized
97 * by an older version, e.g. when reading from a cache.
99 * The cache key also includes information about where the sites were loaded from, e.g.
100 * the name of a database table.
102 * @see SiteList::getSerialVersionId
104 * @return string The cache key.
106 protected function getCacheKey() {
108 if ( $this->cacheKey
=== null ) {
109 $type = 'SiteList#' . SiteList
::getSerialVersionId();
110 $source = $this->sitesTable
->getName();
112 if ( $this->sitesTable
->getTargetWiki() !== false ) {
113 $source = $this->sitesTable
->getTargetWiki() . '.' . $source;
116 $this->cacheKey
= wfMemcKey( "$source/$type" );
119 return $this->cacheKey
;
123 * @see SiteStore::getSites
127 * @param string $source Either 'cache' or 'recache'
131 public function getSites( $source = 'cache' ) {
133 if ( $source === 'cache' ) {
134 if ( $this->sites
=== null ) {
135 $sites = $this->cache
->get( $this->getCacheKey() );
137 if ( is_object( $sites ) ) {
138 $this->sites
= $sites;
152 * Returns a new Site object constructed from the provided ORMRow.
156 * @param ORMRow $siteRow
160 protected function siteFromRow( ORMRow
$siteRow ) {
162 $site = Site
::newForType( $siteRow->getField( 'type', Site
::TYPE_UNKNOWN
) );
164 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
166 $site->setInternalId( $siteRow->getField( 'id' ) );
168 if ( $siteRow->hasField( 'forward' ) ) {
169 $site->setForward( $siteRow->getField( 'forward' ) );
172 if ( $siteRow->hasField( 'group' ) ) {
173 $site->setGroup( $siteRow->getField( 'group' ) );
176 if ( $siteRow->hasField( 'language' ) ) {
177 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
179 : $siteRow->getField( 'language' )
183 if ( $siteRow->hasField( 'source' ) ) {
184 $site->setSource( $siteRow->getField( 'source' ) );
187 if ( $siteRow->hasField( 'data' ) ) {
188 $site->setExtraData( $siteRow->getField( 'data' ) );
191 if ( $siteRow->hasField( 'config' ) ) {
192 $site->setExtraConfig( $siteRow->getField( 'config' ) );
199 * Get a new ORMRow from a Site object
207 protected function getRowFromSite( Site
$site ) {
210 'global_key' => $site->getGlobalId(), // TODO: check not null
211 'type' => $site->getType(),
212 'group' => $site->getGroup(),
213 'source' => $site->getSource(),
214 'language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
215 'protocol' => $site->getProtocol(),
216 'domain' => strrev( $site->getDomain() ) . '.',
217 'data' => $site->getExtraData(),
220 'forward' => $site->shouldForward(),
221 'config' => $site->getExtraConfig(),
224 if ( $site->getInternalId() !== null ) {
225 $fields['id'] = $site->getInternalId();
228 return new ORMRow( $this->sitesTable
, $fields );
232 * Fetches the site from the database and loads them into the sites field.
236 protected function loadSites() {
238 $this->sites
= new SiteList();
240 foreach ( $this->sitesTable
->select() as $siteRow ) {
241 $this->sites
[] = $this->siteFromRow( $siteRow );
244 // Batch load the local site identifiers.
245 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
256 foreach ( $ids as $id ) {
257 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
258 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
259 $site->addLocalId( $id->si_type
, $id->si_key
);
260 $this->sites
->setSite( $site );
264 $this->cache
->set( $this->getCacheKey(), $this->sites
, $this->cacheTimeout
);
269 * @see SiteStore::getSite
273 * @param string $globalId
274 * @param string $source
278 public function getSite( $globalId, $source = 'cache' ) {
280 $sites = $this->getSites( $source );
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 ) {
309 if ( empty( $sites ) ) {
313 $dbw = $this->sitesTable
->getWriteDbConnection();
315 $dbw->startAtomic( __METHOD__
);
319 $internalIds = array();
322 foreach ( $sites as $site ) {
323 if ( $site->getInternalId() !== null ) {
324 $internalIds[] = $site->getInternalId();
327 $siteRow = $this->getRowFromSite( $site );
328 $success = $siteRow->save( __METHOD__
) && $success;
330 foreach ( $site->getLocalIds() as $idType => $ids ) {
331 foreach ( $ids as $id ) {
332 $localIds[] = array( $siteRow->getId(), $idType, $id );
337 if ( $internalIds !== array() ) {
340 array( 'si_site' => $internalIds ),
345 foreach ( $localIds as $localId ) {
349 'si_site' => $localId[0],
350 'si_type' => $localId[1],
351 'si_key' => $localId[2],
357 $dbw->endAtomic( __METHOD__
);
366 * Purges the internal and external cache of the site list, forcing the list
367 * of sites to be re-read from the database.
371 public function reset() {
373 $this->cache
->delete( $this->getCacheKey() );
379 * Clears the list of sites stored in the database.
381 * @see SiteStore::clear()
383 * @return bool Success
385 public function clear() {
386 $dbw = $this->sitesTable
->getWriteDbConnection();
388 $dbw->startAtomic( __METHOD__
);
389 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
390 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
391 $dbw->endAtomic( __METHOD__
);
403 protected function newSitesTable() {
410 'global_key' => 'str',
424 'type' => Site
::TYPE_UNKNOWN
,
425 'group' => Site
::GROUP_NONE
,
426 'source' => Site
::SOURCE_LOCAL
,