Ejemplo n.º 1
0
 /**
  * Copy one file from moodle storage to backup storage
  */
 public static function copy_file_moodle2backup($backupid, $filerecorid)
 {
     global $DB;
     if (!backup_controller_dbops::backup_includes_files($backupid)) {
         // Only include the files if required by the controller.
         return;
     }
     // Normalise param
     if (!is_object($filerecorid)) {
         $filerecorid = $DB->get_record('files', array('id' => $filerecorid));
     }
     // Directory, nothing to do
     if ($filerecorid->filename === '.') {
         return;
     }
     $fs = get_file_storage();
     $file = $fs->get_file_instance($filerecorid);
     // If the file is external file, skip copying.
     if ($file->is_external_file()) {
         return;
     }
     // Calculate source and target paths (use same subdirs strategy for both)
     $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' . self::get_backup_content_file_location($filerecorid->contenthash);
     // Create target dir if necessary
     if (!file_exists(dirname($targetfilepath))) {
         if (!check_dir_exists(dirname($targetfilepath), true, true)) {
             throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath));
         }
     }
     // And copy the file (if doesn't exist already)
     if (!file_exists($targetfilepath)) {
         $file->copy_content_to($targetfilepath);
     }
 }
Ejemplo n.º 2
0
 /**
  * Entry point for executing this step
  */
 protected function define_execution()
 {
     global $CFG;
     backup_controller_dbops::drop_backup_ids_temp_table($this->get_backupid());
     // Drop ids temp table
     //avoid purging old backup dirs to save on performance
     //avoid deleting the current backup dir because it's needed during the restore
     //delete the log file that was created
     $backupid = $this->get_backupid();
     unlink($CFG->dataroot . '/temp/backup/' . $backupid . '.log');
 }
Ejemplo n.º 3
0
 protected function define_execution()
 {
     // Get basepath
     $basepath = $this->get_basepath();
     // Calculate the zip fullpath (in OS temp area it's always backup.imscc)
     $zipfile = $basepath . '/backup.imscc';
     // Perform storage and return it (TODO: shouldn't be array but proper result object)
     // Let's send the file to file storage, everything already defined
     // First of all, get some information from the backup_controller to help us decide
     list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid());
     // Extract useful information to decide
     $file = $sinfo['filename']->value;
     $filename = basename($file, '.' . pathinfo($file, PATHINFO_EXTENSION)) . '.imscc';
     // Backup filename
     $userid = $dinfo[0]->userid;
     // User->id executing the backup
     $id = $dinfo[0]->id;
     // Id of activity/section/course (depends of type)
     $courseid = $dinfo[0]->courseid;
     // Id of the course
     $ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
     $component = 'user';
     $filearea = 'backup';
     $itemid = 0;
     $fs = get_file_storage();
     $fr = array('contextid' => $ctxid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/', 'filename' => $filename, 'userid' => $userid, 'timecreated' => time(), 'timemodified' => time());
     // If file already exists, delete if before
     // creating it again. This is BC behaviour - copy()
     // overwrites by default
     if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
         $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
         $sf = $fs->get_file_by_hash($pathnamehash);
         $sf->delete();
     }
     return array('backup_destination' => $fs->create_file_from_pathname($fr, $zipfile));
 }
