You are here

function demo_reset in Demonstration site (Sandbox / Snapshot) 5

Same name and namespace in other branches
  1. 6 demo.module \demo_reset()
  2. 7 demo.module \demo_reset()
2 calls to demo_reset()
demo_cron in ./demo.module
Implementation of hook_cron().
demo_reset_confirm_submit in ./demo.admin.inc

File

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

Code

function demo_reset($filename = 'demo_site', $verbose = TRUE) {
  $fileconfig = demo_get_fileconfig($filename);
  if (!file_exists($fileconfig['sqlfile']) || !($fp = fopen($fileconfig['sqlfile'], 'r'))) {
    $message = t('Unable to open dump file %filename.', array(
      '%filename' => $fileconfig['sqlfile'],
    ));
    if ($verbose) {
      drupal_set_message($message, 'error');
    }
    watchdog('demo', $message, WATCHDOG_ERROR);
    return FALSE;
  }

  // Load any database information in front of reset.
  $demo_dump_cron = variable_get('demo_dump_cron', $filename);
  $version = demo_get_info($fileconfig['infofile'], 'version');
  $is_version_1_0_dump = version_compare($version, '1.1', '<');

  // Drop tables
  $dt_watchdog = db_prefix_tables('{watchdog}');
  foreach (demo_enum_tables() as $table) {

    // Skip watchdog, except for legacy dumps that included the watchdog table
    if ($table != $dt_watchdog || $is_version_1_0_dump) {
      db_query("DROP TABLE %s", $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) {

    // Allow other modules to act on successful resets.
    module_invoke_all('demo_reset');
    $message = t('Successfully restored database from %filename.', array(
      '%filename' => $fileconfig['sqlfile'],
    ));
  }
  else {
    $message = t('Failed restoring database from %filename.', array(
      '%filename' => $fileconfig['sqlfile'],
    ));
  }
  if ($verbose) {
    drupal_set_message($message, $success ? 'status' : 'error');
  }
  watchdog('demo', $message, $success ? WATCHDOG_NOTICE : WATCHDOG_ERROR);

  // Reset default dump to load on cron.
  variable_set('demo_dump_cron', $demo_dump_cron);
  return $success;
}