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.
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';
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;
+}
+
+
?>