Ejemplo n.º 4
0
 /**
  * What to do when this step is executed.
  */
 protected function define_execution()
 {
     global $DB;
     $this->log('processing file aliases queue', backup::LOG_DEBUG);
     $fs = get_file_storage();
     // Load the queue.
     $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $this->get_restoreid(), 'itemname' => 'file_aliases_queue'), '', 'info');
     // Iterate over aliases in the queue.
     foreach ($rs as $record) {
         $info = backup_controller_dbops::decode_backup_temp_info($record->info);
         // Try to pick a repository instance that should serve the alias.
         $repository = $this->choose_repository($info);
         if (is_null($repository)) {
             $this->notify_failure($info, 'unable to find a matching repository instance');
             continue;
         }
         if ($info->oldfile->repositorytype === 'local' or $info->oldfile->repositorytype === 'coursefiles') {
             // Aliases to Server files and Legacy course files may refer to a file
             // contained in the backup file or to some existing file (if we are on the
             // same site).
             try {
                 $reference = file_storage::unpack_reference($info->oldfile->reference);
             } catch (Exception $e) {
                 $this->notify_failure($info, 'invalid reference field format');
                 continue;
             }
             // Let's see if the referred source file was also included in the backup.
             $candidates = $DB->get_recordset('backup_files_temp', array('backupid' => $this->get_restoreid(), 'contextid' => $reference['contextid'], 'component' => $reference['component'], 'filearea' => $reference['filearea'], 'itemid' => $reference['itemid']), '', 'info, newcontextid, newitemid');
             $source = null;
             foreach ($candidates as $candidate) {
                 $candidateinfo = backup_controller_dbops::decode_backup_temp_info($candidate->info);
                 if ($candidateinfo->filename === $reference['filename'] and $candidateinfo->filepath === $reference['filepath'] and !is_null($candidate->newcontextid) and !is_null($candidate->newitemid)) {
                     $source = $candidateinfo;
                     $source->contextid = $candidate->newcontextid;
                     $source->itemid = $candidate->newitemid;
                     break;
                 }
             }
             $candidates->close();
             if ($source) {
                 // We have an alias that refers to another file also included in
                 // the backup. Let us change the reference field so that it refers
                 // to the restored copy of the original file.
                 $reference = file_storage::pack_reference($source);
                 // Send the new alias to the filepool.
                 $fs->create_file_from_reference($info->newfile, $repository->id, $reference);
                 $this->notify_success($info);
                 continue;
             } else {
                 // This is a reference to some moodle file that was not contained in the backup
                 // file. If we are restoring to the same site, keep the reference untouched
                 // and restore the alias as is if the referenced file exists.
                 if ($this->task->is_samesite()) {
                     if ($fs->file_exists($reference['contextid'], $reference['component'], $reference['filearea'], $reference['itemid'], $reference['filepath'], $reference['filename'])) {
                         $reference = file_storage::pack_reference($reference);
                         $fs->create_file_from_reference($info->newfile, $repository->id, $reference);
                         $this->notify_success($info);
                         continue;
                     } else {
                         $this->notify_failure($info, 'referenced file not found');
                         continue;
                     }
                     // If we are at other site, we can't restore this alias.
                 } else {
                     $this->notify_failure($info, 'referenced file not included');
                     continue;
                 }
             }
         } else {
             if ($info->oldfile->repositorytype === 'user') {
                 if ($this->task->is_samesite()) {
                     // For aliases to user Private files at the same site, we have a chance to check
                     // if the referenced file still exists.
                     try {
                         $reference = file_storage::unpack_reference($info->oldfile->reference);
                     } catch (Exception $e) {
                         $this->notify_failure($info, 'invalid reference field format');
                         continue;
                     }
                     if ($fs->file_exists($reference['contextid'], $reference['component'], $reference['filearea'], $reference['itemid'], $reference['filepath'], $reference['filename'])) {
                         $reference = file_storage::pack_reference($reference);
                         $fs->create_file_from_reference($info->newfile, $repository->id, $reference);
                         $this->notify_success($info);
                         continue;
                     } else {
                         $this->notify_failure($info, 'referenced file not found');
                         continue;
                     }
                     // If we are at other site, we can't restore this alias.
                 } else {
                     $this->notify_failure($info, 'restoring at another site');
                     continue;
                 }
             } else {
                 // This is a reference to some external file such as in boxnet or dropbox.
                 // If we are restoring to the same site, keep the reference untouched and
                 // restore the alias as is.
                 if ($this->task->is_samesite()) {
                     $fs->create_file_from_reference($info->newfile, $repository->id, $info->oldfile->reference);
                     $this->notify_success($info);
                     continue;
                     // If we are at other site, we can't restore this alias.
                 } else {
                     $this->notify_failure($info, 'restoring at another site');
                     continue;
                 }
             }
         }
     }
     $rs->close();
 }
Ejemplo n.º 5
0
 protected function define_structure()
 {
     global $CFG;
     $info = array();
     $info['name'] = $this->get_setting_value('filename');
     $info['moodle_version'] = $CFG->version;
     $info['moodle_release'] = $CFG->release;
     $info['backup_version'] = $CFG->backup_version;
     $info['backup_release'] = $CFG->backup_release;
     $info['backup_date'] = time();
     $info['backup_uniqueid'] = $this->get_backupid();
     $info['mnet_remoteusers'] = backup_controller_dbops::backup_includes_mnet_remote_users($this->get_backupid());
     $info['original_wwwroot'] = $CFG->wwwroot;
     $info['original_site_identifier_hash'] = md5(get_site_identifier());
     $info['original_course_id'] = $this->get_courseid();
     $originalcourseinfo = backup_controller_dbops::backup_get_original_course_info($this->get_courseid());
     $info['original_course_fullname'] = $originalcourseinfo->fullname;
     $info['original_course_shortname'] = $originalcourseinfo->shortname;
     $info['original_course_startdate'] = $originalcourseinfo->startdate;
     $info['original_course_contextid'] = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
     $info['original_system_contextid'] = get_context_instance(CONTEXT_SYSTEM)->id;
     // Get more information from controller
     list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid());
     // Define elements
     $moodle_backup = new backup_nested_element('moodle_backup');
     $information = new backup_nested_element('information', null, array('name', 'moodle_version', 'moodle_release', 'backup_version', 'backup_release', 'backup_date', 'mnet_remoteusers', 'original_wwwroot', 'original_site_identifier_hash', 'original_course_id', 'original_course_fullname', 'original_course_shortname', 'original_course_startdate', 'original_course_contextid', 'original_system_contextid'));
     $details = new backup_nested_element('details');
     $detail = new backup_nested_element('detail', array('backup_id'), array('type', 'format', 'interactive', 'mode', 'execution', 'executiontime'));
     $contents = new backup_nested_element('contents');
     $activities = new backup_nested_element('activities');
     $activity = new backup_nested_element('activity', null, array('moduleid', 'sectionid', 'modulename', 'title', 'directory'));
     $sections = new backup_nested_element('sections');
     $section = new backup_nested_element('section', null, array('sectionid', 'title', 'directory'));
     $course = new backup_nested_element('course', null, array('courseid', 'title', 'directory'));
     $settings = new backup_nested_element('settings');
     $setting = new backup_nested_element('setting', null, array('level', 'section', 'activity', 'name', 'value'));
     // Build the tree
     $moodle_backup->add_child($information);
     $information->add_child($details);
     $details->add_child($detail);
     $information->add_child($contents);
     if (!empty($cinfo['activities'])) {
         $contents->add_child($activities);
         $activities->add_child($activity);
     }
     if (!empty($cinfo['sections'])) {
         $contents->add_child($sections);
         $sections->add_child($section);
     }
     if (!empty($cinfo['course'])) {
         $contents->add_child($course);
     }
     $information->add_child($settings);
     $settings->add_child($setting);
     // Set the sources
     $information->set_source_array(array((object) $info));
     $detail->set_source_array($dinfo);
     $activity->set_source_array($cinfo['activities']);
     $section->set_source_array($cinfo['sections']);
     $course->set_source_array($cinfo['course']);
     $setting->set_source_array($sinfo);
     // Prepare some information to be sent to main moodle_backup.xml file
     return $moodle_backup;
 }
