mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
Bug #5352 - upgrade might be interrupted by user - upgrades are now logged into moddata/upgradelogs/, user abor is ignored and concurrent running of upgrades is prevented from one browser
This commit is contained in:
parent
0c23b59c9b
commit
583fad994c
7 changed files with 173 additions and 10 deletions
|
@ -29,6 +29,9 @@
|
|||
$agreelicence = optional_param('agreelicence',0, PARAM_BOOL);
|
||||
|
||||
|
||||
/// check upgrade status first
|
||||
upgrade_check_running("Upgrade already running, please wait!", 10);
|
||||
|
||||
/// Check some PHP server settings
|
||||
|
||||
$documentationlink = "please read the <a href=\"../doc/?frame=install.html&sub=webserver\">install documentation</a>";
|
||||
|
@ -115,6 +118,7 @@
|
|||
print_header($strdatabasesetup, $strdatabasesetup, $strdatabasesetup,
|
||||
"", $linktoscrolltoerrors, false, " ", " ");
|
||||
if (file_exists("$CFG->libdir/db/$CFG->dbtype.sql")) {
|
||||
upgrade_log_start();
|
||||
$db->debug = true;
|
||||
if (modify_database("$CFG->libdir/db/$CFG->dbtype.sql")) {
|
||||
$db->debug = false;
|
||||
|
@ -126,6 +130,7 @@
|
|||
} else {
|
||||
error("Error: Your database ($CFG->dbtype) is not yet fully supported by Moodle. See the lib/db directory.");
|
||||
}
|
||||
upgrade_log_finish();
|
||||
print_continue("index.php");
|
||||
die;
|
||||
}
|
||||
|
@ -156,6 +161,7 @@
|
|||
$strdatabasesuccess = get_string("databasesuccess");
|
||||
print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
|
||||
"", $linktoscrolltoerrors, false, " ", " ");
|
||||
upgrade_log_start();
|
||||
print_heading($strdatabasechecking);
|
||||
$db->debug=true;
|
||||
if (main_upgrade($CFG->version)) {
|
||||
|
@ -163,6 +169,7 @@
|
|||
if (set_config("version", $version)) {
|
||||
remove_dir($CFG->dataroot . '/cache', true); // flush cache
|
||||
notify($strdatabasesuccess, "green");
|
||||
upgrade_log_finish();
|
||||
print_continue("index.php");
|
||||
exit;
|
||||
} else {
|
||||
|
@ -172,6 +179,7 @@
|
|||
$db->debug=false;
|
||||
notify("Upgrade failed! See /version.php");
|
||||
}
|
||||
upgrade_log_finish();
|
||||
}
|
||||
} else if ($version < $CFG->version) {
|
||||
notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
|
||||
|
@ -183,9 +191,12 @@
|
|||
"", "", false, " ", " ");
|
||||
|
||||
if (!set_config("version", $version)) {
|
||||
upgrade_log_start();
|
||||
$db->debug=true;
|
||||
if (main_upgrade(0)) {
|
||||
upgrade_log_finish();
|
||||
print_continue("index.php");
|
||||
exit;
|
||||
} else {
|
||||
error("A problem occurred inserting current version into databases");
|
||||
}
|
||||
|
@ -200,7 +211,9 @@
|
|||
print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, " ", " ");
|
||||
print_heading("Moodle $release");
|
||||
if (!set_config("release", $release)) {
|
||||
upgrade_log_start();
|
||||
notify("ERROR: Could not update release version in database!!");
|
||||
upgrade_log_finish();
|
||||
}
|
||||
notice(get_string('releasenoteslink', 'admin', 'http://docs.moodle.org/en/Release_Notes'), 'index.php');
|
||||
exit;
|
||||
|
@ -252,6 +265,8 @@
|
|||
require_once("$CFG->dirroot/lib/locallib.php");
|
||||
upgrade_local_db("$CFG->wwwroot/$CFG->admin/index.php"); // Return here afterwards
|
||||
|
||||
/// just make sure upgrade logging is properly terminated
|
||||
upgrade_log_finish();
|
||||
|
||||
/// Set up the overall site name etc.
|
||||
if (! $site = get_site()) {
|
||||
|
|
|
@ -327,12 +327,14 @@
|
|||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, " ", " ");
|
||||
|
||||
upgrade_log_start();
|
||||
$db->debug=true;
|
||||
if (modify_database("$CFG->dirroot/backup/db/$CFG->dbtype.sql")) {
|
||||
$db->debug = false;
|
||||
if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
|
||||
notify(get_string("databasesuccess"), "green");
|
||||
notify(get_string("databaseupgradebackups", "", $backup_version));
|
||||
upgrade_log_finish();
|
||||
print_continue($continueto);
|
||||
exit;
|
||||
} else {
|
||||
|
@ -349,6 +351,7 @@
|
|||
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>');
|
||||
|
||||
upgrade_log_start();
|
||||
require_once ("$CFG->dirroot/backup/db/$CFG->dbtype.php");
|
||||
|
||||
$db->debug=true;
|
||||
|
@ -357,6 +360,7 @@
|
|||
if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
|
||||
notify(get_string("databasesuccess"), "green");
|
||||
notify(get_string("databaseupgradebackups", "", $backup_version));
|
||||
upgrade_log_finish();
|
||||
print_continue($continueto);
|
||||
exit;
|
||||
} else {
|
||||
|
@ -368,8 +372,10 @@
|
|||
}
|
||||
|
||||
} else if ($backup_version < $CFG->backup_version) {
|
||||
upgrade_log_start();
|
||||
notify("WARNING!!! The code you are using is OLDER than the version that made these databases!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
135
lib/adminlib.php
135
lib/adminlib.php
|
@ -26,6 +26,9 @@ function upgrade_plugins($type, $dir, $return) {
|
|||
error('No '.$type.' plugins installed!');
|
||||
}
|
||||
|
||||
$updated_plugins = false;
|
||||
$strpluginsetup = get_string('pluginsetup');
|
||||
|
||||
foreach ($plugs as $plug) {
|
||||
|
||||
$fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
|
||||
|
@ -54,7 +57,14 @@ function upgrade_plugins($type, $dir, $return) {
|
|||
$info->pluginversion = $plugin->version;
|
||||
$info->currentmoodle = $CFG->version;
|
||||
$info->requiremoodle = $plugin->requires;
|
||||
if (!$updated_plugins) {
|
||||
print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
notify(get_string('pluginrequirementsnotmet', 'error', $info));
|
||||
$updated_plugins = true;
|
||||
unset($info);
|
||||
continue;
|
||||
}
|
||||
|
@ -71,14 +81,13 @@ function upgrade_plugins($type, $dir, $return) {
|
|||
if ($CFG->$pluginversion == $plugin->version) {
|
||||
// do nothing
|
||||
} else if ($CFG->$pluginversion < $plugin->version) {
|
||||
if (empty($updated_plugins)) {
|
||||
$strpluginsetup = get_string('pluginsetup');
|
||||
if (!$updated_plugins) {
|
||||
print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
print_heading($plugin->name .' plugin needs upgrading');
|
||||
|
||||
if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
|
||||
if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
|
||||
$db->debug = true;
|
||||
|
@ -112,13 +121,15 @@ function upgrade_plugins($type, $dir, $return) {
|
|||
}
|
||||
echo '<hr />';
|
||||
$updated_plugins = true;
|
||||
|
||||
} else {
|
||||
upgrade_log_start();
|
||||
error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($updated_plugins)) {
|
||||
upgrade_log_finish();
|
||||
|
||||
if ($updated_plugins) {
|
||||
print_continue($return);
|
||||
die;
|
||||
}
|
||||
|
@ -140,6 +151,9 @@ function upgrade_activity_modules($return) {
|
|||
error('No modules installed!');
|
||||
}
|
||||
|
||||
$updated_modules = false;
|
||||
$strmodulesetup = get_string('modulesetup');
|
||||
|
||||
foreach ($mods as $mod) {
|
||||
|
||||
if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
|
||||
|
@ -174,7 +188,14 @@ function upgrade_activity_modules($return) {
|
|||
$info->moduleversion = $module->version;
|
||||
$info->currentmoodle = $CFG->version;
|
||||
$info->requiremoodle = $module->requires;
|
||||
if (!$updated_modules) {
|
||||
print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
notify(get_string('modulerequirementsnotmet', 'error', $info));
|
||||
$updated_modules = true;
|
||||
unset($info);
|
||||
continue;
|
||||
}
|
||||
|
@ -186,12 +207,12 @@ function upgrade_activity_modules($return) {
|
|||
if ($currmodule->version == $module->version) {
|
||||
// do nothing
|
||||
} else if ($currmodule->version < $module->version) {
|
||||
if (empty($updated_modules)) {
|
||||
$strmodulesetup = get_string('modulesetup');
|
||||
if (!$updated_modules) {
|
||||
print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
print_heading($module->name .' module needs upgrading');
|
||||
$upgrade_function = $module->name.'_upgrade';
|
||||
if (function_exists($upgrade_function)) {
|
||||
|
@ -213,16 +234,17 @@ function upgrade_activity_modules($return) {
|
|||
}
|
||||
$updated_modules = true;
|
||||
} else {
|
||||
upgrade_log_start();
|
||||
error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
|
||||
}
|
||||
|
||||
} else { // module not installed yet, so install it
|
||||
if (empty($updated_modules)) {
|
||||
$strmodulesetup = get_string('modulesetup');
|
||||
if (!$updated_modules) {
|
||||
print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
print_heading($module->name);
|
||||
$updated_modules = true;
|
||||
$db->debug = true;
|
||||
|
@ -266,7 +288,9 @@ function upgrade_activity_modules($return) {
|
|||
}
|
||||
}
|
||||
|
||||
if (!empty($updated_modules)) {
|
||||
upgrade_log_finish(); // finish logging if started
|
||||
|
||||
if ($updated_modules) {
|
||||
print_continue($return);
|
||||
print_footer();
|
||||
die;
|
||||
|
@ -355,4 +379,95 @@ function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext=''
|
|||
sleep($sleeptime);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
/// upgrade logging functions
|
||||
////////////////////////////////////////////////
|
||||
|
||||
$upgradeloghandle = false;
|
||||
global $upgradeloghandle; // needed for access from callback funtion
|
||||
|
||||
/**
|
||||
* Check if upgrade is already running.
|
||||
*
|
||||
* If anything goes wrong due to missing call to upgrade_log_finish()
|
||||
* just restart the browser.
|
||||
*
|
||||
* @param string warning message indicating upgrade is already running
|
||||
* @param int page reload timeout
|
||||
*/
|
||||
function upgrade_check_running($message, $timeout) {
|
||||
if (!empty($_SESSION['upgraderunning'])) {
|
||||
print_header();
|
||||
redirect(me(), $message, $timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start logging of output into file (if not disabled) and
|
||||
* prevent aborting and concurrent execution of upgrade script.
|
||||
*
|
||||
* Please note that you can not write into session variables after calling this function!
|
||||
*
|
||||
* This function may be called repeatedly.
|
||||
*/
|
||||
function upgrade_log_start() {
|
||||
global $upgradeloghandle;
|
||||
|
||||
if (!empty($_SESSION['upgraderunning'])) {
|
||||
return; // logging already started
|
||||
}
|
||||
|
||||
@ignore_user_abort(true); // ignore if user stops or otherwise aborts page loading
|
||||
$_SESSION['upgraderunning'] = 1; // set upgrade indicator
|
||||
session_write_close(); // from now on user can reload page - will be displayed warning
|
||||
make_upload_directory('upgradelogs');
|
||||
ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate logging of output, flush all data, allow script aborting
|
||||
* and reopen session for writing. Function error() does terminate the logging too.
|
||||
*
|
||||
* Please make sure that each upgrade_log_start() is properly terminated by
|
||||
* this function or error().
|
||||
*
|
||||
* This function may be called repeatedly.
|
||||
*/
|
||||
function upgrade_log_finish() {
|
||||
global $upgradeloghandle;
|
||||
|
||||
if (empty($_SESSION['upgraderunning'])) {
|
||||
return; // logging already terminated
|
||||
}
|
||||
|
||||
@ob_end_flush();
|
||||
@fclose($upgradeloghandle);
|
||||
@session_start(); // ignore header errors, we only need to reopen session
|
||||
$_SESSION['upgraderunning'] = 0; // clear upgrade indicator
|
||||
if (connection_aborted()) {
|
||||
die;
|
||||
}
|
||||
@ignore_user_abort(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for logging into files. Not more than one file is created per minute,
|
||||
* upgrade session (terminated by upgrade_log_finish()) is always stored in one file.
|
||||
*
|
||||
* This function must not output any characters or throw warnigns and errors!
|
||||
*/
|
||||
function upgrade_log_callback($string) {
|
||||
global $CFG, $upgradeloghandle;
|
||||
|
||||
if (empty($CFG->disableupgradelogging) and ($string != '') and ($upgradeloghandle !== 'error')) {
|
||||
if ($upgradeloghandle or ($upgradeloghandle = @fopen($CFG->dataroot.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) {
|
||||
@fwrite($upgradeloghandle, $string);
|
||||
} else {
|
||||
$upgradeloghandle = 'error';
|
||||
}
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -912,12 +912,14 @@ function upgrade_blocks_db($continueto) {
|
|||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
|
||||
upgrade_log_start();
|
||||
$db->debug=true;
|
||||
if (modify_database($CFG->dirroot .'/blocks/db/'. $CFG->dbtype .'.sql')) {
|
||||
$db->debug = false;
|
||||
if (set_config('blocks_version', $blocks_version)) {
|
||||
notify(get_string('databasesuccess'), 'notifysuccess');
|
||||
notify(get_string('databaseupgradeblocks', '', $blocks_version));
|
||||
upgrade_log_finish();
|
||||
print_continue($continueto);
|
||||
exit;
|
||||
} else {
|
||||
|
@ -933,6 +935,8 @@ function upgrade_blocks_db($continueto) {
|
|||
$strdatabaseupgrades = get_string('databaseupgrades');
|
||||
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>');
|
||||
|
||||
upgrade_log_start();
|
||||
require_once ($CFG->dirroot .'/blocks/db/'. $CFG->dbtype .'.php');
|
||||
|
||||
$db->debug=true;
|
||||
|
@ -941,6 +945,7 @@ function upgrade_blocks_db($continueto) {
|
|||
if (set_config('blocks_version', $blocks_version)) {
|
||||
notify(get_string('databasesuccess'), 'notifysuccess');
|
||||
notify(get_string('databaseupgradeblocks', '', $blocks_version));
|
||||
upgrade_log_finish();
|
||||
print_continue($continueto);
|
||||
exit;
|
||||
} else {
|
||||
|
@ -952,8 +957,10 @@ function upgrade_blocks_db($continueto) {
|
|||
}
|
||||
|
||||
} else if ($blocks_version < $CFG->blocks_version) {
|
||||
upgrade_log_start();
|
||||
notify('WARNING!!! The Blocks version you are using is OLDER than the version that made these databases!');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//This function finds all available blocks and install them
|
||||
|
@ -1069,6 +1076,7 @@ function upgrade_blocks_plugins($continueto) {
|
|||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
print_heading('New version of '.$blocktitle.' ('.$block->name.') exists');
|
||||
$upgrade_function = $block->name.'_upgrade';
|
||||
if (function_exists($upgrade_function)) {
|
||||
|
@ -1098,6 +1106,7 @@ function upgrade_blocks_plugins($continueto) {
|
|||
}
|
||||
$updated_blocks = true;
|
||||
} else {
|
||||
upgrade_log_start();
|
||||
error('Version mismatch: block '. $block->name .' can\'t downgrade '. $currblock->version .' -> '. $block->version .'!');
|
||||
}
|
||||
|
||||
|
@ -1123,6 +1132,7 @@ function upgrade_blocks_plugins($continueto) {
|
|||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
|
||||
false, ' ', ' ');
|
||||
}
|
||||
upgrade_log_start();
|
||||
print_heading($block->name);
|
||||
$updated_blocks = true;
|
||||
$db->debug = true;
|
||||
|
@ -1145,6 +1155,7 @@ function upgrade_blocks_plugins($continueto) {
|
|||
}
|
||||
|
||||
if(!empty($notices)) {
|
||||
upgrade_log_start();
|
||||
foreach($notices as $notice) {
|
||||
notify($notice);
|
||||
}
|
||||
|
@ -1153,6 +1164,7 @@ function upgrade_blocks_plugins($continueto) {
|
|||
// Finally, if we are in the first_install of BLOCKS (this means that we are
|
||||
// upgrading from Moodle < 1.3), put blocks in all existing courses.
|
||||
if ($first_install) {
|
||||
upgrade_log_start();
|
||||
//Iterate over each course
|
||||
if ($courses = get_records('course')) {
|
||||
foreach ($courses as $course) {
|
||||
|
@ -1163,11 +1175,14 @@ function upgrade_blocks_plugins($continueto) {
|
|||
}
|
||||
|
||||
if (!empty($CFG->siteblocksadded)) { /// This is a once-off hack to make a proper upgrade
|
||||
upgrade_log_start();
|
||||
$page = page_create_object(PAGE_COURSE_VIEW, SITEID);
|
||||
blocks_repopulate_page($page);
|
||||
delete_records('config', 'name', 'siteblocksadded');
|
||||
}
|
||||
|
||||
upgrade_log_finish();
|
||||
|
||||
if (!empty($updated_blocks)) {
|
||||
print_continue($continueto);
|
||||
die;
|
||||
|
|
|
@ -93,6 +93,7 @@ function upgrade_local_db($continueto) {
|
|||
print_header($strdatabaseupgrades, $strdatabaseupgrades, $strdatabaseupgrades, '',
|
||||
'<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>');
|
||||
|
||||
upgrade_log_start();
|
||||
require_once ($CFG->dirroot .'/local/db/'. $CFG->dbtype .'.php');
|
||||
|
||||
$db->debug=true;
|
||||
|
@ -101,6 +102,7 @@ function upgrade_local_db($continueto) {
|
|||
if (set_config('local_version', $local_version)) {
|
||||
notify(get_string('databasesuccess'), 'notifysuccess');
|
||||
notify(get_string('databaseupgradelocal', '', $local_version));
|
||||
upgrade_log_finish();
|
||||
print_continue($continueto);
|
||||
exit;
|
||||
} else {
|
||||
|
@ -112,8 +114,10 @@ function upgrade_local_db($continueto) {
|
|||
}
|
||||
|
||||
} else if ($local_version < $CFG->local_version) {
|
||||
upgrade_log_start();
|
||||
notify('WARNING!!! The local version you are using is OLDER than the version that made these databases!');
|
||||
}
|
||||
upgrade_log_finish();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -4089,6 +4089,12 @@ function error ($message, $link='') {
|
|||
for ($i=0;$i<512;$i++) { // Padding to help IE work with 404
|
||||
echo ' ';
|
||||
}
|
||||
|
||||
// clean upgrade indicator if needed; buffer and log file will be closed automatically
|
||||
if (!empty($_SESSION['upgraderunning'])) {
|
||||
$_SESSION['upgraderunning'] = 0;
|
||||
}
|
||||
|
||||
die;
|
||||
}
|
||||
|
||||
|
|
|
@ -2263,6 +2263,7 @@ function assignment_upgrade_submodules() {
|
|||
continue;
|
||||
}
|
||||
|
||||
upgrade_log_start();
|
||||
$db->debug=true;
|
||||
if (!modify_database($fullpath .'/db/'.$CFG->dbtype.'.sql')) {
|
||||
notify("Error installing tables for submodule '$type'!");
|
||||
|
@ -2289,6 +2290,7 @@ function assignment_upgrade_submodules() {
|
|||
|
||||
$upgrade_function = 'assignment_'.$type.'_upgrade';
|
||||
if (function_exists($upgrade_function)) {
|
||||
upgrade_log_start();
|
||||
$db->debug=true;
|
||||
if ($upgrade_function($CFG->$currentversion)) {
|
||||
$db->debug=false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue