You are here

function _demo_reset in Demonstration site (Sandbox / Snapshot) 7

Same name and namespace in other branches
  1. 8 demo.module \_demo_reset()
  2. 6 demo.admin.inc \_demo_reset()

Reset site using snapshot.

Parameters

$filename: Base snapshot filename, without extension.

$verbose: Whether to output status messages.

2 calls to _demo_reset()
demo_reset in ./demo.module
Reset site using snapshot.
demo_reset_confirm_submit in ./demo.admin.inc
Form submit handler for demo_reset_confirm().

File

./demo.admin.inc, line 284
Demonstration Site administrative pages.

Code

function _demo_reset($filename, $verbose = TRUE) {

  // Load database specific functions.
  if (!demo_load_include()) {
    return FALSE;
  }

  // Increase PHP's max_execution_time for large dumps.
  drupal_set_time_limit(600);
  $fileconfig = demo_get_fileconfig($filename);
  if (!file_exists($fileconfig['sqlfile']) || !($fp = fopen($fileconfig['sqlfile'], 'r'))) {
    if ($verbose) {
      drupal_set_message(t('Unable to read file %filename.', array(
        '%filename' => $fileconfig['sqlfile'],
      )), 'error');
    }
    watchdog('demo', 'Unable to read file %filename.', array(
      '%filename' => $fileconfig['sqlfile'],
    ), WATCHDOG_ERROR);
    return FALSE;
  }

  // Load any database information in front of reset.
  $info = demo_get_info($fileconfig['infofile']);
  module_invoke_all('demo_reset_before', $filename, $info, $fileconfig);

  // Retain special variables, so the (demonstration) site keeps operating after
  // the reset. Specify NULL instead of default values, so unconfigured
  // variables are not retained, resp., deleted after the reset.
  $variables = array(
    // Without the snapshot path, subsequent resets will not work.
    'demo_dump_path' => variable_get('demo_dump_path', NULL),
  );

  // Temporarily disable foreign key checks for the time of import and before
  // dropping existing tables. Foreign key checks should already be re-enabled
  // as one of the last operations in the SQL dump file.
  // @see demo_dump_db()
  db_query("SET FOREIGN_KEY_CHECKS = 0;");

  // Drop tables.
  $is_version_1_0_dump = version_compare($info['version'], '1.1', '<');
  $watchdog = Database::getConnection()
    ->prefixTables('{watchdog}');
  foreach (demo_enum_tables() as $table => $dump_options) {

    // Skip watchdog, except for legacy dumps that included the watchdog table.
    if ($table != $watchdog || $is_version_1_0_dump) {
      db_query("DROP TABLE {$table}");
    }
  }

  // Load data from snapshot.
  $success = TRUE;
  $query = '';
  while (!feof($fp)) {
    $line = fgets($fp, 16384);
    if ($line && $line != "\n" && strncmp($line, '--', 2) && strncmp($line, '#', 1)) {
      $query .= $line;
      if (substr($line, -2) == ";\n") {
        $options = array(
          'target' => 'default',
          'return' => Database::RETURN_NULL,
        );
        $stmt = Database::getConnection($options['target'])
          ->prepare($query);
        if (!$stmt
          ->execute(array(), $options)) {
          if ($verbose) {

            // Don't use t() here, as the locale_* tables might not (yet) exist.
            drupal_set_message(strtr('Query failed: %query', array(
              '%query' => $query,
            )), 'error');
          }
          $success = FALSE;
        }
        $query = '';
      }
    }
  }
  fclose($fp);

  // Retain variables.
  foreach ($variables as $key => $value) {
    if (isset($value)) {
      variable_set($key, $value);
    }
    else {
      variable_del($key);
    }
  }
  if ($success) {
    if ($verbose) {
      drupal_set_message(t('Restored site from %filename.', array(
        '%filename' => $fileconfig['sqlfile'],
      )));
    }
    watchdog('demo', 'Restored site from %filename.', array(
      '%filename' => $fileconfig['sqlfile'],
    ), WATCHDOG_NOTICE);

    // Allow other modules to act on successful resets.
    module_invoke_all('demo_reset', $filename, $info, $fileconfig);
  }
  else {
    if ($verbose) {
      drupal_set_message(t('Failed to restore site from %filename.', array(
        '%filename' => $fileconfig['sqlfile'],
      )), 'error');
    }
    watchdog('demo', 'Failed to restore site from %filename.', array(
      '%filename' => $fileconfig['sqlfile'],
    ), WATCHDOG_ERROR);
  }

  // Save request time of last reset, but not during re-installation via
  // demo_profile.
  if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE !== 'install') {
    variable_set('demo_reset_last', REQUEST_TIME);
  }
  return $success;
}