Merge branch 'MDL-6074-master' of git://github.com/mihailges/moodle
[moodle.git] / backup / backup.php
blob3df327aa24a33afd3b8b121b8722cb4ceba26261
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This script is used to configure and execute the backup proccess.
21 * @package core
22 * @subpackage backup
23 * @copyright Moodle
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define('NO_OUTPUT_BUFFERING', true);
29 require_once('../config.php');
30 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
31 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
34 $courseid = required_param('id', PARAM_INT);
35 $sectionid = optional_param('section', null, PARAM_INT);
36 $cmid = optional_param('cm', null, PARAM_INT);
37 /**
38 * Part of the forms in stages after initial, is POST never GET
40 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
42 $url = new moodle_url('/backup/backup.php', array('id'=>$courseid));
43 if ($sectionid !== null) {
44 $url->param('section', $sectionid);
46 if ($cmid !== null) {
47 $url->param('cm', $cmid);
49 $PAGE->set_url($url);
50 $PAGE->set_pagelayout('admin');
52 $id = $courseid;
53 $cm = null;
54 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
55 $type = backup::TYPE_1COURSE;
56 if (!is_null($sectionid)) {
57 $section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$sectionid), '*', MUST_EXIST);
58 $type = backup::TYPE_1SECTION;
59 $id = $sectionid;
61 if (!is_null($cmid)) {
62 $cm = get_coursemodule_from_id(null, $cmid, $course->id, false, MUST_EXIST);
63 $type = backup::TYPE_1ACTIVITY;
64 $id = $cmid;
66 require_login($course, false, $cm);
68 switch ($type) {
69 case backup::TYPE_1COURSE :
70 require_capability('moodle/backup:backupcourse', context_course::instance($course->id));
71 $heading = get_string('backupcourse', 'backup', $course->shortname);
72 break;
73 case backup::TYPE_1SECTION :
74 $coursecontext = context_course::instance($course->id);
75 require_capability('moodle/backup:backupsection', $coursecontext);
76 if ((string)$section->name !== '') {
77 $sectionname = format_string($section->name, true, array('context' => $coursecontext));
78 $heading = get_string('backupsection', 'backup', $sectionname);
79 $PAGE->navbar->add($sectionname);
80 } else {
81 $heading = get_string('backupsection', 'backup', $section->section);
82 $PAGE->navbar->add(get_string('section').' '.$section->section);
84 break;
85 case backup::TYPE_1ACTIVITY :
86 require_capability('moodle/backup:backupactivity', context_module::instance($cm->id));
87 $heading = get_string('backupactivity', 'backup', $cm->name);
88 break;
89 default :
90 print_error('unknownbackuptype');
93 // Backup of large courses requires extra memory. Use the amount configured
94 // in admin settings.
95 raise_memory_limit(MEMORY_EXTRA);
97 if (!($bc = backup_ui::load_controller($backupid))) {
98 $bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
99 backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
101 $backup = new backup_ui($bc);
103 $PAGE->set_title($heading);
104 $PAGE->set_heading($heading);
106 $renderer = $PAGE->get_renderer('core','backup');
107 echo $OUTPUT->header();
109 // Prepare a progress bar which can display optionally during long-running
110 // operations while setting up the UI.
111 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
113 $previous = optional_param('previous', false, PARAM_BOOL);
114 if ($backup->get_stage() == backup_ui::STAGE_SCHEMA && !$previous) {
115 // After schema stage, we are probably going to get to the confirmation stage,
116 // The confirmation stage has 2 sets of progress, so this is needed to prevent
117 // it showing 2 progress bars.
118 $twobars = true;
119 $slowprogress->start_progress('', 2);
120 } else {
121 $twobars = false;
123 $backup->get_controller()->set_progress($slowprogress);
124 $backup->process();
126 if ($backup->enforce_changed_dependencies()) {
127 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
130 $loghtml = '';
131 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
132 // Display an extra backup step bar so that we can show the 'processing' step first.
133 echo html_writer::start_div('', array('id' => 'executionprogress'));
134 echo $renderer->progress_bar($backup->get_progress_bar());
135 $backup->get_controller()->set_progress(new \core\progress\display());
137 // Prepare logger and add to end of chain.
138 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
139 $backup->get_controller()->add_logger($logger);
141 // Carry out actual backup.
142 $backup->execute();
144 // Backup controller gets saved/loaded so the logger object changes and we
145 // have to retrieve it.
146 $logger = $backup->get_controller()->get_logger();
147 while (!is_a($logger, 'core_backup_html_logger')) {
148 $logger = $logger->get_next();
151 // Get HTML from logger.
152 if ($CFG->debugdisplay) {
153 $loghtml = $logger->get_html();
156 // Hide the progress display and first backup step bar (the 'finished' step will show next).
157 echo html_writer::end_div();
158 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
159 } else {
160 $backup->save_controller();
163 // Displaying UI can require progress reporting, so do it here before outputting
164 // the backup stage bar (as part of the existing progress bar, if required).
165 $ui = $backup->display($renderer);
166 if ($twobars) {
167 $slowprogress->end_progress();
170 echo $renderer->progress_bar($backup->get_progress_bar());
172 echo $ui;
173 $backup->destroy();
174 unset($backup);
176 // Display log data if there was any.
177 if ($loghtml != '') {
178 echo $renderer->log_display($loghtml);
181 echo $OUTPUT->footer();