You are here

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

Same filename and directory in other branches
  1. 6 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.
 */
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Url;

/**
 * Implements hook_schema().
 */
function ip2country_schema() {
  $schema['ip2country'] = [
    'description' => 'Association between IP range and Country',
    'fields' => [
      'ip2country_id' => [
        'description' => 'Row number (why is this needed?)',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'country' => [
        'description' => 'ISO 3166 2-character country code',
        'type' => 'char',
        'length' => 3,
        'not null' => TRUE,
      ],
      'registry' => [
        'description' => 'Regional Internet Registry',
        'type' => 'char',
        'length' => 10,
        'not null' => TRUE,
      ],
      'ip_range_first' => [
        'description' => 'Lowest IP address in range',
        'type' => 'int',
        'size' => 'big',
        'not null' => TRUE,
      ],
      'ip_range_last' => [
        'description' => 'Highest IP address in range',
        'type' => 'int',
        'size' => 'big',
        'not null' => TRUE,
      ],
      'ip_range_length' => [
        'description' => 'Size of IP address block',
        'type' => 'int',
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'country_registry' => [
        'country',
        'registry',
      ],
    ],
    'primary key' => [
      '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.
  //
  $populate = \Drupal::state()
    ->get('ip2country_populate_database_on_install');
  if (!isset($populate) || $populate) {
    $ip2country_config = \Drupal::config('ip2country.settings');
    $registry = $ip2country_config
      ->get('rir');
    $md5_checksum = $ip2country_config
      ->get('md5_checksum');
    $batch_size = $ip2country_config
      ->get('batch_size');
    \Drupal::service('ip2country.manager')
      ->updateDatabase($registry, $md5_checksum, $batch_size);
  }
}

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

  // Ensure temporary table is removed.
  \Drupal::database()
    ->schema()
    ->dropTable('ip2country_temp');

  // Remove state variables.
  \Drupal::state()
    ->delete('ip2country_populate_database_on_install');
  \Drupal::state()
    ->delete('ip2country_last_update');
  \Drupal::state()
    ->delete('ip2country_last_update_rir');

  // Remove date formats.
  $formats = DateFormat::loadMultiple([
    'ip2country_date',
    'ip2country_time',
  ]);
  if (!empty($formats)) {
    $format_names = [];
    foreach ($formats as $format) {
      $format_names[] = $format
        ->label();
    }
    \Drupal::messenger()
      ->addWarning(\Drupal::translation()
      ->formatPlural(count($format_names), 'The date format %format has not been deleted. If you need to delete it, <a href=":url">please do so manually</a>.', 'The date formats %formats have not been deleted. If you need to delete them, <a href=":url">please do so manually</a>.', [
      '%format' => $format_names[0],
      '%formats' => implode(', ', $format_names),
      ':url' => Url::fromRoute('entity.date_format.collection')
        ->toString(),
    ]));
  }
}

/**
 * Add batch_size to the default configuration.
 */
function ip2country_update_8101() {
  $config = \Drupal::configFactory()
    ->getEditable('ip2country.settings');
  $config
    ->set('batch_size', 200);
  $config
    ->save(TRUE);
}

Functions

Namesort descending Description
ip2country_install Implements hook_install().
ip2country_schema Implements hook_schema().
ip2country_uninstall Implements hook_uninstall().
ip2country_update_8101 Add batch_size to the default configuration.