MDL-21840 backup - table for files handling on restore. Big thanks to MySQL for being...
[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() {
482aac65
EL
35 $exists = restore_controller_dbops::create_backup_ids_temp_table($this->get_restoreid()); // Create temp table conditionally
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;
57 backup_controller_dbops::drop_backup_ids_temp_table($this->get_restoreid()); // Drop ids temp table
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
EL
86/*
87 * Execution step that will load all the needed files into backup_temp_ids.
88 * - itemname: contains "file*component*fileara"
89 * - itemid: contains the original id of the file
90 * - newitemid: contains the itemid of the file
91 * - parentitemid: contains the context of the file
92 * - info: contains the whole original object (times, names...)
93 * (all them being original ids as loaded from xml)
94 */
95class restore_load_included_files extends restore_structure_step {
96
97 protected function define_structure() {
98
99 $file = new restore_path_element('file', '/files/file');
100
101 return array($file);
102 }
103
104 // Processing functions go here
105 public function process_file($data) {
106
107 $data = (object)$data; // handy
108
109 $itemname = 'file*' . $data->component . '*' . $data->filearea;
110 $itemid = $data->id;
111 $newitemid = $data->itemid;
112 $parentitemid = $data->contextid;
113 $info = $data;
114
115 // load it if needed:
116 // - it it is one of the annotated inforef files (course/section/activity/block)
117 // - it is one "user", "group" or "grade" component file
118 $isfileref = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $itemid);
119 $iscomponent = ($data->component == 'user' || $data->component == 'group' || $data->component == 'grade');
120 if ($isfileref || $iscomponent) {
121 restore_dbops::set_backup_ids_record($this->get_restoreid(), $itemname, $itemid, $newitemid, $parentitemid, $info);
122 }
123 }
124}
125
482aac65
EL
126/**
127 * Execution step that, *conditionally* (if there isn't preloaded information
128 * and users have been selected in settings, will load all the needed users
129 * to backup_temp_ids. They will be stored with "user" itemname and with
76cfb124 130 * their original contextid as paremitemid
482aac65
EL
131 */
132class restore_load_included_users extends restore_execution_step {
133
134 protected function define_execution() {
135
136 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
137 return;
138 }
139 if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do
140 return;
141 }
142 $file = $this->get_basepath() . '/users.xml';
143 restore_dbops::load_users_to_tempids($this->get_restoreid(), $file); // Load needed users to temp_ids
144 }
145}
146
147/**
148 * Execution step that, *conditionally* (if there isn't preloaded information
149 * and users have been selected in settings, will process all the needed users
150 * in order to decide and perform any action with them (create / map / error)
151 * Note: Any error will cause exception, as far as this is the same processing
152 * than the one into restore prechecks (that should have stopped process earlier)
153 */
76cfb124 154class restore_process_included_users extends restore_execution_step {
482aac65
EL
155
156 protected function define_execution() {
157
158 if ($this->task->get_preloaded_information()) { // if info is already preloaded, nothing to do
159 return;
160 }
161 if (!$this->task->get_setting('users')) { // No userinfo being restored, nothing to do
162 return;
163 }
164 restore_dbops::process_included_users($this->get_restoreid(), $this->task->get_courseid(), $this->task->get_userid(), $this->task->is_samesite());
165 }
166}
167
76cfb124
EL
168/**
169 * Execution step that will create all the needed users as calculated
170 * by @restore_process_included_users (those having newiteind = 0)
171 */
172class restore_create_included_users extends restore_execution_step {
173
174 protected function define_execution() {
175
176 restore_dbops::create_included_users($this->get_basepath(), $this->get_restoreid(), $this->get_setting_value('user_files'));
177 }
178}
179
180/**
181 * Structure step that will create all the needed groups and groupings
182 * by loading them from the groups.xml file performing the required matches.
183 * Note group members only will be added if restoring user info
184 */
185class restore_groups_structure_step extends restore_structure_step {
186
187 protected function define_structure() {
188
189 $paths = array(); // Add paths here
190
191 $paths[] = new restore_path_element('group', '/groups/group');
192 if ($this->get_setting_value('users')) {
193 $paths[] = new restore_path_element('member', '/groups/group/group_members/group_member');
194 }
195 $paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
196 $paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
197
198 return $paths;
199 }
200
201 // Processing functions go here
202 public function process_group($data) {
203 global $DB;
204
205 $data = (object)$data; // handy
206 $data->courseid = $this->get_courseid();
207
208 $oldid = $data->id; // need this saved for later
209
210 $restorefiles = false; // Only if we end creating the group
211
212 // Search if the group already exists (by name & description) in the target course
213 $description_clause = '';
214 $params = array('courseid' => $this->get_courseid(), 'grname' => $data->name);
215 if (!empty($data->description)) {
216 $description_clause = ' AND ' .
217 $DB->sql_compare_text('description') . ' = ' . $DB->sql_compare_text(':desc');
218 $params['desc'] = $data->description;
219 }
220 if (!$groupdb = $DB->get_record_sql("SELECT *
221 FROM {groups}
222 WHERE courseid = :courseid
223 AND name = :grname $description_clause", $params)) {
224 // group doesn't exist, create
225 $newitemid = $DB->insert_record('groups', $data);
226 $restorefiles = true; // We'll restore the files
227 } else {
228 // group exists, use it
229 $newitemid = $groupdb->id;
230 }
231 // Save the id mapping
232 $this->set_mapping('group', $oldid, $newitemid, $restorefiles);
233 }
234
235 public function process_member($data) {
236 global $DB;
237
238 $data = (object)$data; // handy
239
240 // get parent group->id
241 $data->groupid = $this->get_new_parentid('group');
242
243 // map user newitemid and insert if not member already
244 if ($data->userid = $this->get_mappingid('user', $data->userid)) {
245 if (!$DB->record_exists('groups_members', array('groupid' => $data->groupid, 'userid' => $data->userid))) {
246 $DB->insert_record('groups_members', $data);
247 }
248 }
249 }
250
251 public function process_grouping($data) {
252 debugging('TODO: Grouping restore not implemented. Detected grouping', DEBUG_DEVELOPER);
253 }
254
255 public function process_grouping_group($data) {
256 debugging('TODO: Grouping restore not implemented. Detected grouping group', DEBUG_DEVELOPER);
257 }
258
259 protected function after_execute() {
260 return;
261 $this->add_related_files('group', 'icon', 'group');
262 $this->add_related_files('group', 'description', 'group');
263 restore_dbops::send_files_to_pool($basepath, $restoreid, 'user', 'private', $recuser->parentitemid);
264 }
265
266}
267
482aac65
EL
268/*
269 * Structure step that will read the course.xml file, loading it and performing
270 * various actions depending of the site/restore settings
271 */
272class restore_course_structure_step extends restore_structure_step {
273
274 protected function define_structure() {
275
276 $course = new restore_path_element('course', '/course', true); // Grouped
277 $category = new restore_path_element('category', '/course/category');
278 $tag = new restore_path_element('tag', '/course/tags/tag');
279 $allowed = new restore_path_element('allowed', '/course/allowed_modules/module');
280
281 return array($course, $category, $tag, $allowed);
282 }
283
284 // Processing functions go here
285 public function process_course($data) {
286 print_object('stopped before processing course. Continue here');
287 }
288
289}