From 6759a210caa0d85fc8513c4c77e188a58371b92b Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 17 Jul 2015 06:34:26 +0000 Subject: [PATCH] WPDB: Remove some of the complexities in `::strip_invalid_text()` associated with switching character sets between queries. Instead of trying to dynamically change connection character sets, we now rely on the value of `::charset`. This also fixes the case where queries were being blocked when `DB_CHARSET` was `utf8`, but the column character set was non-`utf8`. Fixes #32165. Built from https://develop.svn.wordpress.org/trunk@33308 git-svn-id: http://core.svn.wordpress.org/trunk@33280 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/version.php | 2 +- wp-includes/wp-db.php | 50 +++++++++++++---------------------------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/wp-includes/version.php b/wp-includes/version.php index 1a05e8659b..43b3506c58 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-beta3-33300'; +$wp_version = '4.3-beta3-33308'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index a5b17fc8ca..d6f5a6679b 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -2710,55 +2710,37 @@ class wpdb { $queries = array(); foreach ( $data as $col => $value ) { if ( ! empty( $value['db'] ) ) { - if ( ! isset( $queries[ $value['charset'] ] ) ) { - $queries[ $value['charset'] ] = array(); - } - // We're going to need to truncate by characters or bytes, depending on the length value we have. if ( 'byte' === $value['length']['type'] ) { - // Split the CONVERT() calls by charset, so we can make sure the connection is right - $queries[ $value['charset'] ][ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING binary ), %.0f ) USING {$value['charset']} )", $value['value'], $value['length']['length'] ); + // Using binary causes LEFT() to truncate by bytes. + $charset = 'binary'; } else { - $queries[ $value['charset'] ][ $col ] = $this->prepare( "LEFT( CONVERT( %s USING {$value['charset']} ), %.0f )", $value['value'], $value['length']['length'] ); + $charset = $value['charset']; } + $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING {$this->charset} )", $value['value'], $value['length']['length'] ); + unset( $data[ $col ]['db'] ); } } - $connection_charset = $this->charset; - foreach ( $queries as $charset => $query ) { + $sql = array(); + foreach ( $queries as $column => $query ) { if ( ! $query ) { continue; } - // Change the charset to match the string(s) we're converting - if ( $charset !== $connection_charset ) { - $connection_charset = $charset; - $this->set_charset( $this->dbh, $charset ); - } - - $this->check_current_query = false; - - $sql = array(); - foreach ( $query as $column => $column_query ) { - $sql[] = $column_query . " AS x_$column"; - } - - $row = $this->get_row( "SELECT " . implode( ', ', $sql ), ARRAY_A ); - if ( ! $row ) { - $this->set_charset( $this->dbh, $connection_charset ); - return new WP_Error( 'wpdb_strip_invalid_text_failure' ); - } - - foreach ( array_keys( $query ) as $column ) { - $data[ $column ]['value'] = $row["x_$column"]; - } + $sql[] = $query . " AS x_$column"; } - // Don't forget to change the charset back! - if ( $connection_charset !== $this->charset ) { - $this->set_charset( $this->dbh ); + $this->check_current_query = false; + $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 ) { + $data[ $column ]['value'] = $row["x_$column"]; } }