wp/wp-includes/wp-db.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
--- a/wp/wp-includes/wp-db.php	Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-includes/wp-db.php	Mon Oct 14 18:28:13 2019 +0200
@@ -18,6 +18,7 @@
  * @since 0.71
  */
 define( 'OBJECT', 'OBJECT' );
+// phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ConstantNotUpperCase
 define( 'object', 'OBJECT' ); // Back compat.
 
 /**
@@ -174,10 +175,24 @@
 	protected $col_info;
 
 	/**
-	 * Saved queries that were executed
+	 * Log of queries that were executed, for debugging purposes.
 	 *
 	 * @since 1.5.0
-	 * @var array
+	 * @since 2.5.0 The third element in each query log was added to record the calling functions.
+	 * @since 5.1.0 The fourth element in each query log was added to record the start time.
+	 *
+	 * @var array[] {
+	 *     Array of queries that were executed.
+	 *
+	 *     @type array ...$0 {
+	 *         Data for each query.
+	 *
+	 *         @type string $0 The query's SQL.
+	 *         @type float  $1 Total time spent on the query, in seconds.
+	 *         @type string $2 Comma separated list of the calling functions.
+	 *         @type float  $3 Unix timestamp of the time at the start of the query.
+	 *     }
+	 * }
 	 */
 	var $queries;
 
@@ -208,7 +223,7 @@
 	 * @since 3.0.0
 	 * @var string
 	 */
-	 public $base_prefix;
+	public $base_prefix;
 
 	/**
 	 * Whether the database queries are ready to start executing.
@@ -241,8 +256,18 @@
 	 * @see wpdb::tables()
 	 * @var array
 	 */
-	var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta',
-		'terms', 'term_taxonomy', 'term_relationships', 'termmeta', 'commentmeta' );
+	var $tables = array(
+		'posts',
+		'comments',
+		'links',
+		'options',
+		'postmeta',
+		'terms',
+		'term_taxonomy',
+		'term_relationships',
+		'termmeta',
+		'commentmeta',
+	);
 
 	/**
 	 * List of deprecated WordPress tables
@@ -271,8 +296,16 @@
 	 * @see wpdb::tables()
 	 * @var array
 	 */
-	var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta',
-		'sitecategories', 'registration_log', 'blog_versions' );
+	var $ms_global_tables = array(
+		'blogs',
+		'blogmeta',
+		'signups',
+		'site',
+		'sitemeta',
+		'sitecategories',
+		'registration_log',
+		'blog_versions',
+	);
 
 	/**
 	 * WordPress Comments table
@@ -383,6 +416,14 @@
 	public $blogs;
 
 	/**
+	 * Multisite Blog Metadata table
+	 *
+	 * @since 5.1.0
+	 * @var string
+	 */
+	public $blogmeta;
+
+	/**
 	 * Multisite Blog Versions table
 	 *
 	 * @since 3.0.0
@@ -528,8 +569,13 @@
 	 * @since 3.9.0
 	 * @var array
 	 */
-	protected $incompatible_modes = array( 'NO_ZERO_DATE', 'ONLY_FULL_GROUP_BY',
-		'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
+	protected $incompatible_modes = array(
+		'NO_ZERO_DATE',
+		'ONLY_FULL_GROUP_BY',
+		'STRICT_TRANS_TABLES',
+		'STRICT_ALL_TABLES',
+		'TRADITIONAL',
+	);
 
 	/**
 	 * Whether to use mysqli over mysql.
@@ -567,8 +613,9 @@
 	public function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
 		register_shutdown_function( array( $this, '__destruct' ) );
 
-		if ( WP_DEBUG && WP_DEBUG_DISPLAY )
+		if ( WP_DEBUG && WP_DEBUG_DISPLAY ) {
 			$this->show_errors();
+		}
 
 		// Use ext/mysqli if it exists unless WP_USE_EXT_MYSQL is defined as true
 		if ( function_exists( 'mysqli_connect' ) ) {
@@ -579,10 +626,10 @@
 			}
 		}
 
-		$this->dbuser = $dbuser;
+		$this->dbuser     = $dbuser;
 		$this->dbpassword = $dbpassword;
-		$this->dbname = $dbname;
-		$this->dbhost = $dbhost;
+		$this->dbname     = $dbname;
+		$this->dbhost     = $dbhost;
 
 		// wp-config.php creation will manually connect when ready.
 		if ( defined( 'WP_SETUP_CONFIG' ) ) {
@@ -612,8 +659,9 @@
 	 * @return mixed The private member
 	 */
 	public function __get( $name ) {
-		if ( 'col_info' === $name )
+		if ( 'col_info' === $name ) {
 			$this->load_col_info();
+		}
 
 		return $this->$name;
 	}
@@ -632,7 +680,7 @@
 			'table_charset',
 			'check_current_query',
 		);
