MDL-23752 backup - export/import mode. Fixed various details.
authorEloy Lafuente <stronk7@moodle.org>
Tue, 28 Sep 2010 09:33:54 +0000 (09:33 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Tue, 28 Sep 2010 09:33:54 +0000 (09:33 +0000)
backup/import.php
backup/moodle2/backup_final_task.class.php
backup/moodle2/backup_stepslib.php
backup/util/helper/backup_helper.class.php
backup/util/plan/backup_plan.class.php
backup/util/plan/restore_plan.class.php

index 0106e70..0cc71fb 100644 (file)
@@ -59,7 +59,7 @@ require_capability('moodle/backup:backuptargetimport', $importcontext);
 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
 if (!($bc = backup_ui::load_controller($backupid))) {
     $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
-                            backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
+                            backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
     $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
     $settings = $bc->get_plan()->get_settings();
 
@@ -90,23 +90,18 @@ if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
     // First execute the backup
     $backup->execute();
-    $results = $backup->get_controller()->get_results();
-    
-    // Check whether the backup directory still exists and if it doesn't extract the
-    // backup file so that we have it
+
+    // Check whether the backup directory still exists. If missing, something
+    // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
+    // backups don't store resulting files ever
     $tempdestination = $CFG->dataroot . '/temp/backup/' . $backupid;
     if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
-        $file = $results['backup_destination'];
-        $file->extract_to_pathname(get_file_packer('application/zip'), $tempdestination);
-    }
-    if (!empty($results['backup_destination'])) {
-        // Delete the backup file, we only want the directory
-        $results['backup_destination']->delete();
+        print_error('unknownbackupexporterror'); // shouldn't happen ever
     }
 
     // Prepare the restore controller. We don't need a UI here as we will just use what
     // ever the restore has (the user has just chosen).
-    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id, $restoretarget);
+    $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
     // Convert the backup if required.... it should NEVER happed
     if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
         $rc->convert();
@@ -161,4 +156,4 @@ if ($backup->enforce_changed_dependencies()) {
 }
 echo $renderer->progress_bar($backup->get_progress_bar());
 echo $backup->display();
-echo $OUTPUT->footer();
\ No newline at end of file
+echo $OUTPUT->footer();
index 93a07c0..8305886 100644 (file)
@@ -105,14 +105,20 @@ class backup_final_task extends backup_task {
         // to the backup, settings, license, versions and other useful information
         $this->add_step(new backup_main_structure_step('mainfile', 'moodle_backup.xml'));
 
-        // Generate the zip file (mbz extension)
-        $this->add_step(new backup_zip_contents('zip_contents'));
+        // On backup::MODE_IMPORT, we don't have to zip nor store the the file, skip these steps
+        if ($this->plan->get_mode() != backup::MODE_IMPORT) {
+            // Generate the zip file (mbz extension)
+            $this->add_step(new backup_zip_contents('zip_contents'));
 
-        // Copy the generated zip (.mbz) file to final destination
-        $this->add_step(new backup_store_backup_file('save_backupfile'));
+            // Copy the generated zip (.mbz) file to final destination
+            $this->add_step(new backup_store_backup_file('save_backupfile'));
+        }
 
-        // Clean the temp dir (conditionally) and drop temp table
-        $this->add_step(new drop_and_clean_temp_stuff('drop_and_clean_temp_stuff'));
+        // Clean the temp dir (conditionally) and drop temp tables
+        $cleanstep = new drop_and_clean_temp_stuff('drop_and_clean_temp_stuff');
+        // Decide about to delete the temp dir (based on backup::MODE_IMPORT)
+        $cleanstep->skip_cleaning_temp_dir($this->plan->get_mode() == backup::MODE_IMPORT);
+        $this->add_step($cleanstep);
 
         $this->built = true;
     }
index cf61e1e..d76e00c 100644 (file)
@@ -49,14 +49,24 @@ class create_and_clean_temp_stuff extends backup_execution_step {
  */
 class drop_and_clean_temp_stuff extends backup_execution_step {
 
+    protected $skipcleaningtempdir = false;
+
     protected function define_execution() {
         global $CFG;
+
         backup_controller_dbops::drop_backup_ids_temp_table($this->get_backupid()); // Drop ids temp table
         backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60));              // Delete > 4 hours temp dirs
-        if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally
+        // Delete temp dir conditionally:
+        // 1) If $CFG->keeptempdirectoriesonbackup is not enabled
+        // 2) If backup temp dir deletion has been marked to be avoided
+        if (empty($CFG->keeptempdirectoriesonbackup) && !$this->skipcleaningtempdir) {
             backup_helper::delete_backup_dir($this->get_backupid()); // Empty backup dir
         }
     }
+
+    public function skip_cleaning_temp_dir($skip) {
+        $this->skipcleaningtempdir = $skip;
+    }
 }
 
 /**
index 759142d..78d9acf 100644 (file)
@@ -173,7 +173,7 @@ abstract class backup_helper {
 
     /**
      * Given one backupid and the (FS) final generated file, perform its final storage
-     * into Moodle file storage
+     * into Moodle file storage. For stored files it returns the complete file_info object
      */
     static public function store_backup_file($backupid, $filepath) {
 
@@ -191,15 +191,14 @@ abstract class backup_helper {
         $courseid  = $dinfo[0]->courseid;              // Id of the course
 
         // Quick hack. If for any reason, filename is blank, fix it here.
-        // This hack will be out once MDL-22142 - P26 gets fixed
+        // TODO: This hack will be out once MDL-22142 - P26 gets fixed
         if (empty($filename)) {
             $filename = backup_plan_dbops::get_default_backup_filename('moodle2', $backuptype, $id, $hasusers, $isannon);
         }
 
-
         // Backups of type IMPORT aren't stored ever
         if ($backupmode == backup::MODE_IMPORT) {
-            return true;
+            return false;
         }
 
         // Calculate file storage options of id being backup
index bf38ec0..1a90b6d 100644 (file)
@@ -56,6 +56,10 @@ class backup_plan extends base_plan implements loggable {
         return $this->controller->get_backupid();
     }
 
+    public function get_mode() {
+        return $this->controller->get_mode();
+    }
+
     public function get_courseid() {
         return $this->controller->get_courseid();
     }
index 355c4a3..dc78f71 100644 (file)
@@ -73,6 +73,10 @@ class restore_plan extends base_plan implements loggable {
         return $this->controller->get_courseid();
     }
 
+    public function get_mode() {
+        return $this->controller->get_mode();
+    }
+
     public function get_basepath() {
         return $this->basepath;
     }