Permalink
Browse files

Adding some of the base classes and starting to fill out their method…

…s/properties
  • Loading branch information...
1 parent 804ebc7 commit 17252e2d8a6fc7ddf7f9a32447d7e8f90bb5ca19 @mrmark mrmark committed with mudrd8mz Mar 9, 2011
@@ -0,0 +1,7 @@
+<?php
+/**
+ * This will be the Moodle 1 to Moodle 2 Converter
+ */
+abstract class moodle1_converter extends plan_converter {
+
+}
@@ -0,0 +1,20 @@
+<?php
+/**
+ * Do convert plan related set up
+ */
+class convert_create_and_clean_temp_stuff extends convert_execution_step {
+
+ protected function define_execution() {
+ backup_controller_dbops::create_backup_ids_temp_table($this->get_convertid()); // Create ids temp table
+ }
+}
+
+/**
+ * Do convert plan related tear down
+ */
+class convert_drop_and_clean_temp_stuff extends convert_execution_step {
+
+ protected function define_execution() {
+ backup_controller_dbops::drop_backup_ids_temp_table($this->get_convertid()); // Drop ids temp table
+ }
+}
@@ -0,0 +1,92 @@
+<?php
+/**
+ * Base Abstract Converter
+ *
+ * @throws backup_exception|Exception|null
+ */
+abstract class base_converter {
+
+ protected $id;
+ protected $tempdir;
+ protected $convertdir;
+
+ // do we want absolute path instead of tempdir?
+ // Do we need to create a new tempdir to convert into? EG: target...
+ public function __construct($tempdir) {
+ $this->tempdir = $tempdir;
+ $this->convertdir = $this->tempdir.'_'.$this->get_name();
+ $this->id = convert_helper::generate_id($this->convertdir);
+ $this->init();
+ }
+
+ public function init() {
+ }
+
+ public function get_id() {
+ return $this->id;
+ }
+
+ public function get_name() {
+ return array_shift(explode('_', get_class($this)));
+ }
+
+ public function get_convertdir() {
+ global $CFG;
+
+ return "$CFG->dirroot/backup/temp/$this->convertdir";
+ }
+
+ public function get_tempdir() {
+ global $CFG;
+
+ return "$CFG->dirroot/backup/temp/$this->tempdir";
+ }
+
+ public function delete_convertdir() {
+ fulldelete($this->get_convertdir());
+ }
+
+ public function create_convertdir() {
+ $this->delete_convertdir();
+ make_upload_directory($this->get_convertdir());
+ }
+
+ public function replace_tempdir() {
+ fulldelete($this->get_tempdir());
+
+ if (!rename($this->get_convertdir(), $this->get_tempdir())) {
+ throw new backup_exception('failedmoveconvertedintoplace'); // @todo Define this string
+ }
+ }
+
+ /**
+ * @abstract
+ * @return boolean
+ */
+ abstract public function can_convert();
+
+ // Kicks things off
+ public function convert() {
+
+ $e = NULL;
+
+ try {
+ $this->create_convertdir();
+ $this->execute();
+ $this->replace_tempdir();
+ } catch (Exception $e) {
+ }
+ // Do cleanup...
+ $this->destroy();
+
+ if ($e instanceof Exception) {
+ throw $e;
+ }
+ }
+
+ abstract public function execute();
+
+ public function destroy() {
+ $this->delete_convertdir();
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Plan Based Abstract Converter
+ */
+abstract class plan_converter extends base_converter {
+
+ protected $plan;
+
+ /**
+ * @return convert_plan
+ */
+ public function get_plan() {
+ if ($this->plan instanceof convert_plan) {
+ $this->plan = new convert_plan($this);
+ }
+ return $this->plan;
+ }
+
+ abstract public function build_plan();
+
+ public function execute() {
+ $this->get_plan()->build(); // Ends up calling $this->build_plan()
+ $this->get_plan()->execute();
+ }
+
+ public function destroy() {
+ parent::destroy();
+ $this->get_plan()->destroy();
+ }
+}
@@ -0,0 +1,9 @@
+<?php
+/**
+ * General Convert Helper
+ */
+abstract class convert_helper {
+ public static function generate_id($entropy) {
+ return md5(time() . '-' . $entropy . '-' . random_string(20));
+ }
+}
@@ -0,0 +1,26 @@
+<?php
+
+// Prevent direct access to this file
+if (!defined('MOODLE_INTERNAL')) {
+ die('Direct access to this script is forbidden.');
+}
+
+// Include all the convert stuff needed
+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');
+require_once($CFG->dirroot.'/backup/util/plan/base_plan.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/base_step.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/base_task.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_plan.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_step.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_task.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_structure_step.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_execution_step.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_.class.php');
+require_once($CFG->dirroot.'/backup/util/plan/convert_.class.php');
+require_once($CFG->dirroot.'/backup/moodle2/convert_stepslib.php');
+
+// And some moodle stuff too
+require_once($CFG->libdir.'/fileslib.php');
@@ -0,0 +1,15 @@
+<?php
+/**
+ * Executable Step for Converters
+ */
+abstract class convert_execution_step extends convert_step {
+
+ public function execute() {
+ return $this->define_execution();
+ }
+
+ /**
+ * Function that will contain all the code to be executed
+ */
+ abstract protected function define_execution();
+}
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Convert Plan
+ */
+class convert_plan extends base_plan implements loggable {
+
+ protected $converter;
+
+ public function __construct(plan_converter $converter) {
+ global $CFG;
+
+ $this->converter = $converter;
+ $this->basepath = $CFG->dataroot . '/temp/backup/' . $converter->get_backupid();
+ parent::__construct('convert_plan');
+ }
+
+ /**
+ * This function will be responsible for handling the params, and to call
+ * to the corresponding logger->process() once all modifications in params
+ * have been performed
+ */
+ public function log($message, $level, $a = null, $depth = null, $display = false) {
+ // TODO: Implement log() method.
+ }
+
+ public function get_converterid() {
+ return $this->converter->get_id();
+ }
+
+ /**
+ * Function responsible for building the tasks of any plan
+ * with their corresponding settings
+ * (must set the $built property to true)
+ */
+ public function build() {
+ // This seems circular for no real reason....
+ $this->converter->build_plan();
+ $this->built = true;
+ }
+}
@@ -0,0 +1,19 @@
+<?php
+/**
+ * Convert Step
+ *
+ * @throws backup_exception
+ */
+abstract class convert_step extends base_step {
+
+ public function __construct($name, convert_task $task = null) {
+ parent::__construct($name, $task);
+ }
+
+ protected function get_convertid() {
+ if (!$this->task instanceof convert_task) {
+ throw new backup_exception('not_specified_convert_task'); // @todo Define string
+ }
+ return $this->task->get_convertid();
+ }
+}
Oops, something went wrong.

0 comments on commit 17252e2

Please sign in to comment.