Changeset 33478
- Timestamp:
- 07/29/2015 06:35:00 AM (10 years ago)
- Location:
- branches/4.1
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.1/src/wp-includes/wp-db.php
r33477 r33478 2652 2652 foreach ( $data as $col => $value ) { 2653 2653 if ( ! empty( $value['db'] ) ) { 2654 if ( ! isset( $queries[ $value['charset'] ] ) ) {2655 $queries[ $value['charset'] ] = array();2656 }2657 2658 2654 // We're going to need to truncate by characters or bytes, depending on the length value we have. 2659 2655 if ( 'byte' === $value['length']['type'] ) { 2660 // Split the CONVERT() calls by charset, so we can make sure the connection is right2661 $ 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'; 2662 2658 } else { 2663 $ queries[ $value['charset'] ][ $col ] = $this->prepare( "LEFT( CONVERT( %s USING {$value['charset']} ), %.0f )", $value['value'], $value['length']['length'] );2659 $charset = $value['charset']; 2664 2660 } 2661 2662 $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING {$this->charset} )", $value['value'], $value['length']['length'] ); 2665 2663 2666 2664 unset( $data[ $col ]['db'] ); … … 2668 2666 } 2669 2667 2670 $ connection_charset = $this->charset;2671 foreach ( $queries as $c harset=> $query ) {2668 $sql = array(); 2669 foreach ( $queries as $column => $query ) { 2672 2670 if ( ! $query ) { 2673 2671 continue; 2674 2672 } 2675 2673 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"]; 2703 2685 } 2704 2686 } -
branches/4.1/tests/phpunit/tests/db/charset.php
r32402 r33478 244 244 'expected' => str_repeat( "\xcc\xe3", 5 ), 245 245 'length' => array( 'type' => 'byte', 'length' => 10 ), 246 ), 247 'ujis_with_utf8_connection' => array( 248 'charset' => 'ujis', 249 'connection_charset' => 'utf8', 250 'value' => '自動下書き', 251 'expected' => '自動下書き', 252 'length' => array( 'type' => 'byte', 'length' => 100 ), 253 ), 254 'ujis_with_utf8_connection_char_length' => array( 255 'charset' => 'ujis', 256 'connection_charset' => 'utf8', 257 'value' => '自動下書き', 258 'expected' => '自動下書', 259 'length' => array( 'type' => 'char', 'length' => 4 ), 260 ), 261 'ujis_with_utf8_connection_byte_length' => array( 262 'charset' => 'ujis', 263 'connection_charset' => 'utf8', 264 'value' => '自動下書き', 265 'expected' => '自動', 266 'length' => array( 'type' => 'byte', 'length' => 6 ), 246 267 ), 247 268 'false' => array( … … 290 311 $expected = $field; 291 312 $expected['value'] = $expected['expected']; 292 unset( $expected['expected'], $field['expected'] );313 unset( $expected['expected'], $field['expected'], $expected['connection_charset'] ); 293 314 294 315 // We're keeping track of these for our multiple-field test. … … 304 325 } 305 326 306 // Time for our test of multiple fields at once.307 $data_provider[] = array( $multiple, $multiple_expected, 'multiple fields/charsets' );308 309 327 return $data_provider; 310 328 } … … 315 333 */ 316 334 function test_strip_invalid_text( $data, $expected, $message ) { 335 $charset = self::$_wpdb->charset; 336 if ( isset( $data[0]['connection_charset'] ) ) { 337 $new_charset = $data[0]['connection_charset']; 338 unset( $data[0]['connection_charset'] ); 339 } else { 340 $new_charset = $data[0]['charset']; 341 } 342 343 self::$_wpdb->charset = $new_charset; 344 self::$_wpdb->set_charset( self::$_wpdb->dbh, $new_charset ); 345 317 346 $actual = self::$_wpdb->strip_invalid_text( $data ); 347 348 self::$_wpdb->charset = $charset; 349 self::$_wpdb->set_charset( self::$_wpdb->dbh, $charset ); 350 318 351 $this->assertSame( $expected, $actual, $message ); 319 352 }
Note: See TracChangeset
for help on using the changeset viewer.