From 1e8c265c2b007311aa1cad06c3b6747b3119528d Mon Sep 17 00:00:00 2001 From: Mark Nielsen Date: Wed, 9 Mar 2011 16:21:19 -0800 Subject: [PATCH] Added converter format detection and added conversion logic to restore controller --- backup/backup.class.php | 2 - .../controller/restore_controller.class.php | 19 ++++++-- backup/converter/moodle1.class.php | 21 +++++++++ .../util/factories/convert_factory.class.php | 46 +++++++++++++++++++ .../helper/backup_general_helper.class.php | 18 ++------ backup/util/includes/convert_includes.php | 1 + 6 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 backup/util/factories/convert_factory.class.php diff --git a/backup/backup.class.php b/backup/backup.class.php index 936ff8a4e1c41..31ae5b13715bf 100644 --- a/backup/backup.class.php +++ b/backup/backup.class.php @@ -40,8 +40,6 @@ abstract class backup implements checksumable { // Backup format const FORMAT_MOODLE = 'moodle2'; - const FORMAT_MOODLE1 = 'moodle1'; - const FORMAT_IMSCC = 'imscc'; const FORMAT_UNKNOWN = 'unknown'; // Interactive diff --git a/backup/controller/restore_controller.class.php b/backup/controller/restore_controller.class.php index f08b09bb56fa5..fd63d9a7269cb 100644 --- a/backup/controller/restore_controller.class.php +++ b/backup/controller/restore_controller.class.php @@ -380,16 +380,27 @@ public static function get_tempdir_name($courseid = 0, $userid = 0) { * convert from current format to backup::MOODLE format */ public function convert() { + global $CFG; + if ($this->status != backup::STATUS_REQUIRE_CONV) { throw new restore_controller_exception('cannot_convert_not_required_status'); } + 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()); + + 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 ($this->format == backup::FORMAT_MOODLE1) { - // TODO: Implement moodle1 => moodle2 conversion - throw new restore_controller_exception('cannot_convert_yet_from_moodle1_format'); - } // Once conversions have finished, we check again the format $newformat = backup_general_helper::detect_backup_format($tempdir); diff --git a/backup/converter/moodle1.class.php b/backup/converter/moodle1.class.php index 29b5e48a9a27d..c3cd5a1aeef02 100644 --- a/backup/converter/moodle1.class.php +++ b/backup/converter/moodle1.class.php @@ -3,5 +3,26 @@ * This will be the Moodle 1 to Moodle 2 Converter */ abstract class moodle1_converter extends plan_converter { + /** + * @return boolean + */ + public function can_convert() { + // Then look for MOODLE1 (moodle1) format + $filepath = $this->get_tempdir() . '/moodle.xml'; + if (file_exists($filepath)) { // Looks promising, lets load some information + $handle = fopen($filepath, "r"); + $first_chars = fread($handle,200); + fclose($handle); + + // Check if it has the required strings + if (strpos($first_chars,'') !== false && + strpos($first_chars,'') !== false && + strpos($first_chars,'') !== false) { + + return true; + } + } + return false; + } } \ No newline at end of file diff --git a/backup/util/factories/convert_factory.class.php b/backup/util/factories/convert_factory.class.php new file mode 100644 index 0000000000000..a2d2d60850f4c --- /dev/null +++ b/backup/util/factories/convert_factory.class.php @@ -0,0 +1,46 @@ +dirroot/backup/converter/$name.class.php"; + $classname = "{$name}_converter"; + + if (!file_exists($classfile)) { + throw new coding_exception("Converter factory error: class file not found $classfile"); + } + require_once($classfile); + + if (!class_exists($classname)) { + throw new coding_exception("Converter factory error: class not found $classname"); + } + return new $classname($tempdir); + } + + /** + * @static + * @param string $tempdir The temp directory to operate on + * @return array + */ + public static function converters($tempdir) { + global $CFG; + + $converters = array(); + $files = get_directory_list($CFG->dirroot.'/backup/converter'); + foreach ($files as $file) { + $name = array_shift(explode('_', $file)); + $converters[$name] = self::converter($name, $tempdir); + } + return $converters; + } +} \ No newline at end of file diff --git a/backup/util/helper/backup_general_helper.class.php b/backup/util/helper/backup_general_helper.class.php index 99db28701a019..00c18fcbfac7c 100644 --- a/backup/util/helper/backup_general_helper.class.php +++ b/backup/util/helper/backup_general_helper.class.php @@ -255,22 +255,14 @@ public static function detect_backup_format($tempdir) { } } - // Then look for MOODLE1 (moodle1) format - $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle.xml'; - if (file_exists($filepath)) { // Looks promising, lets load some information - $handle = fopen ($filepath, "r"); - $first_chars = fread($handle,200); - $status = fclose ($handle); - // Check if it has the required strings - if (strpos($first_chars,'') !== false && - strpos($first_chars,'') !== false && - strpos($first_chars,'') !== false) { - return backup::FORMAT_MOODLE1; + // See if a converter can identify the format as its own + $converters = convert_factory::converters($tempdir); + foreach ($converters as $name => $converter) { + if ($converter->can_convert()) { + return $name; } } - // Other formats - // Arrived here, unknown format return backup::FORMAT_UNKNOWN; } diff --git a/backup/util/includes/convert_includes.php b/backup/util/includes/convert_includes.php index f85353bfc99f5..f6a743e14a935 100644 --- a/backup/util/includes/convert_includes.php +++ b/backup/util/includes/convert_includes.php @@ -6,6 +6,7 @@ } // Include all the convert stuff needed +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'); require_once($CFG->dirroot.'/backup/util/helper/convert_helper.class.php');