MDL-47729 repository_filesystem: smarter sync of references
[moodle.git] / backup / restore.php
blob25a216cff0f031dcff24a6df60e29ab9589317c5
1 <?php
2 //This script is used to configure and execute the restore proccess.
4 require_once('../config.php');
5 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
7 $contextid = required_param('contextid', PARAM_INT);
8 $stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT);
9 $cancel = optional_param('cancel', '', PARAM_ALPHA);
11 list($context, $course, $cm) = get_context_info_array($contextid);
13 navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)));
14 $PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
15 $PAGE->set_context($context);
16 $PAGE->set_pagelayout('admin');
18 require_login($course, null, $cm);
19 require_capability('moodle/restore:restorecourse', $context);
21 if (is_null($course)) {
22 $coursefullname = $SITE->fullname;
23 $courseshortname = $SITE->shortname;
24 } else {
25 $coursefullname = $course->fullname;
26 $courseshortname = $course->shortname;
29 // Show page header.
30 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
31 $PAGE->set_heading($coursefullname);
33 $renderer = $PAGE->get_renderer('core','backup');
34 if (empty($cancel)) {
35 // Do not print the header if user cancelled the process, as we are going to redirect the user.
36 echo $OUTPUT->header();
39 // Prepare a progress bar which can display optionally during long-running
40 // operations while setting up the UI.
41 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
43 // Overall, allow 10 units of progress.
44 $slowprogress->start_progress('', 10);
46 // This progress section counts for loading the restore controller.
47 $slowprogress->start_progress('', 1, 1);
49 // Restore of large courses requires extra memory. Use the amount configured
50 // in admin settings.
51 raise_memory_limit(MEMORY_EXTRA);
53 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
54 $restore = restore_ui::engage_independent_stage($stage, $contextid);
55 } else {
56 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
57 $rc = restore_ui::load_controller($restoreid);
58 if (!$rc) {
59 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
60 if ($restore->process()) {
61 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
62 backup::MODE_GENERAL, $USER->id, $restore->get_target());
65 if ($rc) {
66 // check if the format conversion must happen first
67 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
68 $rc->convert();
71 $restore = new restore_ui($rc, array('contextid'=>$context->id));
75 // End progress section for loading restore controller.
76 $slowprogress->end_progress();
78 // This progress section is for the 'process' function below.
79 $slowprogress->start_progress('', 1, 9);
81 // Depending on the code branch above, $restore may be a restore_ui or it may
82 // be a restore_ui_independent_stage. Either way, this function exists.
83 $restore->set_progress_reporter($slowprogress);
84 $outcome = $restore->process();
86 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
87 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
90 $loghtml = '';
91 // Finish the 'process' progress reporting section, and the overall count.
92 $slowprogress->end_progress();
93 $slowprogress->end_progress();
95 if (!$restore->is_independent()) {
96 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
97 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
98 $restore->get_controller()->set_progress($precheckprogress);
99 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
100 try {
101 // Div used to hide the 'progress' step once the page gets onto 'finished'.
102 echo html_writer::start_div('', array('id' => 'executionprogress'));
103 // Show the current restore state (header with bolded item).
104 echo $renderer->progress_bar($restore->get_progress_bar());
105 // Start displaying the actual progress bar percentage.
106 $restore->get_controller()->set_progress(new \core\progress\display());
107 // Prepare logger.
108 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
109 $restore->get_controller()->add_logger($logger);
110 // Do actual restore.
111 $restore->execute();
112 // Get HTML from logger.
113 $loghtml = $logger->get_html();
114 // Hide this section because we are now going to make the page show 'finished'.
115 echo html_writer::end_div();
116 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
117 } catch(Exception $e) {
118 $restore->cleanup();
119 throw $e;
121 } else {
122 $restore->save_controller();
126 echo $renderer->progress_bar($restore->get_progress_bar());
127 echo $restore->display($renderer);
128 $restore->destroy();
129 unset($restore);
131 // Display log data if there was any.
132 if ($loghtml != '') {
133 echo $renderer->log_display($loghtml);
136 echo $OUTPUT->footer();