You are here

function drush_ip2country_update in IP-based Determination of a Visitor's Country 8

Same name and namespace in other branches
  1. 7 ip2country.drush.inc \drush_ip2country_update()

Implements drush_hook_COMMAND() for the ip2country-update command.

Updates the database from the selected registry.

File

./ip2country.drush.inc, line 138
ip2country module integration with Drush 8 and earlier.

Code

function drush_ip2country_update() {
  $ip2country_config = \Drupal::config('ip2country.settings');
  $watchdog = $ip2country_config
    ->get('watchdog');
  $registry = (string) drush_get_option('registry', $ip2country_config
    ->get('rir'));
  $md5_checksum = (bool) drush_get_option('md5', $ip2country_config
    ->get('md5_checksum'));
  $batch_size = (int) drush_get_option('batch_size', $ip2country_config
    ->get('batch_size'));
  drush_print(dt('Updating ... '), 0, NULL, FALSE);
  $status = \Drupal::service('ip2country.manager')
    ->updateDatabase($registry, $md5_checksum, $batch_size);
  if ($status != FALSE) {
    drush_print(dt('Completed.'));
    drush_print(dt('Database updated from @registry server. Table contains @rows rows.', [
      '@registry' => mb_strtoupper($registry),
      '@rows' => $status,
    ]));

    // Log update to watchdog, if ip2country logging is enabled.
    if ($watchdog) {
      \Drupal::logger('ip2country')
        ->notice('Drush-initiated database update from @registry server.', [
        '@registry' => mb_strtoupper($registry),
      ]);
    }
  }
  else {
    drush_print(dt('Failed.'));
    drush_print(dt('Database update from @registry server FAILED.', [
      '@registry' => mb_strtoupper($registry),
    ]));

    // Log update failure to watchdog, if ip2country logging is enabled.
    if ($watchdog) {
      \Drupal::logger('ip2country')
        ->warning('Drush-initiated database update from @registry server FAILED.', [
        '@registry' => mb_strtoupper($registry),
      ]);
    }
  }
}