Ejemplo n.º 6
0
 /**
  * Given one backupid and the (FS) final generated file, perform its final storage
  * into Moodle file storage. For stored files it returns the complete file_info object
  *
  * Note: the $filepath is deleted if the backup file is created successfully
  *
  * If you specify the progress monitor, this will start a new progress section
  * to track progress in processing (in case this task takes a long time).
  *
  * @param int $backupid
  * @param string $filepath zip file containing the backup
  * @param \core\progress\base $progress Optional progress monitor
  * @return stored_file if created, null otherwise
  *
  * @throws moodle_exception in case of any problems
  */
 public static function store_backup_file($backupid, $filepath, \core\progress\base $progress = null)
 {
     global $CFG;
     // First of all, get some information from the backup_controller to help us decide
     list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($backupid, $progress);
     // Extract useful information to decide
     $hasusers = (bool) $sinfo['users']->value;
     // Backup has users
     $isannon = (bool) $sinfo['anonymize']->value;
     // Backup is anonymised
     $filename = $sinfo['filename']->value;
     // Backup filename
     $backupmode = $dinfo[0]->mode;
     // Backup mode backup::MODE_GENERAL/IMPORT/HUB
     $backuptype = $dinfo[0]->type;
     // Backup type backup::TYPE_1ACTIVITY/SECTION/COURSE
     $userid = $dinfo[0]->userid;
     // User->id executing the backup
     $id = $dinfo[0]->id;
     // Id of activity/section/course (depends of type)
     $courseid = $dinfo[0]->courseid;
     // Id of the course
     $format = $dinfo[0]->format;
     // Type of backup file
     // Quick hack. If for any reason, filename is blank, fix it here.
     // TODO: This hack will be out once MDL-22142 - P26 gets fixed
     if (empty($filename)) {
         $filename = backup_plan_dbops::get_default_backup_filename('moodle2', $backuptype, $id, $hasusers, $isannon);
     }
     // Backups of type IMPORT aren't stored ever
     if ($backupmode == backup::MODE_IMPORT) {
         return null;
     }
     if (!is_readable($filepath)) {
         // we have a problem if zip file does not exist
         throw new coding_exception('backup_helper::store_backup_file() expects valid $filepath parameter');
     }
     // Calculate file storage options of id being backup
     $ctxid = 0;
     $filearea = '';
     $component = '';
     $itemid = 0;
     switch ($backuptype) {
         case backup::TYPE_1ACTIVITY:
             $ctxid = context_module::instance($id)->id;
             $component = 'backup';
             $filearea = 'activity';
             $itemid = 0;
             break;
         case backup::TYPE_1SECTION:
             $ctxid = context_course::instance($courseid)->id;
             $component = 'backup';
             $filearea = 'section';
             $itemid = $id;
             break;
         case backup::TYPE_1COURSE:
             $ctxid = context_course::instance($courseid)->id;
             $component = 'backup';
             $filearea = 'course';
             $itemid = 0;
             break;
     }
     if ($backupmode == backup::MODE_AUTOMATED) {
         // Automated backups have there own special area!
         $filearea = 'automated';
         // If we're keeping the backup only in a chosen path, just move it there now
         // this saves copying from filepool to here later and filling trashdir.
         $config = get_config('backup');
         $dir = $config->backup_auto_destination;
         if ($config->backup_auto_storage == 1 and $dir and is_dir($dir) and is_writable($dir)) {
             $filedest = $dir . '/' . backup_plan_dbops::get_default_backup_filename($format, $backuptype, $courseid, $hasusers, $isannon, !$config->backup_shortname);
             // first try to move the file, if it is not possible copy and delete instead
             if (@rename($filepath, $filedest)) {
                 return null;
             }
             umask($CFG->umaskpermissions);
             if (copy($filepath, $filedest)) {
                 @chmod($filedest, $CFG->filepermissions);
                 // may fail because the permissions may not make sense outside of dataroot
                 unlink($filepath);
                 return null;
             } else {
                 $bc = backup_controller::load_controller($backupid);
                 $bc->log('Attempt to copy backup file to the specified directory using filesystem failed - ', backup::LOG_WARNING, $dir);
                 $bc->destroy();
             }
             // bad luck, try to deal with the file the old way - keep backup in file area if we can not copy to ext system
         }
     }
     // Backups of type HUB (by definition never have user info)
     // are sent to user's "user_tohub" file area. The upload process
     // will be responsible for cleaning that filearea once finished
     if ($backupmode == backup::MODE_HUB) {
         $ctxid = context_user::instance($userid)->id;
         $component = 'user';
         $filearea = 'tohub';
         $itemid = 0;
     }
     // Backups without user info or with the anonymise functionality
     // enabled are sent to user's "user_backup"
     // file area. Maintenance of such area is responsibility of
     // the user via corresponding file manager frontend
     if ($backupmode == backup::MODE_GENERAL && (!$hasusers || $isannon)) {
         $ctxid = context_user::instance($userid)->id;
         $component = 'user';
         $filearea = 'backup';
         $itemid = 0;
     }
     // Let's send the file to file storage, everything already defined
     $fs = get_file_storage();
     $fr = array('contextid' => $ctxid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/', 'filename' => $filename, 'userid' => $userid, 'timecreated' => time(), 'timemodified' => time());
     // If file already exists, delete if before
     // creating it again. This is BC behaviour - copy()
     // overwrites by default
     if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
         $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
         $sf = $fs->get_file_by_hash($pathnamehash);
         $sf->delete();
     }
     $file = $fs->create_file_from_pathname($fr, $filepath);
     unlink($filepath);
     return $file;
 }
