NOBUG: Added support for after_restore() methods on restore. Will help some blocks.
authorEloy Lafuente <stronk7@moodle.org>
Mon, 1 Nov 2010 15:53:35 +0000 (15:53 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Mon, 1 Nov 2010 15:53:35 +0000 (15:53 +0000)
backup/moodle2/restore_final_task.class.php
backup/moodle2/restore_stepslib.php
backup/util/dbops/restore_dbops.class.php
backup/util/helper/restore_prechecks_helper.class.php
backup/util/plan/base_task.class.php
backup/util/plan/restore_plan.class.php
backup/util/plan/restore_task.class.php

index cd82ab8..95132ff 100644 (file)
@@ -68,12 +68,27 @@ class restore_final_task extends restore_task {
         // Rebuild course cache to see results, whoah!
         $this->add_step(new restore_rebuild_course_cache('rebuild_course_cache'));
 
         // Rebuild course cache to see results, whoah!
         $this->add_step(new restore_rebuild_course_cache('rebuild_course_cache'));
 
+        // Review all the executed tasks having one after_restore method
+        // executing it to perform some final adjustments of information
+        // not available when the task was executed.
+        // This step is always the last one before the one cleaning the temp stuff!
+        $this->add_step(new restore_execute_after_restore('executing_after_restore'));
+
         // 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;
     }
 
         // 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;
     }
 
+    /**
+     * Special method, only available in the restore_final_task, able to invoke the
+     * restore_plan execute_after_restore() method, so restore_execute_after_restore step
+     * will be able to launch all the after_restore() methods of the executed tasks
+     */
+    public function launch_execute_after_restore() {
+        $this->plan->execute_after_restore();
+    }
+
 // Protected API starts here
 
     /**
 // Protected API starts here
 
     /**
index 23c06e5..f0e06ba 100644 (file)
@@ -385,6 +385,21 @@ class restore_rebuild_course_cache extends restore_execution_step {
     }
 }
 
     }
 }
 
+/**
+ * Review all the tasks having one after_restore method
+ * executing it to perform some final adjustments of information
+ * not available when the task was executed.
+ */
+class restore_execute_after_restore extends restore_execution_step {
+
+    protected function define_execution() {
+
+        // Simply call to the execute_after_restore() method of the task
+        // that always is the restore_final_task
+        $this->task->launch_execute_after_restore();
+    }
+}
+
 
 /**
  * Review all the (pending) block positions in backup_ids, matching by
 
 /**
  * Review all the (pending) block positions in backup_ids, matching by
@@ -480,10 +495,14 @@ class restore_load_included_inforef_records extends restore_execution_step {
             return;
         }
 
             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
+        // Get all the included tasks
+        $tasks = restore_dbops::get_included_tasks($this->get_restoreid());
+        foreach ($tasks as $task) {
+            // Load the inforef.xml file if exists
+            $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
+            if (file_exists($inforefpath)) {
+                restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $inforefpath); // Load each inforef file to temp_ids
+            }
         }
     }
 }
         }
     }
 }
index 98fd2ce..0b405c8 100644 (file)
 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
+     * Return one array containing all the tasks that have been included
+     * in the restore process. Note that these tasks aren't built (they
+     * haven't steps nor ids data available)
      */
      */
