You are here

function _ip_geoloc_process_access_log in IP Geolocation Views & Maps 7

Callback for ip_geoloc_sync_with_accesslog batch process.

1 string reference to '_ip_geoloc_process_access_log'
ip_geoloc_sync_with_accesslog in ./ip_geoloc.admin.inc
Bring the visitor location database up to date with the system accesslog.

File

./ip_geoloc.admin.inc, line 437
Admin configuration settings for IPGV&M.

Code

function _ip_geoloc_process_access_log($ips_to_be_processed, $use_smart_ip, $use_google_to_reverse_geocode, &$context) {
  $sandbox =& $context['sandbox'];
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = count($ips_to_be_processed);
    $sandbox['ips'] = $ips_to_be_processed;
  }
  $ip_address = array_shift($sandbox['ips']);
  $location = array(
    'ip_address' => $ip_address,
  );
  if ($use_smart_ip) {

    // Depending on the value of the variable 'smart_ip_use_ipinfodb_service'
    // this will either use the IPInfoDB web service or Smart IP's database, as
    // created by importing the MaxMind CSV archive.
    $location = smart_ip_get_location($location['ip_address']);
  }
  else {
    ip_geoloc_use_geoip_api_if_enabled($location);
  }

  // Now that we have lat/long we can reverse-geocode to the street address.
  // Note, this call is subject to a limit of 2500/day.
  if ($use_google_to_reverse_geocode && ($google_address = ip_geoloc_reverse_geocode($location['latitude'], $location['longitude']))) {

    // To avoid fields contradicting eachother, should we clear out, rather
    // than merge with whatever Smart IP or GeoIP put in the $location?
    // For example Google normally returns 'locality', whereas and Smart IP and
    // GeoIP return 'city' instead. Similarly 'administrative_area_level_1' vs
    // 'region'.
    // $location = array('ip_address' => $ip_address);
    ip_geoloc_flatten_google_address($google_address, $location);
  }
  if (empty($location['formatted_address'])) {

    // Just so that a record is created and the IP is taken off the list.
    $location['formatted_address'] = '-';
  }
  if (ip_geoloc_store_location($location) === FALSE) {
    return;
  }

  // Update our progress information.
  $sandbox['progress']++;

  // Store result for post-processing in the _finished callback.
  $context['results'][] = $location['ip_address'] . ': ' . (empty($location['formatted_address']) ? '?' : $location['formatted_address']);

  // Provide to the batch engine an estimate of the level of completion so far.
  if ($sandbox['progress'] < $sandbox['max']) {

    // Note the addition of 100 in the formula below. This is to make sure that
    // batch sizes of 200 or greater do not terminate prematurely.
    // E.g 199/200 = 0.995 ends up being rounded to 100% causing abort.
    $context['finished'] = floor(100 * $sandbox['progress'] / $sandbox['max']) / 100;
  }
}