Skip to content

Commit

Permalink
MDL-28346 Backup: Backup does not fail when a file is missing
Browse files Browse the repository at this point in the history
Conflicts:

	backup/util/ui/backup_ui_stage.class.php
  • Loading branch information
Frederic Massart committed Sep 3, 2012
1 parent 5ef242f commit aae03dd
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
9 changes: 7 additions & 2 deletions backup/moodle2/backup_custom_fields.php
Expand Up @@ -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);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions backup/util/plan/backup_structure_step.class.php
Expand Up @@ -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;
}

/**
Expand Down
73 changes: 73 additions & 0 deletions backup/util/structure/backup_nested_element.class.php
Expand Up @@ -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.
Expand All @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions backup/util/ui/backup_ui_stage.class.php
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions lang/en/backup.php
Expand Up @@ -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';
Expand Down

0 comments on commit aae03dd

Please sign in to comment.