Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/310762

Change subject: Avoid global state in DatabaseBase::factory()
......................................................................

Avoid global state in DatabaseBase::factory()

Change-Id: Ibb4f1c0dafea071a1c34e0cd5b5c15b8b4bb7bc6
---
M includes/db/Database.php
M includes/db/loadbalancer/LBFactory.php
M includes/libs/rdbms/loadbalancer/LoadBalancer.php
3 files changed, 18 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/62/310762/1

diff --git a/includes/db/Database.php b/includes/db/Database.php
index 0c357cc..391620c 100644
--- a/includes/db/Database.php
+++ b/includes/db/Database.php
@@ -324,8 +324,6 @@
         * @throws InvalidArgumentException If the database driver or extension 
cannot be found
         */
        final public static function factory( $dbType, $p = [] ) {
-               global $wgCommandLineMode;
-
                $canonicalDBTypes = [
                        'mysql' => [ 'mysqli', 'mysql' ],
                        'postgres' => [],
@@ -383,7 +381,7 @@
                                $p['schema'] = isset( $defaultSchemas[$dbType] 
) ? $defaultSchemas[$dbType] : null;
                        }
                        $p['foreign'] = isset( $p['foreign'] ) ? $p['foreign'] 
: false;
-                       $p['cliMode'] = $wgCommandLineMode;
+                       $p['cliMode'] = isset( $p['cliMode'] ) ? $p['cliMode'] 
: PHP_SAPI === 'cli';
 
                        $conn = new $class( $p );
                        if ( isset( $p['connLogger'] ) ) {
@@ -395,7 +393,9 @@
                        if ( isset( $p['errorLogger'] ) ) {
                                $conn->errorLogger = $p['errorLogger'];
                        } else {
-                               $conn->errorLogger = [ 
MWExceptionHandler::class, 'logException' ];
+                               $conn->errorLogger = function ( Exception $e ) {
+                                       trigger_error( get_class( $e ) . ': ' . 
$e->getMessage(), E_WARNING );
+                               };
                        }
                } else {
                        $conn = null;
diff --git a/includes/db/loadbalancer/LBFactory.php 
b/includes/db/loadbalancer/LBFactory.php
index 6fd1550..ebc33e3 100644
--- a/includes/db/loadbalancer/LBFactory.php
+++ b/includes/db/loadbalancer/LBFactory.php
@@ -55,6 +55,9 @@
        /** @var callable[] */
        protected $replicationWaitCallbacks = [];
 
+       /** @var bool */
+       protected $cliMode;
+
        const SHUTDOWN_NO_CHRONPROT = 0; // don't save DB positions at all
        const SHUTDOWN_CHRONPROT_ASYNC = 1; // save DB positions, but don't 
wait on remote DCs
        const SHUTDOWN_CHRONPROT_SYNC = 2; // save DB positions, waiting on all 
DCs
@@ -65,6 +68,8 @@
         * @TODO: inject objects via dependency framework
         */
        public function __construct( array $conf ) {
+               global $wgCommandLineMode;
+
                if ( isset( $conf['readOnlyReason'] ) && is_string( 
$conf['readOnlyReason'] ) ) {
                        $this->readOnlyReason = $conf['readOnlyReason'];
                }
@@ -92,6 +97,7 @@
                $this->replLogger = LoggerFactory::getInstance( 'DBReplication' 
);
                $this->chronProt = $this->newChronologyProtector();
                $this->ticket = mt_rand();
+               $this->cliMode = isset( $params['cliMode'] ) ? 
$params['cliMode'] : $wgCommandLineMode;
        }
 
        /**
@@ -670,7 +676,8 @@
                        'connLogger' => LoggerFactory::getInstance( 
'DBConnection' ),
                        'replLogger' => LoggerFactory::getInstance( 
'DBReplication' ),
                        'errorLogger' => [ MWExceptionHandler::class, 
'logException' ],
-                       'hostname' => wfHostname()
+                       'hostname' => wfHostname(),
+                       'cliMode' => $this->cliMode
                ];
        }
 
diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php 
b/includes/libs/rdbms/loadbalancer/LoadBalancer.php
index 903c160..2cf9733 100644
--- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php
+++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php
@@ -86,6 +86,8 @@
        private $localDomain;
        /** @var string Current server name */
        private $host;
+       /** @var bool */
+       private $cliMode;
 
        /** @var callable Exception logger */
        private $errorLogger;
@@ -173,7 +175,7 @@
                $this->errorLogger = isset( $params['errorLogger'] )
                        ? $params['errorLogger']
                        : function ( Exception $e ) {
-                               trigger_error( E_WARNING, $e->getMessage() );
+                               trigger_error( get_class( $e ) . ': ' . 
$e->getMessage(), E_WARNING );
                        };
 
                foreach ( [ 'replLogger', 'connLogger', 'queryLogger', 
'perfLogger' ] as $key ) {
@@ -183,6 +185,7 @@
                $this->host = isset( $params['hostname'] )
                        ? $params['hostname']
                        : ( gethostname() ?: 'unknown' );
+               $this->cliMode = isset( $params['cliMode'] ) ? 
$params['cliMode'] : PHP_SAPI === 'cli';
        }
 
        /**
@@ -807,6 +810,8 @@
                $server['connLogger'] = $this->connLogger;
                $server['queryLogger'] = $this->queryLogger;
                $server['trxProfiler'] = $this->trxProfiler;
+               $server['cliMode'] = $this->cliMode;
+               $server['errorLogger'] = $this->errorLogger;
 
                // Create a live connection object
                try {

-- 
To view, visit https://gerrit.wikimedia.org/r/310762
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibb4f1c0dafea071a1c34e0cd5b5c15b8b4bb7bc6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to