MDL-21432 backup - fixes in backup final task and reorder restore root task

This commit is contained in:
Eloy Lafuente
2010-07-18 01:37:59 +00:00
parent 2df0f295b4
commit 76cfb124a1
4 changed files with 172 additions and 15 deletions
+7 -7
View File
@@ -48,7 +48,7 @@ class backup_final_task extends backup_task {
// including membership based on setting
$this->add_step(new backup_groups_structure_step('groups', 'groups.xml'));
// Annotate all the user files (conditionally) (private profile and icon files)
// Annotate all the user files (conditionally) (private, profile and icon files)
// Because each user has its own context, we need a separate/specialised step here
// This step also ensures that the contexts for all the users exist, so next
// step can be safely executed (join between users and contexts)
@@ -69,20 +69,20 @@ class backup_final_task extends backup_task {
// the list of role definitions (no assignments nor permissions)
$this->add_step(new backup_final_roles_structure_step('roleslist', 'roles.xml'));
// Generate the scales file with all the annotated scales
// Generate the gradebook file with categories and course grade items. Do it conditionally, using
// execute_condition() so only will be excuted if ALL module grade_items in course have been exported
$this->add_step(new backup_gradebook_structure_step('course_gradebook','gradebook.xml'));
// Generate the scales file with all the (final) annotated scales
$this->add_step(new backup_final_scales_structure_step('scaleslist', 'scales.xml'));
// Generate the outcomes file with all the annotated outcomes
// Generate the outcomes file with all the (final) annotated outcomes
$this->add_step(new backup_final_outcomes_structure_step('outcomeslist', 'outcomes.xml'));
// Migrate the pending annotations to final (prev steps may have added some files)
// This must be executed before backup files
$this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
// Generate the gradebook file with categories and course grade items. Do it conditionally, using
// execute_condition() so only will be excuted if ALL module grade_items in course have been exported
$this->add_step(new backup_gradebook_structure_step('course_gradebook','gradebook.xml'));
// Generate the files.xml file with all the (final) annotated files. At the same
// time copy all the files from moodle storage to backup storage (uses custom
// backup_nested_element for that)
+10 -5
View File
@@ -708,9 +708,9 @@ class backup_gradebook_structure_step extends backup_structure_step {
$letter->set_source_table('grade_letters', array('contextid' => backup::VAR_CONTEXTID));
// Annotations
// Annotations (both as final as far as they are going to be exported in next steps)
$grade_item->annotate_ids('scalefinal', 'scaleid'); // Straight as scalefinal because it's > 0
$grade_item->annotate_ids('outcome', 'outcomeid');
$grade_item->annotate_ids('outcomefinal', 'outcomeid');
// Return the root element
return $gradebook;
@@ -1194,6 +1194,7 @@ class backup_main_structure_step extends backup_structure_step {
$info['original_wwwroot']=$CFG->wwwroot;
$info['original_site_identifier_hash'] = md5(get_site_identifier());
$info['original_course_id'] = $this->get_courseid();
$info['original_course_contextid'] = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
// Get more information from controller
list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid());
@@ -1205,7 +1206,7 @@ class backup_main_structure_step extends backup_structure_step {
$information = new backup_nested_element('information', null, array(
'name', 'moodle_version', 'moodle_release', 'backup_version',
'backup_release', 'backup_date', 'original_wwwroot',
'original_site_identifier_hash', 'original_course_id'));
'original_site_identifier_hash', 'original_course_id', 'original_course_contextid'));
$details = new backup_nested_element('details');
@@ -1397,7 +1398,7 @@ class backup_annotate_scales_from_outcomes extends backup_execution_step {
}
/**
* This step will generate all the user annotations for the already
* This step will generate all the file annotations for the already
* annotated (final) users. Need to do this here because each user
* has its own context and structure tasks only are able to handle
* one context. Also, this step will guarantee that every user has
@@ -1409,7 +1410,11 @@ class backup_annotate_all_user_files extends backup_execution_step {
global $DB;
// List of fileareas we are going to annotate
$fileareas = array('private', 'profile', 'icon');
$fileareas = array('profile', 'icon');
if ($this->get_setting_value('user_files')) { // private files only if enabled in settings
$fileareas[] = 'private';
}
// Fetch all annotated (final) users
$rs = $DB->get_recordset('backup_ids_temp', array(
@@ -48,6 +48,15 @@ class restore_root_task extends restore_task {
// performed the same process so, it's not possible to have errors here
$this->add_step(new restore_process_included_users('process_user_records'));
// Load all the needed files to temp_ids table
$this->add_step(new restore_load_included_files('load_file_records', 'files.xml'));
// Unconditionally, create all the needed users calculated in the previous step
$this->add_step(new restore_create_included_users('create_users'));
// Unconditionally, load create all the needed groups and groupings
$this->add_step(new restore_groups_structure_step('create_groups_and_groupings', 'groups.xml'));
// At the end, mark it as built
$this->built = true;
}
+146 -3
View File
@@ -32,7 +32,6 @@
class restore_create_and_clean_temp_stuff extends restore_execution_step {
protected function define_execution() {
backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs
$exists = restore_controller_dbops::create_backup_ids_temp_table($this->get_restoreid()); // Create temp table conditionally
// If the table already exists, it's because restore_prechecks have been executed in the same
// request (without problems) and it already contains a bunch of preloaded information (users...)
@@ -40,6 +39,10 @@ class restore_create_and_clean_temp_stuff extends restore_execution_step {
if ($exists) { // Inform plan about preloaded information
$this->task->set_preloaded_information();
}
// Create the old-course-ctxid to new-course-ctxid mapping, we need that available since the beginning
$itemid = $this->task->get_old_contextid();
$newitemid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid);
}
}
@@ -80,11 +83,51 @@ class restore_load_included_inforef_records extends restore_execution_step {
}
}
/*
* Execution step that will load all the needed files into backup_temp_ids.
* - itemname: contains "file*component*fileara"
* - itemid: contains the original id of the file
* - newitemid: contains the itemid of the file
* - parentitemid: contains the context of the file
* - info: contains the whole original object (times, names...)
* (all them being original ids as loaded from xml)
*/
class restore_load_included_files extends restore_structure_step {
protected function define_structure() {
$file = new restore_path_element('file', '/files/file');
return array($file);
}
// Processing functions go here
public function process_file($data) {
$data = (object)$data; // handy
$itemname = 'file*' . $data->component . '*' . $data->filearea;
$itemid = $data->id;
$newitemid = $data->itemid;
$parentitemid = $data->contextid;
$info = $data;
// load it if needed:
// - it it is one of the annotated inforef files (course/section/activity/block)
// - it is one "user", "group" or "grade" component file
$isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $itemid);
$iscomponent = ($data->component == 'user' || $data->component == 'group' || $data->component == 'grade');
if ($isfileref || $iscomponent) {
restore_dbops::set_backup_ids_record($this->get_restoreid(), $itemname, $itemid, $newitemid, $parentitemid, $info);
}
}
}
/**
* Execution step that, *conditionally* (if there isn't preloaded information
* and users have been selected in settings, will load all the needed users
* to backup_temp_ids. They will be stored with "user" itemname and with
* their original contextid as paremitemid.
* their original contextid as paremitemid
*/
class restore_load_included_users extends restore_execution_step {
@@ -108,7 +151,7 @@ class restore_load_included_users extends restore_execution_step {
* Note: Any error will cause exception, as far as this is the same processing
* than the one into restore prechecks (that should have stopped process earlier)
*/
class restore_process_included_users extends restore_execution_step {
class restore_process_included_users extends restore_execution_step {
protected function define_execution() {
@@ -122,6 +165,106 @@ class restore_load_included_users extends restore_execution_step {
}
}
/**
* Execution step that will create all the needed users as calculated
* by @restore_process_included_users (those having newiteind = 0)
*/
class restore_create_included_users extends restore_execution_step {
protected function define_execution() {
restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files'));
}
}
/**
* Structure step that will create all the needed groups and groupings
* by loading them from the groups.xml file performing the required matches.
* Note group members only will be added if restoring user info
*/
class restore_groups_structure_step extends restore_structure_step {
protected function define_structure() {
$paths = array(); // Add paths here
$paths[] = new restore_path_element('group', '/groups/group');
if ($this->get_setting_value('users')) {
$paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
}
$paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
$paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
return $paths;
}
// Processing functions go here
public function process_group($data) {
global $DB;
$data = (object)$data; // handy
$data->courseid = $this->get_courseid();
$oldid = $data->id; // need this saved for later
$restorefiles = false; // Only if we end creating the group
// Search if the group already exists (by name & description) in the target course
$description_clause = '';
$params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
if (!empty($data->description)) {
$description_clause = ' AND ' .
$DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc');
$params['desc'] = $data->description;
}
if (!$groupdb = $DB->get_record_sql("SELECT *
FROM {groups}
WHERE courseid = :courseid
AND name = :grname $description_clause", $params)) {
// group doesn't exist, create
$newitemid = $DB->insert_record('groups', $data);
$restorefiles = true; // We'll restore the files
} else {
// group exists, use it
$newitemid = $groupdb->id;
}
// Save the id mapping
$this->set_mapping('group', $oldid, $newitemid, $restorefiles);
}
public function process_member($data) {
global $DB;
$data = (object)$data; // handy
// get parent group->id
$data->groupid = $this->get_new_parentid('group');
// map user newitemid and insert if not member already
if ($data->userid = $this->get_mappingid('user', $data->userid)) {
if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
$DB->insert_record('groups_members', $data);
}
}
}
public function process_grouping($data) {
debugging('TODO: Grouping restore not implemented. Detected grouping', DEBUG_DEVELOPER);
}
public function process_grouping_group($data) {
debugging('TODO: Grouping restore not implemented. Detected grouping group', DEBUG_DEVELOPER);
}
protected function after_execute() {
return;
$this->add_related_files('group', 'icon', 'group');
$this->add_related_files('group', 'description', 'group');
restore_dbops::send_files_to_pool($basepath, $restoreid, 'user', 'private', $recuser->parentitemid);
}
}
/*
* Structure step that will read the course.xml file, loading it and performing
* various actions depending of the site/restore settings