Skip to content

Commit

Permalink
MDL-33430 Do not restore aliases yet, stash them for later processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrd8mz committed Jun 21, 2012
1 parent 79b810f commit 4b6b087
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
13 changes: 2 additions & 11 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,22 +591,14 @@ protected function define_structure() {
}

/**
* Processing functions go here
* Process one <file> element from files.xml
*
* @param array $data one file record including repositoryid and reference
* @param array $data the element data
*/
public function process_file($data) {

$data = (object)$data; // handy

$isreference = !empty($data->repositoryid);
$issamesite = $this->task->is_samesite();

// If it's not samesite, we skip file refernces
if (!$issamesite && $isreference) {
return;
}

// load it if needed:
// - it it is one of the annotated inforef files (course/section/activity/block)
// - it is one "user", "group", "grouping", "grade", "question" or "qtype_xxxx" component file (that aren't sent to inforef ever)
Expand All @@ -617,7 +609,6 @@ public function process_file($data) {
$data->component == 'grouping' || $data->component == 'grade' ||
$data->component == 'question' || substr($data->component, 0, 5) == 'qtype');
if ($isfileref || $iscomponent) {
// Process files
restore_dbops::set_backup_files_record($this->get_restoreid(), $data);
}
}
Expand Down
93 changes: 65 additions & 28 deletions backup/util/dbops/restore_dbops.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,17 @@ 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
*/
public static function send_files_to_pool($basepath, $restoreid, $component, $filearea, $oldcontextid, $dfltuserid, $itemname = null, $olditemid = null, $forcenewcontextid = null, $skipparentitemidctxmatch = false) {
global $DB;
Expand Down Expand Up @@ -872,47 +883,73 @@ public static function send_files_to_pool($basepath, $restoreid, $component, $fi
foreach ($rs as $rec) {
$file = (object)unserialize(base64_decode($rec->info));

$isreference = !empty($file->repositoryid);

// ignore root dirs (they are created automatically)
if ($file->filepath == '/' && $file->filename == '.') {
continue;
}

// set the best possible user
$mappeduser = self::get_backup_ids_record($restoreid, 'user', $file->userid);
$file->userid = !empty($mappeduser) ? $mappeduser->newitemid : $dfltuserid;
// dir found (and not root one), let's create if
$mappeduserid = !empty($mappeduser) ? $mappeduser->newitemid : $dfltuserid;

// dir found (and not root one), let's create it
if ($file->filename == '.') {
$fs->create_directory($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->userid);
$fs->create_directory($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $mappeduserid);
continue;
}

// arrived here, file found
// Find file in backup pool
$backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);
if (empty($file->repositoryid)) {
// this is a regular file, it must be present in the backup pool
$backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);

if (!file_exists($backuppath) && !$isreference) {
throw new restore_dbops_exception('file_not_found_in_pool', $file);
}
if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
$file_record = array(
'contextid' => $newcontextid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $rec->newitemid,
'filepath' => $file->filepath,
'filename' => $file->filename,
'timecreated' => $file->timecreated,
'timemodified'=> $file->timemodified,
'userid' => $file->userid,
'author' => $file->author,
'license' => $file->license,
'sortorder' => $file->sortorder);
if ($isreference) {
$fs->create_file_from_reference($file_record, $file->repositoryid, $file->reference);
} else {
if (!file_exists($backuppath)) {
throw new restore_dbops_exception('file_not_found_in_pool', $file);
}

// create the file in the filepool if it does not exist yet
if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
$file_record = array(
'contextid' => $newcontextid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $rec->newitemid,
'filepath' => $file->filepath,
'filename' => $file->filename,
'timecreated' => $file->timecreated,
'timemodified'=> $file->timemodified,
'userid' => $mappeduserid,
'author' => $file->author,
'license' => $file->license,
'sortorder' => $file->sortorder
);
$fs->create_file_from_pathname($file_record, $backuppath);
}

} else {
// this is an alias - we can't create it yet so we stash it in a temp
// table and will let the final task to deal with it
if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
$info = new stdClass();
// oldfile holds the raw information stored in MBZ (including reference-related info)
$info->oldfile = $file;
// newfile holds the info for the new file_record with the context, user and itemid mapped
$info->newfile = (object)array(
'contextid' => $newcontextid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $rec->newitemid,
'filepath' => $file->filepath,
'filename' => $file->filename,
'timecreated' => $file->timecreated,
'timemodified'=> $file->timemodified,
'userid' => $mappeduserid,
'author' => $file->author,
'license' => $file->license,
'sortorder' => $file->sortorder
);

restore_dbops::set_backup_ids_record($restoreid, 'file_aliases_queue', $file->id, 0, null, $info);
}
}
}
$rs->close();
Expand Down

0 comments on commit 4b6b087

Please sign in to comment.