$progress->progress(1); // 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->tempdir . '/backup/' . $backupid; if (!file_exists($tempdestination) || !is_dir($tempdestination)) { 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_IMPORT, $USER->id, $restoretarget); // Start a progress section for the restore, which will consist of 2 steps // (the precheck and then the actual restore). $progress->start_progress('Restore process', 2); $rc->set_progress($progress); // Set logger for restore. $rc->add_logger($logger); // Convert the backup if required.... it should NEVER happed if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { $rc->convert(); } // Mark the UI finished. $rc->finish_ui(); // Execute prechecks $warnings = false; if (!$rc->execute_precheck()) { $precheckresults = $rc->get_precheck_results(); if (is_array($precheckresults)) { if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
/** * Takes backupid and original course object as arguments and returns a new courseid. * * @param string $backupid The backupid * @param object $newcourse Target course object * @throws coding_exception Moodle coding exception */ private function restore($backupid, $newcourse) { global $CFG, $DB, $USER; // Call restore. $rc = new \restore_controller($backupid, $newcourse->id, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id, \backup::TARGET_EXISTING_ADDING); // Setup custom logger that prints dots. $logger = new logger(\backup::LOG_INFO, false, true); self::$currentlogger = $logger; $logger->potential_dot(); $rc->get_logger()->set_next($logger); $rc->set_progress(new progress()); foreach ($rc->get_plan()->get_tasks() as $taskindex => $task) { $settings = $task->get_settings(); foreach ($settings as $settingindex => $setting) { // Set userinfo true for activity, since we controlled it // more accurately (i.e. true only for glossary) in backup. if (preg_match('/^glossary_([0-9])*_userinfo$$/', $setting->get_name())) { $setting->set_value(true); } if ($taskindex == 0 && isset($this->backupsettings[$setting->get_name()])) { $setting->set_value($this->backupsettings[$setting->get_name()]); } } } if (!$rc->execute_precheck()) { if (empty($CFG->keeptempdirectoriesonbackup)) { fulldelete($this->backupbasepath); } $results = print_r($rc->get_precheck_results(), true); print \html_writer::tag('pre', s($results)); throw new \coding_exception('Restore precheck error.'); } $logger->potential_dot(); $rc->execute_plan(); $rc->destroy(); // Delete backup file. if (empty($CFG->keeptempdirectoriesonbackup)) { fulldelete($this->backupbasepath); } }
/** * Tests the restore_controller. */ public function test_restore_controller_is_executing() { global $CFG; // Make a backup. check_dir_exists($CFG->tempdir . '/backup'); $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid); $backupid = $bc->get_backupid(); $bc->execute_plan(); $bc->destroy(); // The progress class will get called during restore, so we can use that // to check the executing flag is true. $progress = new core_backup_progress_restore_is_executing(); // Set up restore. $rc = new restore_controller($backupid, $this->courseid, backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid, backup::TARGET_EXISTING_ADDING); $this->assertTrue($rc->execute_precheck()); // Check restore is NOT executing. $this->assertFalse(restore_controller::is_executing()); // Execute restore. $rc->set_progress($progress); $rc->execute_plan(); // Check restore is NOT executing afterward either. $this->assertFalse(restore_controller::is_executing()); $rc->destroy(); // During restore, check that executing was true. $this->assertTrue(count($progress->executing) > 0); $alltrue = true; foreach ($progress->executing as $executing) { if (!$executing) { $alltrue = false; break; } } $this->assertTrue($alltrue); }