protected function dispatch_chunk($data)
 {
     // Prepare question_category record
     if ($data['path'] == '/question_categories/question_category') {
         $info = (object) $data['tags'];
         $itemname = 'question_category';
         $itemid = $info->id;
         $parentitemid = $info->contextid;
         $this->lastcatid = $itemid;
         // Prepare question record
     } else {
         if ($data['path'] == '/question_categories/question_category/questions/question') {
             $info = (object) $data['tags'];
             $itemname = 'question';
             $itemid = $info->id;
             $parentitemid = $this->lastcatid;
             // Not question_category nor question, impossible. Throw exception.
         } else {
             throw new progressive_parser_exception('restore_questions_parser_processor_unexpected_path', $data['path']);
         }
     }
     // Only load it if needed (exist same question_categoryref itemid in table)
     if (restore_dbops::get_backup_ids_record($this->restoreid, 'question_categoryref', $this->lastcatid)) {
         restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid, 0, $parentitemid, $info);
     }
 }
 protected function dispatch_chunk($data)
 {
     // Received one inforef chunck, we are going to store it into backup_ids
     // table, with name = itemname + "ref" for later use
     $itemname = basename($data['path']) . 'ref';
     $itemid = $data['tags']['id'];
     restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid);
 }
 protected function dispatch_chunk($data)
 {
     // Received one role chunck, we are going to store it into backup_ids
     // table, with name = role
     $itemname = 'role';
     $itemid = $data['tags']['id'];
     $info = $data['tags'];
     // Only load it if needed (exist same roleref itemid in table)
     if (restore_dbops::get_backup_ids_record($this->restoreid, 'roleref', $itemid)) {
         restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid, 0, null, $info);
     }
 }
 protected function dispatch_chunk($data)
 {
     // Received one user chunck, we are going to store it into backup_ids
     // table, with name = user and parentid = contextid for later use
     $itemname = 'user';
     $itemid = $data['tags']['id'];
     $parentitemid = $data['tags']['contextid'];
     $info = $data['tags'];
     // Only load it if needed (exist same userref itemid in table)
     if (restore_dbops::get_backup_ids_record($this->restoreid, 'userref', $itemid)) {
         restore_dbops::set_backup_ids_record($this->restoreid, $itemname, $itemid, 0, $parentitemid, $info);
     }
 }
Exemplo n.º 5
0
 protected function define_execution()
 {
     global $DB;
     $contexts = restore_dbops::restore_get_question_banks($this->get_restoreid(), CONTEXT_MODULE);
     foreach ($contexts as $contextid => $contextlevel) {
         // Only if context mapping exists (i.e. the module has been restored)
         if ($newcontext = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'context', $contextid)) {
             // Update all the qcats having their parentitemid set to the original contextid
             $modulecats = $DB->get_records_sql("SELECT itemid, newitemid\n                                                      FROM {backup_ids_temp}\n                                                     WHERE backupid = ?\n                                                       AND itemname = 'question_category'\n                                                       AND parentitemid = ?", array($this->get_restoreid(), $contextid));
             foreach ($modulecats as $modulecat) {
                 $DB->set_field('question_categories', 'contextid', $newcontext->newitemid, array('id' => $modulecat->newitemid));
                 // And set new contextid also in question_category mapping (will be
                 // used by {@link restore_create_question_files} later
                 restore_dbops::set_backup_ids_record($this->get_restoreid(), 'question_category', $modulecat->itemid, $modulecat->newitemid, $newcontext->newitemid);
             }
         }
     }
 }
 /**
  * To send ids pairs to backup_ids_table and to store them into paths
  *
  * This method will send the given itemname and old/new ids to the
  * backup_ids_temp table, and, at the same time, will save the new id
  * into the corresponding restore_path_element for easier access
  * by children. Also will inject the known old context id for the task
  * in case it's going to be used for restoring files later
  */
 public function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null)
 {
     if ($restorefiles && $parentid) {
         throw new restore_step_exception('set_mapping_cannot_specify_both_restorefiles_and_parentitemid');
     }
     // If we haven't specified one context for the files, use the task one
     if (is_null($filesctxid)) {
         $parentitemid = $restorefiles ? $this->task->get_old_contextid() : null;
     } else {
         // Use the specified one
         $parentitemid = $restorefiles ? $filesctxid : null;
     }
     // We have passed one explicit parentid, apply it
     $parentitemid = !is_null($parentid) ? $parentid : $parentitemid;
     // Let's call the low level one
     restore_dbops::set_backup_ids_record($this->get_restoreid(), $itemname, $oldid, $newid, $parentitemid);
     // Now, if the itemname matches any pathelement->name, store the latest $newid
     if (array_key_exists($itemname, $this->elementsoldid)) {
         // If present in  $this->elementsoldid, is valid, put both ids
         $this->elementsoldid[$itemname] = $oldid;
         $this->elementsnewid[$itemname] = $newid;
     }
 }
