Skip to content

Commit

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

	backup/util/dbops/restore_dbops.class.php
  • Loading branch information
Frederic Massart committed Sep 3, 2012
1 parent b3ab3bd commit 5ef242f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
22 changes: 21 additions & 1 deletion backup/util/dbops/restore_dbops.class.php
Expand Up @@ -622,10 +622,24 @@ public static function restore_get_questions($restoreid, $qcatid) {
* Given one component/filearea/context and
* optionally one source itemname to match itemids
* put the corresponding files in the pool
*
* @param string $basepath the full path to the root of unzipped backup file
* @param string $restoreid the restore job's identification
* @param string $component
* @param string $filearea
* @param int $oldcontextid
* @param int $dfltuserid default $file->user if the old one can't be mapped
* @param string|null $itemname
* @param int|null $olditemid
* @param int|null $forcenewcontextid explicit value for the new contextid (skip mapping)
* @param bool $skipparentitemidctxmatch
* @return array of result object
*/
public static function send_files_to_pool($basepath, $restoreid, $component, $filearea, $oldcontextid, $dfltuserid, $itemname = null, $olditemid = null, $forcenewcontextid = null, $skipparentitemidctxmatch = false) {
global $DB;

$results = array();

if ($forcenewcontextid) {
// Some components can have "forced" new contexts (example: questions can end belonging to non-standard context mappings,
// with questions originally at system/coursecat context in source being restored to course context in target). So we need
Expand Down Expand Up @@ -701,7 +715,12 @@ public static function send_files_to_pool($basepath, $restoreid, $component, $fi
// Find file in backup pool
$backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);
if (!file_exists($backuppath)) {
throw new restore_dbops_exception('file_not_found_in_pool', $file);
$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;
continue;
}
if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
$file_record = array(
Expand All @@ -721,6 +740,7 @@ public static function send_files_to_pool($basepath, $restoreid, $component, $fi
}
}
$rs->close();
return $results;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions backup/util/plan/restore_structure_step.class.php
Expand Up @@ -218,8 +218,14 @@ public function get_mapping($itemname, $oldid) {
*/
public function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
$filesctxid = is_null($filesctxid) ? $this->task->get_old_contextid() : $filesctxid;
restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), $component,
$filearea, $filesctxid, $this->task->get_userid(), $mappingitemname, $olditemid);
$results = restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), $component,
$filearea, $filesctxid, $this->task->get_userid(), $mappingitemname, $olditemid);
$resultstoadd = array();
foreach ($results as $result) {
$this->log($result->message, $result->level);
$resultstoadd[$result->code] = true;
}
$this->task->add_result($resultstoadd);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions backup/util/ui/restore_ui_stage.class.php
Expand Up @@ -757,6 +757,9 @@ public function display(core_backup_renderer $renderer) {

$html = '';
$html .= $renderer->box_start();
if (array_key_exists('file_missing_in_backup', $this->results)) {
$html .= $renderer->notification(get_string('restorefileweremissing', 'backup'), 'notifyproblem');
}
$html .= $renderer->notification(get_string('restoreexecutionsuccess', 'backup'), 'notifysuccess');
$html .= $renderer->continue_button(new moodle_url('/course/view.php', array(
'id' => $this->get_ui()->get_controller()->get_courseid())), 'get');
Expand Down
1 change: 1 addition & 0 deletions lang/en/backup.php
Expand Up @@ -167,6 +167,7 @@
$string['restorecourse'] = 'Restore course';
$string['restorecoursesettings'] = 'Course settings';
$string['restoreexecutionsuccess'] = 'The course was restored successfully, clicking the continue button below will take you to view the course you restored.';
$string['restorefileweremissing'] = 'Some files could not be restored because they were missing in the backup.';
$string['restorenewcoursefullname'] = 'New course name';
$string['restorenewcourseshortname'] = 'New course short name';
$string['restorenewcoursestartdate'] = 'New start date';
Expand Down

0 comments on commit 5ef242f

Please sign in to comment.