]> git.mjollnir.org Git - moodle.git/commitdiff
MySQL code (etc) to convert all usernames to lowercase ... does a lot more
authormoodler <moodler>
Fri, 25 Apr 2003 03:54:21 +0000 (03:54 +0000)
committermoodler <moodler>
Fri, 25 Apr 2003 03:54:21 +0000 (03:54 +0000)
checking than it really needs to, since MySQL is not case sensitive

lib/db/mysql.php
lib/db/postgres7.php
version.php

index 8d099c95f9c45e54441007c23e0d2a4acdee803e..ad39895bd5405a332272489a6c5764be94f9e3af 100644 (file)
@@ -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.'<br/>' ; 
+            } else {
+                array_push($rarray,$lcname);
+            }
+        }
+
+        if ($cerrors != '') {
+            notify("Error: Cannot convert usernames to lowercase. 
+                    Following usernames would overlap (id->username):<br/> $cerrors . 
+                    Please resolve overlapping errors."); 
+            $result = false;
+        }
+
+        $cerrors = "";
+        echo "Checking userdatabase:<br>";
+        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;
+
 }
 
 ?>
index fd8a28aedcd9c2ca21254d325afcd367109902b7..1151de74cb2864b33028dbdb88adf23a141aecbb 100644 (file)
@@ -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.'<br/>' ; 
-               }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):<br/> $cerrors . Please resolve overlapping errors."; 
+            notify("Error: Cannot convert usernames to lowercase. 
+                    Following usernames would overlap (id->username):<br/> $cerrors . 
+                    Please resolve overlapping errors."); 
             $result = false;
-        }else {
-            $cerrors = '';
-            print "Checking userdatabase:<br>";
-            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:<br>";
+        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;
 }
 ?>    
index 184657ec1b4dee5bbd6f9939405a31f663a236a1..6463e3e7c550ab3a892db809f1d1b2efdb24b0cd 100644 (file)
@@ -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