Skip to content

Commit

Permalink
MDL-27376 MDL-27377 Backup converters API refactored
Browse files Browse the repository at this point in the history
* Several base_converter methods made protected when there was no obvious
reason why they should be public (subject of eventual change still).
* The conversion chain now constructed in advance before any converter
class is instantiated, using Dijkstra's algorithm.
  • Loading branch information
mudrd8mz committed May 10, 2011
1 parent e48477d commit 0164592
Show file tree
Hide file tree
Showing 13 changed files with 607 additions and 166 deletions.
2 changes: 2 additions & 0 deletions backup/backup.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ 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
6 changes: 3 additions & 3 deletions backup/controller/restore_controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,17 @@ public static function get_tempdir_name($courseid = 0, $userid = 0) {
}

/**
* convert from current format to backup::MOODLE format
* Converts from current format to backup::MOODLE format
*/
public function convert() {
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/convert_includes.php');

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

// Run conversion until we have the proper format
// Run conversion to the proper format
convert_helper::to_moodle2_format($this->get_tempdir(), $this->format);

// If no exceptions were thrown, then we are in the proper format
Expand Down
54 changes: 25 additions & 29 deletions backup/converter/moodle1/converter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* All of the task and step classes specific to moodle1 conversion
*
* @package core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
Expand All @@ -26,49 +24,47 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/backup/converter/moodle1/taskslib.php');
require_once($CFG->dirroot.'/backup/converter/moodle1/stepslib.php');
require_once($CFG->dirroot . '/backup/converter/moodle1/taskslib.php');
require_once($CFG->dirroot . '/backup/converter/moodle1/stepslib.php');

/**
* This will be the Moodle 1 to Moodle 2 Converter
* Converter of Moodle 1.9 backup into Moodle 2.x format
*/
class moodle1_converter extends plan_converter {
/**
* The current module being processed
*
* @var string
*/

/** @var string the current module being processed */
protected $currentmod = '';

/**
* The current block being processed
*
* @var string
*/
/** @var string the current block being processed */
protected $currentblock = '';

/**
* @return boolean
* Detects the Moodle 1.9 format of the backup directory
*
* @param string $tempdir the name of the backup directory
* @return null|string backup::FORMAT_MOODLE1 if the Moodle 1.9 is detected, null otherwise
*/
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);
public static function detect_format($tempdir) {
global $CFG;

$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);
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 &&
// check if it has the required strings
if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false and
strpos($first_chars,'<MOODLE_BACKUP>') !== false and
strpos($first_chars,'<INFO>') !== false) {

return true;
return backup::FORMAT_MOODLE1;
}
}
return false;
}

return null;
}

/**
* Path transformation for modules and blocks. Here we
Expand Down Expand Up @@ -116,7 +112,7 @@ public function process($data) {

public function build_plan() {
$this->xmlparser = new progressive_parser();
$this->xmlparser->set_file($this->get_tempdir() . '/moodle.xml');
$this->xmlparser->set_file($this->get_tempdir_path() . '/moodle.xml');
$this->xmlprocessor = new convert_structure_parser_processor($this); // @todo Probably move this
$this->xmlparser->set_processor($this->xmlprocessor);

Expand Down
15 changes: 6 additions & 9 deletions backup/converter/moodle1/simpletest/testconverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/backup/util/includes/convert_includes.php');
require_once($CFG->dirroot . '/backup/converter/moodle1/converter.class.php');

class moodle1_converter_test extends UnitTestCase {

public static $includecoverage = array();

/**
* @var string
*/
/** @var string the name of the directory containing the unpacked Moodle 1.9 backup */
protected $tempdir;

public function setUp() {
Expand All @@ -43,7 +41,7 @@ public function setUp() {
$this->tempdir = convert_helper::generate_id('simpletest');
check_dir_exists("$CFG->dataroot/temp/backup/$this->tempdir");
copy(
$CFG->dirroot.'/backup/converter/moodle1/simpletest/files/moodle.xml',
"$CFG->dirroot/backup/converter/moodle1/simpletest/files/moodle.xml",
"$CFG->dataroot/temp/backup/$this->tempdir/moodle.xml"
);
}
Expand All @@ -55,10 +53,9 @@ public function tearDown() {
}
}

public function test_can_convert() {
$converter = convert_factory::converter('moodle1', $this->tempdir);
$this->assertIsA($converter, 'moodle1_converter');
$this->assertTrue($converter->can_convert());
public function test_detect_format() {
$detected = moodle1_converter::detect_format($this->tempdir);
$this->assertEqual(backup::FORMAT_MOODLE1, $detected);
}

public function test_convert() {
Expand Down
2 changes: 2 additions & 0 deletions backup/converter/moodle1/taskslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/backup/util/includes/convert_includes.php');

class moodle1_root_task extends convert_task {
/**
* Function responsible for building the steps of any task
Expand Down
Loading

0 comments on commit 0164592

Please sign in to comment.