From d3c3ec993bd9b1b4bad9c250481b92fce705901b Mon Sep 17 00:00:00 2001 From: benyovszky Date: Mon, 13 Jan 2025 07:40:15 +0100 Subject: [PATCH] improve version list generation Improve version list generation by the following: - Handles correctly 405 for the 4.5 version and 311 for 3.11 at the same time. - Reduces generation of non-existing versions, which speeds up version-dependent command search and memory footprint. - Function itself runs faster, even though it uses nested loops. --- includes/functions.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index e5bdf3fe..5b931935 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -150,14 +150,26 @@ function moosh_moodle_version($topdir, $default = 23) { } function moosh_generate_version_list($upto, $from = 19) { + // This function assumes that moodle main version is below 10 and subversions are below or equal to 20. + $upto = intval($upto); $from = intval($from); if (!($from && $upto) || $from > $upto) { throw new Exception("Invalid from or upto value; they must both be > 0 and from must be <= upto"); } + + $frommain = (int)(substr($from, 0, 1)); + $fromsub = (int)(substr($from, 1)); + $uptomain = (int)(substr($upto, 0, 1)); + $uptosub = (int)(substr($upto, 1)); + $versions = array(); - foreach (range($from, $upto) as $no) { - $versions[] = 'Moodle' . $no; + foreach (range($frommain, $uptomain) as $nom) { + $frsub = ($nom == $frommain) ? $fromsub : 0; + $tosub = ($nom == $uptomain) ? $uptosub : 20; + foreach(range($frsub, $tosub) as $nos) { + $versions[] = 'Moodle' . $nom . $nos; + } } return $versions; } @@ -552,4 +564,4 @@ function string_ends_with($haystack, $needle) { return true; } return substr( $haystack, -$length ) === $needle; -} \ No newline at end of file +}