]> git.mjollnir.org Git - moodle.git/commitdiff
Added the install_from_xmldb_file() function that
authorstronk7 <stronk7>
Sun, 20 Aug 2006 15:50:27 +0000 (15:50 +0000)
committerstronk7 <stronk7>
Sun, 20 Aug 2006 15:50:27 +0000 (15:50 +0000)
loads, transform and execute one entire XMLDB
file into the correct SQL sentences.

lib/ddllib.php

index a5b189b3af7966a51790e9ac58204c251dbea28d..67f4ee313e65ad815fb5a41436a66dae1598bff6 100644 (file)
     require_once($CFG->libdir . '/xmldb/classes/XMLDBIndex.class.php');
     require_once($CFG->libdir . '/xmldb/classes/XMLDBStatement.class.php');
 
+/// Based on $CFG->dbtype, add the proper generator class
+    require_once($CFG->libdir . '/xmldb/classes/generators/' . $CFG->dbtype . '/' . $CFG->dbtype . '.class.php');
+
+/// Add other libraries
+    require_once($CFG->libdir . '/xmlize.php');
+    
+
 function table_column($table, $oldfield, $field, $type='integer', $size='10',
                       $signed='unsigned', $default='0', $null='not null', $after='') {
     global $CFG, $db, $empty_rs_cache;
@@ -214,4 +221,46 @@ function table_column($table, $oldfield, $field, $type='integer', $size='10',
     }
 }
 
+/**
+ * This function will load one entire XMLDB file, generating all the needed
+ * SQL statements, specific for each RDBMS ($CFG->dbtype) and, finally, it
+ * will execute all those statements against the DB.
+ *
+ * @uses $CFG 
+ * @param $file full path to the XML file to be used
+ * @return boolean (true on success, false on error)
+ */
+function install_from_xmldb_file($file) {
+
+    global $CFG, $db;
+
+    $status = true;
+
+
+    $xmldb_file = new XMLDBFile($file);
+
+    if (!$xmldb_file->fileExists()) {
+        return false;
+    }
+
+    $loaded = $xmldb_file->loadXMLStructure();
+    if (!$loaded || !$xmldb_file->isLoaded()) {
+        return false;
+    }
+
+    $structure = $xmldb_file->getStructure();
+
+    if (!$sqlarr = $structure->getCreateStructureSQL($CFG->dbtype, $CFG->prefix, false)) {
+        return false;
+    }
+
+    foreach($sqlarr as $sql) {
+        if (!execute_sql($sql)) {
+            $status = false;  // Allow to continue to mimic old behaviour
+        }                     // perhaps it would be better to exit false
+    }
+
+    return $status;
+}
+
 ?>