Ejemplo n.º 7
0
 /**
  * Drops the temporary storage of stashed data
  *
  * This implementation uses backup_ids_temp table.
  */
 public function drop_stash_storage() {
     backup_controller_dbops::drop_backup_ids_temp_table($this->get_id());
 }
 /**
  * Given the backupid, determine whether this backup should include
  * files from the moodle file storage system.
  *
  * @param string $backupid The ID of the backup.
  * @return int Indicates whether files should be included in backups.
  */
 public static function backup_includes_files($backupid)
 {
     // This function is called repeatedly in a backup with many files.
     // Loading the controller is a nontrivial operation (in a large test
     // backup it took 0.3 seconds), so we do a temporary cache of it within
     // this request.
     if (self::$includesfilescachebackupid === $backupid) {
         return self::$includesfilescache;
     }
     // Load controller, get value, then destroy controller and return result.
     self::$includesfilescachebackupid = $backupid;
     $bc = self::load_controller($backupid);
     self::$includesfilescache = $bc->get_include_files();
     $bc->destroy();
     return self::$includesfilescache;
 }
Ejemplo n.º 9
0
 protected function define_execution()
 {
     // Get basepath
     $basepath = $this->get_basepath();
     // Calculate the zip fullpath (in OS temp area it's always backup.mbz)
     $zipfile = $basepath . '/backup.mbz';
     $has_file_references = backup_controller_dbops::backup_includes_file_references($this->get_backupid());
     // Perform storage and return it (TODO: shouldn't be array but proper result object)
     return array('backup_destination' => backup_helper::store_backup_file($this->get_backupid(), $zipfile, $this->task->get_progress()), 'include_file_references_to_external_content' => $has_file_references);
 }
