Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Making the conversion process more re-usable

  • Loading branch information...
commit c5c8b3503a7412377549bafcbe889c23bd42f937 1 parent 1e8c265
@mrmark mrmark authored mudrd8mz committed
View
39 backup/controller/restore_controller.class.php
@@ -387,39 +387,22 @@ public function convert() {
}
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();
-
- // 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');
- }
+ // If no exceptions were thrown, then we are in the proper format
+ $this->format = backup::FORMAT_MOODLE;
- // Once conversions have finished, we check again the format
- $newformat = backup_general_helper::detect_backup_format($tempdir);
-
- // If format is moodle2, load plan, apply security and set status based on interactivity
- if ($newformat === backup::FORMAT_MOODLE) {
- // Load plan
- $this->load_plan();
+ // Load plan, apply security and set status based on interactivity
+ $this->load_plan();
- // Perform all initial security checks and apply (2nd param) them to settings automatically
- restore_check::check_security($this, true);
+ // 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);
}
}
View
27 backup/util/helper/convert_helper.class.php
@@ -6,4 +6,31 @@
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
+ }
+ }
}
View
1  backup/util/includes/convert_includes.php
@@ -6,6 +6,7 @@
}
// 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');
Please sign in to comment.
Something went wrong with that request. Please try again.