Changeset 6276
- Timestamp:
- 12/14/2017 11:45:18 PM (7 years ago)
- 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
r6275 r6276 32 32 */ 33 33 define( 'THROTTLE_STICKY_WORDCAMPS', false ); 34 define( 'THROTTLE_GEONAMES', 0 );35 define( 'THROTTLE_IP2LOCATION', 0 );36 37 defined( 'DAY_IN_SECONDS' ) or define( 'DAY_IN_SECONDS', 60 * 60 * 24 );34 define( 'THROTTLE_GEONAMES', 0 ); 35 define( 'THROTTLE_IP2LOCATION', 0 ); 36 37 defined( 'DAY_IN_SECONDS' ) or define( 'DAY_IN_SECONDS', 60 * 60 * 24 ); 38 38 defined( 'WEEK_IN_SECONDS' ) or define( 'WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS ); 39 39 … … 59 59 */ 60 60 function bootstrap() { 61 $base_dir = dirname( dirname( __DIR__ ) );61 $base_dir = dirname( dirname( __DIR__ ) ); 62 62 63 63 require( $base_dir . '/init.php' ); … … 80 80 // If a precise location is known, use a GET request. The values here should come from the `location` key of the result of a POST request. 81 81 if ( isset( $_GET['latitude'] ) ) { 82 $location_args['latitude'] = $_GET['latitude'];82 $location_args['latitude'] = $_GET['latitude']; 83 83 $location_args['longitude'] = $_GET['longitude']; 84 84 } … … 113 113 */ 114 114 $public_ip = filter_var( 115 116 117 115 $_REQUEST['ip'], 116 FILTER_VALIDATE_IP, 117 FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE 118 118 ); 119 119 … … 153 153 if ( ! empty( $location['latitude'] ) ) { 154 154 $event_args['nearby'] = array( 155 'latitude' => $location['latitude'],155 'latitude' => $location['latitude'], 156 156 'longitude' => $location['longitude'], 157 157 ); … … 362 362 if ( false === $long_ip || ! isset( $from, $where ) ) { 363 363 wp_cache_set( $cache_key, '__NOT_FOUND__', $cache_group, $cache_life ); 364 364 365 return false; 365 366 } … … 377 378 if ( ! $row || '-' == $row->country_short ) { 378 379 wp_cache_set( $cache_key, '__NOT_FOUND__', $cache_group, $cache_life ); 380 379 381 return false; 380 382 } … … 476 478 477 479 $country_code = get_country_code_from_locale( $args['locale'] ?? '' ); 478 $guess = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code);480 $guess = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code ); 479 481 480 482 if ( $guess ) { 481 483 $location = array( 482 484 'description' => $guess->name, 483 'latitude' => $guess->latitude,484 'longitude' => $guess->longitude,485 'country' => $guess->country,485 'latitude' => $guess->latitude, 486 'longitude' => $guess->longitude, 487 'country' => $guess->country, 486 488 ); 487 489 } else { … … 596 598 if ( ! $country && $location_word_count >= 2 ) { 597 599 // Catch input like "Vancouver Canada" 598 $country_id 599 $country 600 $country_id = $location_name_parts[ $location_word_count - 1 ]; 601 $country = get_country_from_name( $country_id ); 600 602 } 601 603 … … 684 686 685 687 $wheres = array(); 686 if ( ! empty( $args['type'] ) && in_array( $args['type'], array( 'meetup', 'wordcamp' ) ) ) {687 $wheres[] = '`type` = %s';688 if ( ! empty( $args['type'] ) && in_array( $args['type'], array( 'meetup', 'wordcamp' ) ) ) { 689 $wheres[] = '`type` = %s'; 688 690 $sql_values[] = $args['type']; 689 691 } 690 692 691 693 // If we want nearby events, create a WHERE based on a bounded box of lat/long co-ordinates. 692 if ( ! empty( $args['nearby'] ) ) {694 if ( ! empty( $args['nearby'] ) ) { 693 695 $nearby_where = array(); 694 696 695 697 foreach ( $event_distances as $type => $distance ) { 696 if ( ! empty( $args['type'] ) && $type != $args['type'] ) {698 if ( ! empty( $args['type'] ) && $type != $args['type'] ) { 697 699 continue; 698 700 } 699 $bounded_box = get_bounded_coordinates( $args['nearby']['latitude'], $args['nearby']['longitude'], $distance ); 701 702 $bounded_box = get_bounded_coordinates( $args['nearby']['latitude'], $args['nearby']['longitude'], $distance ); 700 703 $nearby_where[] = '( `type` = %s AND `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )'; 701 $sql_values[] = $type;702 $sql_values[] = $bounded_box['latitude']['min'];703 $sql_values[] = $bounded_box['latitude']['max'];704 $sql_values[] = $bounded_box['longitude']['min'];705 $sql_values[] = $bounded_box['longitude']['max'];704 $sql_values[] = $type; 705 $sql_values[] = $bounded_box['latitude']['min']; 706 $sql_values[] = $bounded_box['latitude']['max']; 707 $sql_values[] = $bounded_box['longitude']['min']; 708 $sql_values[] = $bounded_box['longitude']['max']; 706 709 } 707 710 // Build the nearby where as a OR as different event types have different distances. … … 710 713 711 714 // Allow queries for limiting to specific countries. 712 if ( ! empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) {713 $wheres[] = '`country` = %s';715 if ( ! empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) { 716 $wheres[] = '`country` = %s'; 714 717 $sql_values[] = $args['country']; 715 718 } … … 724 727 // Limit 725 728 if ( isset( $args['number'] ) ) { 726 $sql_limits = 'LIMIT %d';729 $sql_limits = 'LIMIT %d'; 727 730 $sql_values[] = $args['number']; 728 731 } … … 758 761 foreach ( $raw_events as $event ) { 759 762 $events[] = array( 760 'type' => $event->type,761 'title' => $event->title,762 'url' => $event->url,763 'meetup' => $event->meetup,763 'type' => $event->type, 764 'title' => $event->title, 765 'url' => $event->url, 766 'meetup' => $event->meetup, 764 767 'meetup_url' => $event->meetup_url, 765 'date' => $event->date_utc, // TODO: DB stores a local date, not UTC.766 'location' => array(767 'location' => $event->location,768 'country' => $event->country,769 'latitude' => (float) $event->latitude,768 'date' => $event->date_utc, // TODO: DB stores a local date, not UTC. 769 'location' => array( 770 'location' => $event->location, 771 'country' => $event->country, 772 'latitude' => (float) $event->latitude, 770 773 'longitude' => (float) $event->longitude, 771 774 ) … … 775 778 wp_cache_set( $cache_key, $events, $cache_group, $cache_life ); 776 779 777 return $events; 780 return $events; 778 781 } 779 782 … … 858 861 if ( ! empty( $request_args['nearby'] ) ) { 859 862 $bounded_box = get_bounded_coordinates( $request_args['nearby']['latitude'], $request_args['nearby']['longitude'], $distance ); 860 $where[] = '( `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )';861 $values[] = $bounded_box['latitude']['min'];862 $values[] = $bounded_box['latitude']['max'];863 $values[] = $bounded_box['longitude']['min'];864 $values[] = $bounded_box['longitude']['max'];863 $where[] = '( `latitude` BETWEEN %f AND %f AND `longitude` BETWEEN %f AND %f )'; 864 $values[] = $bounded_box['latitude']['min']; 865 $values[] = $bounded_box['latitude']['max']; 866 $values[] = $bounded_box['longitude']['min']; 867 $values[] = $bounded_box['longitude']['max']; 865 868 } 866 869 … … 904 907 */ 905 908 function add_regional_wordcamps( $local_events, $user_agent ) { 906 $time = time();909 $time = time(); 907 910 $regional_wordcamps = array(); 908 911 … … 968 971 * @param float $lon The longitude of the location. 969 972 * @param int $distance_in_km The distance of the bounded box, in KM. 973 * 970 974 * @return array of bounded box. 971 975 */ … … 974 978 975 979 $angular_distance = $distance_in_km / 6371; // 6371 = radius of the earth in KM. 976 $lat = deg2rad( $lat );977 $lon = deg2rad( $lon );980 $lat = deg2rad( $lat ); 981 $lon = deg2rad( $lon ); 978 982 979 983 $earth_min_lat = -1.5707963267949; // = deg2rad( -90 ) = -PI/2 … … 986 990 $minimum_lon = $maximum_lon = 0; 987 991 988 // Ensure that we're not within a pole-area of the world, weirdness will ensu re.992 // Ensure that we're not within a pole-area of the world, weirdness will ensue. 989 993 if ( $minimum_lat > $earth_min_lat && $maximum_lat < $earth_max_lat ) { 990 991 $lon_delta = asin( sin( $angular_distance ) / cos( $lat ) ); 992 994 $lon_delta = asin( sin( $angular_distance ) / cos( $lat ) ); 993 995 $minimum_lon = $lon - $lon_delta; 996 $maximum_lon = $lon + $lon_delta; 997 994 998 if ( $minimum_lon < $earth_min_lon ) { 995 999 $minimum_lon += 2 * pi(); 996 1000 } 997 1001 998 $maximum_lon = $lon + $lon_delta;999 1002 if ( $maximum_lon > $earth_max_lon ) { 1000 1003 $maximum_lon -= 2 * pi(); 1001 1004 } 1002 1003 1005 } else { 1004 1006 // Use a much simpler range in polar regions. -
sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php
r6275 r6276 12 12 function run_tests() { 13 13 global $wpdb; 14 14 15 define( 'RUNNING_TESTS', true ); 15 16 define( 'SAVEQUERIES', true ); … … 23 24 $tests_failed += test_build_response(); 24 25 $tests_failed += test_is_client_core(); 26 25 27 $query_count = count( $wpdb->queries ); 26 28 $query_time = array_sum( array_column( $wpdb->queries, 1 ) ); … … 98 100 } 99 101 100 $passed 102 $passed = $case['expected'] === $actual_result; 101 103 102 104 output_results( $case_id, $passed, $case['expected'], $actual_result ); … … 116 118 */ 117 119 function get_location_test_cases() { 118 120 $cases = array( 119 121 /* 120 122 * Only the country code is given … … 140 142 ), 141 143 'expected' => array( 142 'country' => 'ID',144 'country' => 'ID', 143 145 'description' => 'indonesia', 144 146 ), … … 388 390 * "Yaoundé", but it's correct in the database itself. 389 391 */ 390 392 'city-endonym-accents-africa' => array( 391 393 'input' => array( 392 394 'location_name' => 'Yaoundé', … … 641 643 ), 642 644 'expected' => array( 643 'country' => 'CA',645 'country' => 'CA', 644 646 'description' => 'canada', 645 647 ), … … 653 655 ), 654 656 'expected' => array( 655 'country' => 'DO',657 'country' => 'DO', 656 658 'description' => 'dominican republic', 657 659 ), … … 665 667 ), 666 668 'expected' => array( 667 'country' => 'CF',669 'country' => 'CF', 668 670 'description' => 'central african republic', 669 671 ), … … 677 679 ), 678 680 'expected' => array( 679 'country' => 'GB',681 'country' => 'GB', 680 682 'description' => 'united kingdom', 681 683 ), … … 689 691 ), 690 692 'expected' => array( 691 'country' => 'BI',693 'country' => 'BI', 692 694 'description' => 'burundi', 693 695 ), … … 890 892 891 893 $passed = $case['expected']['count'] === count( $actual_result ) && 892 893 894 894 ! empty( $actual_result[0]['url'] ) && 895 strtotime( $actual_result[0]['date'] ) > time() - ( 2 * 24 * 60 * 60 ) && 896 $case['expected']['country'] === strtoupper( $actual_result[0]['location']['country'] ); 895 897 896 898 output_results( $case_id, $passed, $case['expected'], $actual_result ); … … 928 930 '1-in-australia' => array( 929 931 'input' => array( 930 'number' => '1',932 'number' => '1', 931 933 'country' => 'AU', 932 934 ), … … 958 960 $actual_result = build_response( $case['input']['location'], $case['input']['location_args'] ); 959 961 960 $passed = $case['expected']['location'] === $actual_result['location'] &&961 962 $passed = $case['expected']['location'] === $actual_result['location'] && 963 isset( $case['expected']['error'] ) === isset( $actual_result['error'] ); 962 964 963 965 if ( $passed && $case['expected']['events'] ) { 964 966 $passed = ! empty( $actual_result['events'] ) && 965 966 967 ! empty( $actual_result['events'][0]['url'] ) && 968 strtotime( $actual_result['events'][0]['date'] ) > time() - ( 2 * 24 * 60 * 60 ); 967 969 } 968 970 … … 1063 1065 foreach ( $cases as $user_agent => $expected_result ) { 1064 1066 $actual_result = is_client_core( $user_agent ); 1065 1066 $passed = $expected_result === $actual_result; 1067 $passed = $expected_result === $actual_result; 1067 1068 1068 1069 output_results( $user_agent, $passed, $expected_result, $actual_result );
Note: See TracChangeset
for help on using the changeset viewer.