[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 */ 39 abstract 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 */ 46 public static function execute_prechecks(restore_controller $controller, $droptemptablesafter = false) { 47 global $CFG; 48 49 $errors = array(); 50 $warnings = array(); 51 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(); 59 $rolemappings = $controller->get_info()->role_mappings; 60 $progress = $controller->get_progress(); 61 62 // Start tracking progress. There are currently 8 major steps, corresponding 63 // to $majorstep++ lines in this code; we keep track of the total so as to 64 // verify that it's still correct. If you add a major step, you need to change 65 // the total here. 66 $majorstep = 1; 67 $majorsteps = 8; 68 $progress->start_progress('Carrying out pre-restore checks', $majorsteps); 69 70 // Load all the included tasks to look for inforef.xml files 71 $inforeffiles = array(); 72 $tasks = restore_dbops::get_included_tasks($restoreid); 73 $progress->start_progress('Listing inforef files', count($tasks)); 74 $minorstep = 1; 75 foreach ($tasks as $task) { 76 // Add the inforef.xml file if exists 77 $inforefpath = $task->get_taskbasepath() . '/inforef.xml'; 78 if (file_exists($inforefpath)) { 79 $inforeffiles[] = $inforefpath; 80 } 81 $progress->progress($minorstep++); 82 } 83 $progress->end_progress(); 84 $progress->progress($majorstep++); 85 86 // Create temp tables 87 restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid()); 88 89 // Check we are restoring one backup >= $min20version (very first ok ever) 90 $min20version = 2010072300; 91 if ($controller->get_info()->backup_version < $min20version) { 92 $message = new stdclass(); 93 $message->backup = $controller->get_info()->backup_version; 94 $message->min = $min20version; 95 $errors[] = get_string('errorminbackup20version', 'backup', $message); 96 } 97 98 // Compare Moodle's versions 99 if ($CFG->version < $controller->get_info()->moodle_version) { 100 $message = new stdclass(); 101 $message->serverversion = $CFG->version; 102 $message->serverrelease = $CFG->release; 103 $message->backupversion = $controller->get_info()->moodle_version; 104 $message->backuprelease = $controller->get_info()->moodle_release; 105 $warnings[] = get_string('noticenewerbackup','',$message); 106 } 107 108 // Error if restoring over frontpage 109 // TODO: Review the whole restore process in order to transform this into one warning (see 1.9) 110 if ($controller->get_courseid() == SITEID) { 111 $errors[] = get_string('errorrestorefrontpage', 'backup'); 112 } 113 114 // If restoring to different site and restoring users and backup has mnet users warn/error 115 if (!$samesite && $restoreusers && $hasmnetusers) { 116 // User is admin (can create users at sysctx), warn 117 if (has_capability('moodle/user:create', context_system::instance(), $controller->get_userid())) { 118 $warnings[] = get_string('mnetrestore_extusers_admin', 'admin'); 119 // User not admin 120 } else { 121 $errors[] = get_string('mnetrestore_extusers_noadmin', 'admin'); 122 } 123 } 124 125 // Load all the inforef files, we are going to need them 126 $progress->start_progress('Loading temporary IDs', count($inforeffiles)); 127 $minorstep = 1; 128 foreach ($inforeffiles as $inforeffile) { 129 // Load each inforef file to temp_ids. 130 restore_dbops::load_inforef_to_tempids($restoreid, $inforeffile, $progress); 131 $progress->progress($minorstep++); 132 } 133 $progress->end_progress(); 134 $progress->progress($majorstep++); 135 136 // If restoring users, check we are able to create all them 137 if ($restoreusers) { 138 $file = $controller->get_plan()->get_basepath() . '/users.xml'; 139 // Load needed users to temp_ids. 140 restore_dbops::load_users_to_tempids($restoreid, $file, $progress); 141 $progress->progress($majorstep++); 142 if ($problems = restore_dbops::precheck_included_users($restoreid, $courseid, $userid, $samesite, $progress)) { 143 $errors = array_merge($errors, $problems); 144 } 145 } else { 146 // To ensure consistent number of steps in progress tracking, 147 // mark progress even though we didn't do anything. 148 $progress->progress($majorstep++); 149 } 150 $progress->progress($majorstep++); 151 152 // Note: restore won't create roles at all. Only mapping/skip! 153 $file = $controller->get_plan()->get_basepath() . '/roles.xml'; 154 restore_dbops::load_roles_to_tempids($restoreid, $file); // Load needed roles to temp_ids 155 if ($problems = restore_dbops::precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings)) { 156 $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors; 157 $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings; 158 } 159 $progress->progress($majorstep++); 160 161 // Check we are able to restore and the categories and questions 162 $file = $controller->get_plan()->get_basepath() . '/questions.xml'; 163 restore_dbops::load_categories_and_questions_to_tempids($restoreid, $file); 164 if ($problems = restore_dbops::precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite)) { 165 $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors; 166 $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings; 167 } 168 $progress->progress($majorstep++); 169 170 // Prepare results. 171 $results = array(); 172 if (!empty($errors)) { 173 $results['errors'] = $errors; 174 } 175 if (!empty($warnings)) { 176 $results['warnings'] = $warnings; 177 } 178 // Warnings/errors detected or want to do so explicitly, drop temp tables 179 if (!empty($results) || $droptemptablesafter) { 180 restore_controller_dbops::drop_restore_temp_tables($controller->get_restoreid()); 181 } 182 183 // Finish progress and check we got the initial number of steps right. 184 $progress->progress($majorstep++); 185 if ($majorstep != $majorsteps) { 186 throw new coding_exception('Progress step count wrong: ' . $majorstep); 187 } 188 $progress->end_progress(); 189 190 return $results; 191 } 192 } 193 194 /* 195 * Exception class used by all the @restore_prechecks_helper stuff 196 */ 197 class restore_prechecks_helper_exception extends backup_exception { 198 199 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 200 parent::__construct($errorcode, $a, $debuginfo); 201 } 202 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |