Wordpress to Joomla converter, bugfix

29 Oct, 2008  |  Written by User ImageDan (Who am I?)  |  under Code Snippets, Conversions, Tutorials

I noticed during some routine coding today that I had made an oversight with the user import script, I apologise for this mistake but have provided the below update to the script.

<?php
error_reporting(E_ALL);
//
// Include The configuration file
//
include('config.php');
 
//
// Create a new connection to the Wordpress Database
//
$db = new  ezSQL_mysql($wp_db_user, $wp_db_pass, $wp_db, $wp_db_host);
echo "Initialised WP DB<br/>";
 
//
// Create a connection to the Joomla Database
//
$jb = new  ezSQL_mysql($j_db_user, $j_db_pass, $j_db, $j_db_host);
echo "Initialised JOS DB<br />";
//
// This function simply checks user permissions and then returns the correct one to the Joomla
// At some point we are going to need to get the ARO group id's as well but we can do that with another function
//
function check_user_perms($perm_string)
{
	//I am using an strpos search, which isn't ideal, we should use a regular expression but that is for another tutorial
	if(strpos($perm_string, "subscriber")!==false)
	{
		//we have a subscriber
		$perm = "Registered";
	}
	else if(strpos($perm_string, "author")!==false)
	{
		//we have an author
		$perm = "Author";
	}
	else if(strpos($perm_string, "administrator")!==false)
	{
		$perm = "Super Administrator";
	}
	else if(strpos($perm_string, "editor")!==false)
	{
		$perm = "Editor";
	}
	else if(strpos($perm_string, "contributor")!==false)
	{
		$perm = "Publisher";
	}
 
	return $perm;
}
 
//
// This function just maps the group the user is in with the ARO group ID
//
function aro_acl_group_id($group)
{
	switch ($group) {
	case "Registered":
		$g_id = "18";
		break;
	case "Author":
		$g_id = "19";
		break;
	case "Super Administrator":
		$g_id = "25";
		break;
	case "Editor":
		$g_id = "20";
		break;
	case "Publisher";
		$g_id = "21";
	}
 
	return $g_id;
}
 
//
// Create a new table in your Joomla database to store the new 
// User ID's so that we can map posts and articles to them. We will destroy this table as one of the final steps
//
 
$jb->query( "CREATE TABLE `jos_user_map` (`wp_id` INT NOT NULL ,`jos_id` INT NOT NULL) ENGINE = MYISAM ");
echo "Created User Map table<br />";
 
 
//
// Select the users and their related meta details from Wordpress
// The query looks complicated, but all it does is applies some easy names to each column selected for handling later, it also join the usermeta table and grabs the "capabilities" field
// which tells us what permissions the user has
$users = $db->get_results("select u.ID as wpID, u.user_login as username, u.user_pass as pwd, u.user_email as email, u.user_registered as reg_date, u.display_name as display_name, um.meta_key as mkey, um.meta_value as u_perm
from ".$wp_prefix."users u
join ".$wp_prefix."usermeta um on um.user_id = u.ID
Where um.meta_key='wp_capabilities' order by u.ID");
 
echo "Selected WP Users and beginning insert loop";
//
// Declare the $i integer simple for a count later
//
$i=0;
//
// Loop through the results array and work our magic
//
foreach($users as $user)
{
	//
	// It's sometimes a good idea when throwing in usernames to run some checks and remove any invalid characters, or at least invalid for the new system
	// Luckily Joomla and Wordpress were both written with a great deal fo security in mind and as such, we don't need to write any RegExp to check
	// and remove anything, but if you were coming from a system like e107 it is VITAL that you remove characters like ' from usernames as not only
	// will this cause problems in Joomla, such characters can open you up to SQL injections. But once again we don't need it here.
	//
 
	//
	// First things first, lets see what user they are and then do the mapping.
	//
	$user_perms = check_user_perms($user->u_perm);
 
	//
	// Get ARO Group ID for Joomla
	//
	$aro_group = aro_acl_group_id($user_perms);
 
	//
	// we are ready to start importing users
 
	// Insert the basic user Profile information into the Joomla User table (you may want to change the "display name" to "wp: nice_name" both in the qp query and here
	$jb->query("Insert into ".$j_prefix."users (name, username, email, password, usertype, sendemail, gid) VALUES ('".$user->display_name."', '".$user->username."', '".$user->email."', '".$user->pwd."', '".$user_perms."', 0, ".$aro_group.")");
 
	// Get the ID and store it
	$new_user_id = $jb->insert_id; 
 
	// Use the new User ID to insert into our conversion table so that we can match ID's for the articles
	$jb->query("insert into jos_user_map (wp_id, jos_id) VALUES (".$user->wpID.", ".$new_user_id.")");
	// Insert into the first ACL table, Core ACL
	$jb->query("Insert into ".$j_prefix."core_acl_aro (section_value, value, name) VALUES ('users', ".$new_user_id.", '".$user->username."')");
	// get the aro_id
	$aro_id = $jb->insert_id;
	// Insert into the ACL Groups table
	$jb->query("insert into ".$j_prefix."core_acl_groups_aro_map (group_id, aro_id) VALUES (".$aro_group.", ".$aro_id.")");
	$i++;
}
 
echo $i." records inserted<br /><br /><br />";
 
echo "User Import Complete";
 
//
// TO DO: Add the link to import the next step, categories
//
 
?>

The issue was that during the final insert query, I inserted the user ID into the group_aro_map when I should have inserted the newly created aro_id.

It is a quick fix, we simply grab the newly inserted id via the insert_id; call, place it in a variable and use it for the insert, all the action is seen below

	// get the aro_id
	$aro_id = $jb->insert_id;
	// Insert into the ACL Groups table
	$jb->query("insert into ".$j_prefix."core_acl_groups_aro_map (group_id, aro_id) VALUES (".$aro_group.", ".$aro_id.")");

Sorry again, this should now work fine and the category import script is coming up next.

Rate this:
2.1

Comments

Post a comment   |   Trackback URI   |   Comments RSS feed

No comments yet.

Trackbacks/Pings

  • No trackbacks or pings yet

Leave a Comment

Comment template by SezWho