-    public static function get_needed_inforef_files($restoreid) {
+    public static function get_included_tasks($restoreid) {
         $rc = restore_controller_dbops::load_controller($restoreid);
         $tasks = $rc->get_plan()->get_tasks();
         $rc = restore_controller_dbops::load_controller($restoreid);
         $tasks = $rc->get_plan()->get_tasks();
-        $files = array();
-        foreach ($tasks as $task) {
+        $includedtasks = array();
+        foreach ($tasks as $key => $task) {
             // Calculate if the task is being included
             $included = false;
             // blocks, based in blocks setting and parent activity/course
             // Calculate if the task is being included
             $included = false;
             // blocks, based in blocks setting and parent activity/course
@@ -67,15 +67,12 @@ abstract class restore_dbops {
                 $included = true;
             }
 
                 $included = true;
             }
 
-            // If included and file exists, add it to results
+            // If included, add it
             if ($included) {
             if ($included) {
-                $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
-                if (file_exists($inforefpath)) {
-                    $files[] = $inforefpath;
-                }
+                $includedtasks[] = $task;
             }
         }
             }
         }
-        return $files;
+        return $includedtasks;
     }
 
     /**
     }
 
     /**
index 0433516..53ec22e 100644 (file)
@@ -56,8 +56,17 @@ abstract class restore_prechecks_helper {
         $restoreid = $controller->get_restoreid();
         $courseid = $controller->get_courseid();
         $userid = $controller->get_userid();
         $restoreid = $controller->get_restoreid();
         $courseid = $controller->get_courseid();
         $userid = $controller->get_userid();
-        $inforeffiles = restore_dbops::get_needed_inforef_files($restoreid); // Get all inforef files
         $rolemappings = $controller->get_info()->role_mappings;
         $rolemappings = $controller->get_info()->role_mappings;
+        // Load all the included tasks to look for inforef.xml files
+        $inforeffiles = array();
+        $tasks = restore_dbops::get_included_tasks($restoreid);
+        foreach ($tasks as $task) {
+            // Add the inforef.xml file if exists
+            $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
+            if (file_exists($inforefpath)) {
+                $inforeffiles[] = $inforefpath;
+            }
+        }
 
         // Create temp tables
         restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid());
 
         // Create temp tables
         restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid());
index a066a23..341e71d 100644 (file)
@@ -34,7 +34,8 @@ abstract class base_task implements checksumable, executable, loggable {
     protected $settings;  // One array of base_setting elements to define this task
     protected $steps;     // One array of base_task elements
 
     protected $settings;  // One array of base_setting elements to define this task
     protected $steps;     // One array of base_task elements
 
-    protected $built;     // Flag to know if one plan has been built
+    protected $built;     // Flag to know if one task has been built
+    protected $executed;  // Flag to know if one task has been executed
 
     /**
      * Constructor - instantiates one object of this class
 
     /**
      * Constructor - instantiates one object of this class
@@ -47,7 +48,8 @@ abstract class base_task implements checksumable, executable, loggable {
         $this->plan = $plan;
         $this->settings = array();
         $this->steps    = array();
         $this->plan = $plan;
         $this->settings = array();
         $this->steps    = array();
-        $this->built = false;
+        $this->built    = false;
+        $this->executed = false;
         if (!is_null($plan)) { // Add the task to the plan if specified
             $plan->add_task($this);
         }
         if (!is_null($plan)) { // Add the task to the plan if specified
             $plan->add_task($this);
         }
@@ -134,11 +136,15 @@ abstract class base_task implements checksumable, executable, loggable {
 
     /**
      * Function responsible for executing the steps of any task
 
     /**
      * Function responsible for executing the steps of any task
+     * (setting the $executed property to  true)
      */
     public function execute() {
         if (!$this->built) {
             throw new base_task_exception('base_task_not_built', $this->name);
         }
      */
     public function execute() {
         if (!$this->built) {
             throw new base_task_exception('base_task_not_built', $this->name);
         }
+        if ($this->executed) {
+            throw new base_task_exception('base_task_already_executed', $this->name);
+        }
         foreach ($this->steps as $step) {
             $result = $step->execute();
             // If step returns array, it will be forwarded to plan
         foreach ($this->steps as $step) {
             $result = $step->execute();
             // If step returns array, it will be forwarded to plan
@@ -147,6 +153,10 @@ abstract class base_task implements checksumable, executable, loggable {
                 $this->plan->add_result($result);
             }
         }
                 $this->plan->add_result($result);
             }
         }
+        // Mark as executed if any step has been executed
+        if (!empty($this->steps)) {
+            $this->executed = true;
+        }
     }
 
     public function is_checksum_correct($checksum) {
     }
 
     public function is_checksum_correct($checksum) {
index 0796b51..e38ea45 100644 (file)
@@ -147,6 +147,16 @@ class restore_plan extends base_plan implements loggable {
         parent::execute();
         $this->controller->set_status(backup::STATUS_FINISHED_OK);
     }
         parent::execute();
         $this->controller->set_status(backup::STATUS_FINISHED_OK);
     }
+
+    /**
+     * Execute the after_restore methods of all the executed tasks in the plan
+     */
+    public function execute_after_restore() {
+        // Simply iterate over each task in the plan and delegate to them the execution
+        foreach ($this->tasks as $task) {
+            $task->execute_after_restore();
+        }
+    }
 }
 
 /*
 }
 
 /*
index fa8a43b..cdd525d 100644 (file)
@@ -94,6 +94,16 @@ abstract class restore_task extends base_task {
     public function get_old_system_contextid() {
         return $this->plan->get_info()->original_system_contextid;
     }
     public function get_old_system_contextid() {
         return $this->plan->get_info()->original_system_contextid;
     }
+
+    /**
+     * If the task has been executed, launch its after_restore()
+     * method if available
+     */
+    public function execute_after_restore() {
+        if ($this->executed && method_exists($this, 'after_restore')) {
+            $this->after_restore();
+        }
+    }
 }
 
 /*
 }
 
 /*