Added converter format detection and added conversion logic to restore controller

This commit is contained in:
Mark Nielsen 2011-03-09 16:21:19 -08:00 committed by David Mudrak
parent 7336701ff1
commit 1e8c265c2b
6 changed files with 88 additions and 19 deletions

View File

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

View File

@ -380,16 +380,27 @@ class restore_controller extends backup implements loggable {
* 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 File

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

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

@ -255,22 +255,14 @@ abstract class backup_general_helper extends backup_helper {
}
}
// 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 File

@ -6,6 +6,7 @@ if (!defined('MOODLE_INTERNAL')) {
}
// 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');