From 4b6b087fafe55d8d28b8a1b024db2e977dfa032e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 19 Jun 2012 16:56:33 +0200 Subject: [PATCH] MDL-33430 Do not restore aliases yet, stash them for later processing --- backup/moodle2/restore_stepslib.php | 13 +--- backup/util/dbops/restore_dbops.class.php | 93 ++++++++++++++++------- 2 files changed, 67 insertions(+), 39 deletions(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 60a4086d6c8bd..9bc8d436cf3fb 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -591,22 +591,14 @@ protected function define_structure() { } /** - * Processing functions go here + * Process one 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) @@ -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); } } diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 4254e698e90a0..de3ff5fd06b56 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -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; @@ -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();