From aae03dd4d4fa5aa88f93144463fcf2f2a8c90d0c Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 9 Jul 2012 10:49:25 +0800 Subject: [PATCH] MDL-28346 Backup: Backup does not fail when a file is missing Conflicts: backup/util/ui/backup_ui_stage.class.php --- backup/moodle2/backup_custom_fields.php | 9 ++- .../util/plan/backup_structure_step.class.php | 11 +++ .../structure/backup_nested_element.class.php | 73 +++++++++++++++++++ backup/util/ui/backup_ui_stage.class.php | 3 + lang/en/backup.php | 1 + 5 files changed, 95 insertions(+), 2 deletions(-) diff --git a/backup/moodle2/backup_custom_fields.php b/backup/moodle2/backup_custom_fields.php index d899a65ef32ca..97ff94ab61dc8 100644 --- a/backup/moodle2/backup_custom_fields.php +++ b/backup/moodle2/backup_custom_fields.php @@ -91,14 +91,19 @@ public function process($processor) { if (is_null($this->backupid)) { $this->backupid = $processor->get_var(backup::VAR_BACKUPID); } - parent::process($processor); + return parent::process($processor); } public function fill_values($values) { // Fill values parent::fill_values($values); // Do our own tasks (copy file from moodle to backup) - backup_file_manager::copy_file_moodle2backup($this->backupid, $values); + try { + 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); + } } } diff --git a/backup/util/plan/backup_structure_step.class.php b/backup/util/plan/backup_structure_step.class.php index f62fee936a193..964dd3f9885de 100644 --- a/backup/util/plan/backup_structure_step.class.php +++ b/backup/util/plan/backup_structure_step.class.php @@ -94,11 +94,22 @@ public function execute() { // Process structure definition $structure->process($pr); + // Get the results from the nested elements + $results = $structure->get_results(); + + // Get the log messages to append to the log + $logs = $structure->get_logs(); + foreach ($logs as $log) { + $this->log($log->message, $log->level, $log->a, $log->depth, $log->display); + } + // Close everything $xw->stop(); // Destroy the structure. It helps PHP 5.2 memory a lot! $structure->destroy(); + + return $results; } /** diff --git a/backup/util/structure/backup_nested_element.class.php b/backup/util/structure/backup_nested_element.class.php index 9a479013d91f8..8557ec8ae5335 100644 --- a/backup/util/structure/backup_nested_element.class.php +++ b/backup/util/structure/backup_nested_element.class.php @@ -37,6 +37,8 @@ class backup_nested_element extends base_nested_element implements processable { protected $aliases; // Define DB->final element aliases protected $fileannotations; // array of file areas to be searched by file annotations protected $counter; // Number of instances of this element that have been processed + protected $results; // Logs the results we encounter during the process. + protected $logs; // Some log messages that could be retrieved later. /** * Constructor - instantiates one backup_nested_element, specifying its basic info. @@ -55,8 +57,16 @@ public function __construct($name, $attributes = null, $final_elements = null) { $this->aliases = array(); $this->fileannotations = array(); $this->counter = 0; + $this->results = array(); + $this->logs = array(); } + /** + * Process the nested element + * + * @param object $processor the processor + * @return void + */ public function process($processor) { if (!$processor instanceof base_processor) { // No correct processor, throw exception throw new base_element_struct_exception('incorrect_processor'); @@ -113,6 +123,69 @@ public function process($processor) { $iterator->close(); } + /** + * Saves a log message to an array + * + * @see backup_helper::log() + * @param string $message to add to the logs + * @param int $level level of importance {@link backup::LOG_DEBUG} and other constants + * @param mixed $a to be included in $message + * @param int $depth of the message + * @param display $bool supporting translation via get_string() if true + * @return void + */ + protected function add_log($message, $level, $a = null, $depth = null, $display = false) { + // Adding the result to the oldest parent. + if ($this->get_parent()) { + $parent = $this->get_grandparent(); + $parent->add_log($message, $level, $a, $depth, $display); + } else { + $log = new stdClass(); + $log->message = $message; + $log->level = $level; + $log->a = $a; + $log->depth = $depth; + $log->display = $display; + $this->logs[] = $log; + } + } + + /** + * Saves the results to an array + * + * @param array $result associative array + * @return void + */ + protected function add_result($result) { + if (is_array($result)) { + // Adding the result to the oldest parent. + if ($this->get_parent()) { + $parent = $this->get_grandparent(); + $parent->add_result($result); + } else { + $this->results = array_merge($this->results, $result); + } + } + } + + /** + * Returns the logs + * + * @return array of log objects + */ + public function get_logs() { + return $this->logs; + } + + /** + * Returns the results + * + * @return associative array of results + */ + public function get_results() { + return $this->results; + } + public function set_source_array($arr) { // TODO: Only elements having final elements can set source $this->var_array = $arr; diff --git a/backup/util/ui/backup_ui_stage.class.php b/backup/util/ui/backup_ui_stage.class.php index 02514497a914f..56a1a48f99026 100644 --- a/backup/util/ui/backup_ui_stage.class.php +++ b/backup/util/ui/backup_ui_stage.class.php @@ -469,6 +469,9 @@ public function display() { } echo $OUTPUT->box_start(); + if (!empty($this->results['missing_files_in_pool'])) { + echo $OUTPUT->notification(get_string('missingfilesinpool', 'backup'), 'notifyproblem'); + } echo $OUTPUT->notification(get_string('executionsuccess', 'backup'), 'notifysuccess'); echo $OUTPUT->continue_button($restorerul); echo $OUTPUT->box_end(); diff --git a/lang/en/backup.php b/lang/en/backup.php index 4f41d7bcded30..6b61aa356d4c0 100644 --- a/lang/en/backup.php +++ b/lang/en/backup.php @@ -153,6 +153,7 @@ $string['lockedbyconfig'] = 'This setting has been locked by the default backup settings'; $string['lockedbyhierarchy'] = 'Locked by dependencies'; $string['managefiles'] = 'Manage backup files'; +$string['missingfilesinpool'] = 'Some files could not be saved during the backup, it won\'t be possible to restore them.'; $string['moodleversion'] = 'Moodle version'; $string['moreresults'] = 'There are too many results, enter a more specific search.'; $string['nomatchingcourses'] = 'There are no courses to display';