Merge "Update weblinks in comments from HTTP to HTTPS"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / Database.php
index 0826355..3d35d76 100644 (file)
@@ -25,6 +25,7 @@
  */
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerInterface;
+use Wikimedia\ScopedCallback;
 
 /**
  * Relational database abstraction object
@@ -78,7 +79,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        /** @var callback Error logging callback */
        protected $errorLogger;
 
-       /** @var resource Database connection */
+       /** @var resource|null Database connection */
        protected $mConn = null;
        /** @var bool */
        protected $mOpened = false;
@@ -382,7 +383,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        }
                        if ( !isset( $p['errorLogger'] ) ) {
                                $p['errorLogger'] = function ( Exception $e ) {
-                                       trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_WARNING );
+                                       trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
                                };
                        }
 
@@ -608,6 +609,11 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return !!( $this->mFlags & $flag );
        }
 
+       /**
+        * @param string $name Class field name
+        * @return mixed
+        * @deprecated Since 1.28
+        */
        public function getProperty( $name ) {
                return $this->$name;
        }
@@ -651,14 +657,22 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                if ( $this->htmlErrors !== false ) {
                        ini_set( 'html_errors', $this->htmlErrors );
                }
+
+               return $this->getLastPHPError();
+       }
+
+       /**
+        * @return string|bool Last PHP error for this DB (typically connection errors)
+        */
+       protected function getLastPHPError() {
                if ( $this->mPHPError ) {
                        $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
                        $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
 
                        return $error;
-               } else {
-                       return false;
                }
+
+               return false;
        }
 
        /**
@@ -773,8 +787,11 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         * @return bool
         */
        protected function isTransactableQuery( $sql ) {
-               $verb = $this->getQueryVerb( $sql );
-               return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
+               return !in_array(
+                       $this->getQueryVerb( $sql ),
+                       [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET', 'CREATE', 'ALTER' ],
+                       true
+               );
        }
 
        /**
@@ -1704,9 +1721,9 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                } elseif ( count( $dbDetails ) == 2 ) {
                        list( $database, $table ) = $dbDetails;
                        # We don't want any prefix added in this case
+                       $prefix = '';
                        # In dbs that support it, $database may actually be the schema
                        # but that doesn't affect any of the functionality here
-                       $prefix = '';
                        $schema = '';
                } else {
                        list( $table ) = $dbDetails;
@@ -1728,29 +1745,35 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                # Quote $table and apply the prefix if not quoted.
                # $tableName might be empty if this is called from Database::replaceVars()
                $tableName = "{$prefix}{$table}";
-               if ( $format == 'quoted'
-                       && !$this->isQuotedIdentifier( $tableName ) && $tableName !== ''
+               if ( $format === 'quoted'
+                       && !$this->isQuotedIdentifier( $tableName )
+                       && $tableName !== ''
                ) {
                        $tableName = $this->addIdentifierQuotes( $tableName );
                }
 
-               # Quote $schema and merge it with the table name if needed
-               if ( strlen( $schema ) ) {
-                       if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
-                               $schema = $this->addIdentifierQuotes( $schema );
-                       }
-                       $tableName = $schema . '.' . $tableName;
-               }
+               # Quote $schema and $database and merge them with the table name if needed
+               $tableName = $this->prependDatabaseOrSchema( $schema, $tableName, $format );
+               $tableName = $this->prependDatabaseOrSchema( $database, $tableName, $format );
+
+               return $tableName;
+       }
 
-               # Quote $database and merge it with the table name if needed
-               if ( $database !== '' ) {
-                       if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
-                               $database = $this->addIdentifierQuotes( $database );
+       /**
+        * @param string|null $namespace Database or schema
+        * @param string $relation Name of table, view, sequence, etc...
+        * @param string $format One of (raw, quoted)
+        * @return string Relation name with quoted and merged $namespace as needed
+        */
+       private function prependDatabaseOrSchema( $namespace, $relation, $format ) {
+               if ( strlen( $namespace ) ) {
+                       if ( $format === 'quoted' && !$this->isQuotedIdentifier( $namespace ) ) {
+                               $namespace = $this->addIdentifierQuotes( $namespace );
                        }
-                       $tableName = $database . '.' . $tableName;
+                       $relation = $namespace . '.' . $relation;
                }
 
-               return $tableName;
+               return $relation;
        }
 
        public function tableNames() {
@@ -2828,7 +2851,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
                        throw new DBUnexpectedError(
                                $this,
-                               "$fname: Cannot COMMIT to clear snapshot because writes are pending ($fnames)."
+                               "$fname: Cannot flush snapshot because writes are pending ($fnames)."
                        );
                }
 
@@ -3249,7 +3272,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
                        throw new DBUnexpectedError(
                                $this,
-                               "$fname: Cannot COMMIT to clear snapshot because writes are pending ($fnames)."
+                               "$fname: Cannot flush pre-lock snapshot because writes are pending ($fnames)."
                        );
                }
 
@@ -3368,6 +3391,28 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return true;
        }
 
+       /**
+        * Get the underlying binding handle, mConn
+        *
+        * Makes sure that mConn is set (disconnects and ping() failure can unset it).
+        * This catches broken callers than catch and ignore disconnection exceptions.
+        * Unlike checking isOpen(), this is safe to call inside of open().
+        *
+        * @return resource|object
+        * @throws DBUnexpectedError
+        * @since 1.26
+        */
+       protected function getBindingHandle() {
+               if ( !$this->mConn ) {
+                       throw new DBUnexpectedError(
+                               $this,
+                               'DB connection was already closed or the connection dropped.'
+                       );
+               }
+
+               return $this->mConn;
+       }
+
        /**
         * @since 1.19
         * @return string
@@ -3422,8 +3467,11 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                }
 
                if ( $this->mConn ) {
-                       // Avoid connection leaks for sanity
+                       // Avoid connection leaks for sanity. Normally, resources close at script completion.
+                       // The connection might already be closed in zend/hhvm by now, so suppress warnings.
+                       \MediaWiki\suppressWarnings();
                        $this->closeConnection();
+                       \MediaWiki\restoreWarnings();
                        $this->mConn = false;
                        $this->mOpened = false;
                }