Source for file wpimport.ctrl.php
Documentation is available at wpimport.ctrl.php
/* WordPress 2.3 to b2evolution 2.0 alpha converter
Copyright (C) 2007 V.Harishankar.
Please use this with care and at your own discretion. This script will try and import the following from
This is alpha software and subject to change.
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
// set error reporting to full in order to get useful info when something fails
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>WP to b2evolution Converter</title>
<meta name="GENERATOR" content="Quanta Plus">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body bgcolor="#EEEEEE" text="#000000" link="#0000FF" alink="#FF0000" vlink="#7E0089">
<h1>WordPress 2.3 to b2evolution importer</h1>
[<a href="
<?php echo
$dispatcher ?>?ctrl=tools">Back to b2evolution</a>]
<p><FONT SIZE="-2">Copyright © 2007 <a href="http://hari.literaryforums.org/2007/10/04/wordpress-to-b2evolution-import-script">V.Harishankar</a>. Released under the GNU GPL.</FONT></p>
// Check if user is logged in and is in group #1 (admins)
debug_die( 'You must login with an administrator (group #1) account.' );
// The form has not yet been posted
if ( ! isset
( $_POST['wp_db'] ) ) { ?>
<P>Before running this importer, you must ensure that a proper <font color="#00CC00"><strong><em>NEW, EMPTY</em></strong></font> installation of b2evolution 2 exists! <strong><font color="#FF0000">IMPORTANT</font></strong>: This works <strong>only</strong> with WordPress 2.3 and above.</P>
<p><font color="#FF0000"><strong>Warning!!</strong> Your existing b2evolution posts, categories, tags, comments and users (except admin) will be removed if you run this script. Make sure you have a backup before you proceed.</font></p>
<FORM action="
<?php echo
$dispatcher ?>?ctrl=wpimport" enctype="multipart/form-data" method="POST" >
<td>WordPress database name</td>
<td><INPUT type="text" name="wp_db"><br></td>
<td>WordPress table prefix</td>
<td><INPUT type="text" name="wp_prefix" value="wp_"><br></td>
<td>b2evolution database</td>
<td><INPUT type="text" name="b2evo_db" value="
<?php echo
$db_config['name'] ?>"></td>
<td>b2evolution table prefix</td>
<td><INPUT type="text" name="b2evo_prefix" value="
<?php echo
$tableprefix ?>"></td>
<td><INPUT type="text" name="db_host" value="
<?php echo
$db_config['host'] ?>"></td>
<td><INPUT type="text" name="db_user" value="
<?php echo
$db_config['user'] ?>"></td>
<td><INPUT type="password" name="db_pass" value="
<?php echo
$db_config['password'] ?>"></td>
<td>Default locale for imported posts</td>
<td><INPUT type="text" name="locale" value="en-US"></td>
<td><INPUT type="submit" value="import"></td>
<?php // The form has been posted; do the conversion
// Try to obtain some serious time to do some serious processing (15 minutes)
// required fields initialization
$wp_db =
$_POST['wp_db'];
$evo_db =
$_POST['b2evo_db'];
$host =
$_POST['db_host'];
$user =
$_POST['db_user'];
$password =
$_POST['db_pass'];
$wp =
$_POST['wp_prefix'];
$b2 =
$_POST['b2evo_prefix'];
$locale =
$_POST['locale'];
// establish database connection
die ( 'Error connecting to MySQL. Please check whether the server is running and the host, username and password fields are correct!' );
// First remove existing database items in categories, users, postcats, items__item, comments, blogusers
die ('b2evolution database name is incorrect. Please check your b2evolution installation.');
$query =
'DELETE FROM '.
$b2.
'categories;';
die ('Existing categories deleting failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'items__item;';
die ('Existing posts deletion failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'postcats;';
die ('Existing post categories deletion failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'comments;';
die ('Existing comments deletion failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'items__itemtag;';
die ('Existing post tags deletion failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'items__tag;';
die ('Existing tags deletion failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'users WHERE user_ID <> 1;';
die ('Existing users deletion failed. Cannot proceed.');
$query =
'DELETE FROM '.
$b2.
'blogusers WHERE bloguser_user_ID <> 1;';
die ('Existing user permissions deletion failed. Cannot proceed.');
echo
'<h2>Trying to import categories and tags:</h2>';
// select the wordpress database
die ('WordPress database name is incorrect. Please check the name and try again.');
// get the list of taxonomy terms. includes categories, link cats and tags as well
die ('Query failed. Please check your WordPress installation.');
// in order to establish whether a term is a category or not
FROM '.
$wp.
'term_taxonomy
WHERE term_id='.
$row['term_id'].
';';
die ('Query 2 failed. Please check your WordPress installation.');
// if it is a category only then import. ignore tags and link categories
switch( $row2['taxonomy'] )
echo
'Reading cat: '.
$row['name'].
'<br>';
$cats[$i]['name'] =
$row['name'];
$cats[$i]['slug'] =
$row['slug'];
$cats[$i]['description'] =
$row2['description'];
$cats[$i]['cat_id'] =
$row2['term_taxonomy_id'];
echo
'Reading tag: '.
$row['name'].
'<br>';
$tag_id =
$row2['term_taxonomy_id'];
$tags[$tag_id]['name'] =
strtolower( $row['name'] );
$tags[$tag_id]['slug'] =
$row['slug'];
$tags[$tag_id]['description'] =
$row2['description'];
die( 'There must be at least one category!' );
// Use the first category as the default category in case we find uncategorized posts later on.
$default_category_ID =
$cats[0]['cat_id'];
// select the evolution database
die ('b2evolution database name is incorrect. Please check the name and try again.');
foreach ($cats as $category)
// insert each category into the evolution database
$query =
'INSERT INTO '.
$b2.
'categories (cat_ID, cat_name, cat_urlname, cat_blog_ID, cat_description) VALUES ("'.
$category['cat_id'].
'", "'.
$category['name'].
'", "'.
$category['slug'].
'", "1", "'.
$category['description'].
'");';
die ('Category importing failed. Please check your b2evolution installation.');
echo
'<font color="#00CC00">Categories inserted successfully!</font><br>';
foreach( $tags as $tag_id =>
$tag )
// insert each tags into the evolution database
$query =
'INSERT INTO '.
$b2.
'items__tag(tag_ID, tag_name)
VALUES ( '.
$tag_id.
', "'.
evo_substr($tag['name'],0,50).
'" );';
die ('Tag importing failed. Please check your b2evolution installation.');
echo
'<font color="#00CC00">Tags inserted successfully!</font><br>';
// Now import the posts into b2evolution
echo
'<h2>Trying to import posts</h2>';
die ('WordPress database name is incorrect. Please check the name and try again.');
$query =
'SELECT * FROM '.
$wp.
'posts WHERE post_type="post" OR post_type="page";' ;
die ('Query failed. Please check your WordPress installation.');
$posts[$i]['post_id'] =
$row['ID'];
$posts[$i]['slug'] =
$row['post_name'];
$posts[$i]['title'] =
$row['post_title'];
$posts[$i]['status'] =
$row['post_status'];
$posts[$i]['create_date'] =
$row['post_date'];
$posts[$i]['modified_date'] =
$row['post_modified'];
$posts[$i]['excerpt'] =
$row['post_excerpt'];
$posts[$i]['comment_status'] =
$row['comment_status'];
$posts[$i]['content'] =
$row['post_content'];
$posts[$i]['author'] =
$row['post_author'];
if (strcmp ($row['post_type'], 'page') ==
0)
$posts[$i]['type'] =
1000;
echo
'Reading: '.
$posts[$i]['title'].
'<br>';
// Now to get the cats for each post. Includes both CATS and TAGS
$posts[$i]['cats'] =
array ();
$posts[$i]['tags'] =
array ();
// Get all reltated terms:
FROM '.
$wp.
'term_relationships
WHERE object_id='.
$row['ID'].
'; ';
die ('Query 2 failed. Please check your WordPress installation.');
// Get each specific term:
FROM '.
$wp.
'term_taxonomy
WHERE term_taxonomy_id='.
$row2['term_taxonomy_id'].
';';
die ('Query 3 failed. Please check your WordPress installation.');
switch( $row3['taxonomy'] )
$posts[$i]['cats'][$j] =
$row2['term_taxonomy_id'];
$posts[$i]['tags'][] =
$row2['term_taxonomy_id'];
// select the evolution database
die ('b2evolution database name is incorrect. Please check the name and try again.');
if( $date ==
'0000-00-00 00:00:00' )
$date =
'2000-01-01 00:00:00';
foreach ($posts as $post)
echo
'<br/>Inserting: '.
$post['title'];
// Check that we have at least one category:
if( empty($post['cats']) )
{ // Use default category:
$post['cats'][0] =
$default_category_ID;
// set the post rendering options. TODO: this could probably be an option for the user before importing
$postrenderers =
'b2evSmil.b2evALnk.b2WPAutP';
// Check that slug is not empty. Mind you, in WP it CAN happen!
if( empty( $post['slug'] ) )
$post['slug'] =
preg_replace( '/[^a-z0-9]/i', '-', $post['post_id'].
'-'.
$post['title'] );
echo
'<br /> ** WARNING: generated automatic slug: '.
$post['slug'];
// query to insert the posts into the b2evolution table
$query =
'INSERT INTO '.
$b2.
'items__item (post_ptyp_ID, post_ID, post_main_cat_ID, post_creator_user_ID,
post_lastedit_user_ID, post_datestart, post_datecreated, post_datemodified, post_status,
post_locale, post_content, post_excerpt, post_title, post_urltitle, post_comment_status,
VALUES ("'.
$post['type'].
'", "'.
$post['post_id'].
'", "'.
$post['cats'][0].
'", "'.
$post['author'].
'", "'
.
$post['author'].
'", "'.
fix_date($post['create_date']).
'", "'.
fix_date($post['create_date']).
'", "'
.
$post['comment_status'].
'", "'.
$postrenderers.
'");';
die ('Post importing failed.');
// insert the post categories into the postcats table
foreach($post['cats'] as $cat)
// query to insert each category for the particular post
$query =
'INSERT INTO '.
$b2.
'postcats (postcat_post_ID, postcat_cat_ID) VALUES ("'.
$post['post_id'].
'", "'.
$cat.
'");';
die ('Post categories insertion failed. Please check your b2evolution installation.');
foreach($post['tags'] as $tag_id)
$query =
'INSERT INTO '.
$b2.
'items__itemtag (itag_itm_ID, itag_tag_ID)
VALUES ('.
$post['post_id'].
', '.
$tag_id.
');';
die ('Post tags insertion failed. Please check your b2evolution installation.');
echo
'<font color="#00CC00">Posts and post categories imported successfully!</font>';
// Now import the comments
echo
'<h2>Trying to import comments</h2>';
// select the wordpress database
die ('WordPress database name is incorrect. Please check the name and try again.');
// discard the spam comments. select only comments where status is either 'in moderation' or 'approved'
$query =
'SELECT * FROM '.
$wp.
'comments WHERE comment_approved="0" OR comment_approved="1" ORDER BY comment_date ASC;';
die ('Query failed. Please check your WordPress installation.');
// set the values from comments table
$comments[$i]['comment_id'] =
$row['comment_ID'];
$comments[$i]['post_id'] =
$row['comment_post_ID'];
$comments[$i]['author'] =
$row['comment_author'];
$comments[$i]['email'] =
$row['comment_author_email'];
$comments[$i]['url'] =
$row['comment_author_url'];
$comments[$i]['ip'] =
$row['comment_author_IP'];
$comments[$i]['date'] =
$row['comment_date'];
$comments[$i]['content'] =
$row['comment_content'];
$comments[$i]['author_id'] =
$row['user_id'];
// set default comment status to published
$comments[$i]['status'] =
'published';
// if the comment isn't approved set it to draft
if ($row['comment_approved'] ==
0)
$comments[$i]['status'] =
'draft';
// default comment type is 'comment
$comments[$i]['type'] =
'comment';
// if it is a pingback or trackback change the type accordingly
if ($row['comment_type'] ==
'pingback' ||
$row['comment_type'] ==
'trackback')
$comments[$i]['type'] =
'pingback';
// free the query result set
// select the evolution db
die ('b2evolution database name is incorrect. Please check the name and try again.');
foreach ($comments as $comment)
// escape the string and replace UNIX newlines to line breaks in order to
// render properly in b2evolution
// query to insert the comments into the b2evolution table
$query =
'INSERT INTO '.
$b2.
'comments (comment_ID, comment_post_ID, comment_type, comment_status, comment_author_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_allow_msgform)
VALUES ("'.
$comment['comment_id'].
'", "'.
$comment['post_id'].
'", "'.
$comment['type'].
'", "'
.
$comment['status'].
'", "'.
$comment['author_id'].
'", "'.
evo_substr($comment['author'],0,100).
'", "'.
$comment['email']
.
'", "'.
$comment['url'].
'", "'.
$comment['ip'].
'", "'.
$comment['date'].
'", "'.
$ccontent.
'", "1");';
die ('Comment importing failed. Please check your b2evolution installation.');
echo
'<font color="#00CC00">Comments imported successfully!</font>';
// Now to import users. Note: all users other than admin will be imported and then they will be set to the default level
echo
'<h2>Trying to import all users (except admin)</h2>';
// select the wordpress database
die ('WordPress database name is incorrect. Please check the name and try again.');
// select all users except the admin user
$query =
'SELECT * FROM '.
$wp.
'users WHERE user_login <> "admin";';
die ('Query failed. Please check your WordPress installation.');
// set all the values from the user table
$users[$i]['id'] =
$row['ID'];
$users[$i]['login'] =
$row['user_login'];
$users[$i]['password'] =
$row['user_pass'];
$users[$i]['nickname'] =
$row['user_nicename'];
$users[$i]['email'] =
$row['user_email'];
$users[$i]['url'] =
$row['user_url'];
$users[$i]['date'] =
$row['user_registered'];
$users[$i]['firstname'] =
$row['display_name'];
echo
'Reading: '.
$users[$i]['login'].
'<br>';
// select the evolution db
die ('b2evolution database name is incorrect. Please check the name and try again.');
foreach ($users as $a_user)
$query =
'INSERT INTO '.
$b2.
'users (user_ID, user_login, user_pass, user_firstname, user_nickname, user_email,
user_url, dateYMDhour, user_validated, user_grp_ID)
VALUES ("'.
$a_user['id'].
'", "'.
$a_user['login'].
'", "'.
$a_user['password'].
'", "'
.
$a_user['firstname'].
'", "'.
$a_user['nickname'].
'", "'.
$a_user['email']
.
'", "'.
$a_user['url'].
'", "'.
fix_date($a_user['date']).
'", "1", "4");';
die ('User importing failed. Please check your b2evolution installation.');
// Import the permissions for blog for the user
$query =
'INSERT INTO '.
$b2.
'blogusers (bloguser_blog_ID, bloguser_user_ID, bloguser_ismember) VALUES ("1", "'.
$a_user['id'].
'", "1");';
die ('User (permissions) importing failed. Please check you b2evolution installation.');
echo
'<font color="#00CC00">Users imported successfully! <strong>NOTE:</strong> all users are set to basic users level by default. You should probably reconfigure user permissions in the admin control panel if you want to give them higher privileges.</font>';
echo
'<strong><font color="#00CC00">Everything imported correctly. Try out your new b2evolution blog!</font></strong>';
// close the connection to the MySQL server
<?php // reset the PHP error reporting to the previous level
* $Log: wpimport.ctrl.php,v $
* Revision 1.9 2009/07/09 12:12:49 tblue246
* a) RegExp delimiter *is* needed. b) Removed uppercase A-Z range and made RegExp case-insensitive.
* Revision 1.8 2009/07/09 05:07:08 sam2kb
* Revision 1.7 2009/07/09 00:11:19 fplanque