MDL-21432 backup - allow arbitrary contexts when annotating files. Record orignal...
[moodle.git] / backup / moodle2 / restore_stepslib.php
CommitLineData
482aac65
EL
1<?php
2
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.
14//
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/>.
17
18/**
19 * @package moodlecore
20 * @subpackage backup-moodle2
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Define all the restore steps that will be used by common tasks in restore
27 */
28
29/**
30 * delete old directories and conditionally create backup_temp_ids table
31 */
32class restore_create_and_clean_temp_stuff extends restore_execution_step {
33
34 protected function define_execution() {
b8bb45b0 35 $exists = restore_controller_dbops::create_restore_temp_tables($this->get_restoreid()); // temp tables conditionally
482aac65
EL
36 // If the table already exists, it's because restore_prechecks have been executed in the same
37 // request (without problems) and it already contains a bunch of preloaded information (users...)
38 // that we aren't going to execute again
39 if ($exists) { // Inform plan about preloaded information
40 $this->task->set_preloaded_information();
41 }
76cfb124
EL
42 // Create the old-course-ctxid to new-course-ctxid mapping, we need that available since the beginning
43 $itemid = $this->task->get_old_contextid();
44 $newitemid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
45 restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid);
482aac65
EL
46 }
47}
48
49/**
50 * delete the temp dir used by backup/restore (conditionally),
51 * delete old directories and drop temp ids table
52 */
53class restore_drop_and_clean_temp_stuff extends restore_execution_step {
54
55 protected function define_execution() {
56 global $CFG;
b8bb45b0 57 restore_controller_dbops::drop_restore_temp_tables($this->get_restoreid()); // Drop ids temp table
482aac65
EL
58 backup_helper::delete_old_backup_dirs(time() - (4 * 60 * 60)); // Delete > 4 hours temp dirs
59 if (empty($CFG->keeptempdirectoriesonbackup)) { // Conditionally
60 backup_helper::delete_backup_dir($this->get_restoreid()); // Empty backup dir
61 }
62 }
63}
64
65/*
66 * Execution step that, *conditionally* (if there isn't preloaded information)
67 * will load the inforef files for all the included course/section/activity tasks
68 * to backup_temp_ids. They will be stored with "xxxxref" as itemname
69 */
70class restore_load_included_inforef_records extends restore_execution_step {
71
72 protected function define_execution() {
73
74 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
75 return;
76 }
77
78 // Get all the included inforef files
79 $files = restore_dbops::get_needed_inforef_files($this->get_restoreid());
80 foreach ($files as $file) {
81 restore_dbops::load_inforef_to_tempids($this->get_restoreid(), $file); // Load each inforef file to temp_ids
82 }
83 }
84}
85
76cfb124 86/*
b8bb45b0 87 * Execution step that will load all the needed files into backup_files_temp
76cfb124
EL
88 * - info: contains the whole original object (times, names...)
89 * (all them being original ids as loaded from xml)
90 */
91class restore_load_included_files extends restore_structure_step {
92
93 protected function define_structure() {
94
95 $file = new restore_path_element('file', '/files/file');
96
97 return array($file);
98 }
99
100 // Processing functions go here
101 public function process_file($data) {
102
103 $data = (object)$data; // handy
104
76cfb124
EL
105 // load it if needed:
106 // - it it is one of the annotated inforef files (course/section/activity/block)
107 // - it is one "user", "group" or "grade" component file
b8bb45b0 108 $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id);
76cfb124
EL
109 $iscomponent = ($data->component == 'user' || $data->component == 'group' || $data->component == 'grade');
110 if ($isfileref || $iscomponent) {
b8bb45b0 111 restore_dbops::set_backup_files_record($this->get_restoreid(), $data);
76cfb124
EL
112 }
113 }
114}
115
482aac65
EL
116/**
117 * Execution step that, *conditionally* (if there isn't preloaded information
118 * and users have been selected in settings, will load all the needed users
119 * to backup_temp_ids. They will be stored with "user" itemname and with
76cfb124 120 * their original contextid as paremitemid
482aac65
EL
121 */
122class restore_load_included_users extends restore_execution_step {
123
124 protected function define_execution() {
125
126 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
127 return;
128 }
129 if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do
130 return;
131 }
132 $file = $this->get_basepath() . '/users.xml';
133 restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids
134 }
135}
136
137/**
138 * Execution step that, *conditionally* (if there isn't preloaded information
139 * and users have been selected in settings, will process all the needed users
140 * in order to decide and perform any action with them (create / map / error)
141 * Note: Any error will cause exception, as far as this is the same processing
142 * than the one into restore prechecks (that should have stopped process earlier)
143 */
76cfb124 144class restore_process_included_users extends restore_execution_step {
482aac65
EL
145
146 protected function define_execution() {
147
148 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
149 return;
150 }
151 if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do
152 return;
153 }
154 restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite());
155 }
156}
157
76cfb124
EL
158/**
159 * Execution step that will create all the needed users as calculated
160 * by @restore_process_included_users (those having newiteind = 0)
161 */
162class restore_create_included_users extends restore_execution_step {
163
164 protected function define_execution() {
165
166 restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files'));
167 }
168}
169
170/**
171 * Structure step that will create all the needed groups and groupings
172 * by loading them from the groups.xml file performing the required matches.
173 * Note group members only will be added if restoring user info
174 */
175class restore_groups_structure_step extends restore_structure_step {
176
177 protected function define_structure() {
178
179 $paths = array(); // Add paths here
180
181 $paths[] = new restore_path_element('group', '/groups/group');
182 if ($this->get_setting_value('users')) {
183 $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
184 }
185 $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
186 $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
187
188 return $paths;
189 }
190
191 // Processing functions go here
192 public function process_group($data) {
193 global $DB;
194
195 $data = (object)$data; // handy
196 $data->courseid = $this->get_courseid();
197
198 $oldid = $data->id; // need this saved for later
199
200 $restorefiles = false; // Only if we end creating the group
201
202 // Search if the group already exists (by name & description) in the target course
203 $description_clause = '';
204 $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
205 if (!empty($data->description)) {
206 $description_clause = ' AND ' .
207 $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc');
208 $params['desc'] = $data->description;
209 }
210 if (!$groupdb = $DB->get_record_sql("SELECT *
211 FROM {groups}
212 WHERE courseid = :courseid
213 AND name = :grname $description_clause", $params)) {
214 // group doesn't exist, create
215 $newitemid = $DB->insert_record('groups', $data);
216 $restorefiles = true; // We'll restore the files
217 } else {
218 // group exists, use it
219 $newitemid = $groupdb->id;
220 }
221 // Save the id mapping
222 $this->set_mapping('group', $oldid, $newitemid, $restorefiles);
223 }
224
225 public function process_member($data) {
226 global $DB;
227
228 $data = (object)$data; // handy
229
230 // get parent group->id
231 $data->groupid = $this->get_new_parentid('group');
232
233 // map user newitemid and insert if not member already
234 if ($data->userid = $this->get_mappingid('user', $data->userid)) {
235 if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
236 $DB->insert_record('groups_members', $data);
237 }
238 }
239 }
240
241 public function process_grouping($data) {
242 debugging('TODO: Grouping restore not implemented. Detected grouping', DEBUG_DEVELOPER);
243 }
244
245 public function process_grouping_group($data) {
246 debugging('TODO: Grouping restore not implemented. Detected grouping group', DEBUG_DEVELOPER);
247 }
248
249 protected function after_execute() {
b8bb45b0 250 // Add group related files, matching with "group" mappings
76cfb124
EL
251 $this->add_related_files('group', 'icon', 'group');
252 $this->add_related_files('group', 'description', 'group');
76cfb124
EL
253 }
254
255}
256
482aac65
EL
257/*
258 * Structure step that will read the course.xml file, loading it and performing
259 * various actions depending of the site/restore settings
260 */
261class restore_course_structure_step extends restore_structure_step {
262
263 protected function define_structure() {
264
265 $course = new restore_path_element('course', '/course', true); // Grouped
266 $category = new restore_path_element('category', '/course/category');
267 $tag = new restore_path_element('tag', '/course/tags/tag');
268 $allowed = new restore_path_element('allowed', '/course/allowed_modules/module');
269
270 return array($course, $category, $tag, $allowed);
271 }
272
273 // Processing functions go here
274 public function process_course($data) {
275 print_object('stopped before processing course. Continue here');
276 }
277
278}