You are here

uc_ip2country.install in IP-based Determination of a Visitor's Country 5

File

uc_ip2country.install
View source
<?php

include_once drupal_get_path('module', 'uc_ip2country') . '/uc_ip2country.inc';

/**
 * Implementation of hook_install().
 *
 * Creates database tables needed by this module.
 */
function uc_ip2country_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      db_query("CREATE TABLE IF NOT EXISTS {ip2country} (\n          ip2country_id    int unsigned NOT NULL auto_increment,\n          country          char(3)      NOT NULL,\n          registry         char(10)     NOT NULL,\n          ip_range_first   bigint       NOT NULL,\n          ip_range_last    bigint       NOT NULL,\n          ip_range_date    datetime     NOT NULL,\n          ip_range_length  int          NOT NULL,\n          PRIMARY KEY  (ip2country_id),\n          KEY (country, registry)\n        ) /*!40100 DEFAULT CHARACTER SET UTF8 */");
      break;
    case 'pgsql':

      // pgsql case NOT TESTED !
      db_query("CREATE TABLE IF NOT EXISTS {ip2country} (\n          ip2country_id    serial       NOT NULL,\n          country          char(3)      NOT NULL,\n          registry         char(10)     NOT NULL,\n          ip_range_first   bigint       NOT NULL,\n          ip_range_last    bigint       NOT NULL,\n          ip_range_date    datetime     NOT NULL,\n          ip_range_length  int          NOT NULL,\n          PRIMARY KEY  (ip2country_id)\n        )");
      db_query("CREATE INDEX {ip2country}_index ON {ip2country} (country, registry)");
      break;
  }

  /* Populate the database */
  ip2country_update_database();
}

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

  /* Remove all database tables created by this module */
  db_query("DROP TABLE {ip2country}");

  /* Remove all module variables from the database */
  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_lookup');
  variable_del('ip2country_lookup_button');
  variable_del('ip2country_update_interval');
  variable_del('ip2country_update_database');
  variable_del('ip2country_watchdog');
}

/**
 * Implementation of hook_update().
 *
 */
function uc_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;
}

Functions

Namesort descending Description
uc_ip2country_install Implementation of hook_install().
uc_ip2country_uninstall Implementation of hook_uninstall().
uc_ip2country_update_1 Implementation of hook_update().