diff --git a/lib/setup.php b/lib/setup.php index 5f127b8862b..218b11fe67c 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -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'; diff --git a/lib/setuplib.php b/lib/setuplib.php index 7de8bc4892e..53355f2cefa 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -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; +} + + ?>