Skip to content

Commit

Permalink
MDL-27376 MDL-27377 MDL-27378 Backup converters refactoring - work in…
Browse files Browse the repository at this point in the history
… progress

These are David's changes of Mark's code that replace the
plan/tasks/steps infrastructure with a bit simpler one. The changes will
be described in the next commit that will actually finish the
conversion.

TODO: refactor backup/converter/moodle1/stepslib.php into conversion
handlers.
  • Loading branch information
mudrd8mz committed May 10, 2011
1 parent 0164592 commit 1e2c735
Show file tree
Hide file tree
Showing 19 changed files with 1,046 additions and 779 deletions.
8 changes: 6 additions & 2 deletions backup/controller/restore_controller.class.php
Expand Up @@ -381,14 +381,18 @@ public static function get_tempdir_name($courseid = 0, $userid = 0) {
*/
public function convert() {
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/convert_includes.php');
require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');

if ($this->status != backup::STATUS_REQUIRE_CONV) {
throw new restore_controller_exception('cannot_convert_not_required_status');
}

// Run conversion to the proper format
convert_helper::to_moodle2_format($this->get_tempdir(), $this->format);
if (!convert_helper::to_moodle2_format($this->get_tempdir(), $this->format)) {
// todo - unable to find the conversion path, what to do now?
// throwing the exception as a temporary solution
throw new restore_controller_exception('unable_to_find_conversion_path');
}

// If no exceptions were thrown, then we are in the proper format
$this->format = backup::FORMAT_MOODLE;
Expand Down
Expand Up @@ -16,6 +16,8 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Provides base converter classes
*
* @package core
* @subpackage backup-convert
* @copyright 2011 Mark Nielsen <mark@moodlerooms.com>
Expand All @@ -24,10 +26,14 @@

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

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

/**
* Base abstract converter
* Base converter class
*
* All Moodle backup converters are supposed to extend this base class.
*
* @throws backup_exception|Exception|null
* @throws convert_exception
*/
abstract class base_converter {

Expand Down Expand Up @@ -74,9 +80,6 @@ public function get_name() {
*/
public function convert() {

$e = null;

// try to execute the converter
try {
$this->create_workdir();
$this->execute();
Expand All @@ -88,7 +91,7 @@ public function convert() {
$this->destroy();

// eventually re-throw the execution exception
if ($e instanceof Exception) {
if (isset($e) and ($e instanceof Exception)) {
throw $e;
}
}
Expand Down Expand Up @@ -139,23 +142,10 @@ public static function description() {
);
}

/// end of public API //////////////////////////////////////////////////////

/**
* Initialize the instance if needed, called by the constructor
*/
protected function init() {
}

/**
* Converts the contents of the tempdir into the target format in the workdir
*/
protected abstract function execute();

/**
* @return string the full path to the working directory
*/
protected function get_workdir_path() {
public function get_workdir_path() {
global $CFG;

return "$CFG->dataroot/temp/backup/$this->workdir";
Expand All @@ -164,20 +154,33 @@ protected function get_workdir_path() {
/**
* @return string the full path to the directory with the source backup
*/
protected function get_tempdir_path() {
public function get_tempdir_path() {
global $CFG;

return "$CFG->dataroot/temp/backup/$this->tempdir";
}

/// end of public API //////////////////////////////////////////////////////

/**
* Initialize the instance if needed, called by the constructor
*/
protected function init() {
}

/**
* Converts the contents of the tempdir into the target format in the workdir
*/
protected abstract function execute();

/**
* Prepares a new empty working directory
*/
protected function create_workdir() {

fulldelete($this->get_workdir_path());
if (!check_dir_exists($this->get_workdir_path())) {
throw new backup_exception('failedtocreateworkdir');
throw new convert_exception('failed_create_workdir');
}
}

Expand All @@ -191,15 +194,15 @@ protected function replace_tempdir() {
global $CFG;

if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete($this->get_tempdir_path);
fulldelete($this->get_tempdir_path());
} else {
if (!rename($this->get_tempdir_path, $this->get_tempdir_path . '_' . $this->get_name() . '_' . $this->id . '_source')) {
throw new backup_exception('failedrenamesourcetempdir');
if (!rename($this->get_tempdir_path(), $this->get_tempdir_path() . '_' . $this->get_name() . '_' . $this->id . '_source')) {
throw new convert_exception('failed_rename_source_tempdir');
}
}

if (!rename($this->get_workdir_path(), $this->get_tempdir_path())) {
throw new backup_exception('failedmoveconvertedintoplace');
throw new convert_exception('failed_move_converted_into_place');
}
}

Expand All @@ -213,7 +216,26 @@ protected function destroy() {
global $CFG;

if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete($this->get_workdir_path);
fulldelete($this->get_workdir_path());
}
}
}

/**
* General convert-related exception
*
* @author David Mudrak <david@moodle.com>
*/
class convert_exception extends moodle_exception {

/**
* Constructor
*
* @param string $errorcode key for the corresponding error string
* @param object $a extra words and phrases that might be required in the error string
* @param string $debuginfo optional debugging information
*/
public function __construct($errorcode, $a = null, $debuginfo = null) {
parent::__construct($errorcode, '', '', $a, $debuginfo);
}
}
132 changes: 0 additions & 132 deletions backup/converter/moodle1/converter.class.php

This file was deleted.

0 comments on commit 1e2c735

Please sign in to comment.