From c5c8b3503a7412377549bafcbe889c23bd42f937 Mon Sep 17 00:00:00 2001 From: Mark Nielsen Date: Thu, 10 Mar 2011 10:17:13 -0800 Subject: [PATCH] Making the conversion process more re-usable --- .../controller/restore_controller.class.php | 39 ++++++------------- backup/util/helper/convert_helper.class.php | 27 +++++++++++++ backup/util/includes/convert_includes.php | 1 + 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/backup/controller/restore_controller.class.php b/backup/controller/restore_controller.class.php index fd63d9a7269..93806276314 100644 --- a/backup/controller/restore_controller.class.php +++ b/backup/controller/restore_controller.class.php @@ -387,39 +387,22 @@ class restore_controller extends backup implements loggable { } require_once($CFG->dirroot.'/backup/util/includes/convert_includes.php'); - while (!in_array($this->format, array(backup::FORMAT_MOODLE, backup::FORMAT_UNKNOWN))) { - $converter = convert_factory::converter($this->format, $this->get_tempdir()); + // Run conversion until we have the proper format + convert_helper::to_moodle2_format($this->get_tempdir(), $this->format); - if (!$converter->can_convert()) { - throw new coding_exception('Converter detection failed, the loaded converter cannot convert this format'); - } - $converter->convert(); + // If no exceptions were thrown, then we are in the proper format + $this->format = backup::FORMAT_MOODLE; - // Re-detect format - $this->format = backup_general_helper::detect_backup_format($this->get_tempdir()); - } - if ($this->format == backup::FORMAT_UNKNOWN) { - throw new restore_controller_exception('cannot_convert_from_unknown_format'); - } + // Load plan, apply security and set status based on interactivity + $this->load_plan(); - // Once conversions have finished, we check again the format - $newformat = backup_general_helper::detect_backup_format($tempdir); + // Perform all initial security checks and apply (2nd param) them to settings automatically + restore_check::check_security($this, true); - // If format is moodle2, load plan, apply security and set status based on interactivity - if ($newformat === backup::FORMAT_MOODLE) { - // Load plan - $this->load_plan(); - - // Perform all initial security checks and apply (2nd param) them to settings automatically - restore_check::check_security($this, true); - - if ($this->interactive == backup::INTERACTIVE_YES) { - $this->set_status(backup::STATUS_SETTING_UI); - } else { - $this->set_status(backup::STATUS_NEED_PRECHECK); - } + if ($this->interactive == backup::INTERACTIVE_YES) { + $this->set_status(backup::STATUS_SETTING_UI); } else { - throw new restore_controller_exception('conversion_ended_with_wrong_format', $newformat); + $this->set_status(backup::STATUS_NEED_PRECHECK); } } diff --git a/backup/util/helper/convert_helper.class.php b/backup/util/helper/convert_helper.class.php index 7553d9fe29a..993a1ff38bc 100644 --- a/backup/util/helper/convert_helper.class.php +++ b/backup/util/helper/convert_helper.class.php @@ -6,4 +6,31 @@ abstract class convert_helper { public static function generate_id($entropy) { return md5(time() . '-' . $entropy . '-' . random_string(20)); } + + /** + * @static + * @throws coding_exception|restore_controller_exception + * @param string $tempdir The directory to convert + * @param string $format The current format, if already detected + * @return void + */ + public static function to_moodle2_format($tempdir, $format = NULL) { + if (is_null($format)) { + $format = backup_general_helper::detect_backup_format($tempdir); + } + while (!in_array($format, array(backup::FORMAT_MOODLE, backup::FORMAT_UNKNOWN))) { + $converter = convert_factory::converter($format, $tempdir); + + if (!$converter->can_convert()) { + throw new coding_exception('Converter detection failed, the loaded converter cannot convert this format'); + } + $converter->convert(); + + // Re-detect format + $format = backup_general_helper::detect_backup_format($tempdir); + } + if ($format == backup::FORMAT_UNKNOWN) { + throw new restore_controller_exception('cannot_convert_from_unknown_format'); // @todo Change exception class + } + } } \ No newline at end of file diff --git a/backup/util/includes/convert_includes.php b/backup/util/includes/convert_includes.php index f6a743e14a9..2da8b585537 100644 --- a/backup/util/includes/convert_includes.php +++ b/backup/util/includes/convert_includes.php @@ -6,6 +6,7 @@ if (!defined('MOODLE_INTERNAL')) { } // Include all the convert stuff needed +require_once($CFG->dirroot.'/backup/backup.class.php'); require_once($CFG->dirroot.'/backup/util/factories/convert_factory.class.php'); require_once($CFG->dirroot.'/backup/util/converter/base_converter.class.php'); require_once($CFG->dirroot.'/backup/util/converter/plan_converter.class.php');