Making the conversion process more re-usable

This commit is contained in:
Mark Nielsen 2011-03-10 10:17:13 -08:00 committed by David Mudrak
parent 1e8c265c2b
commit c5c8b3503a
3 changed files with 39 additions and 28 deletions

View File

@ -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);
}
}

View File

@ -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
}
}
}

View File

@ -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');