Merge branch 'MDL-74280' of https://github.com/paulholden/moodle
[moodle.git] / backup / restore.php
blob5250c0984438f5b93095fbee0493b6f4ac5f56cd
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This script is used to configure and execute the restore proccess.
20 * @package core
21 * @subpackage backup
22 * @copyright Moodle
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('NO_OUTPUT_BUFFERING', true);
28 require_once('../config.php');
29 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
31 // Restore of large courses requires extra memory. Use the amount configured
32 // in admin settings.
33 raise_memory_limit(MEMORY_EXTRA);
35 $contextid = required_param('contextid', PARAM_INT);
36 $stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT);
37 $cancel = optional_param('cancel', '', PARAM_ALPHA);
39 // Determine if we are performing realtime for asynchronous backups.
40 $backupmode = backup::MODE_GENERAL;
41 if (async_helper::is_async_enabled()) {
42 $backupmode = backup::MODE_ASYNC;
45 list($context, $course, $cm) = get_context_info_array($contextid);
47 navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)));
48 $PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
49 $PAGE->set_context($context);
50 $PAGE->set_pagelayout('admin');
51 $PAGE->set_secondary_active_tab('coursereuse');
53 require_login($course, null, $cm);
54 require_capability('moodle/restore:restorecourse', $context);
56 if (is_null($course)) {
57 $coursefullname = $SITE->fullname;
58 $courseshortname = $SITE->shortname;
59 $courseurl = new moodle_url('/');
60 } else {
61 $coursefullname = $course->fullname;
62 $courseshortname = $course->shortname;
63 $courseurl = course_get_url($course->id);
66 // Show page header.
67 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
68 $PAGE->set_heading($coursefullname);
70 $renderer = $PAGE->get_renderer('core','backup');
71 if (empty($cancel)) {
72 // Do not print the header if user cancelled the process, as we are going to redirect the user.
73 echo $OUTPUT->header();
76 // Prepare a progress bar which can display optionally during long-running
77 // operations while setting up the UI.
78 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
80 // Overall, allow 10 units of progress.
81 $slowprogress->start_progress('', 10);
83 // This progress section counts for loading the restore controller.
84 $slowprogress->start_progress('', 1, 1);
86 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
87 $restore = restore_ui::engage_independent_stage($stage, $contextid);
88 } else {
89 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
90 $rc = restore_ui::load_controller($restoreid);
91 if (!$rc) {
92 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
93 if ($restore->process()) {
94 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
95 $backupmode, $USER->id, $restore->get_target(), null, backup::RELEASESESSION_YES);
98 if ($rc) {
99 // check if the format conversion must happen first
100 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
101 $rc->convert();
104 $restore = new restore_ui($rc, array('contextid'=>$context->id));
108 // End progress section for loading restore controller.
109 $slowprogress->end_progress();
111 // This progress section is for the 'process' function below.
112 $slowprogress->start_progress('', 1, 9);
114 // Depending on the code branch above, $restore may be a restore_ui or it may
115 // be a restore_ui_independent_stage. Either way, this function exists.
116 $restore->set_progress_reporter($slowprogress);
117 $outcome = $restore->process();
119 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
120 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
123 $loghtml = '';
124 // Finish the 'process' progress reporting section, and the overall count.
125 $slowprogress->end_progress();
126 $slowprogress->end_progress();
128 if (!$restore->is_independent()) {
129 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
130 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
131 $restore->get_controller()->set_progress($precheckprogress);
132 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage() && $backupmode != backup::MODE_ASYNC) {
133 try {
134 // Div used to hide the 'progress' step once the page gets onto 'finished'.
135 echo html_writer::start_div('', array('id' => 'executionprogress'));
136 // Show the current restore state (header with bolded item).
137 echo $renderer->progress_bar($restore->get_progress_bar());
138 // Start displaying the actual progress bar percentage.
139 $restore->get_controller()->set_progress(new \core\progress\display());
140 // Prepare logger.
141 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
142 $restore->get_controller()->add_logger($logger);
143 // Do actual restore.
144 $restore->execute();
145 // Get HTML from logger.
146 if ($CFG->debugdisplay) {
147 $loghtml = $logger->get_html();
149 // Hide this section because we are now going to make the page show 'finished'.
150 echo html_writer::end_div();
151 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
152 } catch(Exception $e) {
153 $restore->cleanup();
154 throw $e;
156 } else {
157 $restore->save_controller();
161 echo $renderer->progress_bar($restore->get_progress_bar());
163 if ($restore->get_stage() != restore_ui::STAGE_PROCESS) {
164 echo $restore->display($renderer);
165 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS && $restore->requires_substage()) {
166 echo $restore->display($renderer);
167 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS
168 && !$restore->requires_substage()
169 && $backupmode == backup::MODE_ASYNC) {
170 // Asynchronous restore.
171 // Create adhoc task for restore.
172 $restoreid = $restore->get_restoreid();
173 $asynctask = new \core\task\asynchronous_restore_task();
174 $asynctask->set_blocking(false);
175 $asynctask->set_userid($USER->id);
176 $asynctask->set_custom_data(array('backupid' => $restoreid));
177 \core\task\manager::queue_adhoc_task($asynctask);
179 // Add ajax progress bar and initiate ajax via a template.
180 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $contextid));
181 $progresssetup = array(
182 'backupid' => $restoreid,
183 'contextid' => $contextid,
184 'courseurl' => $courseurl->out(),
185 'restoreurl' => $restoreurl->out()
187 echo $renderer->render_from_template('core/async_backup_status', $progresssetup);
190 $restore->destroy();
191 unset($restore);
193 // Display log data if there was any.
194 if ($loghtml != '') {
195 echo $renderer->log_display($loghtml);
198 echo $OUTPUT->footer();