From 92480bc82b713d3273105d6a960f893d7eb4dbbc Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Thu, 26 May 2011 02:30:36 +0200 Subject: [PATCH] MDL-27636 restore - add support for after_restore() to plugins. Credit goes to Mark Nielsen. Thanks! --- backup/moodle2/restore_plugin.class.php | 17 +++++++++ .../plan/restore_structure_step.class.php | 37 +++++++++++++++++++ backup/util/plan/restore_task.class.php | 7 ++++ 3 files changed, 61 insertions(+) diff --git a/backup/moodle2/restore_plugin.class.php b/backup/moodle2/restore_plugin.class.php index 02039876352..5ed177ea5f5 100644 --- a/backup/moodle2/restore_plugin.class.php +++ b/backup/moodle2/restore_plugin.class.php @@ -80,6 +80,23 @@ abstract class restore_plugin { } } + /** + * after_restore dispatcher for any restore_plugin class + * + * This method will dispatch execution to the corresponding + * after_restore_xxx() method when available, with xxx + * being the connection point of the instance, so plugin + * classes with multiple connection points will support + * multiple after_restore methods, one for each connection point + */ + public function launch_after_restore_methods() { + // Check if the after_restore method exists and launch it + $afterrestore = 'after_restore_' . basename($this->connectionpoint->get_path()); + if (method_exists($this, $afterrestore)) { + $this->$afterrestore(); + } + } + /** * Returns one array with all the decode contents * to be processed by the links decoder diff --git a/backup/util/plan/restore_structure_step.class.php b/backup/util/plan/restore_structure_step.class.php index ae648bc4911..ddb2fe54729 100644 --- a/backup/util/plan/restore_structure_step.class.php +++ b/backup/util/plan/restore_structure_step.class.php @@ -360,6 +360,43 @@ abstract class restore_structure_step extends restore_step { } + /** + * Launch all the after_restore methods present in all the processing objects + * + * This method will launch all the after_restore methods that can be defined + * both in restore_plugin class + * + * For restore_plugin classes the name of the method to be executed will be + * "after_restore_" + connection point (as far as can be multiple connection + * points in the same class) + */ + public function launch_after_restore_methods() { + $alreadylaunched = array(); // To avoid multiple executions + foreach ($this->pathelements as $pathelement) { + // Get the processing object + $pobject = $pathelement->get_processing_object(); + // Skip null processors (child of grouped ones for sure) + if (is_null($pobject)) { + continue; + } + // Skip restore structure step processors (this) + if ($pobject instanceof restore_structure_step) { + continue; + } + // Skip already launched processing objects + if (in_array($pobject, $alreadylaunched, true)) { + continue; + } + // Add processing object to array of launched ones + $alreadylaunched[] = $pobject; + // If the processing object has support for + // launching after_restore methods, use it + if (method_exists($pobject, 'launch_after_restore_methods')) { + $pobject->launch_after_restore_methods(); + } + } + } + /** * This method will be executed after the whole structure step have been processed * diff --git a/backup/util/plan/restore_task.class.php b/backup/util/plan/restore_task.class.php index cdd525d09a4..5b22b4bd3d5 100644 --- a/backup/util/plan/restore_task.class.php +++ b/backup/util/plan/restore_task.class.php @@ -100,6 +100,13 @@ abstract class restore_task extends base_task { * method if available */ public function execute_after_restore() { + if ($this->executed) { + foreach ($this->steps as $step) { + if (method_exists($step, 'launch_after_restore_methods')) { + $step->launch_after_restore_methods(); + } + } + } if ($this->executed && method_exists($this, 'after_restore')) { $this->after_restore(); }