Make WordPress Core


Ignore:
Timestamp:
07/29/2015 06:35:00 AM (11 years ago)
Author:
pento
Message:

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.

Merge of [33308] to the 4.1 branch.

See #32165.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1/src/wp-includes/wp-db.php

    r33477 r33478  
    26522652                        foreach ( $data as $col => $value ) {
    26532653                                if ( ! empty( $value['db'] ) ) {
    2654                                         if ( ! isset( $queries[ $value['charset'] ] ) ) {
    2655                                                 $queries[ $value['charset'] ] = array();
    2656                                         }
    2657 
    26582654                                        // We're going to need to truncate by characters or bytes, depending on the length value we have.
    26592655                                        if ( 'byte' === $value['length']['type'] ) {
    2660                                                 // Split the CONVERT() calls by charset, so we can make sure the connection is right
    2661                                                 $queries[ $value['charset'] ][ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING binary ), %.0f ) USING {$value['charset']} )", $value['value'], $value['length']['length'] );
     2656                                                // Using binary causes LEFT() to truncate by bytes.
     2657                                                $charset = 'binary';
    26622658                                        } else {
    2663                                                 $queries[ $value['charset'] ][ $col ] = $this->prepare( "LEFT( CONVERT( %s USING {$value['charset']} ), %.0f )", $value['value'], $value['length']['length'] );
     2659                                                $charset = $value['charset'];
    26642660                                        }
     2661
     2662                                        $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING {$this->charset} )", $value['value'], $value['length']['length'] );
    26652663
    26662664                                        unset( $data[ $col ]['db'] );
    … …  
    26682666                        }
    26692667
    2670                         $connection_charset = $this->charset;
    2671                         foreach ( $queries as $charset => $query ) {
     2668                        $sql = array();
     2669                        foreach ( $queries as $column => $query ) {
    26722670                                if ( ! $query ) {
    26732671                                        continue;
    26742672                                }
    26752673
    2676                                 // Change the charset to match the string(s) we're converting
    2677                                 if ( $charset !== $connection_charset ) {
    2678                                         $connection_charset = $charset;
    2679                                         $this->set_charset( $this->dbh, $charset );
    2680                                 }
    2681 
    2682                                 $this->check_current_query = false;
    2683 
    2684                                 $sql = array();
    2685                                 foreach ( $query as $column => $column_query ) {
    2686                                         $sql[] = $column_query . " AS x_$column";
    2687                                 }
    2688 
    2689                                 $row = $this->get_row( "SELECT " . implode( ', ', $sql ), ARRAY_A );
    2690                                 if ( ! $row ) {
    2691                                         $this->set_charset( $this->dbh, $connection_charset );
    2692                                         return new WP_Error( 'wpdb_strip_invalid_text_failure' );
    2693                                 }
    2694 
    2695                                 foreach ( array_keys( $query ) as $column ) {
    2696                                         $data[ $column ]['value'] = $row["x_$column"];
    2697                                 }
    2698                         }
    2699 
    2700                         // Don't forget to change the charset back!
    2701                         if ( $connection_charset !== $this->charset ) {
    2702                                 $this->set_charset( $this->dbh );
     2674                                $sql[] = $query . " AS x_$column";
     2675                        }
     2676
     2677                        $this->check_current_query = false;
     2678                        $row = $this->get_row( "SELECT " . implode( ', ', $sql ), ARRAY_A );
     2679                        if ( ! $row ) {
     2680                                return new WP_Error( 'wpdb_strip_invalid_text_failure' );
     2681                        }
     2682
     2683                        foreach ( array_keys( $data ) as $column ) {
     2684                                $data[ $column ]['value'] = $row["x_$column"];
    27032685                        }
    27042686                }
Note: See TracChangeset for help on using the changeset viewer.