Making WordPress.org

Changeset 9750


Ignore:
Timestamp:
04/20/2020 10:28:21 PM (6 years ago)
Author:
iandunn
Message:

Events: Remove duplicate events after all events have been added.

pin_next_online_wordcamp() will be added in a subsequent commit, and at that point this function will need to be called after all events from been added dynamically.

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

    r9749 r9750  
    182182                        $location
    183183                );
     184
     185                $events = remove_duplicate_events( $events );
    184186
    185187                // Internal location data cannot be exposed in the response, see get_location().
    … …  
    11361138                        }
    11371139                }
    1138         }
    1139 
    1140         /**
    1141          * Remove duplicates events.
    1142          * Favor the regional event since it'll be pinned to the top.
    1143          */
    1144         foreach ( $regional_wordcamps as $regional_event ) {
    1145                 $local_events = array_filter( $local_events, function( $local_event ) use ( $regional_event ) {
    1146                         if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) {
    1147                                 return false;
    1148                         }
    1149 
    1150                         return true;
    1151                 } );
     1140
     1141
    11521142        }
    11531143
    … …  
    12741264
    12751265/**
     1266 * Remove duplicate events, based on the URL.
     1267 *
     1268 * This is needed because sometimes an event gets added from multiple functions; e.g., `get_events()` and also `maybe_add_regional_wordcamps()` and/or `pin_next_online_wordcamp()`.
     1269 *
     1270 * @param array $events
     1271 *
     1272 * @return array
     1273 */
     1274function remove_duplicate_events( $events ) {
     1275        // Re-index them by a URL to overwrite duplicates
     1276        $unique_events = array();
     1277
     1278        foreach ( $events as $event ) {
     1279                $parsed_url = parse_url( $event['url'] );
     1280
     1281                // Take all essential parts to cover current URL structures, and potential future ones.
     1282                $normalized_url = sprintf(
     1283                        '%s%s%s',
     1284                        $parsed_url['host'],
     1285                        $parsed_url['path'],
     1286                        $parsed_url['query']
     1287                );
     1288                $normalized_url = str_replace( '/', '', $normalized_url );
     1289
     1290                $unique_events[ $normalized_url ] = $event;
     1291        }
     1292
     1293        // Restore integer keys.
     1294        return array_values( $unique_events );
     1295}
     1296
     1297/**
    12761298 * Create a bounded latitude/longitude box of x KM around specific coordinates.
    12771299 *
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r9749 r9750  
    2727        $tests_failed += test_is_client_core();
    2828        $tests_failed += test_get_iso_3166_2_country_codes();
     29        $tests_failed += test_remove_duplicate_events();
    2930
    3031        $query_count  = count( $wpdb->queries );
    … …  
    12581259        $tests_expect_changes['core-user-agent'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
    12591260
    1260         // There should only be one entry for an event, even if the local event array already contains the regional event.
    1261         $tests_expect_no_changes['duplicate-event'] = maybe_add_regional_wordcamps( array( $region_data['us']['event'] ), $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );
    1262 
    12631261        foreach ( $tests_expect_no_changes as $name => $result ) {
    1264                 switch ( $name ) {
    1265                         case 'duplicate-event':
    1266                                 if ( $result !== array( $region_data['us']['event'] ) ) {
    1267                                         $failed++;
    1268                                         output_results( $name, false, array( $region_data['us']['event'] ), $result );
    1269                                 }
    1270                                 break;
    1271                         default:
    1272                                 if ( $result !== $local_events ) {
    1273                                         $failed++;
    1274                                         output_results( $name, false, $local_events, $result );
    1275                                 }
    1276                                 break;
     1262                if ( $result !== $local_events ) {
     1263                        $failed++;
     1264                        output_results( $name, false, $local_events, $result );
    12771265                }
    12781266        }
    … …  
    14821470
    14831471/**
     1472 * Test `remove_duplicate_events()`.
     1473 */
     1474function test_remove_duplicate_events() {
     1475        $duplicate_events = array(
     1476                // Each of these represents an event; extraneous fields have been removed for readability.
     1477                array (
     1478                        'url' => 'https://2020.us.wordcamp.org/',
     1479                ),
     1480
     1481                array (
     1482                        'url' => 'https://2020.detroit.wordcamp.org/',
     1483                ),
     1484
     1485                array(
     1486                        // Intentionally missing the trailing slash, to account for inconsistencies in data.
     1487                        'url' => 'https://2020.us.wordcamp.org',
     1488                )
     1489        );
     1490
     1491        printf( "\n\nRunning 1 remove_duplicate_events() test\n" );
     1492
     1493        $expected_result = array(
     1494                array (
     1495                        'url' => 'https://2020.us.wordcamp.org',
     1496                ),
     1497
     1498                array (
     1499                        'url' => 'https://2020.detroit.wordcamp.org/',
     1500                ),
     1501        );
     1502
     1503        $actual_result   = remove_duplicate_events( $duplicate_events );
     1504        $passed          = $expected_result === $actual_result;
     1505
     1506        output_results( 'remove duplicate events', $passed, $expected_result, $actual_result );
     1507
     1508        return $passed ? 0 : 1;
     1509}
     1510
     1511/**
    14841512 * Stub to simulate cache misses, so that the tests always get fresh results
    14851513 *
Note: See TracChangeset for help on using the changeset viewer.