Ejemplo n.º 10
0
 /**
  * Backup structures tests (construction, definition and execution)
  */
 function test_backup_structure_construct()
 {
     global $DB;
     $backupid = 'Testing Backup ID';
     // Official backupid for these tests
     // Create all the elements that will conform the tree
     $forum = new backup_nested_element('forum', array('id'), array('type', 'name', 'intro', 'introformat', 'assessed', 'assesstimestart', 'assesstimefinish', 'scale', 'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype', 'rsstype', 'rssarticles', 'timemodified', 'warnafter', 'blockafter', new backup_final_element('blockperiod'), new mock_skip_final_element('completiondiscussions'), new mock_modify_final_element('completionreplies'), new mock_final_element_interceptor('completionposts')));
     $discussions = new backup_nested_element('discussions');
     $discussion = new backup_nested_element('discussion', array('id'), array('forum', 'name', 'firstpost', 'userid', 'groupid', 'assessed', 'timemodified', 'usermodified', 'timestart', 'timeend'));
     $posts = new backup_nested_element('posts');
     $post = new backup_nested_element('post', array('id'), array('discussion', 'parent', 'userid', 'created', 'modified', 'mailed', 'subject', 'message', 'messageformat', 'messagetrust', 'attachment', 'totalscore', 'mailnow'));
     $ratings = new backup_nested_element('ratings');
     $rating = new backup_nested_element('rating', array('id'), array('userid', 'itemid', 'time', 'post_rating'));
     $reads = new backup_nested_element('readposts');
     $read = new backup_nested_element('read', array('id'), array('userid', 'discussionid', 'postid', 'firstread', 'lastread'));
     $inventeds = new backup_nested_element('invented_elements', array('reason', 'version'));
     $invented = new backup_nested_element('invented', null, array('one', 'two', 'three'));
     $one = $invented->get_final_element('one');
     $one->add_attributes(array('attr1', 'attr2'));
     // Build the tree
     $forum->add_child($discussions);
     $discussions->add_child($discussion);
     $discussion->add_child($posts);
     $posts->add_child($post);
     $post->add_child($ratings);
     $ratings->add_child($rating);
     $forum->add_child($reads);
     $reads->add_child($read);
     $forum->add_child($inventeds);
     $inventeds->add_child($invented);
     // Let's add 1 optigroup with 4 elements
     $alternative1 = new backup_optigroup_element('alternative1', array('name', 'value'), '../../id', 1);
     $alternative2 = new backup_optigroup_element('alternative2', array('name', 'value'), backup::VAR_PARENTID, 2);
     $alternative3 = new backup_optigroup_element('alternative3', array('name', 'value'), '/forum/discussions/discussion/posts/post/id', 3);
     $alternative4 = new backup_optigroup_element('alternative4', array('forumtype', 'forumname'));
     // Alternative without conditions
     // Create the optigroup, adding one element
     $optigroup = new backup_optigroup('alternatives', $alternative1, false);
     // Add second opti element
     $optigroup->add_child($alternative2);
     // Add optigroup to post element
     $post->add_optigroup($optigroup);
     // Add third opti element, on purpose after the add_optigroup() line above to check param evaluation works ok
     $optigroup->add_child($alternative3);
     // Add 4th opti element (the one without conditions, so will be present always)
     $optigroup->add_child($alternative4);
     /// Create some new nested elements, both named 'dupetest1', and add them to alternative1 and alternative2
     /// (not problem as far as the optigroup in not unique)
     $dupetest1 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
     $dupetest2 = new backup_nested_element('dupetest2', null, array('field1', 'field2'));
     $dupetest3 = new backup_nested_element('dupetest3', null, array('field1', 'field2'));
     $dupetest4 = new backup_nested_element('dupetest1', null, array('field1', 'field2'));
     $dupetest1->add_child($dupetest3);
     $dupetest2->add_child($dupetest4);
     $alternative1->add_child($dupetest1);
     $alternative2->add_child($dupetest2);
     // Define sources
     $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
     $discussion->set_source_sql('SELECT *
                                    FROM {forum_discussions}
                                   WHERE forum = ?', array('/forum/id'));
     $post->set_source_table('forum_posts', array('discussion' => '/forum/discussions/discussion/id'));
     $rating->set_source_sql('SELECT *
                                FROM {rating}
                               WHERE itemid = ?', array(backup::VAR_PARENTID));
     $read->set_source_table('forum_read', array('id' => '../../id'));
     $inventeds->set_source_array(array((object) array('reason' => 'I love Moodle', 'version' => '1.0'), (object) array('reason' => 'I love Moodle', 'version' => '2.0')));
     // 2 object array
     $invented->set_source_array(array((object) array('one' => 1, 'two' => 2, 'three' => 3), (object) array('one' => 11, 'two' => 22, 'three' => 33)));
     // 2 object array
     // Set optigroup_element sources
     $alternative1->set_source_array(array((object) array('name' => 'alternative1', 'value' => 1)));
     // 1 object array
     // Skip alternative2 source definition on purpose (will be tested)
     // $alternative2->set_source_array(array((object)array('name' => 'alternative2', 'value' => 2))); // 1 object array
     $alternative3->set_source_array(array((object) array('name' => 'alternative3', 'value' => 3)));
     // 1 object array
     // Alternative 4 source is the forum type and name, so we'll get that in ALL posts (no conditions) that
     // have not another alternative (post4 in our testing data in the only not matching any other alternative)
     $alternative4->set_source_sql('SELECT type AS forumtype, name AS forumname
                                      FROM {forum}
                                     WHERE id = ?', array('/forum/id'));
     // Set children of optigroup_element source
     $dupetest1->set_source_array(array((object) array('field1' => '1', 'field2' => 1)));
     // 1 object array
     $dupetest2->set_source_array(array((object) array('field1' => '2', 'field2' => 2)));
     // 1 object array
     $dupetest3->set_source_array(array((object) array('field1' => '3', 'field2' => 3)));
     // 1 object array
     $dupetest4->set_source_array(array((object) array('field1' => '4', 'field2' => 4)));
     // 1 object array
     // Define some aliases
     $rating->set_source_alias('rating', 'post_rating');
     // Map the 'rating' value from DB to 'post_rating' final element
     // Mark to detect files of type 'forum_intro' in forum (and not item id)
     $forum->annotate_files('mod_forum', 'intro', null);
     // Mark to detect file of type 'forum_post' and 'forum_attachment' in post (with itemid being post->id)
     $post->annotate_files('mod_forum', 'post', 'id');
     $post->annotate_files('mod_forum', 'attachment', 'id');
     // Mark various elements to be annotated
     $discussion->annotate_ids('user1', 'userid');
     $post->annotate_ids('forum_post', 'id');
     $rating->annotate_ids('user2', 'userid');
     $rating->annotate_ids('forum_post', 'itemid');
     // Create the backup_ids_temp table
     backup_controller_dbops::create_backup_ids_temp_table($backupid);
     // Instantiate in memory xml output
     $xo = new memory_xml_output();
     // Instantiate xml_writer and start it
     $xw = new xml_writer($xo);
     $xw->start();
     // Instantiate the backup processor
     $processor = new backup_structure_processor($xw);
     // Set some variables
     $processor->set_var(backup::VAR_ACTIVITYID, $this->forumid);
     $processor->set_var(backup::VAR_BACKUPID, $backupid);
     $processor->set_var(backup::VAR_CONTEXTID, $this->contextid);
     // Process the backup structure with the backup processor
     $forum->process($processor);
     // Stop the xml_writer
     $xw->stop();
     // Check various counters
     $this->assertEquals($forum->get_counter(), $DB->count_records('forum'));
     $this->assertEquals($discussion->get_counter(), $DB->count_records('forum_discussions'));
     $this->assertEquals($rating->get_counter(), $DB->count_records('rating'));
     $this->assertEquals($read->get_counter(), $DB->count_records('forum_read'));
     $this->assertEquals($inventeds->get_counter(), 2);
     // Array
     // Perform some validations with the generated XML
     $dom = new DomDocument();
     $dom->loadXML($xo->get_allcontents());
     $xpath = new DOMXPath($dom);
     // Some more counters
     $query = '/forum/discussions/discussion/posts/post';
     $posts = $xpath->query($query);
     $this->assertEquals($posts->length, $DB->count_records('forum_posts'));
     $query = '/forum/invented_elements/invented';
     $inventeds = $xpath->query($query);
     $this->assertEquals($inventeds->length, 2 * 2);
     // Check ratings information against DB
     $ratings = $dom->getElementsByTagName('rating');
     $this->assertEquals($ratings->length, $DB->count_records('rating'));
     foreach ($ratings as $rating) {
         $ratarr = array();
         $ratarr['id'] = $rating->getAttribute('id');
         foreach ($rating->childNodes as $node) {
             if ($node->nodeType != XML_TEXT_NODE) {
                 $ratarr[$node->nodeName] = $node->nodeValue;
             }
         }
         $this->assertEquals($ratarr['userid'], $DB->get_field('rating', 'userid', array('id' => $ratarr['id'])));
         $this->assertEquals($ratarr['itemid'], $DB->get_field('rating', 'itemid', array('id' => $ratarr['id'])));
         $this->assertEquals($ratarr['post_rating'], $DB->get_field('rating', 'rating', array('id' => $ratarr['id'])));
     }
     // Check forum has "blockeperiod" with value 0 (was declared by object instead of name)
     $query = '/forum[blockperiod="0"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forum is missing "completiondiscussions" (as we are using mock_skip_final_element)
     $query = '/forum/completiondiscussions';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check forum has "completionreplies" with value "original was 0, now changed" (because of mock_modify_final_element)
     $query = '/forum[completionreplies="original was 0, now changed"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forum has "completionposts" with value "intercepted!" (because of mock_final_element_interceptor)
     $query = '/forum[completionposts="intercepted!"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check there isn't any alternative2 tag, as far as it hasn't source defined
     $query = '//alternative2';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check there are 4 "field1" elements
     $query = '/forum/discussions/discussion/posts/post//field1';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 4);
     // Check first post has one name element with value "alternative1"
     $query = '/forum/discussions/discussion/posts/post[@id="1"][name="alternative1"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check there are two "dupetest1" elements
     $query = '/forum/discussions/discussion/posts/post//dupetest1';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 2);
     // Check second post has one name element with value "dupetest2"
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check element "dupetest2" of second post has one field1 element with value "2"
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2[field1="2"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check forth post has no name element
     $query = '/forum/discussions/discussion/posts/post[@id="4"]/name';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check 1st, 2nd and 3rd posts have no forumtype element
     $query = '/forum/discussions/discussion/posts/post[@id="1"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     $query = '/forum/discussions/discussion/posts/post[@id="2"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     $query = '/forum/discussions/discussion/posts/post[@id="3"]/forumtype';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 0);
     // Check 4th post has one forumtype element with value "general"
     // (because it doesn't matches alternatives 1, 2, 3, then alternative 4,
     // the one without conditions is being applied)
     $query = '/forum/discussions/discussion/posts/post[@id="4"][forumtype="general"]';
     $result = $xpath->query($query);
     $this->assertEquals($result->length, 1);
     // Check annotations information against DB
     // Count records in original tables
     $c_postsid = $DB->count_records_sql('SELECT COUNT(DISTINCT id) FROM {forum_posts}');
     $c_dissuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {forum_discussions}');
     $c_ratuserid = $DB->count_records_sql('SELECT COUNT(DISTINCT userid) FROM {rating}');
     // Count records in backup_ids_table
     $f_forumpost = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'forum_post'));
     $f_user1 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user1'));
     $f_user2 = $DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'user2'));
     $c_notbackupid = $DB->count_records_select('backup_ids_temp', 'backupid != ?', array($backupid));
     // Peform tests by comparing counts
     $this->assertEquals($c_notbackupid, 0);
     // there isn't any record with incorrect backupid
     $this->assertEquals($c_postsid, $f_forumpost);
     // All posts have been registered
     $this->assertEquals($c_dissuserid, $f_user1);
     // All users coming from discussions have been registered
     $this->assertEquals($c_ratuserid, $f_user2);
     // All users coming from ratings have been registered
     // Check file annotations against DB
     $fannotations = $DB->get_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => 'file'));
     $ffiles = $DB->get_records('files', array('contextid' => $this->contextid));
     $this->assertEquals(count($fannotations), count($ffiles));
     // Same number of recs in both (all files have been annotated)
     foreach ($fannotations as $annotation) {
         // Check ids annotated
         $this->assertTrue($DB->record_exists('files', array('id' => $annotation->itemid)));
     }
     // Drop the backup_ids_temp table
     backup_controller_dbops::drop_backup_ids_temp_table('testingid');
 }
