MDL-33430 Do not restore aliases yet, stash them for later processing
authorDavid Mudrák <david@moodle.com>
Tue, 19 Jun 2012 14:56:33 +0000 (16:56 +0200)
committerDavid Mudrák <david@moodle.com>
Thu, 21 Jun 2012 00:29:34 +0000 (02:29 +0200)
backup/moodle2/restore_stepslib.php
backup/util/dbops/restore_dbops.class.php

index 60a4086..9bc8d43 100644 (file)
@@ -591,22 +591,14 @@ class restore_load_included_files extends restore_structure_step {
     }
 
     /**
-     * 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)
@@ -617,7 +609,6 @@ class restore_load_included_files extends restore_structure_step {
                         $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);
         }
     }
index 4254e69..de3ff5f 100644 (file)
@@ -808,6 +808,17 @@ abstract class restore_dbops {
      * 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 @@ abstract class restore_dbops {
         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();