-		if (  in_array( $name, $protected_members, true ) ) {
+		if ( in_array( $name, $protected_members, true ) ) {
 			return;
 		}
 		$this->$name = $value;
@@ -671,7 +719,7 @@
 		$charset = '';
 		$collate = '';
 
-		if ( function_exists('is_multisite') && is_multisite() ) {
+		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
 			$charset = 'utf8';
 			if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) {
 				$collate = DB_COLLATE;
@@ -744,10 +792,12 @@
 	 * @param string   $collate Optional. The collation. Default null.
 	 */
 	public function set_charset( $dbh, $charset = null, $collate = null ) {
-		if ( ! isset( $charset ) )
+		if ( ! isset( $charset ) ) {
 			$charset = $this->charset;
-		if ( ! isset( $collate ) )
+		}
+		if ( ! isset( $collate ) ) {
 			$collate = $this->collate;
+		}
 		if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) {
 			$set_charset_succeeded = true;
 
@@ -758,8 +808,9 @@
 
 				if ( $set_charset_succeeded ) {
 					$query = $this->prepare( 'SET NAMES %s', $charset );
-					if ( ! empty( $collate ) )
+					if ( ! empty( $collate ) ) {
 						$query .= $this->prepare( ' COLLATE %s', $collate );
+					}
 					mysqli_query( $dbh, $query );
 				}
 			} else {
@@ -768,8 +819,9 @@
 				}
 				if ( $set_charset_succeeded ) {
 					$query = $this->prepare( 'SET NAMES %s', $charset );
-					if ( ! empty( $collate ) )
+					if ( ! empty( $collate ) ) {
 						$query .= $this->prepare( ' COLLATE %s', $collate );
+					}
 					mysql_query( $query, $dbh );
 				}
 			}
@@ -852,30 +904,36 @@
 	 */
 	public function set_prefix( $prefix, $set_table_names = true ) {
 
-		if ( preg_match( '|[^a-z0-9_]|i', $prefix ) )
-			return new WP_Error('invalid_db_prefix', 'Invalid database prefix' );
+		if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) {
+			return new WP_Error( 'invalid_db_prefix', 'Invalid database prefix' );
+		}
 
 		$old_prefix = is_multisite() ? '' : $prefix;
 
-		if ( isset( $this->base_prefix ) )
+		if ( isset( $this->base_prefix ) ) {
 			$old_prefix = $this->base_prefix;
+		}
 
 		$this->base_prefix = $prefix;
 
 		if ( $set_table_names ) {
-			foreach ( $this->tables( 'global' ) as $table => $prefixed_table )
+			foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) {
 				$this->$table = $prefixed_table;
-
-			if ( is_multisite() && empty( $this->blogid ) )
+			}
+
+			if ( is_multisite() && empty( $this->blogid ) ) {
 				return $old_prefix;
+			}
 
 			$this->prefix = $this->get_blog_prefix();
 
-			foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+			foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) {
 				$this->$table = $prefixed_table;
-
-			foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
+			}
+
+			foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) {
 				$this->$table = $prefixed_table;
+			}
 		}
 		return $old_prefix;
 	}
@@ -899,11 +957,13 @@
 
 		$this->prefix = $this->get_blog_prefix();
 
-		foreach ( $this->tables( 'blog' ) as $table => $prefixed_table )
+		foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) {
 			$this->$table = $prefixed_table;
-
-		foreach ( $this->tables( 'old' ) as $table => $prefixed_table )
+		}
+
+		foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) {
 			$this->$table = $prefixed_table;
+		}
 
 		return $old_blog_id;
 	}
@@ -917,13 +977,15 @@
 	 */
 	public function get_blog_prefix( $blog_id = null ) {
 		if ( is_multisite() ) {
-			if ( null === $blog_id )
+			if ( null === $blog_id ) {
 				$blog_id = $this->blogid;
+			}
 			$blog_id = (int) $blog_id;
-			if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) )
+			if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) {
 				return $this->base_prefix;
-			else
+			} else {
 				return $this->base_prefix . $blog_id . '_';
+			}
 		} else {
 			return $this->base_prefix;
 		}
