Merge branch 'MDL-66712-master-selfautounenrol' of git://github.com/mudrd8mz/moodle
[moodle.git] / backup / restore.php
bloba6b7d76280a4a7781498c51b55bbcf1c4a665159
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 $courseurl = new moodle_url('/');
59 } else {
60 $coursefullname = $course->fullname;
61 $courseshortname = $course->shortname;
62 $courseurl = course_get_url($course->id);
65 // Show page header.
66 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
67 $PAGE->set_heading($coursefullname);
69 $renderer = $PAGE->get_renderer('core','backup');
70 if (empty($cancel)) {
71 // Do not print the header if user cancelled the process, as we are going to redirect the user.
72 echo $OUTPUT->header();
75 // Prepare a progress bar which can display optionally during long-running
76 // operations while setting up the UI.
77 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
79 // Overall, allow 10 units of progress.
80 $slowprogress->start_progress('', 10);
82 // This progress section counts for loading the restore controller.
83 $slowprogress->start_progress('', 1, 1);
85 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
86 $restore = restore_ui::engage_independent_stage($stage, $contextid);
87 } else {
88 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
89 $rc = restore_ui::load_controller($restoreid);
90 if (!$rc) {
91 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
92 if ($restore->process()) {
93 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
94 $backupmode, $USER->id, $restore->get_target(), null, backup::RELEASESESSION_YES);
97 if ($rc) {
98 // check if the format conversion must happen first
99 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
100 $rc->convert();
103 $restore = new restore_ui($rc, array('contextid'=>$context->id));
107 // End progress section for loading restore controller.
108 $slowprogress->end_progress();
110 // This progress section is for the 'process' function below.
111 $slowprogress->start_progress('', 1, 9);
113 // Depending on the code branch above, $restore may be a restore_ui or it may
114 // be a restore_ui_independent_stage. Either way, this function exists.
115 $restore->set_progress_reporter($slowprogress);
116 $outcome = $restore->process();
118 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
119 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
122 $loghtml = '';
123 // Finish the 'process' progress reporting section, and the overall count.
124 $slowprogress->end_progress();
125 $slowprogress->end_progress();
127 if (!$restore->is_independent()) {
128 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
129 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
130 $restore->get_controller()->set_progress($precheckprogress);
131 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage() && $backupmode != backup::MODE_ASYNC) {
132 try {
133 // Div used to hide the 'progress' step once the page gets onto 'finished'.
134 echo html_writer::start_div('', array('id' => 'executionprogress'));
135 // Show the current restore state (header with bolded item).
136 echo $renderer->progress_bar($restore->get_progress_bar());
137 // Start displaying the actual progress bar percentage.
138 $restore->get_controller()->set_progress(new \core\progress\display());
139 // Prepare logger.
140 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
141 $restore->get_controller()->add_logger($logger);
142 // Do actual restore.
143 $restore->execute();
144 // Get HTML from logger.
145 if ($CFG->debugdisplay) {
146 $loghtml = $logger->get_html();
148 // Hide this section because we are now going to make the page show 'finished'.
149 echo html_writer::end_div();
150 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
151 } catch(Exception $e) {
152 $restore->cleanup();
153 throw $e;
155 } else {
156 $restore->save_controller();
160 echo $renderer->progress_bar($restore->get_progress_bar());
162 if ($restore->get_stage() != restore_ui::STAGE_PROCESS) {
163 echo $restore->display($renderer);
164 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS && $restore->requires_substage()) {
165 echo $restore->display($renderer);
166 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS
167 && !$restore->requires_substage()
168 && $backupmode == backup::MODE_ASYNC) {
169 // Asynchronous restore.
170 // Create adhoc task for restore.
171 $restoreid = $restore->get_restoreid();
172 $asynctask = new \core\task\asynchronous_restore_task();
173 $asynctask->set_blocking(false);
174 $asynctask->set_custom_data(array('backupid' => $restoreid));
175 \core\task\manager::queue_adhoc_task($asynctask);
177 // Add ajax progress bar and initiate ajax via a template.
178 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $contextid));
179 $progresssetup = array(
180 'backupid' => $restoreid,
181 'contextid' => $contextid,
182 'courseurl' => $courseurl->out(),
183 'restoreurl' => $restoreurl->out()
185 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();