Ejemplo n.º 11
0
 /**
  *
  * @param string $tempdir Directory under tempdir/backup awaiting restore
  * @param int $courseid Course id where restore is going to happen
  * @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false]
  * @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ]
  * @param int $userid
  * @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ]
  */
 public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target)
 {
     $this->tempdir = $tempdir;
     $this->courseid = $courseid;
     $this->interactive = $interactive;
     $this->mode = $mode;
     $this->userid = $userid;
     $this->target = $target;
     // Apply some defaults
     $this->type = '';
     $this->format = backup::FORMAT_UNKNOWN;
     $this->execution = backup::EXECUTION_INMEDIATE;
     $this->operation = backup::OPERATION_RESTORE;
     $this->executiontime = 0;
     $this->samesite = false;
     $this->checksum = '';
     $this->precheck = null;
     // Apply current backup version and release if necessary
     backup_controller_dbops::apply_version_and_release();
     // Check courseid is correct
     restore_check::check_courseid($this->courseid);
     // Check user is correct
     restore_check::check_user($this->userid);
     // Calculate unique $restoreid
     $this->calculate_restoreid();
     // Default logger chain (based on interactive/execution)
     $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid);
     // Instantiate the output_controller singleton and active it if interactive and inmediate
     $oc = output_controller::get_instance();
     if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) {
         $oc->set_active(true);
     }
     $this->log('instantiating restore controller', backup::LOG_INFO, $this->restoreid);
     // Set initial status
     $this->set_status(backup::STATUS_CREATED);
     // Calculate original restore format
     $this->format = backup_general_helper::detect_backup_format($tempdir);
     // If format is not moodle2, set to conversion needed
     if ($this->format !== backup::FORMAT_MOODLE) {
         $this->set_status(backup::STATUS_REQUIRE_CONV);
         // Else, format is moodle2, load plan, apply security and set status based on interactivity
     } else {
         // Load plan
         $this->load_plan();
         // Perform all initial security checks and apply (2nd param) them to settings automatically
         restore_check::check_security($this, true);
         if ($this->interactive == backup::INTERACTIVE_YES) {
             $this->set_status(backup::STATUS_SETTING_UI);
         } else {
             $this->set_status(backup::STATUS_NEED_PRECHECK);
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * Check backup_includes_files
  */
 function test_backup_controller_dbops_includes_files()
 {
     global $DB;
     $dbman = $DB->get_manager();
     // Going to use some database_manager services for testing
     // A MODE_GENERAL controller - this should include files
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 1);
     // A MODE_IMPORT controller - should not include files
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
     $this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 0);
     // A MODE_SAMESITE controller - should not include files
     $bc = new mock_backup_controller4dbops(backup::TYPE_1COURSE, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid);
     $this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 0);
 }