@@ -958,48 +1020,54 @@
 	 */
 	public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
 		switch ( $scope ) {
-			case 'all' :
+			case 'all':
 				$tables = array_merge( $this->global_tables, $this->tables );
-				if ( is_multisite() )
+				if ( is_multisite() ) {
 					$tables = array_merge( $tables, $this->ms_global_tables );
+				}
 				break;
-			case 'blog' :
+			case 'blog':
 				$tables = $this->tables;
 				break;
-			case 'global' :
+			case 'global':
 				$tables = $this->global_tables;
-				if ( is_multisite() )
+				if ( is_multisite() ) {
 					$tables = array_merge( $tables, $this->ms_global_tables );
+				}
 				break;
-			case 'ms_global' :
+			case 'ms_global':
 				$tables = $this->ms_global_tables;
 				break;
-			case 'old' :
+			case 'old':
 				$tables = $this->old_tables;
 				break;
-			default :
+			default:
 				return array();
 		}
 
 		if ( $prefix ) {
-			if ( ! $blog_id )
+			if ( ! $blog_id ) {
 				$blog_id = $this->blogid;
-			$blog_prefix = $this->get_blog_prefix( $blog_id );
-			$base_prefix = $this->base_prefix;
+			}
+			$blog_prefix   = $this->get_blog_prefix( $blog_id );
+			$base_prefix   = $this->base_prefix;
 			$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
 			foreach ( $tables as $k => $table ) {
-				if ( in_array( $table, $global_tables ) )
+				if ( in_array( $table, $global_tables ) ) {
 					$tables[ $table ] = $base_prefix . $table;
-				else
+				} else {
 					$tables[ $table ] = $blog_prefix . $table;
+				}
 				unset( $tables[ $k ] );
 			}
 
-			if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
+			if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) {
 				$tables['users'] = CUSTOM_USER_TABLE;
-
-			if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
+			}
+
+			if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) {
 				$tables['usermeta'] = CUSTOM_USER_META_TABLE;
+			}
 		}
 
 		return $tables;
@@ -1017,8 +1085,9 @@
 	 * @param resource|null $dbh Optional link identifier.
 	 */
 	public function select( $db, $dbh = null ) {
-		if ( is_null($dbh) )
+		if ( is_null( $dbh ) ) {
 			$dbh = $this->dbh;
+		}
 
 		if ( $this->use_mysqli ) {
 			$success = mysqli_select_db( $dbh, $db );
@@ -1044,7 +1113,7 @@
 				$message .= '<li>' . sprintf(
 					/* translators: 1: database user, 2: database name */
 					__( 'Does the user %1$s have permission to use the %2$s database?' ),
-					'<code>' . htmlspecialchars( $this->dbuser, ENT_QUOTES )  . '</code>',
+					'<code>' . htmlspecialchars( $this->dbuser, ENT_QUOTES ) . '</code>',
 					'<code>' . htmlspecialchars( $db, ENT_QUOTES ) . '</code>'
 				) . "</li>\n";
 
@@ -1052,14 +1121,14 @@
 					/* translators: %s: database name */
 					__( 'On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?' ),
 					htmlspecialchars( $db, ENT_QUOTES )
-				). "</li>\n";
+				) . "</li>\n";
 
 				$message .= "</ul>\n";
 
 				$message .= '<p>' . sprintf(
 					/* translators: %s: support forums URL */
 					__( 'If you don&#8217;t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="%s">WordPress Support Forums</a>.' ),
-					__( 'https://wordpress.org/support/' )
+					__( 'https://wordpress.org/support/forums/' )
 				) . "</p>\n";
 
 				$this->bail( $message, 'db_select_fail' );
@@ -1081,8 +1150,9 @@
 	 * @return string
 	 */
 	function _weak_escape( $string ) {
-		if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) )
+		if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) {
 			_deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' );
+		}
 		return addslashes( $string );
 	}
 
