Make WordPress Core

Ticket #20368: 20368.patch

File 20368.patch, 2.0 KB (added by kurtpayne, 13 years ago)

Proof of concept - convert strings from database to UTF-8 on access

  • wp-includes/wp-db.php

     
    11271127                        }
    11281128
    11291129                        @mysql_free_result( $this->result );
     1130                       
     1131                        if ( function_exists( 'mb_convert_encoding' ) && 'UTF-8' !== $this->get_php_encoding() ) {
     1132                                foreach ( $this->last_result as $key => $row ) {
     1133                                        $i = 0;
     1134                                        foreach ($row as $col => $val ) {
     1135                                                if ( 'string' === $this->get_col_info('type', $i++) ) {
     1136                                                        $this->last_result[$key]->$col = mb_convert_encoding( $val, 'UTF-8', $this->get_php_encoding() );
     1137                                                }
     1138                                        }
     1139                                }
     1140                        }
    11301141
    11311142                        // Log number of rows the query returned
    11321143                        // and return number of rows selected
     
    16021613        function db_version() {
    16031614                return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
    16041615        }
     1616       
     1617        /**
     1618         * Get php's encoding type based on DB_CHARSET
     1619         *
     1620         * @return string  the sanitized string
     1621         *
     1622         * @uses DB_CHARSET  set in wp-config.php to know which $encoding to use
     1623         */
     1624        public function get_php_encoding() {
     1625                static $encoding;
     1626                if ( !isset( $encoding ) ) {
     1627                        switch ( strtolower( DB_CHARSET ) ) {
     1628                                case 'latin1':
     1629                                        $encoding = 'ISO-8859-1';
     1630                                        break;
     1631                                case 'utf8':
     1632                                case 'utf8mb4':
     1633                                        $encoding = 'UTF-8';
     1634                                        break;
     1635                                case 'cp866':
     1636                                        $encoding = 'cp866';
     1637                                        break;
     1638                                case 'cp1251':
     1639                                        $encoding = 'cp1251';
     1640                                        break;
     1641                                case 'koi8r':
     1642                                        $encoding = 'KOI8-R';
     1643                                        break;
     1644                                case 'big5':
     1645                                        $encoding = 'BIG5';
     1646                                        break;
     1647                                case 'gb2312':
     1648                                        $encoding = 'GB2312';
     1649                                        break;
     1650                                case 'sjis':
     1651                                        $encoding = 'Shift_JIS';
     1652                                        break;
     1653                                case 'ujis':
     1654                                        $encoding = 'EUC-JP';
     1655                                        break;
     1656                                case 'macroman':
     1657                                        $encoding = 'MacRoman';
     1658                                        break;
     1659                                default:
     1660                                        $encoding = 'UTF-8';
     1661                                        if ( WP_DEBUG ) {
     1662                                                trigger_error("Your DB_CHARSET doesn't map to a PHP encoding.", E_USER_WARNING);
     1663                                        }
     1664                        }
     1665                }
     1666                return $encoding;
     1667        }
    16051668}