You are here

ip2country.install in IP-based Determination of a Visitor's Country 7

Same filename and directory in other branches
  1. 8 ip2country.install
  2. 6 ip2country.install

Install, update, and uninstall functions for the ip2country module.

File

ip2country.install
View source
<?php

/**
 * @file
 * Install, update, and uninstall functions for the ip2country module.
 */

/**
 * Implements hook_requirements().
 */
function ip2country_requirements($phase) {
  $t = get_t();
  $has_curl = function_exists('curl_init');
  $requirements['ip2country_curl'] = array(
    'title' => $t('cURL'),
    'value' => $has_curl ? $t('Enabled') : $t('Not found'),
  );
  if (!$has_curl) {
    $requirements['ip2country_curl']['severity'] = REQUIREMENT_ERROR;
    $requirements['ip2country_curl']['description'] = $t("ip2country requires the PHP <a href='!curl_url'>cURL</a> library.", array(
      '!curl_url' => 'http://php.net/manual/en/curl.setup.php',
    ));
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function ip2country_schema() {
  $schema['ip2country'] = array(
    'description' => 'Association between IP range and Country',
    'fields' => array(
      'ip2country_id' => array(
        'description' => 'Row number (why is this needed?)',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'country' => array(
        'description' => 'ISO 3166 2-character country code',
        'type' => 'char',
        'length' => 3,
        'not null' => TRUE,
      ),
      'registry' => array(
        'description' => 'Regional Internet Registry',
        'type' => 'char',
        'length' => 10,
        'not null' => TRUE,
      ),
      'ip_range_first' => array(
        'description' => 'Lowest IP address in range',
        'type' => 'int',
        'size' => 'big',
        'not null' => TRUE,
      ),
      'ip_range_last' => array(
        'description' => 'Highest IP address in range',
        'type' => 'int',
        'size' => 'big',
        'not null' => TRUE,
      ),
      'ip_range_length' => array(
        'description' => 'Size of IP address block',
        'type' => 'int',
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'country_registry' => array(
        'country',
        'registry',
      ),
    ),
    'primary key' => array(
      'ip2country_id',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 *
 * Populates database tables needed by this module.
 *
 * By default, this module WILL populate the database. But because it
 * is so time-consuming, an option is available to not populate the
 * database automatically on install, intended for use by SimpleTest.
 */
function ip2country_install() {

  //
  // Set the variable to FALSE before installing this module to
  // leave the database table empty on install.
  //
  if (variable_get('ip2country_populate_database_on_install', TRUE)) {
    module_load_include('inc', 'ip2country');
    ip2country_update_database();
  }

  // Set the default date format for reporting database update date/time.
  variable_set('date_format_ip2country_date', 'n/j/Y');
  variable_set('date_format_ip2country_time', 'H:i:s T');
}

/**
 * Implements hook_uninstall().
 */
function ip2country_uninstall() {

  // Remove all module variables from the database.
  variable_del('ip2country_populate_database_on_install');
  variable_del('ip2country_debug');
  variable_del('ip2country_test_type');
  variable_del('ip2country_test_ip_address');
  variable_del('ip2country_test_country');
  variable_del('ip2country_rir');
  variable_del('ip2country_md5_checksum');
  variable_del('ip2country_last_update');
  variable_del('ip2country_last_update_rir');
  variable_del('ip2country_update_interval');
  variable_del('ip2country_watchdog');

  // Obsolete variables - we no longer store these, but a prior version
  // did. Try to remove just in case they're still hanging around.
  variable_del('ip2country_lookup');
  variable_del('ip2country_lookup_button');
  variable_del('ip2country_update_database');

  // Date/time format settings.
  variable_del('date_format_ip2country_date');
  variable_del('date_format_ip2country_time');
}

/**
 * Renames tables and variables, cleans up database.
 */
function ip2country_update_1() {
  $ret = array();

  // Remove all database tables created by this module.
  $ret[] = update_sql("ALTER TABLE {uc_ip2country} RENAME TO {ip2country}");

  // Rename variables while  preserving setting values.
  variable_set('ip2country_debug', variable_get('uc_ip2country_debug', FALSE));
  variable_set('ip2country_test_type', variable_get('uc_ip2country_test_type', 0));
  variable_set('ip2country_test_ip_address', variable_get('uc_ip2country_test_ip_address', ''));
  variable_set('ip2country_test_country', variable_get('uc_ip2country_test_country', ''));
  variable_set('ip2country_rir', variable_get('uc_ip2country_rir', 'arin'));
  variable_set('ip2country_last_update', variable_get('uc_ip2country_last_update', 0));
  variable_set('ip2country_update_interval', variable_get('uc_ip2country_update_interval', 604800));
  variable_set('ip2country_update_database', variable_get('uc_ip2country_update_database', ''));
  variable_set('ip2country_watchdog', variable_get('uc_ip2country_watchdog', 1));

  // Remove all old variables from the database.
  variable_del('uc_ip2country_debug');
  variable_del('uc_ip2country_test_type');
  variable_del('uc_ip2country_test_ip_address');
  variable_del('uc_ip2country_test_country');
  variable_del('uc_ip2country_rir');
  variable_del('uc_ip2country_last_update');
  variable_del('uc_ip2country_update_interval');
  variable_del('uc_ip2country_update_database');
  variable_del('uc_ip2country_watchdog');
  return $ret;
}

/**
 * Drops ip_range_date column.
 *
 * Drop the ip_range_date column as it is never used and just takes up database
 * space and processing time.
 */
function ip2country_update_2() {
  $ret = array();

  // Drop ip_range_date.
  db_drop_field($ret, 'ip2country', 'ip_range_date');
  return $ret;
}

/**
 * Provides configurable date/time formats for admin page.
 */
function ip2country_update_7100() {

  // Set the default date format for reporting database update date/time.
  variable_set('date_format_ip2country_date', 'n/j/Y');
  variable_set('date_format_ip2country_time', 'H:i:s T');
}

Functions

Namesort descending Description
ip2country_install Implements hook_install().
ip2country_requirements Implements hook_requirements().
ip2country_schema Implements hook_schema().
ip2country_uninstall Implements hook_uninstall().
ip2country_update_1 Renames tables and variables, cleans up database.
ip2country_update_2 Drops ip_range_date column.
ip2country_update_7100 Provides configurable date/time formats for admin page.