Commit 9d2a6850 authored by smk-ka's avatar smk-ka

by smk-ka: Fixed a bug in demo_reset() where long lines could be partially...

by smk-ka: Fixed a bug in demo_reset() where long lines could be partially crippled, resulting in broken imports. This happened when a chunk of data in the middle of long line started with a comment sign, causing the import function to discard it.
parent 18035c24
......@@ -209,64 +209,81 @@ function demo_reset_confirm_submit($form_id, $values) {
}
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);
$fileconfig = demo_get_fileconfig($filename);
$version = demo_get_info($fileconfig['infofile'], 'version');
$is_version_1_0_dump = version_compare($version, '1.1', '<');
if (file_exists($fileconfig['sqlfile']) && $fp = fopen($fileconfig['sqlfile'], 'r')) {
// 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);
}
// 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
$query = '';
$success = TRUE;
while (!feof($fp)) {
$line = fgets($fp, 16384);
if ($line && $line != "\n" && strncmp($line, '--', 2) && strncmp($line, '#', 1)) {
$query .= $line;
if (substr($line, -2) == ";\n") {
if (!_db_query($query, FALSE)) {
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 = '';
}
}
// 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;
}
fclose($fp);
if ($success) {
$message = t('Successfully restored database from %filename.', array('%filename' => $fileconfig['sqlfile']));
$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 {
$message = t('Failed restoring database from %filename.', array('%filename' => $fileconfig['sqlfile']));
}
if ($verbose) {
drupal_set_message($message);
// Continue adding data from the same line.
$new_line = FALSE;
}
watchdog('demo', $message, $success ? WATCHDOG_NOTICE : WATCHDOG_ERROR);
}
fclose($fp);
// Reset default dump to load on cron.
variable_set('demo_dump_cron', $demo_dump_cron);
return TRUE;
if ($success) {
$message = t('Successfully restored database from %filename.', array('%filename' => $fileconfig['sqlfile']));
}
else {
$message = t('Failed restoring database from %filename.', array('%filename' => $fileconfig['sqlfile']));
}
$message = t('Unable to open dump file %filename.', array('%filename' => $fileconfig['sqlfile']));
if ($verbose) {
drupal_set_message($message, 'error');
drupal_set_message($message, $success ? 'status' : 'error');
}
watchdog('demo', $message, WATCHDOG_ERROR);
return FALSE;
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;
}
function demo_get_fileconfig($filename = 'demo_site') {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment