Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added converter format detection and added conversion logic to restor…

…e controller
  • Loading branch information...
commit 1e8c265c2b007311aa1cad06c3b6747b3119528d 1 parent 7336701
@mrmark mrmark authored mudrd8mz committed
View
2  backup/backup.class.php
@@ -40,8 +40,6 @@
// Backup format
const FORMAT_MOODLE = 'moodle2';
- const FORMAT_MOODLE1 = 'moodle1';
- const FORMAT_IMSCC = 'imscc';
const FORMAT_UNKNOWN = 'unknown';
// Interactive
View
19 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);
View
21 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,'<?xml version="1.0" encoding="UTF-8"?>') !== false &&
+ strpos($first_chars,'<MOODLE_BACKUP>') !== false &&
+ strpos($first_chars,'<INFO>') !== false) {
+
+ return true;
+ }
+ }
+ return false;
+ }
}
View
46 backup/util/factories/convert_factory.class.php
@@ -0,0 +1,46 @@
+<?php
+
+abstract class convert_factory {
+ /**
+ * @static
+ * @throws coding_exception
+ * @param $name The converter name
+ * @param $tempdir The temp directory to operate on
+ * @return base_converter|plan_converter
+ */
+ public static function converter($name, $tempdir) {
+ global $CFG;
+
+ $name = clean_param($name, PARAM_SAFEDIR);
+
+ $classfile = "$CFG->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;
+ }
+}
View
18 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,'<?xml version="1.0" encoding="UTF-8"?>') !== false &&
- strpos($first_chars,'<MOODLE_BACKUP>') !== false &&
- strpos($first_chars,'<INFO>') !== 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;
}
View
1  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');
Please sign in to comment.
Something went wrong with that request. Please try again.