From b4009391411dbb6bd805982df31b466f1dd10a97 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Sun, 18 Jul 2010 23:54:06 +0000 Subject: [PATCH] MDL-21432 backup - restore prechecks are now being executed. Pending role mapping one --- .../helper/restore_prechecks_helper.class.php | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 backup/util/helper/restore_prechecks_helper.class.php diff --git a/backup/util/helper/restore_prechecks_helper.class.php b/backup/util/helper/restore_prechecks_helper.class.php new file mode 100644 index 00000000000..87d5c7e61f2 --- /dev/null +++ b/backup/util/helper/restore_prechecks_helper.class.php @@ -0,0 +1,137 @@ +. + +/** + * @package moodlecore + * @subpackage backup-helper + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Non instantiable helper class providing support for restore prechecks + * + * This class contains various prechecks to be performed before executing + * the restore plan. Its entry point is execute_prechecks() that will + * call various stuff. At the end, it will return one array(), if empty + * all the prechecks have passed ok. If not empty, you'll find 1/2 elements + * in the array, warnings and errors, each one containing one description + * of the problem. Warnings aren't stoppers so the restore execution can + * continue after displaying them. In the other side, if errors are returned + * then restore execution cannot continue + * + * TODO: Finish phpdocs + */ +abstract class restore_prechecks_helper { + + /** + * Entry point for all the prechecks to be performed before restore + * + * Returns empty array or warnings/errors array + */ + public static function execute_prechecks($controller) { + global $CFG; + + $errors = array(); + $warnings = array(); + + // Create temp tables + restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid()); + + // Check we are restoring one backup >= $min20version (very first ok ever) + $min20version = 2010071800; + if ($controller->get_info()->backup_version < $min20version) { + $message = new stdclass(); + $message->backup = $controller->get_info()->backup_version; + $message->min = $min20version; + $errors[] = get_string('errorminbackup20version', 'backup', $message); + } + + // Compare Moodle's versions + if ($CFG->version < $controller->get_info()->moodle_version) { + $message = new stdclass(); + $message->serverversion = $CFG->version; + $message->serverrelease = $CFG->release; + $message->backupversion = $controller->get_info()->moodle_version; + $message->backuprelease = $controller->get_info()->moodle_release; + $warnings[] = get_string('noticenewerbackup','',$message); + } + + // Error if restoring over frontpage + // TODO: Review the whole restore process in order to transform this into one warning (see 1.9) + if ($controller->get_courseid() == SITEID) { + $errors[] = get_string('errorrestorefrontpage', 'backup'); + } + + // If restoring to different site and restoring users and backup has mnet users warn/error + $samesite = $controller->is_samesite(); + $restoreusers = $controller->get_plan()->get_setting('users')->get_value(); + $hasmnetusers = (int)$controller->get_info()->mnet_remoteusers; + if (!$samesite && $restoreusers && $hasmnetusers) { + // User is admin (can create users at sysctx), warn + if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM), $controller->get_userid())) { + $warnings[] = get_string('mnetrestore_extusers_admin', 'admin'); + // User not admin + } else { + $errors[] = get_string('mnetrestore_extusers_noadmin', 'admin'); + } + } + + // If restoring users, check we are able to create all them + if ($restoreusers) { + $file = $controller->get_plan()->get_basepath() . '/users.xml'; + $restoreid = $controller->get_restoreid(); + $courseid = $controller->get_courseid(); + $userid = $controller->get_userid(); + $inforeffiles = restore_dbops::get_needed_inforef_files($restoreid); // Get all inforef files + foreach ($inforeffiles as $inforeffile) { + restore_dbops::load_inforef_to_tempids($restoreid, $inforeffile); // Load each inforef file to temp_ids + } + restore_dbops::load_users_to_tempids($restoreid, $file); // Load needed users to temp_ids + if ($problems = restore_dbops::precheck_included_users($restoreid, $courseid, $userid, $samesite)) { + $errors = array_merge($errors, $problems); + } + } + + // TODO: Perform role_mappings, warning about non-mappable ones being ignored (see 1.9) + // (restore won't create roles in any case) + + // Prepare results and return + $results = array(); + if (!empty($errors)) { + $results['errors'] = $errors; + } + if (!empty($warnings)) { + $results['warnings'] = $warnings; + } + // Warnings/errors detected, drop temp tables + if (!empty($results)) { + restore_controller_dbops::drop_restore_temp_tables($controller->get_restoreid()); + } + return $results; + } +} + +/* + * Exception class used by all the @restore_prechecks_helper stuff + */ +class restore_prechecks_helper_exception extends backup_exception { + + public function __construct($errorcode, $a=NULL, $debuginfo=null) { + parent::__construct($errorcode, $a, $debuginfo); + } +} -- 2.43.0