Merge branch 'MDL-47378-master' of git://github.com/danpoltawski/moodle
[moodle.git] / backup / restore.php
blob3e4cc0128f8da0bcefd7e396234ee696ebcd43b8
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);
10 list($context, $course, $cm) = get_context_info_array($contextid);
12 navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)));
13 $PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
14 $PAGE->set_context($context);
15 $PAGE->set_pagelayout('admin');
17 require_login($course, null, $cm);
18 require_capability('moodle/restore:restorecourse', $context);
20 if (is_null($course)) {
21 $coursefullname = $SITE->fullname;
22 $courseshortname = $SITE->shortname;
23 } else {
24 $coursefullname = $course->fullname;
25 $courseshortname = $course->shortname;
28 // Show page header.
29 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
30 $PAGE->set_heading($coursefullname);
32 $renderer = $PAGE->get_renderer('core','backup');
33 echo $OUTPUT->header();
35 // Prepare a progress bar which can display optionally during long-running
36 // operations while setting up the UI.
37 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
39 // Overall, allow 10 units of progress.
40 $slowprogress->start_progress('', 10);
42 // This progress section counts for loading the restore controller.
43 $slowprogress->start_progress('', 1, 1);
45 // Restore of large courses requires extra memory. Use the amount configured
46 // in admin settings.
47 raise_memory_limit(MEMORY_EXTRA);
49 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
50 $restore = restore_ui::engage_independent_stage($stage, $contextid);
51 } else {
52 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
53 $rc = restore_ui::load_controller($restoreid);
54 if (!$rc) {
55 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
56 if ($restore->process()) {
57 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
58 backup::MODE_GENERAL, $USER->id, $restore->get_target());
61 if ($rc) {
62 // check if the format conversion must happen first
63 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
64 $rc->convert();
67 $restore = new restore_ui($rc, array('contextid'=>$context->id));
71 // End progress section for loading restore controller.
72 $slowprogress->end_progress();
74 // This progress section is for the 'process' function below.
75 $slowprogress->start_progress('', 1, 9);
77 // Depending on the code branch above, $restore may be a restore_ui or it may
78 // be a restore_ui_independent_stage. Either way, this function exists.
79 $restore->set_progress_reporter($slowprogress);
80 $outcome = $restore->process();
82 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
83 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
86 $loghtml = '';
87 // Finish the 'process' progress reporting section, and the overall count.
88 $slowprogress->end_progress();
89 $slowprogress->end_progress();
91 if (!$restore->is_independent()) {
92 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
93 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
94 $restore->get_controller()->set_progress($precheckprogress);
95 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
96 try {
97 // Div used to hide the 'progress' step once the page gets onto 'finished'.
98 echo html_writer::start_div('', array('id' => 'executionprogress'));
99 // Show the current restore state (header with bolded item).
100 echo $renderer->progress_bar($restore->get_progress_bar());
101 // Start displaying the actual progress bar percentage.
102 $restore->get_controller()->set_progress(new \core\progress\display());
103 // Prepare logger.
104 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
105 $restore->get_controller()->add_logger($logger);
106 // Do actual restore.
107 $restore->execute();
108 // Get HTML from logger.
109 $loghtml = $logger->get_html();
110 // Hide this section because we are now going to make the page show 'finished'.
111 echo html_writer::end_div();
112 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
113 } catch(Exception $e) {
114 $restore->cleanup();
115 throw $e;
117 } else {
118 $restore->save_controller();
122 echo $renderer->progress_bar($restore->get_progress_bar());
123 echo $restore->display($renderer);
124 $restore->destroy();
125 unset($restore);
127 // Display log data if there was any.
128 if ($loghtml != '') {
129 echo $renderer->log_display($loghtml);
132 echo $OUTPUT->footer();