Skip to content

Commit

Permalink
Added converter format detection and added conversion logic to restor…
Browse files Browse the repository at this point in the history
…e controller
  • Loading branch information
polothy authored and mudrd8mz committed May 10, 2011
1 parent 7336701 commit 1e8c265
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 19 deletions.
2 changes: 0 additions & 2 deletions backup/backup.class.php
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions backup/controller/restore_controller.class.php
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions backup/converter/moodle1.class.php
Expand Up @@ -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;
}

}
46 changes: 46 additions & 0 deletions 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;
}
}
18 changes: 5 additions & 13 deletions backup/util/helper/backup_general_helper.class.php
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions backup/util/includes/convert_includes.php
Expand Up @@ -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');
Expand Down

0 comments on commit 1e8c265

Please sign in to comment.