]> git.mjollnir.org Git - moodle.git/commitdiff
Block core blocks tables can use the XMLDB install & upgrade
authorstronk7 <stronk7>
Wed, 30 Aug 2006 23:18:29 +0000 (23:18 +0000)
committerstronk7 <stronk7>
Wed, 30 Aug 2006 23:18:29 +0000 (23:18 +0000)
lib/blocklib.php

index 8d7b33ca30ada28333ad08f095ce32359e9ac3aa..d7bc21765f308473f77cb09c8451811cb31ff949 100644 (file)
@@ -917,9 +917,20 @@ function upgrade_blocks_db($continueto) {
                 false, '&nbsp;', '&nbsp;');
 
         upgrade_log_start();
+        print_heading('blocks');
         $db->debug=true;
-        if (modify_database($CFG->dirroot .'/blocks/db/'. $CFG->dbtype .'.sql')) {
-            $db->debug = false;
+
+    /// Both old .sql files and new install.xml are supported
+    /// but we priorize install.xml (XMLDB) if present
+        $status = false;
+        if (file_exists($CFG->dirroot . '/blocks/db/install.xml') && $CFG->xmldb_enabled) {
+            $status = install_from_xmldb_file($CFG->dirroot . '/blocks/db/install.xml'); //New method
+        } else if (file_exists($CFG->dirroot . '/blocks/db/' . $CFG->dbtype . '.sql')) {
+            $status = modify_database($CFG->dirroot . '/blocks/db/' . $CFG->dbtype . '.sql'); //Old method
+        }
+
+        $db->debug = false;
+        if ($status) {
             if (set_config('blocks_version', $blocks_version)) {
                 notify(get_string('databasesuccess'), 'notifysuccess');
                 notify(get_string('databaseupgradeblocks', '', $blocks_version), 'notifysuccess');
@@ -933,6 +944,17 @@ function upgrade_blocks_db($continueto) {
         }
     }
 
+/// Upgrading code starts here
+    $oldupgrade = false;
+    $newupgrade = false;
+    if (is_readable($CFG->dirroot . '/blocks/db/' . $CFG->dbtype . '.php')) {
+        include_once($CFG->dirroot . '/blocks/db/' . $CFG->dbtype . '.php');  // defines old upgrading function
+        $oldupgrade = true;
+    }
+    if (is_readable($CFG->dirroot . '/blocks/db/upgrade.php')  && $CFG->xmldb_enabled) {
+        include_once($CFG->dirroot . '/blocks/db/upgrade.php');  // defines new upgrading function
+        $newupgrade = true;
+    }
 
     if ($blocks_version > $CFG->blocks_version) {       // Upgrade tables
         $strdatabaseupgrades = get_string('databaseupgrades');
@@ -940,11 +962,35 @@ function upgrade_blocks_db($continueto) {
                 '<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');
+        print_heading('blocks');
 
-        $db->debug=true;
-        if (blocks_upgrade($CFG->blocks_version)) {
-            $db->debug=false;
+    /// Run de old and new upgrade functions for the module
+        $oldupgrade_function = 'blocks_upgrade';
+        $newupgrade_function = 'xmldb_blocks_upgrade';
+
+    /// First, the old function if exists
+        $oldupgrade_status = true;
+        if ($oldupgrade && function_exists($oldupgrade_function)) {
+            $db->debug = true;
+            $oldupgrade_status = $oldupgrade_function($CFG->blocks_version);
+        } else if ($oldupgrade) {
+            notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
+                     '/blocks/db/' . $CFG->dbtype . '.php');
+        }
+
+    /// Then, the new function if exists and the old one was ok
+        $newupgrade_status = true;
+        if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
+            $db->debug = true;
+            $newupgrade_status = $newupgrade_function($CFG->blocks_version);
+        } else if ($newupgrade) {
+            notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
+                     '/blocks/db/upgrade.php');
+        }
+
+        $db->debug=false;
+    /// Now analyze upgrade results
+        if ($oldupgrade_status && $newupgrade_status) {    // No upgrading failed
             if (set_config('blocks_version', $blocks_version)) {
                 notify(get_string('databasesuccess'), 'notifysuccess');
                 notify(get_string('databaseupgradeblocks', '', $blocks_version), 'notifysuccess');
@@ -954,16 +1000,14 @@ function upgrade_blocks_db($continueto) {
                 error('Upgrade of blocks system failed! (Could not update version in config table)');
             }
         } else {
-            $db->debug=false;
             error('Upgrade failed!  See blocks/version.php');
         }
 
     } 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!');
-        upgrade_log_finish();
     }
-
+    upgrade_log_finish();
 }
 
 //This function finds all available blocks and install them