Exemplo n.º 7
0
    /**
     * Stores some information for later processing
     *
     * This implementation uses backup_ids_temp table to store data. Make
     * sure that the $stashname + $itemid combo is unique.
     *
     * @param string $stashname name of the stash
     * @param mixed $info information to stash
     * @param int $itemid optional id for multiple infos within the same stashname
     */
    public function set_stash($stashname, $info, $itemid = 0) {
        try {
            restore_dbops::set_backup_ids_record($this->get_id(), $stashname, $itemid, 0, null, $info);

        } catch (dml_exception $e) {
            throw new moodle1_convert_storage_exception('unable_to_restore_stash', null, $e->getMessage());
        }
    }
Exemplo n.º 8
0
 /**
  * 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
         // to be able to force the new contextid
         $newcontextid = $forcenewcontextid;
     } else {
         // Get new context, must exist or this will fail
         if (!($newcontextid = self::get_backup_ids_record($restoreid, 'context', $oldcontextid)->newitemid)) {
             throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid);
         }
     }
     // Sometimes it's possible to have not the oldcontextids stored into backup_ids_temp->parentitemid
     // columns (because we have used them to store other information). This happens usually with
     // all the question related backup_ids_temp records. In that case, it's safe to ignore that
     // matching as far as we are always restoring for well known oldcontexts and olditemids
     $parentitemctxmatchsql = ' AND i.parentitemid = f.contextid ';
     if ($skipparentitemidctxmatch) {
         $parentitemctxmatchsql = '';
     }
     // Important: remember how files have been loaded to backup_files_temp
     //   - info: contains the whole original object (times, names...)
     //   (all them being original ids as loaded from xml)
     // itemname = null, we are going to match only by context, no need to use itemid (all them are 0)
     if ($itemname == null) {
         $sql = "SELECT id AS bftid, contextid, component, filearea, itemid, itemid AS newitemid, info\n                      FROM {backup_files_temp}\n                     WHERE backupid = ?\n                       AND contextid = ?\n                       AND component = ?\n                       AND filearea  = ?";
         $params = array($restoreid, $oldcontextid, $component, $filearea);
         // itemname not null, going to join with backup_ids to perform the old-new mapping of itemids
     } else {
         $sql = "SELECT f.id AS bftid, f.contextid, f.component, f.filearea, f.itemid, i.newitemid, f.info\n                      FROM {backup_files_temp} f\n                      JOIN {backup_ids_temp} i ON i.backupid = f.backupid\n                                              {$parentitemctxmatchsql}\n                                              AND i.itemid = f.itemid\n                     WHERE f.backupid = ?\n                       AND f.contextid = ?\n                       AND f.component = ?\n                       AND f.filearea = ?\n                       AND i.itemname = ?";
         $params = array($restoreid, $oldcontextid, $component, $filearea, $itemname);
         if ($olditemid !== null) {
             // Just process ONE olditemid intead of the whole itemname
             $sql .= ' AND i.itemid = ?';
             $params[] = $olditemid;
         }
     }
     $fs = get_file_storage();
     // Get moodle file storage
     $basepath = $basepath . '/files/';
     // Get backup file pool base
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $rec) {
         $file = (object) unserialize(base64_decode($rec->info));
         // 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);
         $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, $mappeduserid);
             continue;
         }
         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);
             // The file is not found in the backup.
             if (!file_exists($backuppath)) {
                 $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;
             }
             // 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);
             }
             // store the the new contextid and the new itemid in case we need to remap
             // references to this file later
             $DB->update_record('backup_files_temp', array('id' => $rec->bftid, 'newcontextid' => $newcontextid, 'newitemid' => $rec->newitemid), true);
         } 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();
     return $results;
 }
Exemplo n.º 9
0
 /**
  * Verify the xxx_ids_cached (in-memory backup_ids cache) stuff works as expected.
  *
  * Note that those private implementations are tested here by using the public
  * backup_ids API and later performing low-level tests.
  */
 public function test_backup_ids_cached()
 {
     global $DB;
     $dbman = $DB->get_manager();
     // We are going to use database_manager services.
     $this->resetAfterTest(true);
     // Playing with temp tables, better reset once finished.
     // Some variables and objects for testing.
     $restoreid = 'testrestoreid';
     $mapping = new stdClass();
     $mapping->itemname = 'user';
     $mapping->itemid = 1;
     $mapping->newitemid = 2;
     $mapping->parentitemid = 3;
     $mapping->info = 'info';
     // Create the backup_ids temp tables used by restore.
     restore_controller_dbops::create_restore_temp_tables($restoreid);
     // Send one mapping using the public api with defaults.
     restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
     // Get that mapping and verify everything is returned as expected.
     $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
     $this->assertSame($mapping->itemname, $result->itemname);
     $this->assertSame($mapping->itemid, $result->itemid);
     $this->assertSame(0, $result->newitemid);
     $this->assertSame(null, $result->parentitemid);
     $this->assertSame(null, $result->info);
     // Drop the backup_xxx_temp temptables manually, so memory cache won't be invalidated.
     $dbman->drop_table(new xmldb_table('backup_ids_temp'));
     $dbman->drop_table(new xmldb_table('backup_files_temp'));
     // Verify the mapping continues returning the same info,
     // now from cache (the table does not exist).
     $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
     $this->assertSame($mapping->itemname, $result->itemname);
     $this->assertSame($mapping->itemid, $result->itemid);
     $this->assertSame(0, $result->newitemid);
     $this->assertSame(null, $result->parentitemid);
     $this->assertSame(null, $result->info);
     // Recreate the temp table, just to drop it using the restore API in
     // order to check that, then, the cache becomes invalid for the same request.
     restore_controller_dbops::create_restore_temp_tables($restoreid);
     restore_controller_dbops::drop_restore_temp_tables($restoreid);
     // No cached info anymore, so the mapping request will arrive to
     // DB leading to error (temp table does not exist).
     try {
         $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
         $this->fail('Expecting an exception, none occurred');
     } catch (Exception $e) {
         $this->assertTrue($e instanceof dml_exception);
         $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
     }
     // Create the backup_ids temp tables once more.
     restore_controller_dbops::create_restore_temp_tables($restoreid);
     // Send one mapping using the public api with complete values.
     restore_dbops::set_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid, $mapping->newitemid, $mapping->parentitemid, $mapping->info);
     // Get that mapping and verify everything is returned as expected.
     $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
     $this->assertSame($mapping->itemname, $result->itemname);
     $this->assertSame($mapping->itemid, $result->itemid);
     $this->assertSame($mapping->newitemid, $result->newitemid);
     $this->assertSame($mapping->parentitemid, $result->parentitemid);
     $this->assertSame($mapping->info, $result->info);
     // Finally, drop the temp tables properly and get the DB error again (memory caches empty).
     restore_controller_dbops::drop_restore_temp_tables($restoreid);
     try {
         $result = restore_dbops::get_backup_ids_record($restoreid, $mapping->itemname, $mapping->itemid);
         $this->fail('Expecting an exception, none occurred');
     } catch (Exception $e) {
         $this->assertTrue($e instanceof dml_exception);
         $this->assertSame('Table "backup_ids_temp" does not exist', $e->getMessage());
     }
 }
