From 40e8f0260cdc2f025b377967d13ef19fcec0a98d Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 11 Nov 2013 17:15:32 +0000 Subject: [PATCH] MDL-42815 Backup/restore: Error saving files - log messages unhelpful This commit contains three changes in the three files: 1. A bug in the backup process meant that anything logged after a certain point did not appear in the on-screen display of the backup log, because the logger was serialised and deserialised but display code referred to the old version. Changed so that code retrieves new object. 2. Add more information to backup log when there is a missing file. 3. Add more information to restore log when there is a missing file (and remove existing code duplication of the current message). The 'missing file' situation is one that generally shouldn't occur in normal usage, but when it does happen, it is useful to have full information about the file. --- backup/backup.php | 7 ++++++ backup/moodle2/backup_custom_fields.php | 14 +++++++++++- backup/util/dbops/restore_dbops.class.php | 28 +++++++++++++++-------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/backup/backup.php b/backup/backup.php index 1c1cf29f6c1..0f3bf7add3f 100644 --- a/backup/backup.php +++ b/backup/backup.php @@ -130,6 +130,13 @@ if ($backup->get_stage() == backup_ui::STAGE_FINAL) { // Carry out actual backup. $backup->execute(); + // Backup controller gets saved/loaded so the logger object changes and we + // have to retrieve it. + $logger = $backup->get_controller()->get_logger(); + while (!is_a($logger, 'core_backup_html_logger')) { + $logger = $logger->get_next(); + } + // Get HTML from logger. $loghtml = $logger->get_html(); diff --git a/backup/moodle2/backup_custom_fields.php b/backup/moodle2/backup_custom_fields.php index 12929de6578..b5c32a5e84b 100644 --- a/backup/moodle2/backup_custom_fields.php +++ b/backup/moodle2/backup_custom_fields.php @@ -107,7 +107,19 @@ class file_nested_element extends backup_nested_element { backup_file_manager::copy_file_moodle2backup($this->backupid, $values); } catch (file_exception $e) { $this->add_result(array('missing_files_in_pool' => true)); - $this->add_log('missing file in pool: ' . $e->debuginfo, backup::LOG_WARNING); + + // Build helpful log message with all information necessary to identify + // file location. + $context = context::instance_by_id($values->contextid, IGNORE_MISSING); + $contextname = ''; + if ($context) { + $contextname = ' \'' . $context->get_context_name() . '\''; + } + $message = 'Missing file in pool: ' . $values->filepath . $values->filename . + ' (context ' . $values->contextid . $contextname . ', component ' . + $values->component . ', filearea ' . $values->filearea . ', itemid ' . + $values->itemid . ') [' . $e->debuginfo . ']'; + $this->add_log($message, backup::LOG_WARNING); } } } diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index a6d5bce169f..00fb8f3616f 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -995,11 +995,7 @@ abstract class restore_dbops { if ($includesfiles) { // The file is not found in the backup. if (!file_exists($backuppath)) { - $result = new stdClass(); - $result->code = 'file_missing_in_backup'; - $result->message = sprintf('missing file %s%s in backup', $file->filepath, $file->filename); - $result->level = backup::LOG_WARNING; - $results[] = $result; + $results[] = self::get_missing_file_result($file); continue; } @@ -1028,11 +1024,7 @@ abstract class restore_dbops { $fs->create_file_from_storedfile($file_record, $foundfile->id); } else { // A matching existing file record was not found in the database. - $result = new stdClass(); - $result->code = 'file_missing_in_backup'; - $result->message = sprintf('missing file %s%s in backup', $file->filepath, $file->filename); - $result->level = backup::LOG_WARNING; - $results[] = $result; + $results[] = self::get_missing_file_result($file); continue; } } @@ -1063,6 +1055,22 @@ abstract class restore_dbops { return $results; } + /** + * Returns suitable entry to include in log when there is a missing file. + * + * @param stdClass $file File definition + * @return stdClass Log entry + */ + protected static function get_missing_file_result($file) { + $result = new stdClass(); + $result->code = 'file_missing_in_backup'; + $result->message = 'Missing file in backup: ' . $file->filepath . $file->filename . + ' (old context ' . $file->contextid . ', component ' . $file->component . + ', filearea ' . $file->filearea . ', old itemid ' . $file->itemid . ')'; + $result->level = backup::LOG_WARNING; + return $result; + } + /** * Given one restoreid, create in DB all the users present * in backup_ids having newitemid = 0, as far as