Making WordPress.org


Ignore:
Timestamp:
03/01/2018 09:13:34 PM (9 years ago)
Author:
iandunn
Message:

Events: Switch to utf8 charset when querying geoname_summary.

This is in conjunction with altering the table schema as described in #3295:comment:8.

Props Otto42, dd32, iandunn.
Fixes #3295

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r6816 r6818  
    282282
    283283        // Exact match
    284         $row = $wpdb->get_row( $wpdb->prepare( "
     284        $query = "
    285285                SELECT name, latitude, longitude, country
    286286                FROM geoname_summary
    … …  
    290290                        FIELD( %s, timezone ) DESC,
    291291                        population DESC
    292                 LIMIT 1",
    293                 $location_name,
    294                 $country,
    295                 $timezone
    296         ) );
     292                LIMIT 1";
     293
     294        $prepared_query = $wpdb->prepare( $query, $location_name, $country, $timezone );
     295        $db_handle      = $wpdb->db_connect( $prepared_query );
     296
     297        $wpdb->set_charset( $db_handle, 'utf8' ); // The content in this table requires a UTF8 connection.
     298        $row = $wpdb->get_row( $prepared_query );
     299        $wpdb->set_charset( $db_handle, 'latin1' ); // Revert to the default charset to avoid affecting other queries.
    297300
    298301        // Wildcard match
    299302        if ( ! $row && $wildcard && 'ASCII' !== mb_detect_encoding( $location_name ) ) {
    300                 $row = $wpdb->get_row( $wpdb->prepare( "
     303                $query = "
    301304                        SELECT name, latitude, longitude, country
    302305                        FROM geoname_summary
    … …  
    306309                                FIELD( %s, timezone ) DESC,
    307310                                population DESC
    308                         LIMIT 1",
    309                         $wpdb->esc_like( $location_name ) . '%',
    310                         $country,
    311                         $timezone
    312                 ) );
     311                        LIMIT 1";
     312
     313                $prepared_query = $wpdb->prepare( $query, $wpdb->esc_like( $location_name ) . '%', $country, $timezone );
     314                $db_handle      = $wpdb->db_connect( $prepared_query );
     315
     316                $wpdb->set_charset( $db_handle, 'utf8' ); // The content in this table requires a UTF8 connection.
     317                $row = $wpdb->get_row( $prepared_query );
     318                $wpdb->set_charset( $db_handle, 'latin1' ); // Revert to the default charset to avoid affecting other queries.
    313319        }
    314320
    … …  
    319325        //       $row->name .= ', ' . $row->state;
    320326        // }
     327
     328        // Strip off null bytes
     329        // @todo Modify geoname script to to this instead?
     330        if ( ! empty( $row->name ) ) {
     331                $row->name = trim( $row->name );
     332        }
    321333
    322334        return $row;
Note: See TracChangeset for help on using the changeset viewer.