Making WordPress.org


Ignore:
Timestamp:
11/30/2020 03:59:30 AM (3 years ago)
Author:
dd32
Message:

Events API: Update the API to accept the new type DB field to disambiguate actual places from States/Countries/others.

As part of this the table now includes non-city locations such as countries too, and so if the best match is a country it's still handled properly (returning country-wide events rather than just distance around the central point of the country).

Updates the tests to take into account the new database changes, but these are likely to become out-of-date as the data will be updated daily going forward.

See #3728.

File:
1 edited

Legend:

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

    r10470 r10474  
    348348    // Exact match
    349349    $query = '
    350         SELECT name, latitude, longitude, country
     350        SELECT name, latitude, longitude, country, type
    351351        FROM geoname_summary
    352352        WHERE name = %s
     
    354354            FIELD( %s, country  ) DESC,
    355355            FIELD( %s, timezone ) DESC,
     356            LEFT( type, 1 ) = "P" DESC,
    356357            population DESC,
    357358            BINARY LOWER( %s ) = BINARY LOWER( name ) DESC
     
    369370    if ( ! $row && $wildcard && 'ASCII' !== mb_detect_encoding( $location_name ) ) {
    370371        $query = '
    371             SELECT name, latitude, longitude, country
     372            SELECT name, latitude, longitude, country, type
    372373            FROM geoname_summary
    373374            WHERE name LIKE %s
     
    375376                FIELD( %s, country  ) DESC,
    376377                FIELD( %s, timezone ) DESC,
     378                LEFT( type, 1 ) = "P" DESC,
    377379                population DESC,
    378380                BINARY LOWER( %s ) = BINARY LOWER( LEFT( name, %d ) ) DESC
     
    547549        $guess        = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code );
    548550
    549         if ( $guess ) {
     551        $country_types = array(
     552            // See http://download.geonames.org/export/dump/featureCodes_en.txt
     553
     554            'A.PCL',   // political entity 
     555            'A.PCLD',  // dependent political entity   
     556            'A.PCLF',  // freely associated state   
     557            'A.PCLH',  // historical political entity   a former political entity
     558            'A.PCLI',  // independent political entity 
     559            'A.PCLIX', // section of independent political entity   
     560            'A.PCLS',  // semi-independent political entity
     561            'A.PRSH',  // parish an ecclesiastical district
     562            'A.TERR',  // territory
     563            'A.ZN',    // zone
     564        );
     565
     566        if ( $guess && in_array( $guess->type, $country_types, true ) ) {
     567            $location = array(
     568                'country'     => $guess->country,
     569                'description' => $guess->name,
     570            );
     571        } elseif ( $guess ) {
    550572            $location = array(
    551573                'description' => $guess->name,
Note: See TracChangeset for help on using the changeset viewer.