From c3ea499d53ea8b8e33ad026137d08c64a34640e1 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Sun, 18 Jul 2010 23:49:55 +0000 Subject: [PATCH] MDL-21432 backup - handle mnet_remoteusers and support for prechecks --- backup/backup.class.php | 2 +- .../controller/restore_controller.class.php | 28 ++++++++++++++----- backup/moodle2/backup_stepslib.php | 3 +- backup/moodle2/restore_root_task.class.php | 4 +++ .../dbops/backup_controller_dbops.class.php | 16 +++++++++++ .../helper/backup_general_helper.class.php | 1 + backup/util/includes/restore_includes.php | 3 +- 7 files changed, 47 insertions(+), 10 deletions(-) diff --git a/backup/backup.class.php b/backup/backup.class.php index 8a3209c53af..d854368c072 100644 --- a/backup/backup.class.php +++ b/backup/backup.class.php @@ -106,7 +106,7 @@ abstract class backup implements checksumable { const OPERATION_RESTORE ='restore';// We are performing one restore // Version (to keep CFG->backup_version (and release) updated automatically) - const VERSION = 2010070500; + const VERSION = 2010071800; const RELEASE = '2.0 Preview 5'; } diff --git a/backup/controller/restore_controller.class.php b/backup/controller/restore_controller.class.php index 659cb6547c1..b8175827b05 100644 --- a/backup/controller/restore_controller.class.php +++ b/backup/controller/restore_controller.class.php @@ -47,6 +47,7 @@ class restore_controller extends backup implements loggable { protected $samesite; // Are we restoring to the same site where the backup was generated protected $status; // Current status of the controller (created, planned, configured...) + protected $precheck; // Results of the execution of restore prechecks protected $info; // Information retrieved from backup contents protected $plan; // Restore execution plan @@ -74,6 +75,7 @@ class restore_controller extends backup implements loggable { $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(); @@ -178,6 +180,7 @@ class restore_controller extends backup implements loggable { 'samesite-' . $this->samesite . 'operation-' . $this->operation . 'status-' . $this->status . + 'precheck-' . backup_general_helper::array_checksum_recursive(array($this->precheck)) . 'execution-' . $this->execution . 'plan-' . backup_general_helper::array_checksum_recursive(array($this->plan)) . 'info-' . backup_general_helper::array_checksum_recursive(array($this->info)) . @@ -263,10 +266,20 @@ class restore_controller extends backup implements loggable { } public function execute_precheck() { - debugging ('TODO: Not applying prechecks yet, need to link them to proper restore_precheck class!', DEBUG_DEVELOPER); - $this->set_status(backup::STATUS_AWAITING); // TODO: Delete this once prechecks and steps are in place - //return $this->precheck->execute(); - return true; + if (is_array($this->precheck)) { + throw new restore_controller_exception('precheck_alredy_executed', $this->status); + } + if ($this->status != backup::STATUS_NEED_PRECHECK) { + throw new restore_controller_exception('cannot_precheck_wrong_status', $this->status); + } + $this->precheck = restore_prechecks_helper::execute_prechecks($this); + if (!array_key_exists('errors', $this->precheck)) { // No errors, can be executed + $this->set_status(backup::STATUS_AWAITING); + } + if (empty($this->precheck)) { // No errors nor warnings, return true + return true; + } + return false; } public function get_results() { @@ -274,9 +287,10 @@ class restore_controller extends backup implements loggable { } public function get_precheck_results() { - debugging ('TODO: Not applying prechecks yet, need to link them to proper restore_precheck class!', DEBUG_DEVELOPER); - return array(); - //return $this->precheck->get_results(); + if (!is_array($this->precheck)) { + throw new restore_controller_exception('precheck_not_executed'); + } + return $this->precheck; } public function log($message, $level, $a = null, $depth = null, $display = false) { diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 708104e5704..38cc9e11472 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -1194,6 +1194,7 @@ class backup_main_structure_step extends backup_structure_step { $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(); @@ -1209,7 +1210,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', + 'backup_release', 'backup_date', 'mnet_remoteusers', 'original_wwwroot', 'original_site_identifier_hash', 'original_course_id', 'original_course_contextid', 'original_system_contextid')); $details = new backup_nested_element('details'); diff --git a/backup/moodle2/restore_root_task.class.php b/backup/moodle2/restore_root_task.class.php index 1a1b2d6e518..004809a04ae 100644 --- a/backup/moodle2/restore_root_task.class.php +++ b/backup/moodle2/restore_root_task.class.php @@ -60,6 +60,10 @@ class restore_root_task extends restore_task { // Unconditionally, load create all the needed scales $this->add_step(new restore_scales_structure_step('create_scales', 'scales.xml')); + // Unconditionally, load create all the needed outcomes. + // TODO: restore outcomes + // $this->add_step(new restore_outcomes_structure_step('create_outcomes', 'outcomes.xml')); + // At the end, mark it as built $this->built = true; } diff --git a/backup/util/dbops/backup_controller_dbops.class.php b/backup/util/dbops/backup_controller_dbops.class.php index db4688b08cf..82523e1cc55 100644 --- a/backup/util/dbops/backup_controller_dbops.class.php +++ b/backup/util/dbops/backup_controller_dbops.class.php @@ -354,6 +354,22 @@ abstract class backup_controller_dbops extends backup_dbops { } } + /** + * Given the backupid, detect if the backup includes "mnet" remote users or no + */ + public static function backup_includes_mnet_remote_users($backupid) { + global $CFG, $DB; + + $sql = "SELECT COUNT(*) + FROM {backup_ids_temp} b + JOIN {user} u ON u.id = b.itemid + WHERE b.backupid = ? + AND b.itemname = 'userfinal' + AND u.mnethostid != ?"; + $count = $DB->count_records_sql($sql, array($backupid, $CFG->mnet_localhost_id)); + return (int)(bool)$count; + } + /** * Sets the controller settings default values from the backup config. * diff --git a/backup/util/helper/backup_general_helper.class.php b/backup/util/helper/backup_general_helper.class.php index d5eff64efeb..aa369dcbde7 100644 --- a/backup/util/helper/backup_general_helper.class.php +++ b/backup/util/helper/backup_general_helper.class.php @@ -136,6 +136,7 @@ abstract class backup_general_helper extends backup_helper { $info->backup_version = $infoarr['backup_version']; $info->backup_release = $infoarr['backup_release']; $info->backup_date = $infoarr['backup_date']; + $info->mnet_remoteusers = $infoarr['mnet_remoteusers']; $info->original_wwwroot = $infoarr['original_wwwroot']; $info->original_site_identifier_hash = $infoarr['original_site_identifier_hash']; $info->original_course_id = $infoarr['original_course_id']; diff --git a/backup/util/includes/restore_includes.php b/backup/util/includes/restore_includes.php index 8a8161eca7d..ad9dca72c6b 100644 --- a/backup/util/includes/restore_includes.php +++ b/backup/util/includes/restore_includes.php @@ -31,14 +31,15 @@ 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/backup.class.php'); require_once($CFG->dirroot . '/backup/util/structure/restore_path_element.class.php'); require_once($CFG->dirroot . '/backup/util/helper/backup_file_manager.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/restore_prechecks_helper.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'); require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php'); require_once($CFG->dirroot . '/backup/util/dbops/restore_dbops.class.php');