You are here

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

Same filename and directory in other branches
  1. 8 ip2country.install
  2. 7 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.
 */

// Include function definition for ip2country_update_database(),
// which is needed to initially fill the database after creation.
include_once drupal_get_path('module', 'ip2country') . '/ip2country.inc';

/**
 * 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().
 *
 * Creates 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() {

  // Create tables
  drupal_install_schema('ip2country');

  //
  // 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();
  }
}

/**
 * Implements hook_uninstall().
 *
 * Removes all tables and variables inserted into the
 * database by this module.
 */
function ip2country_uninstall() {

  // Remove all database tables created by this module
  drupal_uninstall_schema('ip2country');

  // 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_last_update');
  variable_del('ip2country_last_update_rir');
  variable_del('ip2country_lookup');
  variable_del('ip2country_lookup_button');
  variable_del('ip2country_update_interval');
  variable_del('ip2country_update_database');
  variable_del('ip2country_watchdog');
}

/**
 * Rename tables and variables, clean 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;
}

/**
 * Drop ip_range_date column.
 *
 * ip_range_date 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;
}

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 Rename tables and variables, clean up database.
ip2country_update_2 Drop ip_range_date column.