@@ -1130,9 +1200,9 @@
 		if ( is_array( $data ) ) {
 			foreach ( $data as $k => $v ) {
 				if ( is_array( $v ) ) {
-					$data[$k] = $this->_escape( $v );
+					$data[ $k ] = $this->_escape( $v );
 				} else {
-					$data[$k] = $this->_real_escape( $v );
+					$data[ $k ] = $this->_real_escape( $v );
 				}
 			}
 		} else {
@@ -1156,14 +1226,16 @@
 	 * @return mixed
 	 */
 	public function escape( $data ) {
-		if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) )
+		if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) {
 			_deprecated_function( __METHOD__, '3.6.0', 'wpdb::prepare() or esc_sql()' );
+		}
 		if ( is_array( $data ) ) {
 			foreach ( $data as $k => $v ) {
-				if ( is_array( $v ) )
-					$data[$k] = $this->escape( $v, 'recursive' );
-				else
-					$data[$k] = $this->_weak_escape( $v, 'internal' );
+				if ( is_array( $v ) ) {
+					$data[ $k ] = $this->escape( $v, 'recursive' );
+				} else {
+					$data[ $k ] = $this->_weak_escape( $v, 'internal' );
+				}
 			}
 		} else {
 			$data = $this->_weak_escape( $data, 'internal' );
@@ -1182,8 +1254,9 @@
 	 * @param string $string to escape
 	 */
 	public function escape_by_ref( &$string ) {
-		if ( ! is_float( $string ) )
+		if ( ! is_float( $string ) ) {
 			$string = $this->_real_escape( $string );
+		}
 	}
 
 	/**
@@ -1201,7 +1274,7 @@
 	 *
 	 * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example,
 	 * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these
-	 * cannot be inserted directly in the query string. Also see {@see esc_like()}.
+	 * cannot be inserted directly in the query string. Also see wpdb::esc_like().
 	 *
 	 * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination
 	 * of the two is not supported.
@@ -1237,7 +1310,7 @@
 		$passed_as_array = false;
 		if ( is_array( $args[0] ) && count( $args ) == 1 ) {
 			$passed_as_array = true;
-			$args = $args[0];
+			$args            = $args[0];
 		}
 
 		foreach ( $args as $arg ) {
@@ -1262,14 +1335,14 @@
 		 * If a %s placeholder already has quotes around it, removing the existing quotes and re-inserting them
 		 * ensures the quotes are consistent.
 		 *
-		 * For backwards compatibility, this is only applied to %s, and not to placeholders like %1$s, which are frequently
+		 * For backward compatibility, this is only applied to %s, and not to placeholders like %1$s, which are frequently
 		 * used in the middle of longer strings, or as table name placeholders.
 		 */
 		$query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes.
 		$query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes.
 		$query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s.
 
-		$query = preg_replace( "/(?<!%)(%($allowed_format)?f)/" , '%\\2F', $query ); // Force floats to be locale unaware.
+		$query = preg_replace( "/(?<!%)(%($allowed_format)?f)/", '%\\2F', $query ); // Force floats to be locale unaware.
 
 		$query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents.
 
@@ -1289,11 +1362,14 @@
 				 * or we were expecting multiple arguments in an array, throw a warning.
 				 */
 				wp_load_translations_early();
-				_doing_it_wrong( 'wpdb::prepare',
+				_doing_it_wrong(
+					'wpdb::prepare',
 					/* translators: 1: number of placeholders, 2: number of arguments passed */
-					sprintf( __( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
+					sprintf(
+						__( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ),
 						$placeholders,
-						count( $args ) ),
+						count( $args )
+					),
 					'4.8.3'
 				);
 			}
@@ -1344,17 +1420,21 @@
 	public function print_error( $str = '' ) {
 		global $EZSQL_ERROR;
 
-		if ( !$str ) {
+		if ( ! $str ) {
 			if ( $this->use_mysqli ) {
 				$str = mysqli_error( $this->dbh );
 			} else {
 				$str = mysql_error( $this->dbh );
 			}
 		}
-		$EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str );
-
-		if ( $this->suppress_errors )
+		$EZSQL_ERROR[] = array(
+			'query'     => $this->last_query,
+			'error_str' => $str,
+		);
+
+		if ( $this->suppress_errors ) {
 			return false;
+		}
 
 		wp_load_translations_early();
 
@@ -1369,8 +1449,9 @@
 		error_log( $error_str );
 
 		// Are we showing errors?
-		if ( ! $this->show_errors )
+		if ( ! $this->show_errors ) {
 			return false;
+		}
 
 		// If there is an error then take note of it
 		if ( is_multisite() ) {
@@ -1415,7 +1496,7 @@
 	 * @return bool Old value for showing errors.
 	 */
 	public function show_errors( $show = true ) {
-		$errors = $this->show_errors;
+		$errors            = $this->show_errors;
 		$this->show_errors = $show;
 		return $errors;
 	}
@@ -1431,7 +1512,7 @@
 	 * @return bool Whether showing of errors was active
 	 */
 	public function hide_errors() {
-		$show = $this->show_errors;
+		$show              = $this->show_errors;
 		$this->show_errors = false;
 		return $show;
 	}
@@ -1448,7 +1529,7 @@
 	 * @return bool Old value
 	 */
 	public function suppress_errors( $suppress = true ) {
-		$errors = $this->suppress_errors;
+		$errors                = $this->suppress_errors;
 		$this->suppress_errors = (bool) $suppress;
 		return $errors;
 	}
@@ -1459,18 +1540,18 @@
 	 * @since 0.71
 	 */
 	public function flush() {
-		$this->last_result = array();
-		$this->col_info    = null;
-		$this->last_query  = null;
+		$this->last_result   = array();
+		$this->col_info      = null;
+		$this->last_query    = null;
 		$this->rows_affected = $this->num_rows = 0;
-		$this->last_error  = '';
+		$this->last_error    = '';
 
 		if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
 			mysqli_free_result( $this->result );
 			$this->result = null;
 
 			// Sanity check before using the handle
-			if ( empty( $this->dbh ) || !( $this->dbh instanceof mysqli ) ) {
+			if ( empty( $this->dbh ) || ! ( $this->dbh instanceof mysqli ) ) {
 				return;
 			}
 
@@ -1502,7 +1583,7 @@
 		 * Deprecated in 3.9+ when using MySQLi. No equivalent
 		 * $new_link parameter exists for mysqli_* functions.
 		 */
-		$new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
+		$new_link     = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true;
 		$client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0;
 
 		if ( $this->use_mysqli ) {
@@ -1538,10 +1619,10 @@
 
 				/*
 				 * It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if:
-		 		 *  - We haven't previously connected, and
-		 		 *  - WP_USE_EXT_MYSQL isn't set to false, and
-		 		 *  - ext/mysql is loaded.
-		 		 */
+				  *  - We haven't previously connected, and
+				  *  - WP_USE_EXT_MYSQL isn't set to false, and
+				  *  - ext/mysql is loaded.
+				  */
 				$attempt_fallback = true;
 
 				if ( $this->has_connected ) {
@@ -1577,7 +1658,7 @@
 			$message = '<h1>' . __( 'Error establishing a database connection' ) . "</h1>\n";
 
 			$message .= '<p>' . sprintf(
-				/* translators: 1: wp-config.php. 2: database host */
+				/* translators: 1: wp-config.php, 2: database host */
 				__( 'This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.' ),
 				'<code>wp-config.php</code>',
 				'<code>' . htmlspecialchars( $this->dbhost, ENT_QUOTES ) . '</code>'
@@ -1592,7 +1673,7 @@
 			$message .= '<p>' . sprintf(
 				/* translators: %s: support forums URL */
 				__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
-				__( 'https://wordpress.org/support/' )
+				__( 'https://wordpress.org/support/forums/' )
 			) . "</p>\n";
 
 			$this->bail( $message, 'db_connect_fail' );
@@ -1640,7 +1721,7 @@
 		$socket_pos = strpos( $host, ':/' );
 		if ( $socket_pos !== false ) {
 			$socket = substr( $host, $socket_pos + 1 );
-			$host = substr( $host, 0, $socket_pos );
+			$host   = substr( $host, 0, $socket_pos );
 		}
 
 		// We need to check for an IPv6 address first.
@@ -1654,7 +1735,7 @@
 		}
 
 		$matches = array();
-		$result = preg_match( $pattern, $host, $matches );
+		$result  = preg_match( $pattern, $host, $matches );
 
 		if ( 1 !== $result ) {
 			// Couldn't parse the address, bail.
@@ -1750,7 +1831,7 @@
 		$message .= '<p>' . sprintf(
 			/* translators: %s: support forums URL */
 			__( 'If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.' ),
-			__( 'https://wordpress.org/support/' )
+			__( 'https://wordpress.org/support/forums/' )
 		) . "</p>\n";
 
 		// We weren't able to reconnect, so we better bail.
@@ -1768,7 +1849,8 @@
 	 * @since 0.71
 	 *
 	 * @param string $query Database query
-	 * @return int|false Number of rows affected/selected or false on error
+	 * @return int|bool Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows
+	 *                  affected/selected for all other queries. Boolean false on error.
 	 */
 	public function query( $query ) {
 		if ( ! $this->ready ) {
@@ -1858,8 +1940,9 @@
 
 		if ( $this->last_error ) {
 			// Clear insert_id on a subsequent failed insert.
-			if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) )
+			if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) {
 				$this->insert_id = 0;
+			}
 
 			$this->print_error();
 			return false;
@@ -1887,12 +1970,12 @@
 			$num_rows = 0;
 			if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
 				while ( $row = mysqli_fetch_object( $this->result ) ) {
-					$this->last_result[$num_rows] = $row;
+					$this->last_result[ $num_rows ] = $row;
 					$num_rows++;
 				}
 			} elseif ( is_resource( $this->result ) ) {
 				while ( $row = mysql_fetch_object( $this->result ) ) {
-					$this->last_result[$num_rows] = $row;
+					$this->last_result[ $num_rows ] = $row;
 					$num_rows++;
 				}
 			}
@@ -1928,7 +2011,12 @@
 		$this->num_queries++;
 
 		if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
-			$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
+			$this->queries[] = array(
+				$query,
+				$this->timer_stop(),
+				$this->get_caller(),
+				$this->time_start,
+			);
 		}
 	}
 
@@ -1953,7 +2041,7 @@
 
 		/*
 		 * Add the filter to remove the placeholder escaper. Uses priority 0, so that anything
-		 * else attached to this filter will recieve the query with the placeholder string removed.
+		 * else attached to this filter will receive the query with the placeholder string removed.
 		 */
 		if ( ! has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) {
 			add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 );
@@ -2154,10 +2242,10 @@
 			}
 
 			$conditions[] = "`$field` = " . $value['format'];
-			$values[] = $value['value'];
+			$values[]     = $value['value'];
 		}
 
-		$fields = implode( ', ', $fields );
+		$fields     = implode( ', ', $fields );
 		$conditions = implode( ' AND ', $conditions );
 
 		$sql = "UPDATE `$table` SET $fields WHERE $conditions";
@@ -2206,7 +2294,7 @@
 			}
 
 			$conditions[] = "`$field` = " . $value['format'];
-			$values[] = $value['value'];
+			$values[]     = $value['value'];
 		}
 
 		$conditions = implode( ' AND ', $conditions );
@@ -2381,12 +2469,12 @@
 		}
 
 		// Extract var out of cached results based x,y vals
-		if ( !empty( $this->last_result[$y] ) ) {
-			$values = array_values( get_object_vars( $this->last_result[$y] ) );
+		if ( ! empty( $this->last_result[ $y ] ) ) {
+			$values = array_values( get_object_vars( $this->last_result[ $y ] ) );
 		}
 
 		// If there is a value return it else return null
-		return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null;
+		return ( isset( $values[ $x ] ) && $values[ $x ] !== '' ) ? $values[ $x ] : null;
 	}
 
 	/**
@@ -2415,20 +2503,21 @@
 			return null;
 		}
 
-		if ( !isset( $this->last_result[$y] ) )
+		if ( ! isset( $this->last_result[ $y ] ) ) {
 			return null;
+		}
 
 		if ( $output == OBJECT ) {
-			return $this->last_result[$y] ? $this->last_result[$y] : null;
+			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
 		} elseif ( $output == ARRAY_A ) {
-			return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
+			return $this->last_result[ $y ] ? get_object_vars( $this->last_result[ $y ] ) : null;
 		} elseif ( $output == ARRAY_N ) {
-			return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
+			return $this->last_result[ $y ] ? array_values( get_object_vars( $this->last_result[ $y ] ) ) : null;
 		} elseif ( strtoupper( $output ) === OBJECT ) {
 			// Back compat for OBJECT being previously case insensitive.
-			return $this->last_result[$y] ? $this->last_result[$y] : null;
+			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
 		} else {
-			$this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" );
+			$this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' );
 		}
 	}
 
@@ -2445,7 +2534,7 @@
 	 * @param int         $x     Optional. Column to return. Indexed from 0.
 	 * @return array Database query result. Array indexed from 0 by SQL result row number.
 	 */
-	public function get_col( $query = null , $x = 0 ) {
+	public function get_col( $query = null, $x = 0 ) {
 		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
 			$this->check_current_query = false;
 		}
@@ -2456,8 +2545,10 @@
 
 		$new_array = array();
 		// Extract the column values
-		for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
-			$new_array[$i] = $this->get_var( null, $x, $i );
+		if ( $this->last_result ) {
+			for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
+				$new_array[ $i ] = $this->get_var( null, $x, $i );
+			}
 		}
 		return $new_array;
 	}
