Skip to content

Commit

Permalink
improve version list generation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benyovszky authored and tmuras committed Jan 14, 2025
1 parent b0c6623 commit d3c3ec9
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -552,4 +564,4 @@ function string_ends_with($haystack, $needle) {
return true;
}
return substr( $haystack, -$length ) === $needle;
}
}

0 comments on commit d3c3ec9

Please sign in to comment.