Ejemplo n.º 13
0
 /**
  * For the target course context, put as many custom role names as possible
  */
 public static function set_course_role_names($restoreid, $courseid)
 {
     global $DB;
     // Get the course context
     $coursectx = context_course::instance($courseid);
     // Get all the mapped roles we have
     $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid, info, newitemid');
     foreach ($rs as $recrole) {
         $info = backup_controller_dbops::decode_backup_temp_info($recrole->info);
         // If it's one mapped role and we have one name for it
         if (!empty($recrole->newitemid) && !empty($info['nameincourse'])) {
             // If role name doesn't exist, add it
             $rolename = new stdclass();
             $rolename->roleid = $recrole->newitemid;
             $rolename->contextid = $coursectx->id;
             if (!$DB->record_exists('role_names', (array) $rolename)) {
                 $rolename->name = $info['nameincourse'];
                 $DB->insert_record('role_names', $rolename);
             }
         }
     }
     $rs->close();
 }
 public static function create_restore_temp_tables($restoreid)
 {
     global $CFG, $DB;
     $dbman = $DB->get_manager();
     // We are going to use database_manager services
     if ($dbman->table_exists('backup_ids_temp')) {
         // Table exists, from restore prechecks
         // TODO: Improve this by inserting/selecting some record to see there is restoreid match
         // TODO: If not match, exception, table corresponds to another backup/restore operation
         return true;
     }
     backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_ids_template', 'backup_ids_temp');
     backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_files_template', 'backup_files_temp');
     return false;
 }
