X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/607b7e02d77e7326161e8ec15639052d2040f745..refs/heads/pristine:/wp-includes/wp-db.php diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 2f8dbcf9..74dedd51 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -734,6 +734,9 @@ class wpdb { * @since 3.1.0 */ public function init_charset() { + $charset = ''; + $collate = ''; + if ( function_exists('is_multisite') && is_multisite() ) { $charset = 'utf8'; if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) { @@ -776,6 +779,11 @@ class wpdb { $charset = 'utf8mb4'; } + if ( 'utf8mb4' === $charset && ! $this->has_cap( 'utf8mb4' ) ) { + $charset = 'utf8'; + $collate = str_replace( 'utf8mb4_', 'utf8_', $collate ); + } + if ( 'utf8mb4' === $charset ) { // _general_ is outdated, so we can upgrade it to _unicode_, instead. if ( ! $collate || 'utf8_general_ci' === $collate ) { @@ -808,22 +816,29 @@ class wpdb { if ( ! isset( $collate ) ) $collate = $this->collate; if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { + $set_charset_succeeded = true; + if ( $this->use_mysqli ) { if ( function_exists( 'mysqli_set_charset' ) && $this->has_cap( 'set_charset' ) ) { - mysqli_set_charset( $dbh, $charset ); + $set_charset_succeeded = mysqli_set_charset( $dbh, $charset ); + } + + if ( $set_charset_succeeded ) { + $query = $this->prepare( 'SET NAMES %s', $charset ); + if ( ! empty( $collate ) ) + $query .= $this->prepare( ' COLLATE %s', $collate ); + mysqli_query( $dbh, $query ); } - $query = $this->prepare( 'SET NAMES %s', $charset ); - if ( ! empty( $collate ) ) - $query .= $this->prepare( ' COLLATE %s', $collate ); - mysqli_query( $dbh, $query ); } else { if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { - mysql_set_charset( $charset, $dbh ); + $set_charset_succeeded = mysql_set_charset( $charset, $dbh ); + } + if ( $set_charset_succeeded ) { + $query = $this->prepare( 'SET NAMES %s', $charset ); + if ( ! empty( $collate ) ) + $query .= $this->prepare( ' COLLATE %s', $collate ); + mysql_query( $query, $dbh ); } - $query = $this->prepare( 'SET NAMES %s', $charset ); - if ( ! empty( $collate ) ) - $query .= $this->prepare( ' COLLATE %s', $collate ); - mysql_query( $query, $dbh ); } } } @@ -1174,18 +1189,19 @@ class wpdb { * * @uses wpdb::_real_escape() * @since 2.8.0 - * @access private + * @access public * * @param string|array $data * @return string|array escaped */ - function _escape( $data ) { + public function _escape( $data ) { if ( is_array( $data ) ) { foreach ( $data as $k => $v ) { - if ( is_array($v) ) + if ( is_array( $v ) ) { $data[$k] = $this->_escape( $v ); - else + } else { $data[$k] = $this->_real_escape( $v ); + } } } else { $data = $this->_real_escape( $data ); @@ -1258,8 +1274,8 @@ class wpdb { * * Both %d and %s should be left unquoted in the query string. * - * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 ) - * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); + * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 ); + * $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); * * @link https://secure.php.net/sprintf Description of syntax. * @since 2.3.0 @@ -1348,10 +1364,13 @@ class wpdb { wp_load_translations_early(); - if ( $caller = $this->get_caller() ) + if ( $caller = $this->get_caller() ) { + /* translators: 1: Database error message, 2: SQL query, 3: Name of the calling function */ $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); - else + } else { + /* translators: 1: Database error message, 2: SQL query */ $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); + } error_log( $error_str ); @@ -2278,10 +2297,8 @@ class wpdb { * @since 0.71 * * @param string|null $query SQL query. - * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. - * Return an associative array (column => value, ...), - * a numerically indexed array (0 => value, ...) or - * an object ( ->column = value ), respectively. + * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to + * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT. * @param int $y Optional. Row to return. Indexed from 0. * @return array|object|null|void Database query result in format specified by $output or null on failure */ @@ -3018,17 +3035,23 @@ class wpdb { . '|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)*(?:\s+FROM)?' + . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:.+?FROM)?' . ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is', $query, $maybe ) ) { return str_replace( '`', '', $maybe[1] ); } - // SHOW TABLE STATUS and SHOW TABLES - if ( preg_match( '/^\s*(?:' - . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)' - . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)' - . ')\W((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\W/is', $query, $maybe ) ) { - return str_replace( '`', '', $maybe[1] ); + // SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts' + if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) { + return $maybe[2]; + } + + // SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%' + // This quoted LIKE operand seldom holds a full table name. + // It is usually a pattern for matching a prefix so we just + // strip the trailing % and unescape the _ to get 'wp_123_' + // which drop-ins can use for routing these SQL statements. + if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) { + return str_replace( '\\_', '_', $maybe[2] ); } // Big pattern for the rest of the table-related queries. @@ -3193,8 +3216,10 @@ class wpdb { 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( __( 'ERROR: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); + } } /**