mediawiki.inspect: add CSS report
[mediawiki.git] / includes / site / SiteSQLStore.php
blob11141e07df5844286c4a7bf94bc12ecc78fb6304
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 {
33 /**
34 * @since 1.21
36 * @var SiteList|null
38 protected $sites = null;
40 /**
41 * @var ORMTable
43 protected $sitesTable;
45 /**
46 * @var string|null
48 private $cacheKey = null;
50 /**
51 * @var int
53 private $cacheTimeout = 3600;
55 /**
56 * @since 1.21
58 * @param ORMTable|null $sitesTable
60 * @return SiteStore
62 public static function newInstance( ORMTable $sitesTable = null ) {
63 return new static( $sitesTable );
66 /**
67 * Constructor.
69 * @since 1.21
71 * @param ORMTable|null $sitesTable
73 protected function __construct( ORMTable $sitesTable = null ) {
74 if ( $sitesTable === null ) {
75 $sitesTable = $this->newSitesTable();
78 $this->sitesTable = $sitesTable;
81 /**
82 * Constructs a cache key to use for caching the list of sites.
84 * This includes the concrete class name of the site list as well as a version identifier
85 * for the list's serialization, to avoid problems when unserializing site lists serialized
86 * by an older version, e.g. when reading from a cache.
88 * The cache key also includes information about where the sites were loaded from, e.g.
89 * the name of a database table.
91 * @see SiteList::getSerialVersionId
93 * @return String The cache key.
95 protected function getCacheKey() {
96 wfProfileIn( __METHOD__ );
98 if ( $this->cacheKey === null ) {
99 $type = 'SiteList#' . SiteList::getSerialVersionId();
100 $source = $this->sitesTable->getName();
102 if ( $this->sitesTable->getTargetWiki() !== false ) {
103 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
106 $this->cacheKey = wfMemcKey( "$source/$type" );
109 wfProfileOut( __METHOD__ );
110 return $this->cacheKey;
114 * @see SiteStore::getSites
116 * @since 1.21
118 * @param string $source either 'cache' or 'recache'
120 * @return SiteList
122 public function getSites( $source = 'cache' ) {
123 wfProfileIn( __METHOD__ );
125 if ( $source === 'cache' ) {
126 if ( $this->sites === null ) {
127 $cache = wfGetMainCache();
128 $sites = $cache->get( $this->getCacheKey() );
130 if ( is_object( $sites ) ) {
131 $this->sites = $sites;
132 } else {
133 $this->loadSites();
137 else {
138 $this->loadSites();
141 wfProfileOut( __METHOD__ );
142 return $this->sites;
146 * Returns a new Site object constructed from the provided ORMRow.
148 * @since 1.21
150 * @param ORMRow $siteRow
152 * @return Site
154 protected function siteFromRow( ORMRow $siteRow ) {
155 wfProfileIn( __METHOD__ );
157 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
159 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
161 $site->setInternalId( $siteRow->getField( 'id' ) );
163 if ( $siteRow->hasField( 'forward' ) ) {
164 $site->setForward( $siteRow->getField( 'forward' ) );
167 if ( $siteRow->hasField( 'group' ) ) {
168 $site->setGroup( $siteRow->getField( 'group' ) );
171 if ( $siteRow->hasField( 'language' ) ) {
172 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ? null : $siteRow->getField( 'language' ) );
175 if ( $siteRow->hasField( 'source' ) ) {
176 $site->setSource( $siteRow->getField( 'source' ) );
179 if ( $siteRow->hasField( 'data' ) ) {
180 $site->setExtraData( $siteRow->getField( 'data' ) );
183 if ( $siteRow->hasField( 'config' ) ) {
184 $site->setExtraConfig( $siteRow->getField( 'config' ) );
187 wfProfileOut( __METHOD__ );
188 return $site;
192 * Get a new ORMRow from a Site object
194 * @since 1.22
196 * @param Site
198 * @return ORMRow
200 protected function getRowFromSite( Site $site ) {
201 $fields = array(
202 // Site data
203 'global_key' => $site->getGlobalId(), // TODO: check not null
204 'type' => $site->getType(),
205 'group' => $site->getGroup(),
206 'source' => $site->getSource(),
207 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
208 'protocol' => $site->getProtocol(),
209 'domain' => strrev( $site->getDomain() ) . '.',
210 'data' => $site->getExtraData(),
212 // Site config
213 'forward' => $site->shouldForward(),
214 'config' => $site->getExtraConfig(),
217 if ( $site->getInternalId() !== null ) {
218 $fields['id'] = $site->getInternalId();
221 return new ORMRow( $this->sitesTable, $fields );
225 * Fetches the site from the database and loads them into the sites field.
227 * @since 1.21
229 protected function loadSites() {
230 wfProfileIn( __METHOD__ );
232 $this->sites = new SiteList();
234 foreach ( $this->sitesTable->select() as $siteRow ) {
235 $this->sites[] = $this->siteFromRow( $siteRow );
238 // Batch load the local site identifiers.
239 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
240 'site_identifiers',
241 array(
242 'si_site',
243 'si_type',
244 'si_key',
246 array(),
247 __METHOD__
250 foreach ( $ids as $id ) {
251 if ( $this->sites->hasInternalId( $id->si_site ) ) {
252 $site = $this->sites->getSiteByInternalId( $id->si_site );
253 $site->addLocalId( $id->si_type, $id->si_key );
254 $this->sites->setSite( $site );
258 $cache = wfGetMainCache();
259 $cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
261 wfProfileOut( __METHOD__ );
265 * @see SiteStore::getSite
267 * @since 1.21
269 * @param string $globalId
270 * @param string $source
272 * @return Site|null
274 public function getSite( $globalId, $source = 'cache' ) {
275 wfProfileIn( __METHOD__ );
277 $sites = $this->getSites( $source );
279 wfProfileOut( __METHOD__ );
280 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
284 * @see SiteStore::saveSite
286 * @since 1.21
288 * @param Site $site
290 * @return boolean Success indicator
292 public function saveSite( Site $site ) {
293 return $this->saveSites( array( $site ) );
297 * @see SiteStore::saveSites
299 * @since 1.21
301 * @param Site[] $sites
303 * @return boolean Success indicator
305 public function saveSites( array $sites ) {
306 wfProfileIn( __METHOD__ );
308 if ( empty( $sites ) ) {
309 wfProfileOut( __METHOD__ );
310 return true;
313 $dbw = $this->sitesTable->getWriteDbConnection();
315 $trx = $dbw->trxLevel();
317 if ( $trx == 0 ) {
318 $dbw->begin( __METHOD__ );
321 $success = true;
323 $internalIds = array();
324 $localIds = array();
326 foreach ( $sites as $site ) {
327 if ( $site->getInternalId() !== null ) {
328 $internalIds[] = $site->getInternalId();
331 $siteRow = $this->getRowFromSite( $site );
332 $success = $siteRow->save( __METHOD__ ) && $success;
334 foreach ( $site->getLocalIds() as $idType => $ids ) {
335 foreach ( $ids as $id ) {
336 $localIds[] = array( $siteRow->getId(), $idType, $id );
341 if ( $internalIds !== array() ) {
342 $dbw->delete(
343 'site_identifiers',
344 array( 'si_site' => $internalIds ),
345 __METHOD__
349 foreach ( $localIds as $localId ) {
350 $dbw->insert(
351 'site_identifiers',
352 array(
353 'si_site' => $localId[0],
354 'si_type' => $localId[1],
355 'si_key' => $localId[2],
357 __METHOD__
361 if ( $trx == 0 ) {
362 $dbw->commit( __METHOD__ );
365 // purge cache
366 $this->reset();
368 wfProfileOut( __METHOD__ );
369 return $success;
373 * Purges the internal and external cache of the site list, forcing the list
374 * of sites to be re-read from the database.
376 * @since 1.21
378 public function reset() {
379 wfProfileIn( __METHOD__ );
380 // purge cache
381 $cache = wfGetMainCache();
382 $cache->delete( $this->getCacheKey() );
383 $this->sites = null;
385 wfProfileOut( __METHOD__ );
389 * Clears the list of sites stored in the database.
391 * @see SiteStore::clear()
393 * @return bool success
395 public function clear() {
396 wfProfileIn( __METHOD__ );
397 $dbw = $this->sitesTable->getWriteDbConnection();
399 $trx = $dbw->trxLevel();
401 if ( $trx == 0 ) {
402 $dbw->begin( __METHOD__ );
405 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
406 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
408 if ( $trx == 0 ) {
409 $dbw->commit( __METHOD__ );
412 $this->reset();
414 wfProfileOut( __METHOD__ );
415 return $ok;
419 * @since 1.21
421 * @return ORMTable
423 protected function newSitesTable() {
424 return new ORMTable(
425 'sites',
426 array(
427 'id' => 'id',
429 // Site data
430 'global_key' => 'str',
431 'type' => 'str',
432 'group' => 'str',
433 'source' => 'str',
434 'language' => 'str',
435 'protocol' => 'str',
436 'domain' => 'str',
437 'data' => 'array',
439 // Site config
440 'forward' => 'bool',
441 'config' => 'array',
443 array(
444 'type' => Site::TYPE_UNKNOWN,
445 'group' => Site::GROUP_NONE,
446 'source' => Site::SOURCE_LOCAL,
447 'data' => array(),
449 'forward' => false,
450 'config' => array(),
451 'language' => '',
453 'ORMRow',
454 'site_'
461 * @deprecated
463 class Sites extends SiteSQLStore {
466 * Factory for creating new site objects.
468 * @since 1.21
469 * @deprecated
471 * @param string|boolean false $globalId
473 * @return Site
475 public static function newSite( $globalId = false ) {
476 $site = new Site();
478 if ( $globalId !== false ) {
479 $site->setGlobalId( $globalId );
482 return $site;
486 * @deprecated
487 * @return SiteStore
489 public static function singleton() {
490 static $singleton;
492 if ( $singleton === null ) {
493 $singleton = new static();
496 return $singleton;
500 * @deprecated
501 * @return SiteList
503 public function getSiteGroup( $group ) {
504 return $this->getSites()->getGroup( $group );