Ejemplo n.º 15
0
 /**
  * Given one backupid and the (FS) final generated file, perform its final storage
  * into Moodle file storage. For stored files it returns the complete file_info object
  */
 public static function store_backup_file($backupid, $filepath)
 {
     // First of all, get some information from the backup_controller to help us decide
     list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($backupid);
     // Extract useful information to decide
     $hasusers = (bool) $sinfo['users']->value;
     // Backup has users
     $isannon = (bool) $sinfo['anonymize']->value;
     // Backup is anonymised
     $filename = $sinfo['filename']->value;
     // Backup filename
     $backupmode = $dinfo[0]->mode;
     // Backup mode backup::MODE_GENERAL/IMPORT/HUB
     $backuptype = $dinfo[0]->type;
     // Backup type backup::TYPE_1ACTIVITY/SECTION/COURSE
     $userid = $dinfo[0]->userid;
     // User->id executing the backup
     $id = $dinfo[0]->id;
     // Id of activity/section/course (depends of type)
     $courseid = $dinfo[0]->courseid;
     // Id of the course
     // Quick hack. If for any reason, filename is blank, fix it here.
     // TODO: This hack will be out once MDL-22142 - P26 gets fixed
     if (empty($filename)) {
         $filename = backup_plan_dbops::get_default_backup_filename('moodle2', $backuptype, $id, $hasusers, $isannon);
     }
     // Backups of type IMPORT aren't stored ever
     if ($backupmode == backup::MODE_IMPORT) {
         return false;
     }
     // Calculate file storage options of id being backup
     $ctxid = 0;
     $filearea = '';
     $component = '';
     $itemid = 0;
     switch ($backuptype) {
         case backup::TYPE_1ACTIVITY:
             $ctxid = get_context_instance(CONTEXT_MODULE, $id)->id;
             $component = 'backup';
             $filearea = 'activity';
             $itemid = 0;
             break;
         case backup::TYPE_1SECTION:
             $ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
             $component = 'backup';
             $filearea = 'section';
             $itemid = $id;
             break;
         case backup::TYPE_1COURSE:
             $ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
             $component = 'backup';
             $filearea = 'course';
             $itemid = 0;
             break;
     }
     if ($backupmode == backup::MODE_AUTOMATED) {
         // Automated backups have there own special area!
         $filearea = 'automated';
     }
     // Backups of type HUB (by definition never have user info)
     // are sent to user's "user_tohub" file area. The upload process
     // will be responsible for cleaning that filearea once finished
     if ($backupmode == backup::MODE_HUB) {
         $ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
         $component = 'user';
         $filearea = 'tohub';
         $itemid = 0;
     }
     // Backups without user info or with the anonymise functionality
     // enabled are sent to user's "user_backup"
     // file area. Maintenance of such area is responsibility of
     // the user via corresponding file manager frontend
     if ($backupmode == backup::MODE_GENERAL && (!$hasusers || $isannon)) {
         $ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
         $component = 'user';
         $filearea = 'backup';
         $itemid = 0;
     }
     // Let's send the file to file storage, everything already defined
     $fs = get_file_storage();
     $fr = array('contextid' => $ctxid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/', 'filename' => $filename, 'userid' => $userid, 'timecreated' => time(), 'timemodified' => time());
     // If file already exists, delete if before
     // creating it again. This is BC behaviour - copy()
     // overwrites by default
     if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
         $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
         $sf = $fs->get_file_by_hash($pathnamehash);
         $sf->delete();
     }
     return $fs->create_file_from_pathname($fr, $filepath);
 }
Ejemplo n.º 16
0
 function test_backup_controller_dbops()
 {
     global $DB;
     $dbman = $DB->get_manager();
     // Going to use some database_manager services for testing
     // Instantiate non interactive backup_controller
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $this->assertTrue($bc instanceof backup_controller);
     // Calculate checksum
     $checksum = $bc->calculate_checksum();
     $this->assertEqual(strlen($checksum), 32);
     // is one md5
     // save controller
     $recid = backup_controller_dbops::save_controller($bc, $checksum);
     $this->assertTrue($recid);
     $this->todelete[] = array('backup_controllers', $recid);
     // mark this record for deletion
     // save it again (should cause update to happen)
     $recid2 = backup_controller_dbops::save_controller($bc, $checksum);
     $this->assertTrue($recid2);
     $this->todelete[] = array('backup_controllers', $recid2);
     // mark this record for deletion
     $this->assertEqual($recid, $recid2);
     // Same record in both save operations
     // Try incorrect checksum
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $checksum = $bc->calculate_checksum();
     try {
         $recid = backup_controller_dbops::save_controller($bc, 'lalala');
         $this->assertTrue(false, 'backup_dbops_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_dbops_exception);
         $this->assertEqual($e->errorcode, 'backup_controller_dbops_saving_checksum_mismatch');
     }
     // Try to save non backup_controller object
     $bc = new stdclass();
     try {
         $recid = backup_controller_dbops::save_controller($bc, 'lalala');
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEqual($e->errorcode, 'backup_controller_expected');
     }
     // save and load controller (by backupid). Then compare
     $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $checksum = $bc->calculate_checksum();
     // Calculate checksum
     $backupid = $bc->get_backupid();
     $this->assertEqual(strlen($backupid), 32);
     // is one md5
     $recid = backup_controller_dbops::save_controller($bc, $checksum);
     // save controller
     $this->todelete[] = array('backup_controllers', $recid);
     // mark this record for deletion
     $newbc = backup_controller_dbops::load_controller($backupid);
     // load controller
     $this->assertTrue($newbc instanceof backup_controller);
     $newchecksum = $newbc->calculate_checksum();
     $this->assertEqual($newchecksum, $checksum);
     // try to load non-existing controller
     try {
         $bc = backup_controller_dbops::load_controller('1234567890');
         $this->assertTrue(false, 'backup_dbops_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_dbops_exception);
         $this->assertEqual($e->errorcode, 'backup_controller_dbops_nonexisting');
     }
     // backup_ids_temp table tests
     // If, for any reason table exists, drop it
     if ($dbman->table_exists('backup_ids_temp')) {
         $dbman->drop_temp_table(new xmldb_table('backup_ids_temp'));
     }
     // Check backup_ids_temp table doesn't exist
     $this->assertFalse($dbman->table_exists('backup_ids_temp'));
     // Create and check it exists
     backup_controller_dbops::create_backup_ids_temp_table('testingid');
     $this->assertTrue($dbman->table_exists('backup_ids_temp'));
     // Drop and check it doesn't exists anymore
     backup_controller_dbops::drop_backup_ids_temp_table('testingid');
     $this->assertFalse($dbman->table_exists('backup_ids_temp'));
 }
Ejemplo n.º 17
0
 protected function apply_defaults()
 {
     $this->log('applying plan defaults', backup::LOG_DEBUG);
     backup_controller_dbops::apply_config_defaults($this);
     $this->set_status(backup::STATUS_CONFIGURED);
     $this->set_include_files();
 }
 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;
 }