MDL-21432 backup - restore plan builder (still missing blocks support)
authorEloy Lafuente <stronk7@moodle.org>
Tue, 6 Jul 2010 22:30:49 +0000 (22:30 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Tue, 6 Jul 2010 22:30:49 +0000 (22:30 +0000)
backup/moodle2/restore_plan_builder.class.php [new file with mode: 0644]
backup/util/factories/restore_factory.class.php [new file with mode: 0644]
backup/util/plan/restore_plan.class.php
backup/util/plan/restore_task.class.php

diff --git a/backup/moodle2/restore_plan_builder.class.php b/backup/moodle2/restore_plan_builder.class.php
new file mode 100644 (file)
index 0000000..be074ad
--- /dev/null
@@ -0,0 +1,173 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once($CFG->dirroot . '/backup/moodle2/restore_root_task.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
+//require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
+//require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
+//require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
+//require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
+
+// Load all the activity tasks for moodle2 format
+$mods = get_plugin_list('mod');
+foreach ($mods as $mod => $moddir) {
+    $taskpath = $moddir . '/backup/moodle2/restore_' . $mod . '_activity_task.class.php';
+    if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
+        if (file_exists($taskpath)) {
+            require_once($taskpath);
+        }
+    }
+}
+
+// Load all the block tasks for moodle2 format
+$blocks = get_plugin_list('block');
+foreach ($blocks as $block => $blockdir) {
+    $taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
+    if (file_exists($taskpath)) {
+        require_once($taskpath);
+    }
+}
+
+/**
+ * Abstract class defining the static method in charge of building the whole
+ * restore plan, based in @restore_controller preferences.
+ *
+ * TODO: Finish phpdocs
+ */
+abstract class restore_plan_builder {
+
+    /**
+     * Dispatches, based on type to specialised builders
+     */
+    static public function build_plan($controller) {
+
+        $plan = $controller->get_plan();
+
+        // Add the root task, responsible for
+        // preparing everything, creating the
+        // needed structures (users, roles),
+        // preloading information to temp table
+        // and other init tasks
+        $plan->add_task(new restore_root_task('root_task'));
+
+        switch ($controller->get_type()) {
+            case backup::TYPE_1ACTIVITY:
+                self::build_activity_plan($controller, $controller->get_courseid());
+                break;
+            case backup::TYPE_1SECTION:
+                self::build_section_plan($controller, $controller->get_courseid());
+                break;
+            case backup::TYPE_1COURSE:
+                self::build_course_plan($controller, $controller->get_courseid());
+                break;
+        }
+
+        // Add the final task, responsible for closing
+        // all the pending bits (remapings, inter-links
+        // conversion...)
+        // and perform other various final actions.
+        $plan->add_task(new restore_final_task('final_task'));
+    }
+
+
+// Protected API starts here
+
+    /**
+     * Restore one 1-activity backup
+     */
+    static protected function build_activity_plan($controller, $activityid) {
+
+        $plan = $controller->get_plan();
+        $info = $controller->get_info();
+        $infoactivity = $info->activities[$activityid];
+
+        // Add the activity task, responsible for restoring
+        // all the module related information. So it conditionally
+        // as far as the module can be missing on restore
+        if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
+            $plan->add_task($task);
+        }
+
+        // For the given activity, add as many block tasks as necessary
+        // TODO: Add blocks, we need to introspect xml here
+        //$blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
+        //foreach ($blockids as $blockid) {
+        //    $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
+        //}
+    }
+
+    /**
+     * Restore one 1-section backup
+     */
+    static protected function build_section_plan($controller, $sectionid) {
+
+        $plan = $controller->get_plan();
+        $info = $controller->get_info();
+        $infosection = $info->sections[$sectionid];
+
+        // Add the section task, responsible for restoring
+        // all the section related information
+        $plan->add_task(restore_factory::get_restore_section_task($infosection));
+        // For the given section, add as many activity tasks as necessary
+        foreach ($info->activities as $activityid => $activity) {
+            if ($activity->sectionid != $infosection->sectionid) {
+                continue;
+            }
+            if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
+                self::build_activity_plan($controller, $activityid);
+            } else {
+                // TODO: Debug information about module not supported
+            }
+        }
+    }
+
+    /**
+     * Restore one 1-course backup
+     */
+    static protected function build_course_plan($controller, $courseid) {
+
+        $plan = $controller->get_plan();
+        $info = $controller->get_info();
+
+        // Add the course task, responsible for restoring
+        // all the course related information
+        $plan->add_task(restore_factory::get_restore_course_task($info->course, $courseid));
+
+        // For the given course, add as many section tasks as necessary
+        foreach ($info->sections as $sectionid => $section) {
+            self::build_section_plan($controller, $sectionid);
+        }
+
+        // For the given course, add as many block tasks as necessary
+        // TODO: Add blocks, we need to introspect xml here
+        //$blockids = backup_plan_dbops::get_blockids_from_courseid($id);
+        //foreach ($blockids as $blockid) {
+        //    $plan->add_task(restore_factory::get_restore_block_task($controller->get_format(), $blockid));
+        //}
+    }
+}
diff --git a/backup/util/factories/restore_factory.class.php b/backup/util/factories/restore_factory.class.php
new file mode 100644 (file)
index 0000000..58d9ed4
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * @package    moodlecore
+ * @subpackage backup-factories
+ * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Non instantiable factory class providing different restore object instances
+ *
+ * This class contains various methods available in order to easily
+ * create different parts of the restore architecture in an easy way
+ *
+ * TODO: Finish phpdocs
+ */
+abstract class restore_factory {
+
+    public static function get_restore_activity_task($info) {
+
+        $classname = 'restore_' . $info->modulename . '_activity_task';
+        if (class_exists($classname)) {
+            return new $classname($info->title, $info);
+        }
+    }
+
+    public static function get_restore_block_task($format, $blockid, $moduleid = null) {
+        global $CFG, $DB;
+
+        // Check blockid exists
+        if (!$block = $DB->get_record('block_instances', array('id' => $blockid))) {
+            throw new restore_task_exception('block_task_block_instance_not_found', $blockid);
+        }
+
+        // Set default block backup task
+        $classname = 'restore_default_block_task';
+        $testname  = 'restore_' . $block->blockname . '_block_task';
+        // If the block has custom backup/restore task class (testname), use it
+        if (class_exists($testname)) {
+            $classname = $testname;
+        }
+        return new $classname($block->blockname, $blockid, $moduleid);
+    }
+
+    public static function get_restore_section_task($info) {
+
+        return new restore_section_task($info->title, $info);
+    }
+
+    public static function get_restore_course_task($info, $courseid) {
+        global $DB;
+
+        // Check course exists
+        if (!$course = $DB->get_record('course', array('id' => $courseid))) {
+            throw new restore_task_exception('course_task_course_not_found', $courseid);
+        }
+
+        return new restore_course_task($info->title, $info);
+    }
+}
index 3aa3518..557bc86 100644 (file)
@@ -42,7 +42,7 @@ class restore_plan extends base_plan implements loggable {
             throw new restore_plan_exception('wrong_restore_controller_specified');
         }
         $this->controller = $controller;
-        $this->basepath   = $CFG->dataroot . '/temp/backup/' . $controller->get_restoreid();
+        $this->basepath   = $CFG->dataroot . '/temp/backup/' . $controller->get_tempdir();
         parent::__construct('restore_plan');
     }
 
@@ -67,6 +67,10 @@ class restore_plan extends base_plan implements loggable {
         return $this->controller->get_logger();
     }
 
+    public function get_info() {
+        return $this->controller->get_info();
+    }
+
     public function log($message, $level, $a = null, $depth = null, $display = false) {
         backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
     }
index 46e4073..dadf658 100644 (file)
@@ -42,6 +42,10 @@ abstract class restore_task extends base_task {
     public function get_restoreid() {
         return $this->plan->get_restoreid();
     }
+
+    public function get_info() {
+        return $this->plan->get_info();
+    }
 }
 
 /*