WordPress.org

Making WordPress.org

Ticket #3295: 3295.diff

File 3295.diff, 3.0 KB (added by iandunn, 7 months ago)
  • 1.0/index.php

    diff --git 1.0/index.php 1.0/index.php
    index 908cfa2..554f258 100644
    function guess_location_from_city( $location_name, $timezone, $country_code ) { 
    216216
    217217        $cache_key = 'guess_location_from_city:' . md5( $location_name . ':' . $timezone . ':' . $country_code );
    218218        $guess     = wp_cache_get( $cache_key, $cache_group );
     219        $guess     = false;   // @todo tmp disable caching for manual testing
    219220
    220221        if ( $guess ) {
    221222                if ( '__NOT_FOUND__' == $guess ) {
    function guess_location_from_geonames( $location_name, $timezone, $country, $wil 
    267268        // The FIELD() orderings give preference to rows that match the country and/or timezone, without excluding rows that don't match.
    268269        // And we sort by population desc, assuming that the biggest matching location is the most likely one.
    269270
     271        add_db_table( 'geoip', 'geoname_summary_utf8mb4_test' ); // @todo tmp
     272
    270273        // Exact match
    271         $row = $wpdb->get_row( $wpdb->prepare( "
     274        $query = "
    272275                SELECT name, latitude, longitude, country
    273                 FROM geoname_summary
     276                FROM geoname_summary_utf8mb4_test
    274277                WHERE name = %s
    275278                ORDER BY
    276279                        FIELD( %s, country  ) DESC,
    277280                        FIELD( %s, timezone ) DESC,
    278281                        population DESC
    279                 LIMIT 1",
    280                 $location_name,
    281                 $country,
    282                 $timezone
    283         ) );
     282                LIMIT 1";
     283
     284        $prepared_query = $wpdb->prepare( $query, $location_name, $country, $timezone );
     285        $db_handle      = $wpdb->db_connect( $prepared_query );
     286
     287        $wpdb->set_charset( $db_handle, 'utf8' ); // The content in this table requires a UTF8 connection.
     288        $row = $wpdb->get_row( $prepared_query );
     289        $wpdb->set_charset( $db_handle, 'latin1' ); // Revert to the default charset to avoid affecting other queries.
    284290
    285291        // Wildcard match
    286292        if ( ! $row && $wildcard && 'ASCII' !== mb_detect_encoding( $location_name ) ) {
    287                 $row = $wpdb->get_row( $wpdb->prepare( "
     293                $query = "
    288294                        SELECT name, latitude, longitude, country
    289                         FROM geoname_summary
     295                        FROM geoname_summary_utf8mb4_test
    290296                        WHERE name LIKE %s
    291297                        ORDER BY
    292298                                FIELD( %s, country  ) DESC,
    293299                                FIELD( %s, timezone ) DESC,
    294300                                population DESC
    295                         LIMIT 1",
    296                         $wpdb->esc_like( $location_name ) . '%',
    297                         $country,
    298                         $timezone
    299                 ) );
     301                        LIMIT 1";
     302
     303                $prepared_query = $wpdb->prepare( $query, $wpdb->esc_like( $location_name ) . '%', $country, $timezone );
     304                $db_handle      = $wpdb->db_connect( $prepared_query );
     305
     306                $wpdb->set_charset( $db_handle, 'utf8' ); // The content in this table requires a UTF8 connection.
     307                $row = $wpdb->get_row( $prepared_query );
     308                $wpdb->set_charset( $db_handle, 'latin1' ); // Revert to the default charset to avoid affecting other queries.
    300309        }
    301310
    302311        // Suffix the "State", good in some countries (western countries) horrible in others
    function guess_location_from_geonames( $location_name, $timezone, $country, $wil 
    306315        //       $row->name .= ', ' . $row->state;
    307316        // }
    308317
     318        // Strip off null bytes - @todo should be possible to use avoid these in the raw data?
     319        if ( ! empty( $row->name ) ) {
     320                $row->name = trim( $row->name );
     321        }
     322
    309323        return $row;
    310324}
    311325