(Bug 42461) Remove call to "new Database()"
authorPlatonides <platonides@gmail.com>
Sat, 26 Jan 2013 15:38:29 +0000 (16:38 +0100)
committerTim Starling <tstarling@wikimedia.org>
Mon, 18 Mar 2013 02:49:44 +0000 (13:49 +1100)
 * Refactored LoadBalancer::reportConnectionError()
 * Removed PHP4-style reference from DatabaseError constructors
 * Allowed to not pass a database to the DBError family of exceptions.

Change-Id: If9570b45ea7285de2b8b2391e704bc01f76be48a

includes/db/DatabaseError.php
includes/db/LoadBalancer.php

index 331b8ae..628a2af 100644 (file)
@@ -37,7 +37,7 @@ class DBError extends MWException {
         * @param $db DatabaseBase object which threw the error
         * @param string $error A simple error message to be used for debugging
         */
-       function __construct( DatabaseBase &$db, $error ) {
+       function __construct( DatabaseBase $db = null, $error ) {
                $this->db = $db;
                parent::__construct( $error );
        }
@@ -91,7 +91,7 @@ class DBError extends MWException {
 class DBConnectionError extends DBError {
        public $error;
 
-       function __construct( DatabaseBase &$db, $error = 'unknown error' ) {
+       function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
                $msg = 'DB connection error';
 
                if ( trim( $error ) != '' ) {
@@ -158,7 +158,7 @@ class DBConnectionError extends DBError {
                # No database access
                MessageCache::singleton()->disable();
 
-               if ( trim( $this->error ) == '' ) {
+               if ( trim( $this->error ) == '' && $this->db ) {
                        $this->error = $this->db->getProperty( 'mServer' );
                }
 
@@ -288,7 +288,7 @@ class DBQueryError extends DBError {
         * @param $sql string
         * @param $fname string
         */
-       function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
+       function __construct( DatabaseBase $db, $error, $errno, $sql, $fname ) {
                $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading?  See: https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
                        "Query: $sql\n" .
                        "Function: $fname\n" .
index aeebb13..1e85927 100644 (file)
@@ -481,7 +481,7 @@ class LoadBalancer {
                        if ( $i === false ) {
                                $this->mLastError = 'No working slave server: ' . $this->mLastError;
                                wfProfileOut( __METHOD__ );
-                               return $this->reportConnectionError( $this->mErrorConnection );
+                               return $this->reportConnectionError();
                        }
                }
 
@@ -489,7 +489,7 @@ class LoadBalancer {
                $conn = $this->openConnection( $i, $wiki );
                if ( !$conn ) {
                        wfProfileOut( __METHOD__ );
-                       return $this->reportConnectionError( $this->mErrorConnection );
+                       return $this->reportConnectionError();
                }
 
                wfProfileOut( __METHOD__ );
@@ -708,17 +708,18 @@ class LoadBalancer {
        }
 
        /**
-        * @param $conn
-        * @return bool
         * @throws DBConnectionError
+        * @return bool
         */
-       function reportConnectionError( &$conn ) {
+       private function reportConnectionError() {
+               $conn = $this->mErrorConnection; // The connection which caused the error
+
                if ( !is_object( $conn ) ) {
                        // No last connection, probably due to all servers being too busy
                        wfLogDBError( "LB failure with no last connection. Connection error: {$this->mLastError}\n" );
-                       $conn = new Database;
+
                        // If all servers were busy, mLastError will contain something sensible
-                       throw new DBConnectionError( $conn, $this->mLastError );
+                       throw new DBConnectionError( null, $this->mLastError );
                } else {
                        $server = $conn->getProperty( 'mServer' );
                        wfLogDBError( "Connection error: {$this->mLastError} ({$server})\n" );