MDL-61102 core_search: Restore search manager instance
[moodle.git] / backup / restore.php
blob06f58e65a342180e6eb697202ab6bc5d61f033bc
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 $contextid = required_param('contextid', PARAM_INT);
32 $stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT);
33 $cancel = optional_param('cancel', '', PARAM_ALPHA);
35 list($context, $course, $cm) = get_context_info_array($contextid);
37 navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)));
38 $PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
39 $PAGE->set_context($context);
40 $PAGE->set_pagelayout('admin');
42 require_login($course, null, $cm);
43 require_capability('moodle/restore:restorecourse', $context);
45 if (is_null($course)) {
46 $coursefullname = $SITE->fullname;
47 $courseshortname = $SITE->shortname;
48 } else {
49 $coursefullname = $course->fullname;
50 $courseshortname = $course->shortname;
53 // Show page header.
54 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
55 $PAGE->set_heading($coursefullname);
57 $renderer = $PAGE->get_renderer('core','backup');
58 if (empty($cancel)) {
59 // Do not print the header if user cancelled the process, as we are going to redirect the user.
60 echo $OUTPUT->header();
63 // Prepare a progress bar which can display optionally during long-running
64 // operations while setting up the UI.
65 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
67 // Overall, allow 10 units of progress.
68 $slowprogress->start_progress('', 10);
70 // This progress section counts for loading the restore controller.
71 $slowprogress->start_progress('', 1, 1);
73 // Restore of large courses requires extra memory. Use the amount configured
74 // in admin settings.
75 raise_memory_limit(MEMORY_EXTRA);
77 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
78 $restore = restore_ui::engage_independent_stage($stage, $contextid);
79 } else {
80 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
81 $rc = restore_ui::load_controller($restoreid);
82 if (!$rc) {
83 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
84 if ($restore->process()) {
85 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
86 backup::MODE_GENERAL, $USER->id, $restore->get_target());
89 if ($rc) {
90 // check if the format conversion must happen first
91 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
92 $rc->convert();
95 $restore = new restore_ui($rc, array('contextid'=>$context->id));
99 // End progress section for loading restore controller.
100 $slowprogress->end_progress();
102 // This progress section is for the 'process' function below.
103 $slowprogress->start_progress('', 1, 9);
105 // Depending on the code branch above, $restore may be a restore_ui or it may
106 // be a restore_ui_independent_stage. Either way, this function exists.
107 $restore->set_progress_reporter($slowprogress);
108 $outcome = $restore->process();
110 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
111 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
114 $loghtml = '';
115 // Finish the 'process' progress reporting section, and the overall count.
116 $slowprogress->end_progress();
117 $slowprogress->end_progress();
119 if (!$restore->is_independent()) {
120 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
121 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
122 $restore->get_controller()->set_progress($precheckprogress);
123 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
124 try {
125 // Div used to hide the 'progress' step once the page gets onto 'finished'.
126 echo html_writer::start_div('', array('id' => 'executionprogress'));
127 // Show the current restore state (header with bolded item).
128 echo $renderer->progress_bar($restore->get_progress_bar());
129 // Start displaying the actual progress bar percentage.
130 $restore->get_controller()->set_progress(new \core\progress\display());
131 // Prepare logger.
132 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
133 $restore->get_controller()->add_logger($logger);
134 // Do actual restore.
135 $restore->execute();
136 // Get HTML from logger.
137 if ($CFG->debugdisplay) {
138 $loghtml = $logger->get_html();
140 // Hide this section because we are now going to make the page show 'finished'.
141 echo html_writer::end_div();
142 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
143 } catch(Exception $e) {
144 $restore->cleanup();
145 throw $e;
147 } else {
148 $restore->save_controller();
152 echo $renderer->progress_bar($restore->get_progress_bar());
153 echo $restore->display($renderer);
154 $restore->destroy();
155 unset($restore);
157 // Display log data if there was any.
158 if ($loghtml != '') {
159 echo $renderer->log_display($loghtml);
162 echo $OUTPUT->footer();