From 7104d80dbc95c96ec1eab3907b8f68392dee890e Mon Sep 17 00:00:00 2001 From: stronk7 Date: Mon, 27 Mar 2006 11:24:14 +0000 Subject: [PATCH] Now setup.php detects is CFG->unicode is not set and examines 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 | 31 +++++++++++++++++++++++-------- lib/setuplib.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/lib/setup.php b/lib/setup.php index 5f127b8862..218b11fe67 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 7de8bc4892..53355f2cef 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; +} + + ?> -- 2.39.5