Saturday, July 15, 2006

Importing User Data Into Joomla

I had a need to import user data into Joomla with the Community Builder RC1 component installed. After a bit of discovery I found a method. First, I had to export my users from a proprietory Access database to something I could import using phpMyAdmin. I ultimately found that MySQL seems to like tab delimited files better, since phpMyAdmin likes to have a character set surrounding each cell for import purposes, and Excel doesn't export csv that way (at least not to my knowledge). Before any of these changes were attempted, I exported the tables marked for updating. Backups are good juju.... Once that was done, I had to form the data to prepare for import. The first table I imported was jos_users. I used a nice php script found somewhere on the net. Replace $TABLENAME, $FILENAME, $DATABASENAME, $PASSWORD, $USERNAME, and $HOSTNAME with appropriate values.

 1    # first get a mysql connection
 2   $dbh=mysql_connect ("$HOSTNAME", "$USERNAME", "$PASSWORD") or die ('I cannot connect to the database because: ' . mysql_error());
 3 mysql_select_db ("$DATABASENAME");
 4 
 5   $fcontents = file ('./$FILENAME'); 
 6   # expects the csv file to be in the same dir as this script
 7 
 8   for($i=0; $i<sizeof($fcontents); $i++) { 
 9       $line = trim($fcontents[$i]); 
10       $arr = explode("\t", $line); 
11       #if your data is comma separated
12       # instead of tab separated, 
13       # change the '\t' above to ',' 
14      
15       $sql = "insert into $TABLENAME values ('". 
16                   implode("','", $arr) ."')"; 
17       mysql_query($sql);
18       echo $sql ."\n";
19       if(mysql_error()) {
20          echo mysql_error() ."\n";
21       } 
22 }
Use the UPDATE command to convert passwords to MD5 in a SQL query like this: UPDATE `jos_users` SET `password`=md5(`tmp_pass`) That would set the password field in jos_users to an md5 hashed value found in a temporary column called tmp_pass. You could then delete the unused column. I then found that there is yet another couple of tables to update called jos_comprofiler, jos_core_acl_aro, and jos_core_acl_groups_aro_map. Do the updates to these tables in this order, since it's easy to spot the patterns in data, so you can modify your original Excel data to fit. Fun!