You are here

function _demo_reset in Demonstration site (Sandbox / Snapshot) 6

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

Reset site using snapshot.

Parameters

$filename: Base snapshot filename, without extension.

$verbose: Whether to output status messages.

3 calls to _demo_reset()
demo_drush_reset in ./demo.drush.inc
Callback for drush command 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 227
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.
  @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 open dump file %filename.', array(
        '%filename' => $fileconfig['sqlfile'],
      )), 'error');
    }
    watchdog('demo', 'Unable to open dump 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);

  // 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 = db_prefix_tables('{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 = '';
  $new_line = TRUE;
  while (!feof($fp)) {

    // Better performance on PHP 5.2.x when leaving out buffer size to
    // fgets().
    $data = fgets($fp);
    if ($data === FALSE) {
      break;
    }

    // Skip empty lines (including lines that start with a comment).
    if ($new_line && ($data == "\n" || !strncmp($data, '--', 2) || !strncmp($data, '#', 1))) {
      continue;
    }
    $query .= $data;
    $len = strlen($data);
    if ($data[$len - 1] == "\n") {
      if ($data[$len - 2] == ';') {

        // Reached the end of a query, now execute it.
        if (!_db_query($query, FALSE)) {
          $success = FALSE;
        }
        $query = '';
      }
      $new_line = TRUE;
    }
    else {

      // Continue adding data from the same line.
      $new_line = FALSE;
    }
  }
  fclose($fp);
  if ($success) {
    if ($verbose) {
      drupal_set_message(t('Successfully restored database from %filename.', array(
        '%filename' => $fileconfig['sqlfile'],
      )));
    }
    watchdog('demo', 'Successfully restored database 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 restoring database from %filename.', array(
        '%filename' => $fileconfig['sqlfile'],
      )), 'error');
    }
    watchdog('demo', 'Failed restoring database 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', $_SERVER['REQUEST_TIME']);
  }
  return $success;
}