MDL-21432 backup - root task, inforef and users handling completed, various fixes
This commit is contained in:
@@ -44,6 +44,7 @@ class restore_controller extends backup implements loggable {
|
||||
protected $userid; // user id executing the restore
|
||||
protected $operation; // Type of operation (backup/restore)
|
||||
protected $target; // Restoring to new/existing/current_adding/_deleting
|
||||
protected $samesite; // Are we restoring to the same site where the backup was generated
|
||||
|
||||
protected $status; // Current status of the controller (created, planned, configured...)
|
||||
|
||||
@@ -71,6 +72,7 @@ class restore_controller extends backup implements loggable {
|
||||
$this->execution = backup::EXECUTION_INMEDIATE;
|
||||
$this->operation = backup::OPERATION_RESTORE;
|
||||
$this->executiontime = 0;
|
||||
$this->samesite = false;
|
||||
$this->checksum = '';
|
||||
|
||||
// Apply current backup version and release if necessary
|
||||
@@ -173,6 +175,7 @@ class restore_controller extends backup implements loggable {
|
||||
'mode-' . $this->mode .
|
||||
'userid-' . $this->userid .
|
||||
'target-' . $this->target .
|
||||
'samesite-' . $this->samesite .
|
||||
'operation-' . $this->operation .
|
||||
'status-' . $this->status .
|
||||
'execution-' . $this->execution .
|
||||
@@ -227,6 +230,10 @@ class restore_controller extends backup implements loggable {
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
public function is_samesite() {
|
||||
return $this->samesite;
|
||||
}
|
||||
|
||||
public function get_status() {
|
||||
return $this->status;
|
||||
}
|
||||
@@ -355,6 +362,9 @@ class restore_controller extends backup implements loggable {
|
||||
// Set the controller type to the one found in the information
|
||||
$this->type = $this->info->type;
|
||||
|
||||
// Set the controller samesite flag as needed
|
||||
$this->samesite = backup_general_helper::backup_is_samesite($this->info);
|
||||
|
||||
// Now we load the plan that will be configured following the
|
||||
// information provided by the $info
|
||||
$this->log('loading controller plan', backup::LOG_DEBUG);
|
||||
|
||||
@@ -89,6 +89,46 @@ class backup_section_task extends backup_task {
|
||||
$this->built = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceptionally override the execute method, so, based in the section_included setting, we are able
|
||||
* to skip the execution of one task completely
|
||||
*/
|
||||
public function execute() {
|
||||
|
||||
// Find section_included_setting
|
||||
if (!$this->get_setting_value('included')) {
|
||||
$this->log('section skipped by _included setting', backup::LOG_DEBUG, $this->name);
|
||||
|
||||
} else { // Setting tells us it's ok to execute
|
||||
parent::execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialisation that, first of all, looks for the setting within
|
||||
* the task with the the prefix added and later, delegates to parent
|
||||
* without adding anything
|
||||
*/
|
||||
public function get_setting($name) {
|
||||
$namewithprefix = 'section_' . $this->sectionid . '_' . $name;
|
||||
$result = null;
|
||||
foreach ($this->settings as $key => $setting) {
|
||||
if ($setting->get_name() == $namewithprefix) {
|
||||
if ($result != null) {
|
||||
throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
|
||||
} else {
|
||||
$result = $setting;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($result) {
|
||||
return $result;
|
||||
} else {
|
||||
// Fallback to parent
|
||||
return parent::get_setting($name);
|
||||
}
|
||||
}
|
||||
|
||||
// Protected API starts here
|
||||
|
||||
/**
|
||||
|
||||
@@ -859,7 +859,7 @@ class backup_users_structure_step extends backup_structure_step {
|
||||
$normalfields = array(
|
||||
'confirmed', 'policyagreed', 'deleted',
|
||||
'lang', 'theme', 'timezone', 'firstaccess',
|
||||
'lastaccess', 'lastlogin', 'currentlogin', 'secret',
|
||||
'lastaccess', 'lastlogin', 'currentlogin',
|
||||
'mailformat', 'maildigest', 'maildisplay', 'htmleditor',
|
||||
'ajax', 'autosubscribe', 'trackforums', 'timecreated',
|
||||
'timemodified', 'trustbitmask', 'screenreader');
|
||||
@@ -867,7 +867,10 @@ class backup_users_structure_step extends backup_structure_step {
|
||||
// Then, the fields potentially needing anonymization
|
||||
$anonfields = array(
|
||||
'username', 'idnumber', 'firstname', 'lastname',
|
||||
'email', 'emailstop', 'lastip', 'picture',
|
||||
'email', 'emailstop', 'icq', 'skype',
|
||||
'yahoo', 'aim', 'msn', 'phone1',
|
||||
'phone2', 'institution', 'department', 'address',
|
||||
'city', 'country', 'lastip', 'picture',
|
||||
'url', 'description', 'description_format', 'imagealt', 'auth');
|
||||
|
||||
// Add anonymized fields to $userfields with custom final element
|
||||
@@ -1086,9 +1089,8 @@ class backup_inforef_structure_step extends backup_structure_step {
|
||||
|
||||
protected function define_structure() {
|
||||
|
||||
// Items we want to include in the inforef file. NOTE: Important to keep this
|
||||
// list 100% sync with the one in next step! Until we get better place for it (backup:CONST)
|
||||
$items = array('user', 'grouping', 'group', 'role', 'file', 'scale', 'outcome', 'grade_item');
|
||||
// Items we want to include in the inforef file.
|
||||
$items = backup_helper::get_inforef_itemnames();
|
||||
|
||||
// Build the tree
|
||||
|
||||
@@ -1098,7 +1100,7 @@ class backup_inforef_structure_step extends backup_structure_step {
|
||||
foreach ($items as $itemname) {
|
||||
if (backup_structure_dbops::annotations_exist($this->get_backupid(), $itemname)) {
|
||||
$elementroot = new backup_nested_element($itemname . 'ref');
|
||||
$element = new backup_nested_element($itemname, array('id'));
|
||||
$element = new backup_nested_element($itemname, array(), array('id'));
|
||||
$inforef->add_child($elementroot);
|
||||
$elementroot->add_child($element);
|
||||
$element->set_source_sql("
|
||||
@@ -1125,9 +1127,8 @@ class move_inforef_annotations_to_final extends backup_execution_step {
|
||||
|
||||
protected function define_execution() {
|
||||
|
||||
// Items we want to include in the inforef file. NOTE: Important to keep this
|
||||
// list 100% sync with the one in prev step! Until we get better place for it (backup:CONST)
|
||||
$items = array('user', 'grouping', 'group', 'role', 'file', 'scale', 'outcome', 'grade_item');
|
||||
// Items we want to include in the inforef file
|
||||
$items = backup_helper::get_inforef_itemnames();
|
||||
foreach ($items as $itemname) {
|
||||
// Delegate to dbops
|
||||
backup_structure_dbops::move_annotations_to_final($this->get_backupid(), $itemname);
|
||||
@@ -1191,7 +1192,7 @@ class backup_main_structure_step extends backup_structure_step {
|
||||
$info['backup_date'] = time();
|
||||
$info['backup_uniqueid']= $this->get_backupid();
|
||||
$info['original_wwwroot']=$CFG->wwwroot;
|
||||
$info['original_site_identifier'] = get_site_identifier();
|
||||
$info['original_site_identifier_hash'] = md5(get_site_identifier());
|
||||
$info['original_course_id'] = $this->get_courseid();
|
||||
|
||||
// Get more information from controller
|
||||
@@ -1204,7 +1205,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', 'original_course_id'));
|
||||
'original_site_identifier_hash', 'original_course_id'));
|
||||
|
||||
$details = new backup_nested_element('details');
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ abstract class restore_block_task extends restore_task {
|
||||
* Constructor - instantiates one object of this class
|
||||
*/
|
||||
public function __construct($name, $taskbasepath, $plan = null) {
|
||||
$this->$taskbasepath = $taskbasepath;
|
||||
$this->taskbasepath = $taskbasepath;
|
||||
parent::__construct($name, $plan);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ abstract class restore_block_task extends restore_task {
|
||||
* Block tasks have their own directory to write files
|
||||
*/
|
||||
public function get_taskbasepath() {
|
||||
return $this->$taskbasepath;
|
||||
return $this->taskbasepath;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +58,9 @@ abstract class restore_block_task extends restore_task {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: See backup. If "child" of activity task and it has been excluded, nothing to do
|
||||
|
||||
|
||||
// TODO: Link all the activity steps here
|
||||
|
||||
// At the end, mark it as built
|
||||
|
||||
@@ -54,6 +54,10 @@ class restore_course_task extends restore_task {
|
||||
public function build() {
|
||||
|
||||
// TODO: Link all the course steps here
|
||||
// Executed conditionally if restoring to new course or if overwrite_conf setting is enabled
|
||||
if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) {
|
||||
$this->add_step(new restore_course_structure_step('course_info', 'course.xml'));
|
||||
}
|
||||
|
||||
// At the end, mark it as built
|
||||
$this->built = true;
|
||||
@@ -73,7 +77,7 @@ class restore_course_task extends restore_task {
|
||||
// Protected API starts here
|
||||
|
||||
/**
|
||||
* Define the common setting that any restore section will have
|
||||
* Define the common setting that any restore course will have
|
||||
*/
|
||||
protected function define_settings() {
|
||||
|
||||
|
||||
@@ -35,7 +35,9 @@ class restore_final_task extends restore_task {
|
||||
*/
|
||||
public function build() {
|
||||
|
||||
// TODO: Link all the final steps here
|
||||
// Clean the temp dir (conditionally) and drop temp table
|
||||
$this->add_step(new restore_drop_and_clean_temp_stuff('drop_and_clean_temp_stuff'));
|
||||
|
||||
$this->built = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
|
||||
require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
|
||||
//require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
|
||||
require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
|
||||
//require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
|
||||
require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
|
||||
|
||||
// Load all the activity tasks for moodle2 format
|
||||
$mods = get_plugin_list('mod');
|
||||
|
||||
@@ -34,7 +34,19 @@ class restore_root_task extends restore_task {
|
||||
*/
|
||||
public function build() {
|
||||
|
||||
// TODO: Link all the preloading/precreation steps here
|
||||
// Conditionally create the temp table (can exist from prechecks) and delete old stuff
|
||||
$this->add_step(new restore_create_and_clean_temp_stuff('create_and_clean_temp_stuff'));
|
||||
|
||||
// If we haven't preloaded information, load all the included inforef records to temp_ids table
|
||||
$this->add_step(new restore_load_included_inforef_records('load_inforef_records'));
|
||||
|
||||
// If we haven't preloaded information and are restoring user info, load all the needed users to temp_ids table
|
||||
$this->add_step(new restore_load_included_users('load_user_records'));
|
||||
|
||||
// If we haven't preloaded information and are restoring user info, process all those needed users
|
||||
// creating/mapping them as needed. Any problem here will cause exception as far as prechecks have
|
||||
// performed the same process so, it's not possible to have errors here
|
||||
$this->add_step(new restore_process_included_users('process_user_records'));
|
||||
|
||||
// At the end, mark it as built
|
||||
$this->built = true;
|
||||
|
||||
@@ -45,7 +45,7 @@ class restore_section_task extends restore_task {
|
||||
*/
|
||||
public function get_taskbasepath() {
|
||||
|
||||
return $this->get_basepath() . '/sections/section_' . $info->sectionid;
|
||||
return $this->get_basepath() . '/sections/section_' . $this->info->sectionid;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,6 +59,46 @@ class restore_section_task extends restore_task {
|
||||
$this->built = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceptionally override the execute method, so, based in the section_included setting, we are able
|
||||
* to skip the execution of one task completely
|
||||
*/
|
||||
public function execute() {
|
||||
|
||||
// Find activity_included_setting
|
||||
if (!$this->get_setting_value('included')) {
|
||||
$this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name);
|
||||
|
||||
} else { // Setting tells us it's ok to execute
|
||||
parent::execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialisation that, first of all, looks for the setting within
|
||||
* the task with the the prefix added and later, delegates to parent
|
||||
* without adding anything
|
||||
*/
|
||||
public function get_setting($name) {
|
||||
$namewithprefix = 'section_' . $this->info->sectionid . '_' . $name;
|
||||
$result = null;
|
||||
foreach ($this->settings as $key => $setting) {
|
||||
if ($setting->get_name() == $namewithprefix) {
|
||||
if ($result != null) {
|
||||
throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
|
||||
} else {
|
||||
$result = $setting;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($result) {
|
||||
return $result;
|
||||
} else {
|
||||
// Fallback to parent
|
||||
return parent::get_setting($name);
|
||||
}
|
||||
}
|
||||
|
||||
// Protected API starts here
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* @package moodlecore
|
||||
* @subpackage backup-moodle2
|
||||
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
/**
|
||||
* Define all the restore steps that will be used by common tasks in restore
|
||||
*/
|
||||
|
||||
/**
|
||||
* delete old directories and conditionally create backup_temp_ids table
|
||||
*/
|
||||
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...)
|
||||
// that we aren't going to execute again
|
||||
if ($exists) { // Inform plan about preloaded information
|
||||
$this->task->set_preloaded_information();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delete the temp dir used by backup/restore (conditionally),
|
||||
* delete old directories and drop temp ids table
|
||||
*/
|
||||
class restore_drop_and_clean_temp_stuff extends restore_execution_step {
|
||||
|
||||
protected function define_execution() {
|
||||
global $CFG;
|
||||
backup_controller_dbops::drop_backup_ids_temp_table($this->get_restoreid()); // Drop ids temp table
|
||||
backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs
|
||||
if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally
|
||||
backup_helper::delete_backup_dir($this->get_restoreid()); // Empty backup dir
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Execution step that, *conditionally* (if there isn't preloaded information)
|
||||
* will load the inforef files for all the included course/section/activity tasks
|
||||
* to backup_temp_ids. They will be stored with "xxxxref" as itemname
|
||||
*/
|
||||
class restore_load_included_inforef_records extends restore_execution_step {
|
||||
|
||||
protected function define_execution() {
|
||||
|
||||
if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
// Get all the included inforef files
|
||||
$files = restore_dbops::get_needed_inforef_files($this->get_restoreid());
|
||||
foreach ($files as $file) {
|
||||
restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $file); // Load each inforef file to temp_ids
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
class restore_load_included_users extends restore_execution_step {
|
||||
|
||||
protected function define_execution() {
|
||||
|
||||
if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
|
||||
return;
|
||||
}
|
||||
if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do
|
||||
return;
|
||||
}
|
||||
$file = $this->get_basepath() . '/users.xml';
|
||||
restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execution step that, *conditionally* (if there isn't preloaded information
|
||||
* and users have been selected in settings, will process all the needed users
|
||||
* in order to decide and perform any action with them (create / map / error)
|
||||
* 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 {
|
||||
|
||||
protected function define_execution() {
|
||||
|
||||
if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
|
||||
return;
|
||||
}
|
||||
if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do
|
||||
return;
|
||||
}
|
||||
restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Structure step that will read the course.xml file, loading it and performing
|
||||
* various actions depending of the site/restore settings
|
||||
*/
|
||||
class restore_course_structure_step extends restore_structure_step {
|
||||
|
||||
protected function define_structure() {
|
||||
|
||||
$course = new restore_path_element('course', '/course', true); // Grouped
|
||||
$category = new restore_path_element('category', '/course/category');
|
||||
$tag = new restore_path_element('tag', '/course/tags/tag');
|
||||
$allowed = new restore_path_element('allowed', '/course/allowed_modules/module');
|
||||
|
||||
return array($course, $category, $tag, $allowed);
|
||||
}
|
||||
|
||||
// Processing functions go here
|
||||
public function process_course($data) {
|
||||
print_object('stopped before processing course. Continue here');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -83,4 +83,17 @@ abstract class restore_controller_dbops extends restore_dbops {
|
||||
}
|
||||
return $controller;
|
||||
}
|
||||
|
||||
public static function create_backup_ids_temp_table($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_backup_ids_temp_table($restoreid); // Create ids temp table
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,595 @@
|
||||
*
|
||||
* TODO: Finish phpdocs
|
||||
*/
|
||||
abstract class restore_dbops { }
|
||||
abstract class restore_dbops {
|
||||
|
||||
/**
|
||||
* Return all the inforef.xml files to be loaded into the temp_ids table
|
||||
* We do that by loading the controller from DB, then iterating over all the
|
||||
* included tasks and calculating all the inforef files for them
|
||||
*/
|
||||
public static function get_needed_inforef_files($restoreid) {
|
||||
$rc = restore_controller_dbops::load_controller($restoreid);
|
||||
$tasks = $rc->get_plan()->get_tasks();
|
||||
$files = array();
|
||||
foreach ($tasks as $task) {
|
||||
// Calculate if the task is being included
|
||||
$included = false;
|
||||
// blocks, based in blocks setting and parent activity/course
|
||||
if ($task instanceof restore_block_task) {
|
||||
if (!$task->get_setting('blocks')) { // Blocks not included, continue
|
||||
continue;
|
||||
}
|
||||
$parent = basename(dirname(dirname($task->get_taskbasepath())));
|
||||
if ($parent == 'course') { // Parent is course, always included if present
|
||||
$included = true;
|
||||
|
||||
} else { // Look for activity_included setting
|
||||
$included = $task->get_setting_value($parent . '_included');
|
||||
}
|
||||
|
||||
// ativities, based on included setting
|
||||
} else if ($task instanceof restore_activity_task) {
|
||||
$included = $task->get_setting_value('included');
|
||||
|
||||
// sections, based on included setting
|
||||
} else if ($task instanceof restore_section_task) {
|
||||
$included = $task->get_setting_value('included');
|
||||
|
||||
// course always included if present
|
||||
} else if ($task instanceof restore_course_task) {
|
||||
$included = true;
|
||||
}
|
||||
|
||||
// If included and file exists, add it to results
|
||||
if ($included) {
|
||||
$inforefpath = $task->get_taskbasepath() . '/inforef.xml';
|
||||
if (file_exists($inforefpath)) {
|
||||
$files[] = $inforefpath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load one inforef.xml file to backup_ids table for future reference
|
||||
*/
|
||||
public static function load_inforef_to_tempids($restoreid, $inforeffile) {
|
||||
|
||||
if (!file_exists($inforeffile)) { // Shouldn't happen ever, but...
|
||||
throw new backup_helper_exception('missing_inforef_xml_file', $inforeffile);
|
||||
}
|
||||
// Let's parse, custom processor will do its work, sending info to DB
|
||||
$xmlparser = new progressive_parser();
|
||||
$xmlparser->set_file($inforeffile);
|
||||
$xmlprocessor = new restore_inforef_parser_processor($restoreid);
|
||||
$xmlparser->set_processor($xmlprocessor);
|
||||
$xmlparser->process();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the needed users.xml file to backup_ids table for future reference
|
||||
*/
|
||||
public static function load_users_to_tempids($restoreid, $usersfile) {
|
||||
|
||||
if (!file_exists($usersfile)) { // Shouldn't happen ever, but...
|
||||
throw new backup_helper_exception('missing_users_xml_file', $inforeffile);
|
||||
}
|
||||
// Let's parse, custom processor will do its work, sending info to DB
|
||||
$xmlparser = new progressive_parser();
|
||||
$xmlparser->set_file($usersfile);
|
||||
$xmlprocessor = new restore_users_parser_processor($restoreid);
|
||||
$xmlparser->set_processor($xmlprocessor);
|
||||
$xmlparser->process();
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one restoreid, create in DB all the users present
|
||||
* in backup_ids having newitemid = 0, as far as
|
||||
* precheck_included_users() have left them there
|
||||
* ready to be created. Also, annotate their newids
|
||||
* once created for later reference
|
||||
*/
|
||||
protected static function create_included_users($restoreid) {
|
||||
global $CFG, $DB;
|
||||
|
||||
$authcache = array(); // Cache to get some bits from authentication plugins
|
||||
$languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search later
|
||||
$themes = get_list_of_themes(); // Get themes for quick search later
|
||||
|
||||
// Iterate over all the included users with newitemid = 0, have to create them
|
||||
$rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'user', 'newitemid' => 0), '', 'itemid, parentitemid');
|
||||
foreach ($rs as $recuser) {
|
||||
$user = (object)self::get_backup_ids_record($restoreid, 'user', $recuser->itemid)->info;
|
||||
|
||||
// if user lang doesn't exist here, use site default
|
||||
if (!array_key_exists($user->lang, $languages)) {
|
||||
$user->lang = $CFG->lang;
|
||||
}
|
||||
|
||||
// if user theme isn't available on target site or they are disabled, reset theme
|
||||
if (!empty($user->theme)) {
|
||||
if (empty($CFG->allowuserthemes) || !in_array($user->theme, $themes)) {
|
||||
$user->theme = '';
|
||||
}
|
||||
}
|
||||
|
||||
// if user to be created has mnet auth and its mnethostid is $CFG->mnet_localhost_id
|
||||
// that's 100% impossible as own server cannot be accesed over mnet. Change auth to email/manual
|
||||
if ($user->auth == 'mnet' && $user->mnethostid == $CFG->mnet_localhost_id) {
|
||||
// Respect registerauth
|
||||
if ($CFG->registerauth == 'email') {
|
||||
$user->auth = 'email';
|
||||
} else {
|
||||
$user->auth = 'manual';
|
||||
}
|
||||
}
|
||||
unset($user->mnethosturl); // Not needed anymore
|
||||
|
||||
// Disable pictures based on global setting
|
||||
if (!empty($CFG->disableuserimages)) {
|
||||
$user->picture = 0;
|
||||
}
|
||||
|
||||
// We need to analyse the AUTH field to recode it:
|
||||
// - if the auth isn't enabled in target site, $CFG->registerauth will decide
|
||||
// - finally, if the auth resulting isn't enabled, default to 'manual'
|
||||
if (!is_enabled_auth($user->auth)) {
|
||||
if ($CFG->registerauth == 'email') {
|
||||
$user->auth = 'email';
|
||||
} else {
|
||||
$user->auth = 'manual';
|
||||
}
|
||||
}
|
||||
if (!is_enabled_auth($user->auth)) { // Final auth check verify, default to manual if not enabled
|
||||
$user->auth = 'manual';
|
||||
}
|
||||
|
||||
// Now that we know the auth method, for users to be created without pass
|
||||
// if password handling is internal and reset password is available
|
||||
// we set the password to "restored" (plain text), so the login process
|
||||
// will know how to handle that situation in order to allow the user to
|
||||
// recover the password. MDL-20846
|
||||
if (empty($user->password)) { // Only if restore comes without password
|
||||
if (!array_key_exists($user->auth, $authcache)) { // Not in cache
|
||||
$userauth = new stdClass();
|
||||
$authplugin = get_auth_plugin($user->auth);
|
||||
$userauth->preventpassindb = $authplugin->prevent_local_passwords();
|
||||
$userauth->isinternal = $authplugin->is_internal();
|
||||
$userauth->canresetpwd = $authplugin->can_reset_password();
|
||||
$authcache[$user->auth] = $userauth;
|
||||
} else {
|
||||
$userauth = $authcache[$user->auth]; // Get from cache
|
||||
}
|
||||
|
||||
// Most external plugins do not store passwords locally
|
||||
if (!empty($userauth->preventpassindb)) {
|
||||
$user->password = 'not cached';
|
||||
|
||||
// If Moodle is responsible for storing/validating pwd and reset functionality is available, mark
|
||||
} else if ($userauth->isinternal and $userauth->canresetpwd) {
|
||||
$user->password = 'restored';
|
||||
}
|
||||
}
|
||||
|
||||
// Creating new user, we must reset the policyagreed always
|
||||
$user->policyagreed = 0;
|
||||
|
||||
// Set time created if empty
|
||||
if (empty($user->timecreated)) {
|
||||
$user->timecreated = time();
|
||||
}
|
||||
|
||||
// Done, let's create the user and annotate its id
|
||||
$newuserid = $DB->insert_record('user', $user);
|
||||
self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $newuserid);
|
||||
// Let's create the user context and annotate it (we need it for sure at least for files)
|
||||
$newuserctxid = get_context_instance(CONTEXT_USER, $newuserid)->id;
|
||||
self::set_backup_ids_record($restoreid, 'context', $recuser->parentitemid, $newuserctxid);
|
||||
|
||||
// Process custom fields
|
||||
if (isset($user->custom_fields)) { // if present in backup
|
||||
foreach($user->custom_fields['custom_field'] as $udata) {
|
||||
$udata = (object)$udata;
|
||||
// If the profile field has data and the profile shortname-datatype is defined in server
|
||||
if ($udata->field_data) {
|
||||
if ($field = $DB->get_record('user_info_field', array('shortname'=>$udata->field_name, 'datatype'=>$udata->field_type))) {
|
||||
/// Insert the user_custom_profile_field
|
||||
$rec = new object();
|
||||
$rec->userid = $newuserid;
|
||||
$rec->fieldid = $field->id;
|
||||
$rec->data = $udata->field_data;
|
||||
$DB->insert_record('user_info_data', $rec);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process tags
|
||||
if (!empty($CFG->usetags) && isset($user->tags)) { // if enabled in server and present in backup
|
||||
$tags = array();
|
||||
foreach($user->tags['tag'] as $usertag) {
|
||||
$usertag = (object)$usertag;
|
||||
$tags[] = $usertag->rawname;
|
||||
}
|
||||
tag_set('user', $newuserid, $tags);
|
||||
}
|
||||
|
||||
// Process preferences
|
||||
if (isset($user->preferences)) { // if present in backup
|
||||
foreach($user->preferences as $preference) {
|
||||
$preference = (object)$preference;
|
||||
// Prepare the record and insert it
|
||||
$preference->userid = $newuserid;
|
||||
$status = $DB->insert_record('user_preferences', $preference);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
$rs->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one user object (from backup file), perform all the neccesary
|
||||
* checks is order to decide how that user will be handled on restore.
|
||||
*
|
||||
* Note the function requires $user->mnethostid to be already calculated
|
||||
* so it's caller responsibility to set it
|
||||
*
|
||||
* This function is used both by @restore_precheck_users() and
|
||||
* @restore_create_users() to get consistent results in both places
|
||||
*
|
||||
* It returns:
|
||||
* - one user object (from DB), if match has been found and user will be remapped
|
||||
* - boolean true if the user needs to be created
|
||||
* - boolean false if some conflict happened and the user cannot be handled
|
||||
*
|
||||
* Each test is responsible for returning its results and interrupt
|
||||
* execution. At the end, boolean true (user needs to be created) will be
|
||||
* returned if no test has interrupted that.
|
||||
*
|
||||
* Here it's the logic applied, keep it updated:
|
||||
*
|
||||
* If restoring users from same site backup:
|
||||
* 1A - Normal check: If match by id and username and mnethost => ok, return target user
|
||||
* 1B - Handle users deleted in DB and "alive" in backup file:
|
||||
* If match by id and mnethost and user is deleted in DB and
|
||||
* (match by username LIKE 'backup_email.%' or by non empty email = md5(username)) => ok, return target user
|
||||
* 1C - Handle users deleted in backup file and "alive" in DB:
|
||||
* If match by id and mnethost and user is deleted in backup file
|
||||
* and match by email = email_without_time(backup_email) => ok, return target user
|
||||
* 1D - Conflict: If match by username and mnethost and doesn't match by id => conflict, return false
|
||||
* 1E - None of the above, return true => User needs to be created
|
||||
*
|
||||
* if restoring from another site backup (cannot match by id here, replace it by email/firstaccess combination):
|
||||
* 2A - Normal check: If match by username and mnethost and (email or non-zero firstaccess) => ok, return target user
|
||||
* 2B - Handle users deleted in DB and "alive" in backup file:
|
||||
* 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
|
||||
* (username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
|
||||
* 2B2 - If match by mnethost and user is deleted in DB and
|
||||
* username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
|
||||
* (to cover situations were md5(username) wasn't implemented on delete we requiere both)
|
||||
* 2C - Handle users deleted in backup file and "alive" in DB:
|
||||
* If match mnethost and user is deleted in backup file
|
||||
* and by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
|
||||
* 2D - Conflict: If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
|
||||
* 2E - None of the above, return true => User needs to be created
|
||||
*
|
||||
* Note: for DB deleted users email is stored in username field, hence we
|
||||
* are looking there for emails. See delete_user()
|
||||
* Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
|
||||
* hence we are looking there for usernames if not empty. See delete_user()
|
||||
*/
|
||||
protected static function precheck_user($user, $samesite) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// Handle checks from same site backups
|
||||
if ($samesite && empty($CFG->forcedifferentsitecheckingusersonrestore)) {
|
||||
|
||||
// 1A - If match by id and username and mnethost => ok, return target user
|
||||
if ($rec = $DB->get_record('user', array('id'=>$user->id, 'username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
|
||||
return $rec; // Matching user found, return it
|
||||
}
|
||||
|
||||
// 1B - Handle users deleted in DB and "alive" in backup file
|
||||
// Note: for DB deleted users email is stored in username field, hence we
|
||||
// are looking there for emails. See delete_user()
|
||||
// Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
|
||||
// hence we are looking there for usernames if not empty. See delete_user()
|
||||
// If match by id and mnethost and user is deleted in DB and
|
||||
// match by username LIKE 'backup_email.%' or by non empty email = md5(username) => ok, return target user
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE id = ?
|
||||
AND mnethostid = ?
|
||||
AND deleted = 1
|
||||
AND (
|
||||
username LIKE ?
|
||||
OR (
|
||||
".$DB->sql_isnotempty('user', 'email', false, false)."
|
||||
AND email = ?
|
||||
)
|
||||
)",
|
||||
array($user->id, $user->mnethostid, $user->email.'.%', md5($user->username)))) {
|
||||
return $rec; // Matching user, deleted in DB found, return it
|
||||
}
|
||||
|
||||
// 1C - Handle users deleted in backup file and "alive" in DB
|
||||
// If match by id and mnethost and user is deleted in backup file
|
||||
// and match by email = email_without_time(backup_email) => ok, return target user
|
||||
if ($user->deleted) {
|
||||
// Note: for DB deleted users email is stored in username field, hence we
|
||||
// are looking there for emails. See delete_user()
|
||||
// Trim time() from email
|
||||
$trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE id = ?
|
||||
AND mnethostid = ?
|
||||
AND email = ?",
|
||||
array($user->id, $user->mnethostid, $trimemail))) {
|
||||
return $rec; // Matching user, deleted in backup file found, return it
|
||||
}
|
||||
}
|
||||
|
||||
// 1D - If match by username and mnethost and doesn't match by id => conflict, return false
|
||||
if ($rec = $DB->get_record('user', array('username'=>$user->username, 'mnethostid'=>$user->mnethostid))) {
|
||||
if ($user->id != $rec->id) {
|
||||
return false; // Conflict, username already exists and belongs to another id
|
||||
}
|
||||
}
|
||||
|
||||
// Handle checks from different site backups
|
||||
} else {
|
||||
|
||||
// 2A - If match by username and mnethost and
|
||||
// (email or non-zero firstaccess) => ok, return target user
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE username = ?
|
||||
AND mnethostid = ?
|
||||
AND (
|
||||
email = ?
|
||||
OR (
|
||||
firstaccess != 0
|
||||
AND firstaccess = ?
|
||||
)
|
||||
)",
|
||||
array($user->username, $user->mnethostid, $user->email, $user->firstaccess))) {
|
||||
return $rec; // Matching user found, return it
|
||||
}
|
||||
|
||||
// 2B - Handle users deleted in DB and "alive" in backup file
|
||||
// Note: for DB deleted users email is stored in username field, hence we
|
||||
// are looking there for emails. See delete_user()
|
||||
// Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
|
||||
// hence we are looking there for usernames if not empty. See delete_user()
|
||||
// 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
|
||||
// (by username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE mnethostid = ?
|
||||
AND deleted = 1
|
||||
AND ".$DB->sql_isnotempty('user', 'email', false, false)."
|
||||
AND email = ?
|
||||
AND (
|
||||
username LIKE ?
|
||||
OR (
|
||||
firstaccess != 0
|
||||
AND firstaccess = ?
|
||||
)
|
||||
)",
|
||||
array($user->mnethostid, md5($user->username), $user->email.'.%', $user->firstaccess))) {
|
||||
return $rec; // Matching user found, return it
|
||||
}
|
||||
|
||||
// 2B2 - If match by mnethost and user is deleted in DB and
|
||||
// username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
|
||||
// (this covers situations where md5(username) wasn't being stored so we require both
|
||||
// the email & non-zero firstaccess to match)
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE mnethostid = ?
|
||||
AND deleted = 1
|
||||
AND username LIKE ?
|
||||
AND firstaccess != 0
|
||||
AND firstaccess = ?",
|
||||
array($user->mnethostid, $user->email.'.%', $user->firstaccess))) {
|
||||
return $rec; // Matching user found, return it
|
||||
}
|
||||
|
||||
// 2C - Handle users deleted in backup file and "alive" in DB
|
||||
// If match mnethost and user is deleted in backup file
|
||||
// and match by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
|
||||
if ($user->deleted) {
|
||||
// Note: for DB deleted users email is stored in username field, hence we
|
||||
// are looking there for emails. See delete_user()
|
||||
// Trim time() from email
|
||||
$trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE mnethostid = ?
|
||||
AND email = ?
|
||||
AND firstaccess != 0
|
||||
AND firstaccess = ?",
|
||||
array($user->mnethostid, $trimemail, $user->firstaccess))) {
|
||||
return $rec; // Matching user, deleted in backup file found, return it
|
||||
}
|
||||
}
|
||||
|
||||
// 2D - If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
|
||||
if ($rec = $DB->get_record_sql("SELECT *
|
||||
FROM {user} u
|
||||
WHERE username = ?
|
||||
AND mnethostid = ?
|
||||
AND NOT (
|
||||
email = ?
|
||||
OR (
|
||||
firstaccess != 0
|
||||
AND firstaccess = ?
|
||||
)
|
||||
)",
|
||||
array($user->username, $user->mnethostid, $user->email, $user->firstaccess))) {
|
||||
return false; // Conflict, username/mnethostid already exist and belong to another user (by email/firstaccess)
|
||||
}
|
||||
}
|
||||
|
||||
// Arrived here, return true as the user will need to be created and no
|
||||
// conflicts have been found in the logic above. This covers:
|
||||
// 1E - else => user needs to be created, return true
|
||||
// 2E - else => user needs to be created, return true
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check all the included users, deciding the action to perform
|
||||
* for each one (mapping / creation) and returning one array
|
||||
* of problems in case something is wrong (lack of permissions,
|
||||
* conficts)
|
||||
*/
|
||||
public static function precheck_included_users($restoreid, $courseid, $userid, $samesite) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// To return any problem found
|
||||
$problems = array();
|
||||
|
||||
// We are going to map mnethostid, so load all the available ones
|
||||
$mnethosts = $DB->get_records('mnet_host', array(), 'wwwroot', 'wwwroot, id');
|
||||
|
||||
// Calculate the context we are going to use for capability checking
|
||||
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
||||
|
||||
// Calculate if we have perms to create users, by checking:
|
||||
// to 'moodle/restore:createuser' and 'moodle/restore:userinfo'
|
||||
// and also observe $CFG->disableusercreationonrestore
|
||||
$cancreateuser = false;
|
||||
if (has_capability('moodle/restore:createuser', $context, $userid) and
|
||||
has_capability('moodle/restore:userinfo', $context, $userid) and
|
||||
empty($CFG->disableusercreationonrestore)) { // Can create users
|
||||
|
||||
$cancreateuser = true;
|
||||
}
|
||||
|
||||
// Iterate over all the included users
|
||||
$rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'user'), '', 'itemid');
|
||||
foreach ($rs as $recuser) {
|
||||
$user = (object)self::get_backup_ids_record($restoreid, 'user', $recuser->itemid)->info;
|
||||
|
||||
// Find the correct mnethostid for user before performing any further check
|
||||
if (empty($user->mnethosturl) || $user->mnethosturl === $CFG->wwwroot) {
|
||||
$user->mnethostid = $CFG->mnet_localhost_id;
|
||||
} else {
|
||||
// fast url-to-id lookups
|
||||
if (isset($mnethosts[$user->mnethosturl])) {
|
||||
$user->mnethostid = $mnethosts[$user->mnethosturl]->id;
|
||||
} else {
|
||||
$user->mnethostid = $CFG->mnet_localhost_id;
|
||||
}
|
||||
}
|
||||
|
||||
// Now, precheck that user and, based on returned results, annotate action/problem
|
||||
$usercheck = self::precheck_user($user, $samesite);
|
||||
|
||||
if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to
|
||||
// Annotate it, for later process. Set newitemid to mapping user->id
|
||||
self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, $usercheck->id);
|
||||
|
||||
} else if ($usercheck === false) { // Found conflict, report it as problem
|
||||
$problems[] = get_string('restoreuserconflict', '', $user->username);
|
||||
|
||||
} else if ($usercheck === true) { // User needs to be created, check if we are able
|
||||
if ($cancreateuser) { // Can create user, set newitemid to 0 so will be created later
|
||||
self::set_backup_ids_record($restoreid, 'user', $recuser->itemid, 0, null, (array)$user);
|
||||
|
||||
} else { // Cannot create user, report it as problem
|
||||
$problems[] = get_string('restorecannotcreateuser', '', $user->username);
|
||||
}
|
||||
|
||||
} else { // Shouldn't arrive here ever, something is for sure wrong. Exception
|
||||
throw new restore_dbops_exception('restore_error_processing_user', $user->username);
|
||||
}
|
||||
}
|
||||
$rs->close();
|
||||
return $problems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the needed users in order to create / map them
|
||||
*
|
||||
* Just wrap over precheck_included_users(), returning
|
||||
* exception if any problem is found or performing the
|
||||
* required user creations if needed
|
||||
*/
|
||||
public static function process_included_users($restoreid, $courseid, $userid, $samesite) {
|
||||
global $DB;
|
||||
|
||||
// Just let precheck_included_users() to do all the hard work
|
||||
$problems = self::precheck_included_users($restoreid, $courseid, $userid, $samesite);
|
||||
|
||||
// With problems, throw exception, shouldn't happen if prechecks were originally
|
||||
// executed, so be radical here.
|
||||
if (!empty($problems)) {
|
||||
throw new restore_dbops_exception('restore_problems_processing_users', null, implode(', ', $problems));
|
||||
}
|
||||
|
||||
// Now, create all the users that we haven't been able to map
|
||||
self::create_included_users($restoreid);
|
||||
}
|
||||
|
||||
|
||||
public static function set_backup_ids_record($restoreid, $itemname, $itemid, $newitemid = 0, $parentitemid = null, $info = null) {
|
||||
global $DB;
|
||||
|
||||
// Build the basic (mandatory) record info
|
||||
$record = array(
|
||||
'backupid' => $restoreid,
|
||||
'itemname' => $itemname,
|
||||
'itemid' => $itemid
|
||||
);
|
||||
// Build conditionally the extra record info
|
||||
$extrarecord = array();
|
||||
if ($newitemid != 0) {
|
||||
$extrarecord['newitemid'] = $newitemid;
|
||||
}
|
||||
if ($parentitemid != null) {
|
||||
$extrarecord['parentitemid'] = $parentitemid;
|
||||
}
|
||||
if ($info != null) {
|
||||
$extrarecord['info'] = base64_encode(serialize($info));
|
||||
}
|
||||
|
||||
// TODO: Analyze if some static (and limited) cache by the 3 params could save us a bunch of get_record() calls
|
||||
// Note: Sure it will! And also will improve getter
|
||||
if (!$dbrec = $DB->get_record('backup_ids_temp', $record)) { // Need to insert the complete record
|
||||
$DB->insert_record('backup_ids_temp', array_merge($record, $extrarecord));
|
||||
|
||||
} else { // Need to update the extra record info if there is something to
|
||||
if (!empty($extrarecord)) {
|
||||
$extrarecord['id'] = $dbrec->id;
|
||||
$DB->update_record('backup_ids_temp', $extrarecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function get_backup_ids_record($restoreid, $itemname, $itemid) {
|
||||
global $DB;
|
||||
|
||||
// Build the basic (mandatory) record info to look for
|
||||
$record = array(
|
||||
'backupid' => $restoreid,
|
||||
'itemname' => $itemname,
|
||||
'itemid' => $itemid
|
||||
);
|
||||
// TODO: Analyze if some static (and limited) cache by the 3 params could save us a bunch of get_record() calls
|
||||
if ($dbrec = $DB->get_record('backup_ids_temp', $record)) {
|
||||
if ($dbrec->info != null) {
|
||||
$dbrec->info = unserialize(base64_decode($dbrec->info));
|
||||
}
|
||||
}
|
||||
return $dbrec;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Exception class used by all the @dbops stuff
|
||||
|
||||
@@ -82,6 +82,54 @@ class backup_anonymizer_helper {
|
||||
return 1; // Stop email for anon users
|
||||
}
|
||||
|
||||
public static function process_user_icq($value) {
|
||||
return ''; // Clean icq
|
||||
}
|
||||
|
||||
public static function process_user_skype($value) {
|
||||
return ''; // Clean skype
|
||||
}
|
||||
|
||||
public static function process_user_yahoo($value) {
|
||||
return ''; // Clean yahoo
|
||||
}
|
||||
|
||||
public static function process_user_aim($value) {
|
||||
return ''; // Clean aim
|
||||
}
|
||||
|
||||
public static function process_user_msn($value) {
|
||||
return ''; // Clean msn
|
||||
}
|
||||
|
||||
public static function process_user_phone1($value) {
|
||||
return ''; // Clean phone1
|
||||
}
|
||||
|
||||
public static function process_user_phone2($value) {
|
||||
return ''; // Clean phone2
|
||||
}
|
||||
|
||||
public static function process_user_institution($value) {
|
||||
return ''; // Clean institution
|
||||
}
|
||||
|
||||
public static function process_user_department($value) {
|
||||
return ''; // Clean department
|
||||
}
|
||||
|
||||
public static function process_user_address($value) {
|
||||
return ''; // Clean address
|
||||
}
|
||||
|
||||
public static function process_user_city($value) {
|
||||
return 'Perth'; // Set city
|
||||
}
|
||||
|
||||
public static function process_user_country($value) {
|
||||
return 'AU'; // Set country
|
||||
}
|
||||
|
||||
public static function process_user_lastip($value) {
|
||||
return '127.0.0.1'; // Set lastip to localhost
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ abstract class backup_general_helper extends backup_helper {
|
||||
|
||||
$moodlefile = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle_backup.xml';
|
||||
if (!file_exists($moodlefile)) { // Shouldn't happen ever, but...
|
||||
throw new backup_helper_exception('missing_moodle_backup_xml_file');
|
||||
throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile);
|
||||
}
|
||||
// Load the entire file to in-memory array
|
||||
$xmlparser = new progressive_parser();
|
||||
@@ -137,7 +137,7 @@ abstract class backup_general_helper extends backup_helper {
|
||||
$info->backup_release = $infoarr['backup_release'];
|
||||
$info->backup_date = $infoarr['backup_date'];
|
||||
$info->original_wwwroot = $infoarr['original_wwwroot'];
|
||||
$info->original_site_identifier = $infoarr['original_site_identifier'];
|
||||
$info->original_site_identifier_hash = $infoarr['original_site_identifier_hash'];
|
||||
$info->original_course_id = $infoarr['original_course_id'];
|
||||
$info->type = $infoarr['details']['detail'][0]['type'];
|
||||
$info->format = $infoarr['details']['detail'][0]['format'];
|
||||
@@ -196,6 +196,25 @@ abstract class backup_general_helper extends backup_helper {
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the information fetched from moodle_backup.xml file
|
||||
* decide if we are restoring in the same site the backup was
|
||||
* generated or no. Behavior of various parts of restore are
|
||||
* dependent of this.
|
||||
*
|
||||
* Use site_identifier (hashed) and fallback to wwwroot, thought
|
||||
* any 2.0 backup should have the former. See MDL-16614
|
||||
*/
|
||||
public static function backup_is_samesite($info) {
|
||||
global $CFG;
|
||||
$hashedsiteid = md5(get_site_identifier());
|
||||
if (isset($info->original_site_identifier_hash) && !empty($info->original_site_identifier_hash)) {
|
||||
return $info->original_site_identifier_hash == $hashedsiteid;
|
||||
} else {
|
||||
return $info->original_wwwroot == $CFG->wwwroot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given one temp/backup/xxx dir, detect its format
|
||||
*
|
||||
|
||||
@@ -281,6 +281,14 @@ abstract class backup_helper {
|
||||
public static function is_sqlparam($value) {
|
||||
return array('sqlparam' => $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns one array of itemnames that are being handled by
|
||||
* inforef.xml files. Used both by backup and restore
|
||||
*/
|
||||
public static function get_inforef_itemnames() {
|
||||
return array('user', 'grouping', 'group', 'role', 'file', 'scale', 'outcome', 'grade_item');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -48,8 +48,8 @@ class restore_structure_parser_processor extends grouped_parser_processor {
|
||||
* Provide NULL and legacy file.php uses decoding
|
||||
*/
|
||||
public function process_cdata($cdata) {
|
||||
if (is_null($cdata)) { // Some cases we know we can skip complete processing
|
||||
return '$@NULL@$';
|
||||
if ($cdata === '$@NULL@$') { // Some cases we know we can skip complete processing
|
||||
return null;
|
||||
} else if ($cdata === '') {
|
||||
return '';
|
||||
} else if (is_numeric($cdata)) {
|
||||
|
||||
@@ -31,7 +31,11 @@ if (!defined('MOODLE_INTERNAL')) {
|
||||
require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/interfaces/loggable.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/structure/restore_path_element.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/helper/restore_moodlexml_parser_processor.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/helper/restore_inforef_parser_processor.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/helper/restore_users_parser_processor.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/helper/restore_structure_parser_processor.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/xml/parser/progressive_parser.class.php');
|
||||
require_once($CFG->dirroot . '/backup/backup.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/output/output_controller.class.php');
|
||||
@@ -62,6 +66,7 @@ require_once($CFG->dirroot . '/backup/util/plan/base_task.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/plan/restore_task.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/plan/base_step.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/plan/restore_step.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/plan/restore_structure_step.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/plan/restore_execution_step.class.php');
|
||||
require_once($CFG->dirroot . '/backup/moodle2/restore_plan_builder.class.php');
|
||||
require_once($CFG->dirroot . '/backup/controller/restore_controller.class.php');
|
||||
@@ -70,3 +75,4 @@ require_once($CFG->dirroot . '/backup/controller/restore_controller.class.php');
|
||||
require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php');
|
||||
|
||||
// And some moodle stuff too
|
||||
require_once ($CFG->dirroot . '/tag/lib.php');
|
||||
|
||||
@@ -31,6 +31,7 @@ class restore_plan extends base_plan implements loggable {
|
||||
|
||||
protected $controller; // The restore controller building/executing this plan
|
||||
protected $basepath; // Fullpath to dir where backup is available
|
||||
protected $preloaded; // When executing the plan, do we have preloaded (from checks) info
|
||||
|
||||
/**
|
||||
* Constructor - instantiates one object of this class
|
||||
@@ -43,6 +44,7 @@ class restore_plan extends base_plan implements loggable {
|
||||
}
|
||||
$this->controller = $controller;
|
||||
$this->basepath = $CFG->dataroot . '/temp/backup/' . $controller->get_tempdir();
|
||||
$this->preloaded = false;
|
||||
parent::__construct('restore_plan');
|
||||
}
|
||||
|
||||
@@ -75,6 +77,22 @@ class restore_plan extends base_plan implements loggable {
|
||||
return $this->controller->get_target();
|
||||
}
|
||||
|
||||
public function get_userid() {
|
||||
return $this->controller->get_userid();
|
||||
}
|
||||
|
||||
public function is_samesite() {
|
||||
return $this->controller->is_samesite();
|
||||
}
|
||||
|
||||
public function set_preloaded_information() {
|
||||
$this->preloaded = true;
|
||||
}
|
||||
|
||||
public function get_preloaded_information() {
|
||||
return $this->preloaded;
|
||||
}
|
||||
|
||||
public function log($message, $level, $a = null, $depth = null, $display = false) {
|
||||
backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
|
||||
}
|
||||
|
||||
@@ -46,6 +46,10 @@ abstract class restore_structure_step extends restore_step {
|
||||
|
||||
public function execute() {
|
||||
|
||||
if (!$this->execute_condition()) { // Check any condition to execute this
|
||||
return;
|
||||
}
|
||||
|
||||
$fullpath = $this->task->get_taskbasepath();
|
||||
|
||||
// We MUST have one fullpath here, else, error
|
||||
@@ -151,6 +155,18 @@ abstract class restore_structure_step extends restore_step {
|
||||
return false; // no grouped parent found
|
||||
}
|
||||
|
||||
/**
|
||||
* To conditionally decide if one step will be executed or no
|
||||
*
|
||||
* For steps needing to be executed conditionally, based in dynamic
|
||||
* conditions (at execution time vs at declaration time) you must
|
||||
* override this function. It will return true if the step must be
|
||||
* executed and false if not
|
||||
*/
|
||||
protected function execute_condition() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that will return the structure to be processed by this restore_step.
|
||||
* Must return one array of @restore_path_element elements
|
||||
|
||||
@@ -50,6 +50,22 @@ abstract class restore_task extends base_task {
|
||||
public function get_target() {
|
||||
return $this->plan->get_target();
|
||||
}
|
||||
|
||||
public function get_userid() {
|
||||
return $this->plan->get_userid();
|
||||
}
|
||||
|
||||
public function is_samesite() {
|
||||
return $this->plan->is_samesite();
|
||||
}
|
||||
|
||||
public function set_preloaded_information() {
|
||||
$this->plan->set_preloaded_information();
|
||||
}
|
||||
|
||||
public function get_preloaded_information() {
|
||||
return $this->plan->get_preloaded_information();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user