From 648a575eda9f16bc9bce8874013ec9f8bab9413b Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Mon, 1 Nov 2010 15:53:35 +0000 Subject: [PATCH] NOBUG: Added support for after_restore() methods on restore. Will help some blocks. --- backup/moodle2/restore_final_task.class.php | 15 +++++++++++ backup/moodle2/restore_stepslib.php | 27 ++++++++++++++++--- backup/util/dbops/restore_dbops.class.php | 21 +++++++-------- .../helper/restore_prechecks_helper.class.php | 11 +++++++- backup/util/plan/base_task.class.php | 14 ++++++++-- backup/util/plan/restore_plan.class.php | 10 +++++++ backup/util/plan/restore_task.class.php | 10 +++++++ 7 files changed, 89 insertions(+), 19 deletions(-) diff --git a/backup/moodle2/restore_final_task.class.php b/backup/moodle2/restore_final_task.class.php index cd82ab8f3e2..95132ff42c0 100644 --- a/backup/moodle2/restore_final_task.class.php +++ b/backup/moodle2/restore_final_task.class.php @@ -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')); + // 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; } + /** + * 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 /** diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 23c06e59db9..f0e06ba2e29 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -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 @@ -480,10 +495,14 @@ class restore_load_included_inforef_records extends restore_execution_step { 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 + } } } } diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 98fd2ce0534..0b405c87f21 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -30,15 +30,15 @@ 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(); - $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 @@ -67,15 +67,12 @@ abstract class restore_dbops { $included = true; } - // If included and file exists, add it to results + // If included, add it if ($included) { - $inforefpath = $task->get_taskbasepath() . '/inforef.xml'; - if (file_exists($inforefpath)) { - $files[] = $inforefpath; - } + $includedtasks[] = $task; } } - return $files; + return $includedtasks; } /** diff --git a/backup/util/helper/restore_prechecks_helper.class.php b/backup/util/helper/restore_prechecks_helper.class.php index 04335160ef6..53ec22ea3df 100644 --- a/backup/util/helper/restore_prechecks_helper.class.php +++ b/backup/util/helper/restore_prechecks_helper.class.php @@ -56,8 +56,17 @@ abstract class restore_prechecks_helper { $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; + // 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()); diff --git a/backup/util/plan/base_task.class.php b/backup/util/plan/base_task.class.php index a066a235407..341e71d4fd1 100644 --- a/backup/util/plan/base_task.class.php +++ b/backup/util/plan/base_task.class.php @@ -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 $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 @@ -47,7 +48,8 @@ abstract class base_task implements checksumable, executable, loggable { $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); } @@ -134,11 +136,15 @@ abstract class base_task implements checksumable, executable, loggable { /** * 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); } + 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 @@ -147,6 +153,10 @@ abstract class base_task implements checksumable, executable, loggable { $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) { diff --git a/backup/util/plan/restore_plan.class.php b/backup/util/plan/restore_plan.class.php index 0796b5183a6..e38ea45fc1c 100644 --- a/backup/util/plan/restore_plan.class.php +++ b/backup/util/plan/restore_plan.class.php @@ -147,6 +147,16 @@ class restore_plan extends base_plan implements loggable { 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(); + } + } } /* diff --git a/backup/util/plan/restore_task.class.php b/backup/util/plan/restore_task.class.php index fa8a43bdacd..cdd525d09a4 100644 --- a/backup/util/plan/restore_task.class.php +++ b/backup/util/plan/restore_task.class.php @@ -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; } + + /** + * 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(); + } + } } /* -- 2.43.0