]> git.mjollnir.org Git - moodle.git/commitdiff
Now setup.php detects is CFG->unicode is not set and examines
authorstronk7 <stronk7>
Mon, 27 Mar 2006 11:24:14 +0000 (11:24 +0000)
committerstronk7 <stronk7>
Mon, 27 Mar 2006 11:24:14 +0000 (11:24 +0000)
DB to detect such value dinamically. Useful for installations
not using the standard installer but manually edited config.php
files.
Little TODO: modify a bit the initial config tables insets to
insert this value, avoiding to call it for each request. Coming
soon (although not mandatory for 1.6beta). Tested against both
MySQL and PostgreSQL with unicode and non-unicode DBs.

lib/setup.php
lib/setuplib.php

index 5f127b8862b15a1d1fc38e89d36de8d3dd261a21..218b11fe67c4473b6b7ace02180e3ae6daa520cf 100644 (file)
@@ -149,14 +149,9 @@ global $HTTPSPAGEREQUIRED;
         die;
     }
 
-    /// Set the client/server and connection to utf8 if necessary
-    if ($dbconnected && $CFG->unicodedb) {
-        if ($db->databaseType == 'mysql') {
-            $db->Execute("SET NAMES 'utf8'");
-        } else if ($db->databaseType == 'postgres7') {
-            $db->Execute("SET NAMES 'utf8'");
-        }
-    }
+/// Starting here we have a correct DB conection but me must avoid
+/// to execute any DB transaction until "set names" has been executed
+/// some lines below!
 
     error_reporting(E_ALL);       // Show errors from now on.
 
@@ -201,6 +196,26 @@ global $HTTPSPAGEREQUIRED;
     error_reporting($CFG->debug);
 
 
+/// If $CFG->unicodedb is not set, calculate it because we need
+/// "set names" (below) properly executed *before* performing any
+/// DB transaction
+    if ($dbconnected && !isset($CFG->unicodedb)) {
+        $CFG->unicodedb = setup_is_unicodedb();
+    }
+
+
+/// Set the client/server and connection to utf8 if necessary
+    if ($dbconnected && $CFG->unicodedb) {
+        if ($db->databaseType == 'mysql') {
+            $db->Execute("SET NAMES 'utf8'");
+        } else if ($db->databaseType == 'postgres7') {
+            $db->Execute("SET NAMES 'utf8'");
+        }
+    }
+/// Now that "set names" has been executed it is safe to
+/// work with the DB, but never before this!
+
+
 /// Set a default enrolment configuration (see bug 1598)
     if (!isset($CFG->enrol)) {
         $CFG->enrol = 'manual';
index 7de8bc4892e7c8e353074edd3c7d0b6e7b39dddb..53355f2cefad306d014cd83655574e7666554564 100644 (file)
@@ -80,4 +80,42 @@ function make_upload_directory($directory, $shownotices=true) {
     return $currdir;
 }
 
+/**
+ * This function will introspect inside DB to detect it it's a UTF-8 DB or no
+ * Used from setup.php to set correctly "set names" when the installation
+ * process is performed without the initial and beautiful installer
+ */
+function setup_is_unicodedb() {
+
+    global $CFG, $db;
+
+    $unicodedb = false;
+
+    switch ($CFG->dbtype) {
+        case 'mysql':
+        /// Get MySQL character_set_database value
+            $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
+            if ($rs && $rs->RecordCount() > 0) {
+                $records = $rs->GetAssoc(true);
+                $encoding = $records['character_set_database']['Value'];
+                if (strtoupper($encoding) == 'UTF8') {
+                    $unicodedb = true;
+                }
+            }
+            break;
+        case 'postgres7':
+        /// Get PostgreSQL server_encoding value
+            $rs = $db->Execute("SHOW server_encoding");
+            if ($rs && $rs->RecordCount() > 0) {
+                $encoding = $rs->fields['server_encoding'];
+                if (strtoupper($encoding) == 'UNICODE') {
+                    $unicodedb = true;
+                }
+            }
+            break;
+    }
+    return $unicodedb;
+}
+
+
 ?>