/** * 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); } }
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['include_files'] = backup_controller_dbops::backup_includes_files($this->get_backupid()); $info['include_file_references_to_external_content'] = backup_controller_dbops::backup_includes_file_references($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_format'] = $originalcourseinfo->format; $info['original_course_fullname'] = $originalcourseinfo->fullname; $info['original_course_shortname'] = $originalcourseinfo->shortname; $info['original_course_startdate'] = $originalcourseinfo->startdate; $info['original_course_contextid'] = context_course::instance($this->get_courseid())->id; $info['original_system_contextid'] = context_system::instance()->id; // Get more information from controller list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid(), $this->get_task()->get_progress()); // 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', 'include_files', 'include_file_references_to_external_content', 'original_wwwroot', 'original_site_identifier_hash', 'original_course_id', 'original_course_format', '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; }
/** * 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); }