Exemplo n.º 10
0
 /**
  * Given one component/filearea/context and
  * optionally one source itemname to match itemids
  * put the corresponding files in the pool
  *
  * If you specify a progress reporter, it will get called once per file with
  * indeterminate progress.
  *
  * @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
  * @param \core\progress\base $progress Optional progress reporter
  * @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, \core\progress\base $progress = null)
 {
     global $DB, $CFG;
     $backupinfo = backup_general_helper::get_backup_information(basename($basepath));
     $includesfiles = $backupinfo->include_files;
     $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
         // to be able to force the new contextid
         $newcontextid = $forcenewcontextid;
     } else {
         // Get new context, must exist or this will fail
         $newcontextrecord = self::get_backup_ids_record($restoreid, 'context', $oldcontextid);
         if (!$newcontextrecord || !$newcontextrecord->newitemid) {
             throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid);
         }
         $newcontextid = $newcontextrecord->newitemid;
     }
     // Sometimes it's possible to have not the oldcontextids stored into backup_ids_temp->parentitemid
     // columns (because we have used them to store other information). This happens usually with
     // all the question related backup_ids_temp records. In that case, it's safe to ignore that
     // matching as far as we are always restoring for well known oldcontexts and olditemids
     $parentitemctxmatchsql = ' AND i.parentitemid = f.contextid ';
     if ($skipparentitemidctxmatch) {
         $parentitemctxmatchsql = '';
     }
     // Important: remember how files have been loaded to backup_files_temp
     //   - info: contains the whole original object (times, names...)
     //   (all them being original ids as loaded from xml)
     // itemname = null, we are going to match only by context, no need to use itemid (all them are 0)
     if ($itemname == null) {
         $sql = "SELECT id AS bftid, contextid, component, filearea, itemid, itemid AS newitemid, info\n                      FROM {backup_files_temp}\n                     WHERE backupid = ?\n                       AND contextid = ?\n                       AND component = ?\n                       AND filearea  = ?";
         $params = array($restoreid, $oldcontextid, $component, $filearea);
         // itemname not null, going to join with backup_ids to perform the old-new mapping of itemids
     } else {
         $sql = "SELECT f.id AS bftid, f.contextid, f.component, f.filearea, f.itemid, i.newitemid, f.info\n                      FROM {backup_files_temp} f\n                      JOIN {backup_ids_temp} i ON i.backupid = f.backupid\n                                              {$parentitemctxmatchsql}\n                                              AND i.itemid = f.itemid\n                     WHERE f.backupid = ?\n                       AND f.contextid = ?\n                       AND f.component = ?\n                       AND f.filearea = ?\n                       AND i.itemname = ?";
         $params = array($restoreid, $oldcontextid, $component, $filearea, $itemname);
         if ($olditemid !== null) {
             // Just process ONE olditemid intead of the whole itemname
             $sql .= ' AND i.itemid = ?';
             $params[] = $olditemid;
         }
     }
     $fs = get_file_storage();
     // Get moodle file storage
     $basepath = $basepath . '/files/';
     // Get backup file pool base
     // Report progress before query.
     if ($progress) {
         $progress->progress();
     }
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $rec) {
         // Report progress each time around loop.
         if ($progress) {
             $progress->progress();
         }
         $file = (object) backup_controller_dbops::decode_backup_temp_info($rec->info);
         // 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);
         $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, $mappeduserid);
             continue;
         }
         // The file record to restore.
         $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, 'source' => $file->source, 'author' => $file->author, 'license' => $file->license, 'sortorder' => $file->sortorder);
         if (empty($file->repositoryid)) {
             // If contenthash is empty then gracefully skip adding file.
             if (empty($file->contenthash)) {
                 $result = new stdClass();
                 $result->code = 'file_missing_in_backup';
                 $result->message = sprintf('missing file (%s) contenthash in backup for component %s', $file->filename, $component);
                 $result->level = backup::LOG_WARNING;
                 $results[] = $result;
                 continue;
             }
             // 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);
             // Some file types do not include the files as they should already be
             // present. We still need to create entries into the files table.
             if ($includesfiles) {
                 // The file is not found in the backup.
                 if (!file_exists($backuppath)) {
                     $results[] = self::get_missing_file_result($file);
                     continue;
                 }
                 // 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)) {
                     // If no license found, use default.
                     if ($file->license == null) {
                         $file->license = $CFG->sitedefaultlicense;
                     }
                     $fs->create_file_from_pathname($file_record, $backuppath);
                 }
             } else {
                 // This backup does not include the files - they should be available in moodle filestorage already.
                 // 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)) {
                     // Even if a file has been deleted since the backup was made, the file metadata will remain in the
                     // files table, and the file will not be moved to the trashdir.
                     // Files are not cleared from the files table by cron until several days after deletion.
                     if ($foundfiles = $DB->get_records('files', array('contenthash' => $file->contenthash), '', '*', 0, 1)) {
                         // Only grab one of the foundfiles - the file content should be the same for all entries.
                         $foundfile = reset($foundfiles);
                         $fs->create_file_from_storedfile($file_record, $foundfile->id);
                     } else {
                         // A matching existing file record was not found in the database.
                         $results[] = self::get_missing_file_result($file);
                         continue;
                     }
                 }
             }
             // store the the new contextid and the new itemid in case we need to remap
             // references to this file later
             $DB->update_record('backup_files_temp', array('id' => $rec->bftid, 'newcontextid' => $newcontextid, 'newitemid' => $rec->newitemid), true);
         } 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) $file_record;
                 restore_dbops::set_backup_ids_record($restoreid, 'file_aliases_queue', $file->id, 0, null, $info);
             }
         }
     }
     $rs->close();
     return $results;
 }
 public function add_related_legacy_files($component, $filearea, $mappingitemname)
 {
     global $CFG, $DB;
     $results = array();
     $restoreid = $this->get_restoreid();
     $oldcontextid = $this->task->get_old_contextid();
     $component = 'mod_dialogue';
     $newfilearea = $filearea;
     if ($filearea == 'entry') {
         $newfilearea = 'message';
     }
     if ($filearea == 'attachment') {
         $newfilearea = 'attachment';
     }
     // Get new context, must exist or this will fail
     if (!($newcontextid = restore_dbops::get_backup_ids_record($restoreid, 'context', $oldcontextid)->newitemid)) {
         throw new restore_dbops_exception('unknown_context_mapping', $oldcontextid);
     }
     $sql = "SELECT id AS bftid, contextid, component, filearea, itemid, itemid AS newitemid, info\n                      FROM {backup_files_temp}\n                     WHERE backupid = ?\n                       AND contextid = ?\n                       AND component = ?\n                       AND filearea  = ?";
     $params = array($restoreid, $oldcontextid, $component, $filearea);
     $fs = get_file_storage();
     // Get moodle file storage
     $basepath = $this->get_basepath() . '/files/';
     // Get backup file pool base
     $rs = $DB->get_recordset_sql($sql, $params);
     foreach ($rs as $rec) {
         // get mapped id
         $rec->newitemid = $this->get_mappingid('dialogue_message', $rec->itemid);
         if (BACKUP::RELEASE >= '2.6') {
             // new line of code for 2.6 or breaks
             $file = (object) backup_controller_dbops::decode_backup_temp_info($rec->info);
         } else {
             $file = (object) unserialize(base64_decode($rec->info));
         }
         // ignore root dirs (they are created automatically)
         if ($file->filepath == '/' && $file->filename == '.') {
             continue;
         }
         // set the best possible user
         $mappeduser = restore_dbops::get_backup_ids_record($restoreid, 'user', $file->userid);
         $mappeduserid = !empty($mappeduser) ? $mappeduser->newitemid : $this->task->get_userid();
         // dir found (and not root one), let's create it
         if ($file->filename == '.') {
             $fs->create_directory($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $mappeduserid);
             continue;
         }
         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);
             // The file is not found in the backup.
             if (!file_exists($backuppath)) {
                 $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;
             }
             // 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)) {
                 // If no license found, use default.
                 if ($file->license == null) {
                     $file->license = $CFG->sitedefaultlicense;
                 }
                 $file_record = array('contextid' => $newcontextid, 'component' => $component, 'filearea' => $newfilearea, '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);
             }
             // store the the new contextid and the new itemid in case we need to remap
             // references to this file later
             $DB->update_record('backup_files_temp', array('id' => $rec->bftid, 'newcontextid' => $newcontextid, 'newitemid' => $rec->newitemid), true);
         } 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' => $newfilearea, '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();
     return $results;
 }
 protected function process_module($data)
 {
     global $CFG, $DB;
     $data = (object) $data;
     $oldid = $data->id;
     $this->task->set_old_moduleversion($data->version);
     // Get the current course module data.
     $newitemid = $this->task->get_moduleid();
     $params = array('id' => $newitemid);
     $cmdata = $DB->get_record('course_modules', $params, '*', MUST_EXIST);
     // Group mode and Grouping.
     $cmdata->groupmode = $data->groupmode;
     $cmdata->groupingid = $this->get_mappingid('grouping', $data->groupingid);
     // Idnumber uniqueness.
     if (!grade_verify_idnumber($data->idnumber, $this->get_courseid())) {
         $data->idnumber = '';
     }
     $cmdata->idnumber = $data->idnumber;
     // Completion.
     if (!empty($CFG->enablecompletion)) {
         $cmdata->completion = $data->completion;
         $cmdata->completiongradeitemnumber = $data->completiongradeitemnumber;
         $cmdata->completionview = $data->completionview;
         $cmdata->completionexpected = $this->apply_date_offset($data->completionexpected);
     }
     // Availability.
     if (empty($CFG->enableavailability)) {
         $data->availability = null;
     }
     if (empty($data->availability)) {
         // If there are legacy availablility data fields (and no new format data),
         // convert the old fields.
         $data->availability = \core_availability\info::convert_legacy_fields($data, false);
     } else {
         if (!empty($data->groupmembersonly)) {
             // There is current availability data, but it still has groupmembersonly
             // as well (2.7 backups), convert just that part.
             require_once $CFG->dirroot . '/lib/db/upgradelib.php';
             $data->availability = upgrade_group_members_only($data->groupingid, $data->availability);
         }
     }
     $cmdata->availability = $data->availability;
     // Backups that did not include showdescription, set it to default 0
     // (this is not totally necessary as it has a db default, but just to
     // be explicit).
     if (!isset($data->showdescription)) {
         $data->showdescription = 0;
     }
     $cmdata->showdescription = $data->showdescription;
     // Course_module record ready, update it.
     $DB->update_record('course_modules', $cmdata);
     // Save mapping.
     $this->set_mapping('course_module', $oldid, $newitemid);
     // Set the new course_module id in the task.
     $this->task->set_moduleid($newitemid);
     // We can now create the context safely.
     $ctxid = context_module::instance($newitemid)->id;
     // Set the new context id in the task.
     $this->task->set_contextid($ctxid);
     // If there is the legacy showavailability data, store this for later use.
     // (This data is not present when restoring 'new' backups.)
     if (isset($cmdata->showavailability)) {
         // Cache the showavailability flag using the backup_ids data field.
         restore_dbops::set_backup_ids_record($this->get_restoreid(), 'module_showavailability', $newitemid, 0, null, (object) array('showavailability' => $cmdata->showavailability));
     }
 }