]> git.mjollnir.org Git - moodle.git/commitdiff
Bug #5352 - upgrade might be interrupted by user - upgrades are now logged into modda...
authorskodak <skodak>
Tue, 1 Aug 2006 07:46:19 +0000 (07:46 +0000)
committerskodak <skodak>
Tue, 1 Aug 2006 07:46:19 +0000 (07:46 +0000)
admin/index.php
backup/lib.php
lib/adminlib.php
lib/blocklib.php
lib/locallib.php
lib/weblib.php
mod/assignment/lib.php

index ad662797a6bc5a6f8e346cad88609e96bed93a71..899d99925d80552b269dceff073e724331ced549 100644 (file)
@@ -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&amp;sub=webserver\">install documentation</a>";
         print_header($strdatabasesetup, $strdatabasesetup, $strdatabasesetup,
                         "", $linktoscrolltoerrors, false, "&nbsp;", "&nbsp;");
         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;
         } 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;
     }
                 $strdatabasesuccess  = get_string("databasesuccess");
                 print_header($strdatabasechecking, $stradministration, $strdatabasechecking,
                         "", $linktoscrolltoerrors, false, "&nbsp;", "&nbsp;");
+                upgrade_log_start();
                 print_heading($strdatabasechecking);
                 $db->debug=true;
                 if (main_upgrade($CFG->version)) {
                     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 {
                     $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!");
                      "", "", false, "&nbsp;", "&nbsp;");
 
         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");
             }
         print_header($strcurrentrelease, $strcurrentrelease, $strcurrentrelease, "", "", false, "&nbsp;", "&nbsp;");
         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;
     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()) {
index 465719130b75fdc1cec82e43bd52cbd39ace6797..3ca0d7b0690c505ee9e1790c3cb31ce02c9cd173 100644 (file)
                     '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
                     false, "&nbsp;", "&nbsp;");
 
+            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 {
             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;
                 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 {
             }
 
         } 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!");
         }
+
     }
 
  
index a7f814498d7dd62c7075776cf531346416e7fe4e..db991bc7a200b024d16fd1874b1fdf9a8e66982a 100644 (file)
@@ -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, '&nbsp;', '&nbsp;');
+                }
+                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, '&nbsp;', '&nbsp;');
             }
+            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, '&nbsp;', '&nbsp;');
+                }
+                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, '&nbsp;', '&nbsp;');
                 }
+                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, '&nbsp;', '&nbsp;');
             }
+            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;
+}
+
 ?>
index c73a53b83f3825c76ba3430c35ce3c402365374d..4b546223b1d7676c4a7f4d46c72354cd26f3ab0c 100644 (file)
@@ -912,12 +912,14 @@ function upgrade_blocks_db($continueto) {
                 '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
                 false, '&nbsp;', '&nbsp;');
 
+        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, '&nbsp;', '&nbsp;');
                 }
+                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, '&nbsp;', '&nbsp;');
             }
+            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;
index 01a2db0ea10c5fd66e42a3333b349a62d147d723..e8b5515d47108205f172316117e67b4722db8054 100644 (file)
@@ -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();
 }
 
 /**
index 5b2c3ae16f6e85fc037a6ccacb6cbcb1f5100ce8..0d4fb1bca5836013b63cac2700acccbea5771ec8 100644 (file)
@@ -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;
 }
 
index a510e7fbe0871cb14586507237009a1ee840c625..227467c6411b71ea4feb59de7409b96874e99d5f 100644 (file)
@@ -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;