@@ -2497,11 +2588,14 @@
 		} elseif ( $output == OBJECT_K ) {
 			// Return an array of row objects with keys from column 1
 			// (Duplicates are discarded)
-			foreach ( $this->last_result as $row ) {
-				$var_by_ref = get_object_vars( $row );
-				$key = array_shift( $var_by_ref );
-				if ( ! isset( $new_array[ $key ] ) )
-					$new_array[ $key ] = $row;
+			if ( $this->last_result ) {
+				foreach ( $this->last_result as $row ) {
+					$var_by_ref = get_object_vars( $row );
+					$key        = array_shift( $var_by_ref );
+					if ( ! isset( $new_array[ $key ] ) ) {
+						$new_array[ $key ] = $row;
+					}
+				}
 			}
 			return $new_array;
 		} elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
@@ -2559,8 +2653,8 @@
 		$charsets = $columns = array();
 
 		$table_parts = explode( '.', $table );
-		$table = '`' . implode( '`.`', $table_parts ) . '`';
-		$results = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
+		$table       = '`' . implode( '`.`', $table_parts ) . '`';
+		$results     = $this->get_results( "SHOW FULL COLUMNS FROM $table" );
 		if ( ! $results ) {
 			return new WP_Error( 'wpdb_get_table_charset_failure' );
 		}
@@ -2636,7 +2730,7 @@
 	 *                               character set. WP_Error object if there was an error.
 	 */
 	public function get_col_charset( $table, $column ) {
-		$tablekey = strtolower( $table );
+		$tablekey  = strtolower( $table );
 		$columnkey = strtolower( $column );
 
 		/**
@@ -2701,7 +2795,7 @@
 	 *                              WP_Error object if there was an error.
 	 */
 	public function get_col_length( $table, $column ) {
-		$tablekey = strtolower( $table );
+		$tablekey  = strtolower( $table );
 		$columnkey = strtolower( $column );
 
 		// Skip this entirely if this isn't a MySQL database.
@@ -2730,7 +2824,7 @@
 			$length = false;
 		}
 
-		switch( $type ) {
+		switch ( $type ) {
 			case 'char':
 			case 'varchar':
 				return array(
@@ -2831,7 +2925,7 @@
 		}
 
 		$this->checking_collation = true;
-		$collation = $this->get_table_charset( $table );
+		$collation                = $this->get_table_charset( $table );
 		$this->checking_collation = false;
 
 		// Tables with no collation, or latin1 only, don't need extra checking.
@@ -2878,7 +2972,7 @@
 			$charset = $value['charset'];
 
 			if ( is_array( $value['length'] ) ) {
-				$length = $value['length']['length'];
+				$length                  = $value['length']['length'];
 				$truncate_by_byte_length = 'byte' === $value['length']['type'];
 			} else {
 				$length = false;
@@ -2907,7 +3001,7 @@
 				( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) )
 			) {
 				$truncate_by_byte_length = true;
-				$needs_validation = false;
+				$needs_validation        = false;
 			}
 
 			if ( $truncate_by_byte_length ) {
@@ -2941,13 +3035,12 @@
 					';
 				}
 
-				$regex .= '){1,40}                          # ...one or more times
+				$regex         .= '){1,40}                          # ...one or more times
 					)
 					| .                                  # anything else
 					/x';
 				$value['value'] = preg_replace( $regex, '$1', $value['value'] );
 
-
 				if ( false !== $length && mb_strlen( $value['value'], 'UTF-8' ) > $length ) {
 					$value['value'] = mb_substr( $value['value'], 0, $length, 'UTF-8' );
 				}
@@ -2982,9 +3075,9 @@
 					}
 
 					if ( is_array( $value['length'] ) ) {
-						$length = sprintf( '%.0f', $value['length']['length'] );
+						$length          = sprintf( '%.0f', $value['length']['length'] );
 						$queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING $connection_charset )", $value['value'] );
-					} else if ( 'binary' !== $charset ) {
+					} elseif ( 'binary' !== $charset ) {
 						// If we don't have a length, there's no need to convert binary - it will always return the same result.
 						$queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING $connection_charset )", $value['value'] );
 					}
@@ -3003,14 +3096,14 @@
 			}
 
 			$this->check_current_query = false;
-			$row = $this->get_row( "SELECT " . implode( ', ', $sql ), ARRAY_A );
+			$row                       = $this->get_row( 'SELECT ' . implode( ', ', $sql ), ARRAY_A );
 			if ( ! $row ) {
 				return new WP_Error( 'wpdb_strip_invalid_text_failure' );
 			}
 
 			foreach ( array_keys( $data ) as $column ) {
-				if ( isset( $row["x_$column"] ) ) {
-					$data[ $column ]['value'] = $row["x_$column"];
+				if ( isset( $row[ "x_$column" ] ) ) {
+					$data[ $column ]['value'] = $row[ "x_$column" ];
 				}
 			}
 		}
@@ -3092,7 +3185,7 @@
 				'value'   => $value,
 				'charset' => $charset,
 				'length'  => $this->get_col_length( $table, $column ),
-			)
+			),
 		);
 
 		$data = $this->strip_invalid_text( $data );
@@ -3122,13 +3215,17 @@
 		$query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', $query );
 
 		// Quickly match most common queries.
-		if ( preg_match( '/^\s*(?:'
+		if ( preg_match(
+			'/^\s*(?:'
 				. 'SELECT.*?\s+FROM'
 				. '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
 				. '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
 				. '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
 				. '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:.+?FROM)?'
-				. ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is', $query, $maybe ) ) {
+			. ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is',
+			$query,
+			$maybe
+		) ) {
 			return str_replace( '`', '', $maybe[1] );
 		}
 
@@ -3147,7 +3244,8 @@
 		}
 
 		// Big pattern for the rest of the table-related queries.
-		if ( preg_match( '/^\s*(?:'
+		if ( preg_match(
+			'/^\s*(?:'
 				. '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
 				. '|DESCRIBE|DESC|EXPLAIN|HANDLER'
 				. '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
@@ -3161,7 +3259,10 @@
 				. '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
 				. '|(?:GRANT|REVOKE).*ON\s+TABLE'
 				. '|SHOW\s+(?:.*FROM|.*TABLE)'
-				. ')\s+\(*\s*((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is', $query, $maybe ) ) {
+			. ')\s+\(*\s*((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is',
+			$query,
+			$maybe
+		) ) {
 			return str_replace( '`', '', $maybe[1] );
 		}
 
@@ -3172,11 +3273,11 @@
 	 * Load the column metadata from the last query.
 	 *
 	 * @since 3.5.0
-	 *
 	 */
 	protected function load_col_info() {
-		if ( $this->col_info )
+		if ( $this->col_info ) {
 			return;
+		}
 
 		if ( $this->use_mysqli ) {
 			$num_fields = mysqli_num_fields( $this->result );
@@ -3205,15 +3306,15 @@
 
 		if ( $this->col_info ) {
 			if ( $col_offset == -1 ) {
-				$i = 0;
+				$i         = 0;
 				$new_array = array();
 				foreach ( (array) $this->col_info as $col ) {
-					$new_array[$i] = $col->{$info_type};
+					$new_array[ $i ] = $col->{$info_type};
 					$i++;
 				}
 				return $new_array;
 			} else {
-				return $this->col_info[$col_offset]->{$info_type};
+				return $this->col_info[ $col_offset ]->{$info_type};
 			}
 		}
 	}
@@ -3253,15 +3354,37 @@
 	 * @return false|void
 	 */
 	public function bail( $message, $error_code = '500' ) {
-		if ( !$this->show_errors ) {
+		if ( $this->show_errors ) {
+			$error = '';
+
+			if ( $this->use_mysqli ) {
+				if ( $this->dbh instanceof mysqli ) {
+					$error = mysqli_error( $this->dbh );
+				} elseif ( mysqli_connect_errno() ) {
+					$error = mysqli_connect_error();
+				}
+			} else {
+				if ( is_resource( $this->dbh ) ) {
+					$error = mysql_error( $this->dbh );
+				} else {
+					$error = mysql_error();
+				}
+			}
+
+			if ( $error ) {
+				$message = '<p><code>' . $error . "</code></p>\n" . $message;
+			}
+
+			wp_die( $message );
+		} else {
 			if ( class_exists( 'WP_Error', false ) ) {
-				$this->error = new WP_Error($error_code, $message);
+				$this->error = new WP_Error( $error_code, $message );
 			} else {
 				$this->error = $message;
 			}
+
 			return false;
 		}
-		wp_die($message);
 	}
 
 
@@ -3285,8 +3408,8 @@
 		}
 
 		if ( $closed ) {
-			$this->dbh = null;
-			$this->ready = false;
+			$this->dbh           = null;
+			$this->ready         = false;
 			$this->has_connected = false;
 		}
 
@@ -3306,9 +3429,9 @@
 	public function check_database_version() {
 		global $wp_version, $required_mysql_version;
 		// Make sure the server has the required MySQL version
-		if ( version_compare($this->db_version(), $required_mysql_version, '<') ) {
+		if ( version_compare( $this->db_version(), $required_mysql_version, '<' ) ) {
 			/* translators: 1: WordPress version number, 2: Minimum required MySQL version number */
-			return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ));
+			return new WP_Error( 'database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ) );
 		}
 	}
 
@@ -3339,10 +3462,12 @@
 	public function get_charset_collate() {
 		$charset_collate = '';
 
-		if ( ! empty( $this->charset ) )
+		if ( ! empty( $this->charset ) ) {
 			$charset_collate = "DEFAULT CHARACTER SET $this->charset";
-		if ( ! empty( $this->collate ) )
+		}
+		if ( ! empty( $this->collate ) ) {
 			$charset_collate .= " COLLATE $this->collate";
+		}
 
 		return $charset_collate;
 	}
@@ -3365,13 +3490,13 @@
 		$version = $this->db_version();
 
 		switch ( strtolower( $db_cap ) ) {
-			case 'collation' :    // @since 2.5.0
-			case 'group_concat' : // @since 2.7.0
-			case 'subqueries' :   // @since 2.7.0
+			case 'collation':    // @since 2.5.0
+			case 'group_concat': // @since 2.7.0
+			case 'subqueries':   // @since 2.7.0
 				return version_compare( $version, '4.1', '>=' );
-			case 'set_charset' :
+			case 'set_charset':
 				return version_compare( $version, '5.0.7', '>=' );
-			case 'utf8mb4' :      // @since 4.1.0
+			case 'utf8mb4':      // @since 4.1.0
 				if ( version_compare( $version, '5.5.3', '<' ) ) {
 					return false;
 				}
@@ -3391,7 +3516,7 @@
 				} else {
 					return version_compare( $client_version, '5.5.3', '>=' );
 				}
-			case 'utf8mb4_520' : // @since 4.6.0
+			case 'utf8mb4_520': // @since 4.6.0
 				return version_compare( $version, '5.6', '>=' );
 		}
 
@@ -3406,7 +3531,7 @@
 	 *
 	 * @since 2.5.0
 	 *
-	 * @return string|array The name of the calling function
+	 * @return string Comma separated list of the calling functions.
 	 */
 	public function get_caller() {
 		return wp_debug_backtrace_summary( __CLASS__ );