MDL-21432 backup - handle mnet_remoteusers and support for prechecks
authorEloy Lafuente <stronk7@moodle.org>
Sun, 18 Jul 2010 23:49:55 +0000 (23:49 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Sun, 18 Jul 2010 23:49:55 +0000 (23:49 +0000)
backup/backup.class.php
backup/controller/restore_controller.class.php
backup/moodle2/backup_stepslib.php
backup/moodle2/restore_root_task.class.php
backup/util/dbops/backup_controller_dbops.class.php
backup/util/helper/backup_general_helper.class.php
backup/util/includes/restore_includes.php

index 8a3209c..d854368 100644 (file)
@@ -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';
 }
 
index 659cb65..b817582 100644 (file)
@@ -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) {
index 708104e..38cc9e1 100644 (file)
@@ -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');
index 1a1b2d6..004809a 100644 (file)
@@ -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;
     }
index db4688b..82523e1 100644 (file)
@@ -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.
      * 
index d5eff64..aa369dc 100644 (file)
@@ -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'];
index 8a8161e..ad9dca7 100644 (file)
@@ -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');