From: moodler Date: Fri, 25 Apr 2003 03:54:21 +0000 (+0000) Subject: MySQL code (etc) to convert all usernames to lowercase ... does a lot more X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=047d30d1ebdf4b37256f515c0f69bb27bf397028;p=moodle.git MySQL code (etc) to convert all usernames to lowercase ... does a lot more checking than it really needs to, since MySQL is not case sensitive --- diff --git a/lib/db/mysql.php b/lib/db/mysql.php index 8d099c95f9..ad39895bd5 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -21,6 +21,8 @@ function main_upgrade($oldversion=0) { global $CFG; + $result = true; + if ($oldversion == 0) { execute_sql(" CREATE TABLE `config` ( @@ -300,7 +302,53 @@ function main_upgrade($oldversion=0) { } } - return true; + if ($oldversion < 2003042500) { + // Convert all usernames to lowercase. + $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user"); + $cerrors = ""; + $rarray = array(); + + foreach ($users as $user) { // Check for possible conflicts + $lcname = trim(moodle_strtolower($user->username)); + if (in_array($lcname, $rarray)) { + $cerrors .= $user->id."->".$lcname.'
' ; + } else { + array_push($rarray,$lcname); + } + } + + if ($cerrors != '') { + notify("Error: Cannot convert usernames to lowercase. + Following usernames would overlap (id->username):
$cerrors . + Please resolve overlapping errors."); + $result = false; + } + + $cerrors = ""; + echo "Checking userdatabase:
"; + foreach ($users as $user) { + $lcname = trim(moodle_strtolower($user->username)); + if ($lcname != $user->username) { + $convert = set_field("user" , "username" , $lcname, "id", $user->id); + if (!$convert) { + if ($cerrors){ + $cerrors .= ", "; + } + $cerrors .= $item; + } else { + echo "."; + } + } + } + if ($cerrors != '') { + notify("There were errors when converting following usernames to lowercase. + '$cerrors' . Sorry, but you will need to fix your database by hand."); + $result = false; + } + } + + return $result; + } ?> diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index fd8a28aedc..1151de74cb 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -40,19 +40,19 @@ function main_upgrade($oldversion=0) { insert_record("log_display", $new); } - //support user based course creating + //support user based course creating if ($oldversion < 2003032400) { - execute_sql("CREATE TABLE $CFG->prefix_user_coursecreators ( + execute_sql("CREATE TABLE $CFG->prefix_user_coursecreators ( id int8 SERIAL PRIMARY KEY, userid int8 NOT NULL default '0' )"); - } + } - if ($oldversion < 2003041400) { + if ($oldversion < 2003041400) { table_column("course_modules", "", "visible", "integer", "1", "unsigned", "1", "not null", "score"); } - if ($oldversion < 2003042104) { // Try to update permissions of all files + if ($oldversion < 2003042104) { // Try to update permissions of all files if ($files = get_directory_list($CFG->dataroot)) { echo "Attempting to update permissions for all files... ignore any errors."; foreach ($files as $file) { @@ -76,44 +76,51 @@ function main_upgrade($oldversion=0) { } } - if ($oldversion < 2003042401) { - // Convert usernames to lowercase - $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user"); - $cerrors = ""; - $rarray = array(); - foreach ($users as $user) { - $lcname = trim(moodle_strtolower($user->username)); - if (in_array($lcname, $rarray)) { + if ($oldversion < 2003042500) { + // Convert all usernames to lowercase. + $users = get_records_sql("SELECT id, username FROM {$CFG->prefix}user"); + $cerrors = ""; + $rarray = array(); + + foreach ($users as $user) { // Check for possible conflicts + $lcname = trim(moodle_strtolower($user->username)); + if (in_array($lcname, $rarray)) { $cerrors .= $user->id."->".$lcname.'
' ; - }else { - array_push($rarray,$lcname); - } + } else { + array_push($rarray,$lcname); + } } - /// Do convert or give error message + if ($cerrors != '') { - print "Error: Cannot convert usernames to lowercase. Following usernames would overlap (id->username):
$cerrors . Please resolve overlapping errors."; + notify("Error: Cannot convert usernames to lowercase. + Following usernames would overlap (id->username):
$cerrors . + Please resolve overlapping errors."); $result = false; - }else { - $cerrors = ''; - print "Checking userdatabase:
"; - foreach ($users as $user) { - $lcname = trim(moodle_strtolower($user->username)); - $convert = set_field("user" , "username" , $lcname, "id", $user->id); - if (!$convert) { + } + + $cerrors = ""; + echo "Checking userdatabase:
"; + foreach ($users as $user) { + $lcname = trim(moodle_strtolower($user->username)); + if ($lcname != $user->username) { + $convert = set_field("user" , "username" , $lcname, "id", $user->id); + if (!$convert) { if ($cerrors){ $cerrors .= ", "; } $cerrors .= $item; - } else { - print "."; + } else { + echo "."; } } - if ($cerrors != '') { - print "There was errors when converting following usernames to lowercase. '$cerrors' . Please maintain your database by hand."; - $result=false; - } + } + if ($cerrors != '') { + notify("There were errors when converting following usernames to lowercase. + '$cerrors' . Sorry, but you will need to fix your database by hand."); + $result = false; } } + return $result; } ?> diff --git a/version.php b/version.php index 184657ec1b..6463e3e7c5 100644 --- a/version.php +++ b/version.php @@ -5,7 +5,7 @@ // database to determine whether upgrades should // be performed (see lib/db/*.php) -$version = 2003042400; // The current version is a date (YYYYMMDDXX) +$version = 2003042500; // The current version is a date (YYYYMMDDXX) $release = "1.0.9 development"; // User-friendly version number