Make WordPress Core

Ticket #32165: 32165.3.diff

File 32165.3.diff, 3.7 KB (added by pento, 10 years ago)
  • src/wp-includes/wp-db.php

     
    27102710                        $queries = array();
    27112711                        foreach ( $data as $col => $value ) {
    27122712                                if ( ! empty( $value['db'] ) ) {
    2713                                         if ( ! isset( $queries[ $value['charset'] ] ) ) {
    2714                                                 $queries[ $value['charset'] ] = array();
    2715                                         }
    2716 
    27172713                                        // We're going to need to truncate by characters or bytes, depending on the length value we have.
    27182714                                        if ( 'byte' === $value['length']['type'] ) {
    27192715                                                // Split the CONVERT() calls by charset, so we can make sure the connection is right
    2720                                                 $queries[ $value['charset'] ][ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING binary ), %.0f ) USING {$value['charset']} )", $value['value'], $value['length']['length'] );
     2716                                                $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING binary ), %.0f ) USING {$value['charset']} )", $value['value'], $value['length']['length'] );
    27212717                                        } else {
    2722                                                 $queries[ $value['charset'] ][ $col ] = $this->prepare( "LEFT( CONVERT( %s USING {$value['charset']} ), %.0f )", $value['value'], $value['length']['length'] );
     2718                                                $queries[ $col ] = $this->prepare( "LEFT( CONVERT( %s USING {$value['charset']} ), %.0f )", $value['value'], $value['length']['length'] );
    27232719                                        }
    27242720
    27252721                                        unset( $data[ $col ]['db'] );
     
    27262722                                }
    27272723                        }
    27282724
    2729                         $connection_charset = $this->charset;
    2730                         foreach ( $queries as $charset => $query ) {
     2725                        $this->check_current_query = false;
     2726
     2727                        $sql = array();
     2728                        foreach ( $queries as $column => $query ) {
    27312729                                if ( ! $query ) {
    27322730                                        continue;
    27332731                                }
    27342732
    2735                                 // Change the charset to match the string(s) we're converting
    2736                                 if ( $charset !== $connection_charset ) {
    2737                                         $connection_charset = $charset;
    2738                                         $this->set_charset( $this->dbh, $charset );
    2739                                 }
     2733                                $sql[] = $query . " AS x_$column";
     2734                        }
    27402735
    2741                                 $this->check_current_query = false;
    2742 
    2743                                 $sql = array();
    2744                                 foreach ( $query as $column => $column_query ) {
    2745                                         $sql[] = $column_query . " AS x_$column";
    2746                                 }
    2747 
    2748                                 $row = $this->get_row( "SELECT " . implode( ', ', $sql ), ARRAY_A );
    2749                                 if ( ! $row ) {
    2750                                         $this->set_charset( $this->dbh, $connection_charset );
    2751                                         return new WP_Error( 'wpdb_strip_invalid_text_failure' );
    2752                                 }
    2753 
    2754                                 foreach ( array_keys( $query ) as $column ) {
    2755                                         $data[ $column ]['value'] = $row["x_$column"];
    2756                                 }
     2736                        $row = $this->get_row( "SELECT " . implode( ', ', $sql ), ARRAY_A );
     2737                        if ( ! $row ) {
     2738                                return new WP_Error( 'wpdb_strip_invalid_text_failure' );
    27572739                        }
    27582740
    2759                         // Don't forget to change the charset back!
    2760                         if ( $connection_charset !== $this->charset ) {
    2761                                 $this->set_charset( $this->dbh );
     2741                        foreach ( array_keys( $data ) as $column ) {
     2742                                $data[ $column ]['value'] = $row["x_$column"];
    27622743                        }
    27632744                }
    27642745
  • tests/phpunit/tests/db/charset.php

     
    303303                        $data_provider[] = array( $data, $expected, $test_case );
    304304                }
    305305
    306                 // Time for our test of multiple fields at once.
    307                 $data_provider[] = array( $multiple, $multiple_expected, 'multiple fields/charsets' );
    308 
    309306                return $data_provider;
    310307        }
    311308
     
    318315                        $this->markTestSkipped( 'This test fails in PHP 5.2 on Windows. See https://core.trac.wordpress.org/ticket/31262' );
    319316                }
    320317
     318                $charset = self::$_wpdb->charset;
     319                self::$_wpdb->set_charset( self::$_wpdb->dbh, $data[0]['charset'] );
     320
    321321                $actual = self::$_wpdb->strip_invalid_text( $data );
     322
     323                self::$_wpdb->set_charset( self::$_wpdb->dbh, $charset );
     324
    322325                $this->assertSame( $expected, $actual, $message );
    323326        }
    324327