Merge branch 'MDL-66457-37' of git://github.com/andrewnicols/moodle into MOODLE_37_STABLE
[moodle.git] / backup / restore.php
blob4ee134e0093ca4858cbc602f21ba792b83cd3ac2
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');
52 require_login($course, null, $cm);
53 require_capability('moodle/restore:restorecourse', $context);
55 if (is_null($course)) {
56 $coursefullname = $SITE->fullname;
57 $courseshortname = $SITE->shortname;
58 } else {
59 $coursefullname = $course->fullname;
60 $courseshortname = $course->shortname;
63 // Show page header.
64 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
65 $PAGE->set_heading($coursefullname);
67 $renderer = $PAGE->get_renderer('core','backup');
68 if (empty($cancel)) {
69 // Do not print the header if user cancelled the process, as we are going to redirect the user.
70 echo $OUTPUT->header();
73 // Prepare a progress bar which can display optionally during long-running
74 // operations while setting up the UI.
75 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
77 // Overall, allow 10 units of progress.
78 $slowprogress->start_progress('', 10);
80 // This progress section counts for loading the restore controller.
81 $slowprogress->start_progress('', 1, 1);
83 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
84 $restore = restore_ui::engage_independent_stage($stage, $contextid);
85 } else {
86 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
87 $rc = restore_ui::load_controller($restoreid);
88 if (!$rc) {
89 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
90 if ($restore->process()) {
91 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
92 $backupmode, $USER->id, $restore->get_target());
95 if ($rc) {
96 // check if the format conversion must happen first
97 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
98 $rc->convert();
101 $restore = new restore_ui($rc, array('contextid'=>$context->id));
105 // End progress section for loading restore controller.
106 $slowprogress->end_progress();
108 // This progress section is for the 'process' function below.
109 $slowprogress->start_progress('', 1, 9);
111 // Depending on the code branch above, $restore may be a restore_ui or it may
112 // be a restore_ui_independent_stage. Either way, this function exists.
113 $restore->set_progress_reporter($slowprogress);
114 $outcome = $restore->process();
116 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
117 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
120 $loghtml = '';
121 // Finish the 'process' progress reporting section, and the overall count.
122 $slowprogress->end_progress();
123 $slowprogress->end_progress();
125 if (!$restore->is_independent()) {
126 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
127 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
128 $restore->get_controller()->set_progress($precheckprogress);
129 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage() && $backupmode != backup::MODE_ASYNC) {
130 try {
131 // Div used to hide the 'progress' step once the page gets onto 'finished'.
132 echo html_writer::start_div('', array('id' => 'executionprogress'));
133 // Show the current restore state (header with bolded item).
134 echo $renderer->progress_bar($restore->get_progress_bar());
135 // Start displaying the actual progress bar percentage.
136 $restore->get_controller()->set_progress(new \core\progress\display());
137 // Prepare logger.
138 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
139 $restore->get_controller()->add_logger($logger);
140 // Do actual restore.
141 $restore->execute();
142 // Get HTML from logger.
143 if ($CFG->debugdisplay) {
144 $loghtml = $logger->get_html();
146 // Hide this section because we are now going to make the page show 'finished'.
147 echo html_writer::end_div();
148 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
149 } catch(Exception $e) {
150 $restore->cleanup();
151 throw $e;
153 } else {
154 $restore->save_controller();
158 echo $renderer->progress_bar($restore->get_progress_bar());
160 if ($restore->get_stage() != restore_ui::STAGE_PROCESS) {
161 echo $restore->display($renderer);
162 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS && $restore->requires_substage()) {
163 echo $restore->display($renderer);
164 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS
165 && !$restore->requires_substage()
166 && $backupmode == backup::MODE_ASYNC) {
167 // Asynchronous restore.
168 // Create adhoc task for restore.
169 $restoreid = $restore->get_restoreid();
170 $asynctask = new \core\task\asynchronous_restore_task();
171 $asynctask->set_blocking(false);
172 $asynctask->set_custom_data(array('backupid' => $restoreid));
173 \core\task\manager::queue_adhoc_task($asynctask);
175 // Add ajax progress bar and initiate ajax via a template.
176 $courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
177 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $contextid));
178 $progresssetup = array(
179 'backupid' => $restoreid,
180 'contextid' => $contextid,
181 'courseurl' => $courseurl->out(),
182 'restoreurl' => $restoreurl->out()
184 echo $renderer->render_from_template('core/async_backup_status', $progresssetup);
188 $restore->destroy();
189 unset($restore);
191 // Display log data if there was any.
192 if ($loghtml != '') {
193 echo $renderer->log_display($loghtml);
196 echo $OUTPUT->footer();