- $cache = wfGetMainCache();
- }
-
- return new static( $cache, $sitesTable );
- }
-
- /**
- * Constructor.
- *
- * @since 1.21
- *
- * @param BagOStuff $cache
- * @param ORMTable|null $sitesTable
- */
- protected function __construct( BagOStuff $cache, ORMTable $sitesTable = null ) {
- if ( $sitesTable === null ) {
- $sitesTable = $this->newSitesTable();
- }
-
- $this->cache = $cache;
- $this->sitesTable = $sitesTable;
- }
-
- /**
- * Constructs a cache key to use for caching the list of sites.
- *
- * This includes the concrete class name of the site list as well as a version identifier
- * for the list's serialization, to avoid problems when unserializing site lists serialized
- * by an older version, e.g. when reading from a cache.
- *
- * The cache key also includes information about where the sites were loaded from, e.g.
- * the name of a database table.
- *
- * @see SiteList::getSerialVersionId
- *
- * @return string The cache key.
- */
- protected function getCacheKey() {
-
- if ( $this->cacheKey === null ) {
- $type = 'SiteList#' . SiteList::getSerialVersionId();
- $source = $this->sitesTable->getName();
-
- if ( $this->sitesTable->getTargetWiki() !== false ) {
- $source = $this->sitesTable->getTargetWiki() . '.' . $source;
- }
-
- $this->cacheKey = wfMemcKey( "$source/$type" );
- }
-
- return $this->cacheKey;
- }
-
- /**
- * @see SiteStore::getSites
- *
- * @since 1.21
- *
- * @param string $source Either 'cache' or 'recache'
- *
- * @return SiteList
- */
- public function getSites( $source = 'cache' ) {
-
- if ( $source === 'cache' ) {
- if ( $this->sites === null ) {
- $sites = $this->cache->get( $this->getCacheKey() );
-
- if ( is_object( $sites ) ) {
- $this->sites = $sites;
- } else {
- $this->loadSites();
- }
- }
- }
- else {
- $this->loadSites();
- }
-
- return $this->sites;
- }
-
- /**
- * Returns a new Site object constructed from the provided ORMRow.
- *
- * @since 1.21
- *
- * @param ORMRow $siteRow
- *
- * @return Site
- */
- protected function siteFromRow( ORMRow $siteRow ) {
-
- $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
-
- $site->setGlobalId( $siteRow->getField( 'global_key' ) );
-
- $site->setInternalId( $siteRow->getField( 'id' ) );
-
- if ( $siteRow->hasField( 'forward' ) ) {
- $site->setForward( $siteRow->getField( 'forward' ) );
- }
-
- if ( $siteRow->hasField( 'group' ) ) {
- $site->setGroup( $siteRow->getField( 'group' ) );
- }
-
- if ( $siteRow->hasField( 'language' ) ) {
- $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
- ? null
- : $siteRow->getField( 'language' )
- );
- }
-
- if ( $siteRow->hasField( 'source' ) ) {
- $site->setSource( $siteRow->getField( 'source' ) );
- }
-
- if ( $siteRow->hasField( 'data' ) ) {
- $site->setExtraData( $siteRow->getField( 'data' ) );
- }
-
- if ( $siteRow->hasField( 'config' ) ) {
- $site->setExtraConfig( $siteRow->getField( 'config' ) );
- }
-
- return $site;
- }
-
- /**
- * Get a new ORMRow from a Site object
- *
- * @since 1.22
- *
- * @param Site $site
- *
- * @return ORMRow
- */
- protected function getRowFromSite( Site $site ) {
- $fields = array(
- // Site data
- 'global_key' => $site->getGlobalId(), // TODO: check not null
- 'type' => $site->getType(),
- 'group' => $site->getGroup(),
- 'source' => $site->getSource(),
- 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
- 'protocol' => $site->getProtocol(),
- 'domain' => strrev( $site->getDomain() ) . '.',
- 'data' => $site->getExtraData(),
-
- // Site config
- 'forward' => $site->shouldForward(),
- 'config' => $site->getExtraConfig(),
- );
-
- if ( $site->getInternalId() !== null ) {
- $fields['id'] = $site->getInternalId();
- }
-
- return new ORMRow( $this->sitesTable, $fields );
- }
-
- /**
- * Fetches the site from the database and loads them into the sites field.
- *
- * @since 1.21
- */
- protected function loadSites() {
-
- $this->sites = new SiteList();
-
- foreach ( $this->sitesTable->select() as $siteRow ) {
- $this->sites[] = $this->siteFromRow( $siteRow );
- }
-
- // Batch load the local site identifiers.
- $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
- 'site_identifiers',
- array(
- 'si_site',
- 'si_type',
- 'si_key',
- ),
- array(),
- __METHOD__
- );
-
- foreach ( $ids as $id ) {
- if ( $this->sites->hasInternalId( $id->si_site ) ) {
- $site = $this->sites->getSiteByInternalId( $id->si_site );
- $site->addLocalId( $id->si_type, $id->si_key );
- $this->sites->setSite( $site );
- }
- }
-
- $this->cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
-
- }
-
- /**
- * @see SiteStore::getSite
- *
- * @since 1.21
- *
- * @param string $globalId
- * @param string $source
- *
- * @return Site|null
- */
- public function getSite( $globalId, $source = 'cache' ) {
-
- $sites = $this->getSites( $source );
-
- return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
- }
-
- /**
- * @see SiteStore::saveSite
- *
- * @since 1.21
- *
- * @param Site $site
- *
- * @return bool Success indicator
- */
- public function saveSite( Site $site ) {
- return $this->saveSites( array( $site ) );
- }
-
- /**
- * @see SiteStore::saveSites
- *
- * @since 1.21
- *
- * @param Site[] $sites
- *
- * @return bool Success indicator
- */
- public function saveSites( array $sites ) {
-
- if ( empty( $sites ) ) {
- return true;