From e1a203603c1b982c677c0a35aab6685ee4be8f13 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Wed, 2 May 2018 22:12:12 +0200 Subject: [PATCH] Replace wfGetLB @deprecated since 1.27 Change-Id: Ibdd49fdfc0d1511503e1ed2173a592c612996c53 --- docs/database.txt | 4 ++-- includes/DefaultSettings.php | 4 ++-- includes/dao/DBAccessBase.php | 8 +++++--- includes/filerepo/ForeignDBViaLBRepo.php | 18 +++++++++++++++--- includes/interwiki/Interwiki.php | 2 +- includes/user/User.php | 3 ++- maintenance/getReplicaServer.php | 4 +++- maintenance/initEditCount.php | 5 ++++- maintenance/lag.php | 5 +++-- maintenance/mssql/tables.sql | 2 +- maintenance/tables.sql | 2 +- maintenance/updateSpecialPages.php | 17 +++++++++++++---- tests/parser/ParserTestRunner.php | 2 +- .../includes/Storage/RevisionStoreDbTest.php | 3 ++- .../includes/Storage/RevisionStoreTest.php | 10 ++++++---- .../phpunit/includes/site/DBSiteStoreTest.php | 5 ++++- 16 files changed, 65 insertions(+), 29 deletions(-) diff --git a/docs/database.txt b/docs/database.txt index 91b7e77a92..6e88d681f4 100644 --- a/docs/database.txt +++ b/docs/database.txt @@ -71,7 +71,7 @@ want to write code destined for Wikipedia. It's often the case that the best algorithm to use for a given task depends on whether or not replication is in use. Due to our unabashed Wikipedia-centrism, we often just use the replication-friendly version, -but if you like, you can use wfGetLB()->getServerCount() > 1 to +but if you like, you can use LoadBalancer::getServerCount() > 1 to check to see if replication is in use. === Lag === @@ -107,7 +107,7 @@ in the session, and then at the start of each request, waiting for the slave to catch up to that position before doing any reads from it. If this wait times out, reads are allowed anyway, but the request is considered to be in "lagged slave mode". Lagged slave mode can be -checked by calling wfGetLB()->getLaggedReplicaMode(). The only +checked by calling LoadBalancer::getLaggedReplicaMode(). The only practical consequence at present is a warning displayed in the page footer. diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 4f4fa86e18..6051511da0 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1919,8 +1919,8 @@ $wgSQLiteDataDir = ''; * $wgSharedSchema is the table schema for the shared database. It defaults to * $wgDBmwschema. * - * @deprecated since 1.21 In new code, use the $wiki parameter to wfGetLB() to - * access remote databases. Using wfGetLB() allows the shared database to + * @deprecated since 1.21 In new code, use the $wiki parameter to LBFactory::getMainLB() to + * access remote databases. Using LBFactory::getMainLB() allows the shared database to * reside on separate servers to the wiki's own database, with suitable * configuration of $wgLBFactoryConf. */ diff --git a/includes/dao/DBAccessBase.php b/includes/dao/DBAccessBase.php index 3947f4b19c..beac91e0e1 100644 --- a/includes/dao/DBAccessBase.php +++ b/includes/dao/DBAccessBase.php @@ -1,5 +1,6 @@ wiki ); + $loadBalancer = $this->getLoadBalancer(); return $loadBalancer->getConnection( $id, $groups, $this->wiki ); } @@ -83,13 +84,14 @@ abstract class DBAccessBase implements IDBAccessObject { /** * Get the database type used for read operations. * - * @see wfGetLB + * @see MediaWikiServices::getDBLoadBalancer * * @since 1.21 * * @return LoadBalancer The database load balancer object */ public function getLoadBalancer() { - return wfGetLB( $this->wiki ); + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + return $lbFactory->getMainLB( $this->wiki ); } } diff --git a/includes/filerepo/ForeignDBViaLBRepo.php b/includes/filerepo/ForeignDBViaLBRepo.php index 249cd27cfe..302b194a2c 100644 --- a/includes/filerepo/ForeignDBViaLBRepo.php +++ b/includes/filerepo/ForeignDBViaLBRepo.php @@ -21,6 +21,10 @@ * @ingroup FileRepo */ +use MediaWiki\MediaWikiServices; +use Wikimedia\Rdbms\IDatabase; +use Wikimedia\Rdbms\LoadBalancer; + /** * A foreign repository with a MediaWiki database accessible via the configured LBFactory * @@ -59,14 +63,14 @@ class ForeignDBViaLBRepo extends LocalRepo { * @return IDatabase */ function getMasterDB() { - return wfGetLB( $this->wiki )->getConnectionRef( DB_MASTER, [], $this->wiki ); + return $this->getDBLoadBalancer()->getConnectionRef( DB_MASTER, [], $this->wiki ); } /** * @return IDatabase */ function getReplicaDB() { - return wfGetLB( $this->wiki )->getConnectionRef( DB_REPLICA, [], $this->wiki ); + return $this->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, [], $this->wiki ); } /** @@ -74,10 +78,18 @@ class ForeignDBViaLBRepo extends LocalRepo { */ protected function getDBFactory() { return function ( $index ) { - return wfGetLB( $this->wiki )->getConnectionRef( $index, [], $this->wiki ); + return $this->getDBLoadBalancer()->getConnectionRef( $index, [], $this->wiki ); }; } + /** + * @return LoadBalancer + */ + protected function getDBLoadBalancer() { + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + return $lbFactory->getMainLB( $this->wiki ); + } + function hasSharedCache() { return $this->hasSharedCache; } diff --git a/includes/interwiki/Interwiki.php b/includes/interwiki/Interwiki.php index 5a996d9d11..657849adab 100644 --- a/includes/interwiki/Interwiki.php +++ b/includes/interwiki/Interwiki.php @@ -36,7 +36,7 @@ class Interwiki { protected $mAPI; /** @var string The name of the database (for a connection to be established - * with wfGetLB( 'wikiid' )) + * with LBFactory::getMainLB( 'wikiid' )) */ protected $mWikiID; diff --git a/includes/user/User.php b/includes/user/User.php index b8186d6f2c..5a5139d66c 100644 --- a/includes/user/User.php +++ b/includes/user/User.php @@ -414,7 +414,8 @@ class User implements IDBAccessObject, UserIdentity { break; case 'actor': // Make sure this thread sees its own changes - if ( wfGetLB()->hasOrMadeRecentMasterChanges() ) { + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); + if ( $lb->hasOrMadeRecentMasterChanges() ) { $flags |= self::READ_LATEST; $this->queryFlagsUsed = $flags; } diff --git a/maintenance/getReplicaServer.php b/maintenance/getReplicaServer.php index 43e876ea07..c0c628494e 100644 --- a/maintenance/getReplicaServer.php +++ b/maintenance/getReplicaServer.php @@ -23,6 +23,8 @@ require_once __DIR__ . '/Maintenance.php'; +use MediaWiki\MediaWikiServices; + /** * Maintenance script that reports the hostname of a replica DB server. * @@ -43,7 +45,7 @@ class GetSlaveServer extends Maintenance { $db = $this->getDB( DB_REPLICA, $this->getOption( 'group' ) ); $host = $db->getServer(); } else { - $lb = wfGetLB(); + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); $i = $lb->getReaderIndex(); $host = $lb->getServerName( $i ); } diff --git a/maintenance/initEditCount.php b/maintenance/initEditCount.php index f7ef7a28a2..1b9dac068d 100644 --- a/maintenance/initEditCount.php +++ b/maintenance/initEditCount.php @@ -24,6 +24,8 @@ require_once __DIR__ . '/Maintenance.php'; +use MediaWiki\MediaWikiServices; + class InitEditCount extends Maintenance { public function __construct() { parent::__construct(); @@ -48,7 +50,8 @@ in the load balancer, usually indicating a replication environment.' ); } elseif ( $this->hasOption( 'quick' ) ) { $backgroundMode = false; } else { - $backgroundMode = wfGetLB()->getServerCount() > 1; + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); + $backgroundMode = $lb->getServerCount() > 1; } $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' ); diff --git a/maintenance/lag.php b/maintenance/lag.php index f041e15cc5..08df8dc347 100644 --- a/maintenance/lag.php +++ b/maintenance/lag.php @@ -23,6 +23,8 @@ require_once __DIR__ . '/Maintenance.php'; +use MediaWiki\MediaWikiServices; + /** * Maintenance script to show database lag. * @@ -36,8 +38,8 @@ class DatabaseLag extends Maintenance { } public function execute() { + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); if ( $this->hasOption( 'r' ) ) { - $lb = wfGetLB(); echo 'time '; $serverCount = $lb->getServerCount(); @@ -58,7 +60,6 @@ class DatabaseLag extends Maintenance { sleep( 5 ); } } else { - $lb = wfGetLB(); $lags = $lb->getLagTimes(); foreach ( $lags as $i => $lag ) { $name = $lb->getServerName( $i ); diff --git a/maintenance/mssql/tables.sql b/maintenance/mssql/tables.sql index 65ede90ea8..0c78712ad9 100644 --- a/maintenance/mssql/tables.sql +++ b/maintenance/mssql/tables.sql @@ -1091,7 +1091,7 @@ CREATE TABLE /*_*/interwiki ( -- The URL of the file api.php iw_api nvarchar(max) NOT NULL, - -- The name of the database (for a connection to be established with wfGetLB( 'wikiid' )) + -- The name of the database (for a connection to be established with LBFactory::getMainLB( 'wikiid' )) iw_wikiid nvarchar(64) NOT NULL, -- A boolean value indicating whether the wiki is in this project diff --git a/maintenance/tables.sql b/maintenance/tables.sql index df3264a43c..8c9be05016 100644 --- a/maintenance/tables.sql +++ b/maintenance/tables.sql @@ -1540,7 +1540,7 @@ CREATE TABLE /*_*/interwiki ( -- The URL of the file api.php iw_api blob NOT NULL, - -- The name of the database (for a connection to be established with wfGetLB( 'wikiid' )) + -- The name of the database (for a connection to be established with LBFactory::getMainLB( 'wikiid' )) iw_wikiid varchar(64) NOT NULL, -- A boolean value indicating whether the wiki is in this project diff --git a/maintenance/updateSpecialPages.php b/maintenance/updateSpecialPages.php index 1c6f9b339d..1491e62ac5 100644 --- a/maintenance/updateSpecialPages.php +++ b/maintenance/updateSpecialPages.php @@ -24,6 +24,9 @@ require_once __DIR__ . '/Maintenance.php'; +use MediaWiki\MediaWikiServices; +use Wikimedia\Rdbms\DBReplicationWaitError; + /** * Maintenance script to update cached special pages. * @@ -119,16 +122,22 @@ class UpdateSpecialPages extends Maintenance { * mysql connection to "go away" */ private function reopenAndWaitForReplicas() { - if ( !wfGetLB()->pingAll() ) { + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lb = $lbFactory->getMainLB(); + if ( !$lb->pingAll() ) { $this->output( "\n" ); do { $this->error( "Connection failed, reconnecting in 10 seconds..." ); sleep( 10 ); - } while ( !wfGetLB()->pingAll() ); + } while ( !$lb->pingAll() ); $this->output( "Reconnected\n\n" ); } - # Wait for the replica DB to catch up - wfWaitForSlaves(); + // Wait for the replica DB to catch up + try { + $lbFactory->waitForReplication(); + } catch ( DBReplicationWaitError $e ) { + // ignore + } } public function doSpecialPageCacheUpdates( $dbw ) { diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index 91ddf24acb..df4e67d90f 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -1244,7 +1244,7 @@ class ParserTestRunner { $teardown[] = $this->markSetupDone( 'setupDatabase' ); # CREATE TEMPORARY TABLE breaks if there is more than one server - if ( wfGetLB()->getServerCount() != 1 ) { + if ( MediaWikiServices::getInstance()->getDBLoadBalancer()->getServerCount() != 1 ) { $this->useTemporaryTables = false; } diff --git a/tests/phpunit/includes/Storage/RevisionStoreDbTest.php b/tests/phpunit/includes/Storage/RevisionStoreDbTest.php index 7d6906c1fa..2a9295628e 100644 --- a/tests/phpunit/includes/Storage/RevisionStoreDbTest.php +++ b/tests/phpunit/includes/Storage/RevisionStoreDbTest.php @@ -1259,7 +1259,8 @@ class RevisionStoreDbTest extends MediaWikiTestCase { */ public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) { $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); - $blobStore = new SqlBlobStore( wfGetLB(), $cache ); + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); + $blobStore = new SqlBlobStore( $lb, $cache ); $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) ); $factory = $this->getMockBuilder( BlobStoreFactory::class ) diff --git a/tests/phpunit/includes/Storage/RevisionStoreTest.php b/tests/phpunit/includes/Storage/RevisionStoreTest.php index 0bce572da1..fed9a0c3d5 100644 --- a/tests/phpunit/includes/Storage/RevisionStoreTest.php +++ b/tests/phpunit/includes/Storage/RevisionStoreTest.php @@ -614,11 +614,12 @@ class RevisionStoreTest extends MediaWikiTestCase { */ public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) { $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); - $blobStore = new SqlBlobStore( wfGetLB(), $cache ); + $blobStore = new SqlBlobStore( $lb, $cache ); $blobStore->setLegacyEncoding( $encoding, Language::factory( $locale ) ); - $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache ); + $store = $this->getRevisionStore( $lb, $blobStore, $cache ); $record = $store->newRevisionFromRow( $this->makeRow( $row ), @@ -640,11 +641,12 @@ class RevisionStoreTest extends MediaWikiTestCase { ]; $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); - $blobStore = new SqlBlobStore( wfGetLB(), $cache ); + $blobStore = new SqlBlobStore( $lb, $cache ); $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) ); - $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache ); + $store = $this->getRevisionStore( $lb, $blobStore, $cache ); $record = $store->newRevisionFromRow( $this->makeRow( $row ), diff --git a/tests/phpunit/includes/site/DBSiteStoreTest.php b/tests/phpunit/includes/site/DBSiteStoreTest.php index 7c16f6c51f..da6e9f9305 100644 --- a/tests/phpunit/includes/site/DBSiteStoreTest.php +++ b/tests/phpunit/includes/site/DBSiteStoreTest.php @@ -1,5 +1,7 @@ getDBLoadBalancer(); + return new DBSiteStore( $lb ); } /** -- 2.20.1