Make Special:ChangeContentModel field labels consistently use colons
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMysqlBase.php
index 417b464..b1a88ed 100644 (file)
@@ -24,7 +24,7 @@ namespace Wikimedia\Rdbms;
 
 use DateTime;
 use DateTimeZone;
-use Wikimedia;
+use Wikimedia\AtEase\AtEase;
 use InvalidArgumentException;
 use Exception;
 use RuntimeException;
@@ -96,7 +96,7 @@ abstract class DatabaseMysqlBase extends Database {
         *   - sslCiphers : array list of allowable ciphers [default: null]
         * @param array $params
         */
-       function __construct( array $params ) {
+       public function __construct( array $params ) {
                $this->lagDetectionMethod = $params['lagDetectionMethod'] ?? 'Seconds_Behind_Master';
                $this->lagDetectionOptions = $params['lagDetectionOptions'] ?? [];
                $this->useGTIDs = !empty( $params['useGTIDs' ] );
@@ -125,7 +125,7 @@ abstract class DatabaseMysqlBase extends Database {
                $this->close();
 
                if ( $schema !== null ) {
-                       throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
+                       throw $this->newExceptionAfterConnectError( "Got schema '$schema'; not supported." );
                }
 
                $this->server = $server;
@@ -135,23 +135,14 @@ abstract class DatabaseMysqlBase extends Database {
                $this->installErrorHandler();
                try {
                        $this->conn = $this->mysqlConnect( $this->server, $dbName );
-               } catch ( Exception $ex ) {
+               } catch ( Exception $e ) {
                        $this->restoreErrorHandler();
-                       throw $ex;
+                       throw $this->newExceptionAfterConnectError( $e->getMessage() );
                }
                $error = $this->restoreErrorHandler();
 
-               # Always log connection errors
                if ( !$this->conn ) {
-                       $error = $error ?: $this->lastError();
-                       $this->connLogger->error(
-                               "Error connecting to {db_server}: {error}",
-                               $this->getLogContext( [ 'method' => __METHOD__, 'error' => $error ] )
-                       );
-                       $this->connLogger->debug( "DB connection error\n" .
-                               "Server: $server, User: $user, Password: " .
-                               substr( $password, 0, 3 ) . "..., error: " . $error . "\n" );
-                       throw new DBConnectionError( $this, $error );
+                       throw $this->newExceptionAfterConnectError( $error ?: $this->lastError() );
                }
 
                try {
@@ -160,7 +151,6 @@ abstract class DatabaseMysqlBase extends Database {
                                null,
                                $tablePrefix
                        );
-
                        // Abstract over any insane MySQL defaults
                        $set = [ 'group_concat_max_len = 262144' ];
                        // Set SQL mode, default is turning them all off, can be overridden or skipped with null
@@ -185,16 +175,16 @@ abstract class DatabaseMysqlBase extends Database {
                                );
                        }
                } catch ( Exception $e ) {
-                       // Connection was not fully initialized and is not safe for use
-                       $this->conn = false;
+                       throw $this->newExceptionAfterConnectError( $e->getMessage() );
                }
-
-               return true;
        }
 
        protected function doSelectDomain( DatabaseDomain $domain ) {
                if ( $domain->getSchema() !== null ) {
-                       throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
+                       throw new DBExpectedError(
+                               $this,
+                               __CLASS__ . ": domain '{$domain->getId()}' has a schema component"
+                       );
                }
 
                $database = $domain->getDatabase();
@@ -231,7 +221,7 @@ abstract class DatabaseMysqlBase extends Database {
         *
         * @param string $realServer
         * @param string|null $dbName
-        * @return mixed Raw connection
+        * @return mixed|null Driver connection handle
         * @throws DBConnectionError
         */
        abstract protected function mysqlConnect( $realServer, $dbName );
@@ -241,9 +231,9 @@ abstract class DatabaseMysqlBase extends Database {
         * @throws DBUnexpectedError
         */
        public function freeResult( $res ) {
-               Wikimedia\suppressWarnings();
+               AtEase::suppressWarnings();
                $ok = $this->mysqlFreeResult( ResultWrapper::unwrap( $res ) );
-               Wikimedia\restoreWarnings();
+               AtEase::restoreWarnings();
                if ( !$ok ) {
                        throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
                }
@@ -263,9 +253,9 @@ abstract class DatabaseMysqlBase extends Database {
         * @throws DBUnexpectedError
         */
        public function fetchObject( $res ) {
-               Wikimedia\suppressWarnings();
+               AtEase::suppressWarnings();
                $row = $this->mysqlFetchObject( ResultWrapper::unwrap( $res ) );
-               Wikimedia\restoreWarnings();
+               AtEase::restoreWarnings();
 
                $errno = $this->lastErrno();
                // Unfortunately, mysql_fetch_object does not reset the last errno.
@@ -296,9 +286,9 @@ abstract class DatabaseMysqlBase extends Database {
         * @throws DBUnexpectedError
         */
        public function fetchRow( $res ) {
-               Wikimedia\suppressWarnings();
+               AtEase::suppressWarnings();
                $row = $this->mysqlFetchArray( ResultWrapper::unwrap( $res ) );
-               Wikimedia\restoreWarnings();
+               AtEase::restoreWarnings();
 
                $errno = $this->lastErrno();
                // Unfortunately, mysql_fetch_array does not reset the last errno.
@@ -332,9 +322,9 @@ abstract class DatabaseMysqlBase extends Database {
                if ( is_bool( $res ) ) {
                        $n = 0;
                } else {
-                       Wikimedia\suppressWarnings();
+                       AtEase::suppressWarnings();
                        $n = $this->mysqlNumRows( ResultWrapper::unwrap( $res ) );
-                       Wikimedia\restoreWarnings();
+                       AtEase::restoreWarnings();
                }
 
                // Unfortunately, mysql_num_rows does not reset the last errno.
@@ -430,12 +420,12 @@ abstract class DatabaseMysqlBase extends Database {
        public function lastError() {
                if ( $this->conn ) {
                        # Even if it's non-zero, it can still be invalid
-                       Wikimedia\suppressWarnings();
+                       AtEase::suppressWarnings();
                        $error = $this->mysqlError( $this->conn );
                        if ( !$error ) {
                                $error = $this->mysqlError();
                        }
-                       Wikimedia\restoreWarnings();
+                       AtEase::restoreWarnings();
                } else {
                        $error = $this->mysqlError();
                }