Making WordPress.org

Changeset 10474


Ignore:
Timestamp:
11/30/2020 03:59:30 AM (4 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.

Location:
sites/trunk/api.wordpress.org/public_html/events/1.0
Files:
2 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,
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r10271 r10474  
    189189        'city-invalid-private-ip' => array(
    190190            'input' => array(
    191                 'location_name' => 'Rivendell',
     191                'location_name' => 'InvalidCity',
    192192                'ip'            => '127.0.0.1',
    193193            ),
     
    409409        /*
    410410         * The city endonym, locale, and timezone are given
    411          *
    412          * @todo
    413          * This is currently failing. A query from PHP shows row id 2220957 has "Yaound?" instead of
    414          * "Yaoundé", but it's correct in the database itself.
    415411         */
    416412        'city-endonym-accents-africa' => array(
     
    421417            ),
    422418            'expected' => array(
    423                 'description' => 'yaounde',
     419                'description' => 'yaoundé',
    424420                'latitude'    => '3.867',
    425421                'longitude'   => '11.517',
     
    563559            'expected' => array(
    564560                'description' => 'Санкт-Петербург',
    565                 'latitude'    => '59.894',
    566                 'longitude'   => '30.264',
     561                'latitude'    => '59.939',
     562                'longitude'   => '30.314',
    567563                'country'     => 'RU',
    568564            ),
     
    578574            'expected' => array(
    579575                'description' => 'Санкт-Петербург',
    580                 'latitude'    => '59.894',
    581                 'longitude'   => '30.264',
     576                'latitude'    => '59.939',
     577                'longitude'   => '30.314',
    582578                'country'     => 'RU',
    583579            ),
     
    639635            'expected' => array(
    640636                'description' => 'portland',
    641                 'latitude'    => '43.661',
    642                 'longitude'   => '-70.255',
     637                'latitude'    => '43.657',
     638                'longitude'   => '-70.259',
    643639                'country'     => 'US',
    644640            ),
     
    652648            ),
    653649            'expected' => array(
    654                 'description' => 'sao',
     650                'description' => 'são paulo',
    655651                'latitude'    => '-23.548',
    656652                'longitude'   => '-46.636',
Note: See TracChangeset for help on using the changeset viewer.