NOBUG: Added support for after_restore() methods on restore. Will help some blocks.
[moodle.git] / backup / util / helper / restore_prechecks_helper.class.php
CommitLineData
b4009391
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-helper
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 * Non instantiable helper class providing support for restore prechecks
27 *
28 * This class contains various prechecks to be performed before executing
29 * the restore plan. Its entry point is execute_prechecks() that will
30 * call various stuff. At the end, it will return one array(), if empty
31 * all the prechecks have passed ok. If not empty, you'll find 1/2 elements
32 * in the array, warnings and errors, each one containing one description
33 * of the problem. Warnings aren't stoppers so the restore execution can
34 * continue after displaying them. In the other side, if errors are returned
35 * then restore execution cannot continue
36 *
37 * TODO: Finish phpdocs
38 */
39abstract class restore_prechecks_helper {
40
41 /**
42 * Entry point for all the prechecks to be performed before restore
43 *
44 * Returns empty array or warnings/errors array
45 */
8d4e41f4 46 public static function execute_prechecks($controller, $droptemptablesafter = false) {
b4009391
EL
47 global $CFG;
48
49 $errors = array();
50 $warnings = array();
51
71a50b13
EL
52 // Some handy vars to be used along the prechecks
53 $samesite = $controller->is_samesite();
54 $restoreusers = $controller->get_plan()->get_setting('users')->get_value();
55 $hasmnetusers = (int)$controller->get_info()->mnet_remoteusers;
56 $restoreid = $controller->get_restoreid();
57 $courseid = $controller->get_courseid();
58 $userid = $controller->get_userid();
8d4e41f4 59 $rolemappings = $controller->get_info()->role_mappings;
648a575e
EL
60 // Load all the included tasks to look for inforef.xml files
61 $inforeffiles = array();
62 $tasks = restore_dbops::get_included_tasks($restoreid);
63 foreach ($tasks as $task) {
64 // Add the inforef.xml file if exists
65 $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
66 if (file_exists($inforefpath)) {
67 $inforeffiles[] = $inforefpath;
68 }
69 }
71a50b13 70
b4009391
EL
71 // Create temp tables
72 restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid());
73
74 // Check we are restoring one backup >= $min20version (very first ok ever)
2d7cd798 75 $min20version = 2010072300;
b4009391
EL
76 if ($controller->get_info()->backup_version < $min20version) {
77 $message = new stdclass();
78 $message->backup = $controller->get_info()->backup_version;
79 $message->min = $min20version;
80 $errors[] = get_string('errorminbackup20version', 'backup', $message);
81 }
82
83 // Compare Moodle's versions
84 if ($CFG->version < $controller->get_info()->moodle_version) {
85 $message = new stdclass();
86 $message->serverversion = $CFG->version;
87 $message->serverrelease = $CFG->release;
88 $message->backupversion = $controller->get_info()->moodle_version;
89 $message->backuprelease = $controller->get_info()->moodle_release;
90 $warnings[] = get_string('noticenewerbackup','',$message);
91 }
92
93 // Error if restoring over frontpage
94 // TODO: Review the whole restore process in order to transform this into one warning (see 1.9)
95 if ($controller->get_courseid() == SITEID) {
96 $errors[] = get_string('errorrestorefrontpage', 'backup');
97 }
98
99 // If restoring to different site and restoring users and backup has mnet users warn/error
b4009391
EL
100 if (!$samesite && $restoreusers && $hasmnetusers) {
101 // User is admin (can create users at sysctx), warn
102 if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM), $controller->get_userid())) {
103 $warnings[] = get_string('mnetrestore_extusers_admin', 'admin');
104 // User not admin
105 } else {
106 $errors[] = get_string('mnetrestore_extusers_noadmin', 'admin');
107 }
108 }
109
71a50b13
EL
110 // Load all the inforef files, we are going to need them
111 foreach ($inforeffiles as $inforeffile) {
112 restore_dbops::load_inforef_to_tempids($restoreid, $inforeffile); // Load each inforef file to temp_ids
113 }
114
b4009391
EL
115 // If restoring users, check we are able to create all them
116 if ($restoreusers) {
117 $file = $controller->get_plan()->get_basepath() . '/users.xml';
b4009391
EL
118 restore_dbops::load_users_to_tempids($restoreid, $file); // Load needed users to temp_ids
119 if ($problems = restore_dbops::precheck_included_users($restoreid, $courseid, $userid, $samesite)) {
120 $errors = array_merge($errors, $problems);
121 }
122 }
123
71a50b13
EL
124 // Note: restore won't create roles at all. Only mapping/skip!
125 $file = $controller->get_plan()->get_basepath() . '/roles.xml';
8d4e41f4
EL
126 restore_dbops::load_roles_to_tempids($restoreid, $file); // Load needed roles to temp_ids
127 if ($problems = restore_dbops::precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings)) {
71a50b13 128 $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors;
41941110
EL
129 $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings;
130 }
131
132 // Check we are able to restore and the categories and questions
133 $file = $controller->get_plan()->get_basepath() . '/questions.xml';
134 restore_dbops::load_categories_and_questions_to_tempids($restoreid, $file);
135 if ($problems = restore_dbops::precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite)) {
136 $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors;
71a50b13
EL
137 $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings;
138 }
b4009391
EL
139
140 // Prepare results and return
141 $results = array();
142 if (!empty($errors)) {
143 $results['errors'] = $errors;
144 }
145 if (!empty($warnings)) {
146 $results['warnings'] = $warnings;
147 }
8d4e41f4
EL
148 // Warnings/errors detected or want to do so explicitly, drop temp tables
149 if (!empty($results) || $droptemptablesafter) {
b4009391
EL
150 restore_controller_dbops::drop_restore_temp_tables($controller->get_restoreid());
151 }
152 return $results;
153 }
154}
155
156/*
157 * Exception class used by all the @restore_prechecks_helper stuff
158 */
159class restore_prechecks_helper_exception extends backup_exception {
160
161 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
162 parent::__construct($errorcode, $a, $debuginfo);
163 }
164}