Replace wfGetLB
authorUmherirrender <umherirrender_de.wp@web.de>
Wed, 2 May 2018 20:12:12 +0000 (22:12 +0200)
committerUmherirrender <umherirrender_de.wp@web.de>
Wed, 2 May 2018 20:30:24 +0000 (22:30 +0200)
@deprecated since 1.27

Change-Id: Ibdd49fdfc0d1511503e1ed2173a592c612996c53

16 files changed:
docs/database.txt
includes/DefaultSettings.php
includes/dao/DBAccessBase.php
includes/filerepo/ForeignDBViaLBRepo.php
includes/interwiki/Interwiki.php
includes/user/User.php
maintenance/getReplicaServer.php
maintenance/initEditCount.php
maintenance/lag.php
maintenance/mssql/tables.sql
maintenance/tables.sql
maintenance/updateSpecialPages.php
tests/parser/ParserTestRunner.php
tests/phpunit/includes/Storage/RevisionStoreDbTest.php
tests/phpunit/includes/Storage/RevisionStoreTest.php
tests/phpunit/includes/site/DBSiteStoreTest.php

index 91b7e77..6e88d68 100644 (file)
@@ -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.
 
index 4f4fa86..6051511 100644 (file)
@@ -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.
  */
index 3947f4b..beac91e 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\LoadBalancer;
 
@@ -59,7 +60,7 @@ abstract class DBAccessBase implements IDBAccessObject {
         * @return IDatabase
         */
        protected function getConnection( $id, $groups = [] ) {
-               $loadBalancer = wfGetLB( $this->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 );
        }
 }
index 249cd27..302b194 100644 (file)
  * @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;
        }
index 5a996d9..657849a 100644 (file)
@@ -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;
 
index b8186d6..5a5139d 100644 (file)
@@ -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;
                                }
index 43e876e..c0c6284 100644 (file)
@@ -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 );
                }
index f7ef7a2..1b9dac0 100644 (file)
@@ -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' );
index f041e15..08df8dc 100644 (file)
@@ -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 );
index 65ede90..0c78712 100644 (file)
@@ -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
index df3264a..8c9be05 100644 (file)
@@ -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
index 1c6f9b3..1491e62 100644 (file)
@@ -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 ) {
index 91ddf24..df4e67d 100644 (file)
@@ -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;
                }
 
index 7d6906c..2a92956 100644 (file)
@@ -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 )
index 0bce572..fed9a0c 100644 (file)
@@ -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 ),
index 7c16f6c..da6e9f9 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Tests for the DBSiteStore class.
  *
@@ -37,7 +39,8 @@ class DBSiteStoreTest extends MediaWikiTestCase {
        private function newDBSiteStore() {
                // NOTE: Use the real DB load balancer for now. Eventually, the test framework should
                // provide a LoadBalancer that is safe to use in unit tests.
-               return new DBSiteStore( wfGetLB() );
+               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+               return new DBSiteStore( $lb );
        }
 
        /**