From b7100ae5b14dcb04bf09c4b07437bc4db0a85f03 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Tue, 18 Aug 2020 12:22:59 +0200 Subject: [PATCH] MDL-69475 backup: Proper handling of backup::RELEASE versions 1) Remove any floatval() casting. They are breaking / killing .10 versions (converting them to .1). Since Moodle 2.0 all the backup::RELEASE have been 100% numerical values. 2) Use version_compare() always to compare backup::RELEASE values. They are always versions and the function is aware of versions > .9, able to clean/ignore alpha chars... and everything else. Note that I've also changed a couple of cases in formats (topics and weeks) that were correct, but just added the same comment and used the same version_compare() comparison parameters style, so all uses in core are consistent (and safe to be copied out there). --- backup/moodle2/restore_stepslib.php | 17 ++++++++--------- backup/util/dbops/restore_dbops.class.php | 4 ++-- .../restore_format_topics_plugin.class.php | 4 ++-- .../restore_format_weeks_plugin.class.php | 4 ++-- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 223b8491bd3..baf3b044853 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -122,7 +122,7 @@ class restore_gradebook_structure_step extends restore_structure_step { } // Identify the backup we're dealing with. - $backuprelease = floatval($this->get_task()->get_info()->backup_release); // The major version: 2.9, 3.0, ... + $backuprelease = $this->get_task()->get_info()->backup_release; // The major version: 2.9, 3.0, 3.10... $backupbuild = 0; preg_match('/(\d{8})/', $this->get_task()->get_info()->moodle_release, $matches); if (!empty($matches[1])) { @@ -132,7 +132,7 @@ class restore_gradebook_structure_step extends restore_structure_step { // On older versions the freeze value has to be converted. // We do this from here as it is happening right before the file is read. // This only targets the backup files that can contain the legacy freeze. - if ($backupbuild > 20150618 && ($backuprelease < 3.0 || $backupbuild < 20160527)) { + if ($backupbuild > 20150618 && (version_compare($backuprelease, '3.0', '<') || $backupbuild < 20160527)) { $this->rewrite_step_backup_file_for_legacy_freeze($fullpath); } @@ -505,8 +505,7 @@ class restore_gradebook_structure_step extends restore_structure_step { $gradebookcalculationsfreeze = get_config('core', 'gradebook_calculations_freeze_' . $this->get_courseid()); preg_match('/(\d{8})/', $this->get_task()->get_info()->moodle_release, $matches); $backupbuild = (int)$matches[1]; - // The function floatval will return a float even if there is text mixed with the release number. - $backuprelease = floatval($this->get_task()->get_info()->backup_release); + $backuprelease = $this->get_task()->get_info()->backup_release; // The major version: 2.9, 3.0, 3.10... // Extra credits need adjustments only for backups made between 2.8 release (20141110) and the fix release (20150619). if (!$gradebookcalculationsfreeze && $backupbuild >= 20141110 && $backupbuild < 20150619) { @@ -521,7 +520,7 @@ class restore_gradebook_structure_step extends restore_structure_step { // Courses from before 3.1 (20160518) may have a letter boundary problem and should be checked for this issue. // Backups from before and including 2.9 could have a build number that is greater than 20160518 and should // be checked for this problem. - if (!$gradebookcalculationsfreeze && ($backupbuild < 20160518 || $backuprelease <= 2.9)) { + if (!$gradebookcalculationsfreeze && ($backupbuild < 20160518 || version_compare($backuprelease, '2.9', '<='))) { require_once($CFG->libdir . '/db/upgradelib.php'); upgrade_course_letter_boundary($this->get_courseid()); } @@ -4631,11 +4630,11 @@ class restore_create_categories_and_questions extends restore_structure_step { // Before 3.5, question categories could be created at top level. // From 3.5 onwards, all question categories should be a child of a special category called the "top" category. - $backuprelease = floatval($this->get_task()->get_info()->backup_release); + $backuprelease = $this->get_task()->get_info()->backup_release; // The major version: 2.9, 3.0, 3.10... preg_match('/(\d{8})/', $this->get_task()->get_info()->moodle_release, $matches); $backupbuild = (int)$matches[1]; $before35 = false; - if ($backuprelease < 3.5 || $backupbuild < 20180205) { + if (version_compare($backuprelease, '3.5', '<') || $backupbuild < 20180205) { $before35 = true; } if (empty($mapping->info->parent) && $before35) { @@ -4892,11 +4891,11 @@ class restore_move_module_questions_categories extends restore_execution_step { protected function define_execution() { global $DB; - $backuprelease = floatval($this->task->get_info()->backup_release); + $backuprelease = $this->task->get_info()->backup_release; // The major version: 2.9, 3.0, 3.10... preg_match('/(\d{8})/', $this->task->get_info()->moodle_release, $matches); $backupbuild = (int)$matches[1]; $after35 = false; - if ($backuprelease >= 3.5 && $backupbuild > 20180205) { + if (version_compare($backuprelease, '3.5', '>=') && $backupbuild > 20180205) { $after35 = true; } diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index eef73fc1a72..a5ca4515141 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -581,11 +581,11 @@ abstract class restore_dbops { $rc = restore_controller_dbops::load_controller($restoreid); $restoreinfo = $rc->get_info(); $rc->destroy(); // Always need to destroy. - $backuprelease = floatval($restoreinfo->backup_release); + $backuprelease = $restoreinfo->backup_release; // The major version: 2.9, 3.0, 3.10... preg_match('/(\d{8})/', $restoreinfo->moodle_release, $matches); $backupbuild = (int)$matches[1]; $after35 = false; - if ($backuprelease >= 3.5 && $backupbuild > 20180205) { + if (version_compare($backuprelease, '3.5', '>=') && $backupbuild > 20180205) { $after35 = true; } diff --git a/course/format/topics/backup/moodle2/restore_format_topics_plugin.class.php b/course/format/topics/backup/moodle2/restore_format_topics_plugin.class.php index 076b99fb690..4fbe668e03c 100644 --- a/course/format/topics/backup/moodle2/restore_format_topics_plugin.class.php +++ b/course/format/topics/backup/moodle2/restore_format_topics_plugin.class.php @@ -48,8 +48,8 @@ class restore_format_topics_plugin extends restore_format_plugin { */ protected function need_restore_numsections() { $backupinfo = $this->step->get_task()->get_info(); - $backuprelease = $backupinfo->backup_release; - return version_compare($backuprelease, '3.3', 'lt'); + $backuprelease = $backupinfo->backup_release; // The major version: 2.9, 3.0, 3.10... + return version_compare($backuprelease, '3.3', '<'); } /** diff --git a/course/format/weeks/backup/moodle2/restore_format_weeks_plugin.class.php b/course/format/weeks/backup/moodle2/restore_format_weeks_plugin.class.php index e0ec9c80187..64e3b33b5e8 100644 --- a/course/format/weeks/backup/moodle2/restore_format_weeks_plugin.class.php +++ b/course/format/weeks/backup/moodle2/restore_format_weeks_plugin.class.php @@ -48,8 +48,8 @@ class restore_format_weeks_plugin extends restore_format_plugin { */ protected function is_pre_33_backup() { $backupinfo = $this->step->get_task()->get_info(); - $backuprelease = $backupinfo->backup_release; - return version_compare($backuprelease, '3.3', 'lt'); + $backuprelease = $backupinfo->backup_release; // The major version: 2.9, 3.0, 3.10... + return version_compare($backuprelease, '3.3', '<'); } /**