You are here

function smart_ip_store_location_csv in Smart IP 6

Same name and namespace in other branches
  1. 6.2 includes/smart_ip.utility.inc \smart_ip_store_location_csv()
  2. 7.2 includes/smart_ip.utility.inc \smart_ip_store_location_csv()
  3. 7 includes/smart_ip.utility.inc \smart_ip_store_location_csv()
1 string reference to 'smart_ip_store_location_csv'
smart_ip_update_db_batch in includes/smart_ip.utility.inc
Prepare a batch definition

File

includes/smart_ip.utility.inc, line 163
Utility routines to load the Smart IP database.

Code

function smart_ip_store_location_csv(&$context) {
  $last_cache = variable_get('smart_ip_store_location_csv_done', FALSE);
  $cache_intact = cache_get('smart_ip:' . $last_cache, 'cache_smart_ip');
  if ($cache_intact) {

    // We are in recovery mode. Using previous stored locations.
    $context['finished'] = TRUE;
    $context['message'] = t('Using previous stored locations (recovery mode)');
    return;
  }
  variable_set('smart_ip_store_location_csv_done', FALSE);

  // If this update was aborted in a previous step, or has a dependency that
  // was aborted in a previous step, go no further.
  if (isset($context['results']['#abort'])) {
    return;
  }
  $fp = @fopen($context['results']['#location_csv_file'], 'r');
  if ($fp === FALSE) {

    // Error occured then stop the process
    $message = t('Opening CSV file %file failed.', array(
      '%file' => $context['results']['#location_csv_file'],
    ));
    $context['results']['#abort'] = $message;
    $context['message'] = $message;
    unset($context['results']['#location_csv_file']);
    variable_set('smart_ip_extract_zip_done', FALSE);
    variable_set('smart_ip_get_zip_done', FALSE);
  }
  else {
    if (!isset($context['sandbox']['#location_csv_pointer'])) {
      $location_csv_pointer = variable_get('smart_ip_location_csv_pointer', 0);
      if ($location_csv_pointer) {

        // Recover from the last interrupted pointer
        @fseek($fp, $location_csv_pointer);
      }
      else {

        // New update, clear the cache
        cache_clear_all('smart_ip:', 'cache_smart_ip', TRUE);

        // Record the last pointer
        $fp_check = @fopen($context['results']['#location_csv_file'], 'r');
        @fseek($fp_check, -1, SEEK_END);
        variable_set('smart_ip_location_csv_last_pointer', @ftell($fp_check));
      }
    }
    else {
      @fseek($fp, $context['sandbox']['#location_csv_pointer']);
    }
    $data = @fgetcsv($fp);
    $context['sandbox']['#location_csv_pointer'] = @ftell($fp);
    if (feof($fp)) {
      fclose($fp);

      // Update our progress information.
      $context['finished'] = TRUE;
      $context['message'] = t('Processing %location done', array(
        '%location' => basename($context['results']['#location_csv_file']),
      ));
      unset($context['results']['#location_csv_file']);

      // An indicator that is to be used in recovery mode
      variable_set('smart_ip_store_location_csv_done', (int) variable_get('smart_ip_indicator_last_ip', NULL));

      // Reset our last IP indicator
      variable_set('smart_ip_indicator_last_ip', FALSE);
      variable_set('smart_ip_location_csv_pointer', 0);
      return;
    }
    else {
      $current_pointer = $context['sandbox']['#location_csv_pointer'];
      $last_pointer = variable_get('smart_ip_location_csv_last_pointer', 0);
      $estimated_progress = floor(100 * ($current_pointer / $last_pointer));

      // Update our progress information.
      $context['finished'] = $estimated_progress / 100;
      $context['message'] = t('Parsing %location at line number: @value of @end', array(
        '%location' => basename($context['results']['#location_csv_file']),
        '@value' => $current_pointer,
        '@end' => $last_pointer,
      ));
    }
    if (count($data) == 9 && is_numeric($data[0])) {
      $data_location = array(
        'country_code' => $data[1],
        'region' => $data[2],
        'city' => $data[3],
        'zip' => $data[4],
        'latitude' => $data[5],
        'longitude' => $data[6],
      );
      variable_set('smart_ip_location_csv_pointer', $context['sandbox']['#location_csv_pointer']);
      variable_set('smart_ip_indicator_last_ip', $data[0]);
      cache_set('smart_ip:' . $data[0], $data_location, 'cache_smart_ip');
    }
  }
}