or * 10. Imports ALL users, and comments from registered users get denormalized properly. * * --> Special thanks to Graham http://tin-men.net for graciously rooting out annoying bugs. * * Isaac Z. Schlueter, August 2005 * Removed importing the post_excerpt, since that isn't really necessary at all, and just makes * certain cool tricks harder to perform later on. * * David J. Leach, Jr - August 7, 2006 * ** Note that these changes have only been tested against WP 2.0.4 ** * 1. Added conditional porting of trackback comments. Mainly because my blog had a ton of spam trackbacks. * 2. Fixed importing of users to import into the format required by WP2.0.4. This includes adding support for the metauser * user data * 3. Attempted to map the b2evolution user level to the wp_user_level and wp_capabilities settings of WP2.0.4 * 4. Fixed importing of comments to set all of the proper fields (gmt, user, etc..). * 5. Fixed importing of posts to get correct author, GMT values, correct comment count. * * Eddy Carroll - January 2015: b2Evolution 0.9 to Wordpress 2.7 compatibility * * 1. Changed some table/column names for compatibility with Wordpress 2.7 * 2. Some additional debugging/cleanup * 3. Not guaranteed to work with any other combinations...! Caveat emptor. * * For older revisions, see * * https://web.archive.org/web/20130401000000* /http://themikecam.com/downloads/import-b2evolution-wp2.php.txt* (Contatenate the two lines so there is no space between the '*' and '/';) * for older revisions. * */ if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. You must install WordPress before you import any entries."); require('../wp-config.php'); $linkblogs_imported = array(); function import_linkblog( $lbid ) { global $linkblogs_imported; if( in_array($lbid, $linkblogs_imported) ) return true; else $linkblogs_imported[] = $lbid; echo '
Importing Links from Blog #' . $lbid . '
'; global $resb2, $b2_prefix, $wpdb; // map the linkblog cats to wp_linkcategories $evocats = mysql_query( 'SELECT cat_ID, cat_name FROM `' . $b2_prefix . 'categories` WHERE cat_blog_ID = ' . $lbid); $catcnt = 0; $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->linkcategories'"); $newCatID = $id_result->Auto_increment; $catmap = array(); $linkcats = array(); while( $evocat = mysql_fetch_object($evocats) ) { $evocat->cat_name = $wpdb->escape($evocat->cat_name); $wpdb->query("INSERT INTO $wpdb->linkcategories (cat_ID, cat_name) VALUES ($newCatID, '$evocat->cat_name')"); $catmap[$evocat->cat_ID] = $newCatID ++; $catcnt++; } // echo '$catmap: '; // print_r($catmap); // echo ''; $cnt = 0; // now import the linkblog posts. $lbposts = mysql_query("SELECT post_URL as 'link_url', post_mod_date as 'link_updated', post_status, post_title as 'link_name', post_category, post_content as 'link_description' from `" . $b2_prefix . "posts` " . 'INNER JOIN `' . $b2_prefix . 'categories` ON post_category = cat_ID ' . ' AND cat_blog_id = ' . $lbid); while( $lbpost = mysql_fetch_object($lbposts) ) { if( preg_match('#
$matches: '; // print_r($matches); // echo ''; $lbpost->link_image = $matches[1]; $lbpost->link_name = strip_tags($lbpost->link_name); if(!$lbpost->link_name) $lbpost->link_name = '[Image]'; } else { $lbpost->link_image = ''; } $lbpost->link_category = $catmap[$lbpost->post_category]; $post_content = $lbposts->post_content; $post_excerpt = $post_content; if( strpos(strtolower($post_excerpt), '') !== false ) { $post_excerpt = preg_replace('//i','',$post_excerpt); } if( strpos(strtolower($post_excerpt), '') !== false ) { $post_excerpt = preg_replace('//i','',$post_excerpt); $post_excerpt = explode('', $post_excerpt); $post_excerpt = $post_excerpt[0]; } $post_content = $wpdb->escape($lbpost->link_description); $post_excerpt = $wpdb->escape($post_excerpt); $lbpost->link_description = substr($post_excerpt, 0, 255); $lbpost->link_notes = $post_content; $lbpost->link_name = $wpdb->escape($lbpost->link_name); $lbpost->link_visible = ($lbpost->post_status == 'published' ? 'Y' : 'N'); $sql = "INSERT INTO $wpdb->links (link_url, link_name , link_category, link_description, link_visible, link_updated, link_notes, link_image) " . "VALUES ('$lbpost->link_url', '$lbpost->link_name', $lbpost->link_category, '$lbpost->link_description', '$lbpost->link_visible', '$lbpost->link_updated', '$lbpost->link_notes', '$lbpost->link_image')"; echo "Running SQL query: $sql\n"; $wpdb->query($sql); $cnt ++; echo '
Welcome to the WordPress b2evolution import utility. Before getting started, we need some information on your b2evolution database.
1 '; } else { $sql .= ' WHERE blog_ID IN (' . $b2_blog . ')'; } $sql .= ' ORDER BY blog_ID'; $b2blogs = mysql_query($sql,$resB2); if (!$b2blogs) exit("No blogs returned from the b2evolution database! " . $sql); ?>All right sparky, this is where the actual import takes place! Do you feel lucky today? :p
get_var("SELECT option_value FROM $wpdb->options WHERE option_name='fileupload_url'");
// $filepath_b2 = "http://".$b2_url."/media/";
// echo "b2Evolution file path: ".$filepath_b2."
";
// echo "WordPress file path: ".$filepath_wp."
";
// get authors for blog
echo "Importing User records ...
";
$sql = "SELECT DISTINCT * FROM `" . $b2_prefix . "users` WHERE `user_login` <> 'admin'";
$results = mysql_query($sql,$resB2) or die("Invalid query: " . mysql_error() . "
SQL : " . $sql);
if ($results) {
$cnt = 0;
while ($result = mysql_fetch_object($results)) {
//
// DJL: 8/7/06
//
$wpdb->query("INSERT INTO $wpdb->users (user_login, user_pass, user_email, user_url, user_registered, user_nicename, display_name)
VALUES ('$result->user_login', '$result->user_pass', '$result->user_email', '$result->user_url',
'$result->dateYMDhour', '$result->user_login', '$result->user_nickname')");
$user_id = $wpdb->insert_id;
$arUser[$result->ID] = $user_id;
if ($result->user_level > 9 ) {
$user_level = 10;
$wp_capabilities = serialize(array("administrator" => TRUE));
} else if ($result->user_level > 6 ) {
$user_level = 7;
$wp_capabilities = serialize(array("editor" => TRUE));
} else if ($result->user_level > 1 ) {
$user_level = 2;
$wp_capabilities = serialize(array("author" => TRUE));
} else if ($result->user_level > 0 ) {
$user_level = 1;
$wp_capabilities = serialize(array("contributor" => TRUE));
} else {
$user_level = 0;
$wp_capabilities = serialize(array("subscriber" => TRUE));
}
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'first_name', '$result->user_firstname' )");
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'last_name', '$result->user_lastname' )");
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'nickname', '$result->user_nickname' )");
// $wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
// VALUES ( '$user_id', 'description', '' )");
// $wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
// VALUES ( '$user_id', 'jabber', '' )");
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'aim', '$result->user_aim' )");
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'yim', '$result->user_yim' )");
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'wp_user_level', '$user_level' )");
$wpdb->query("INSERT INTO $wpdb->usermeta ( user_id, meta_key, meta_value)
VALUES ( '$user_id', 'wp_capabilities', '$wp_capabilities' )");
$cnt = $cnt + 1;
}
echo $cnt . " User record(s) imported!
";
} else {
echo "No User records found!
";
}
//
// get categories
//
echo "Importing Category records ...
";
$sql = 'SELECT cat_name, cat_ID, cat_parent_id, cat_blog_ID, cat_description FROM `' . $b2_prefix . 'categories`';
if( $b2_blog != 'all' ) $sql .= ' WHERE cat_blog_ID IN (' . $b2_blog . ')';
$sql .= ' ORDER BY cat_blog_ID, cat_parent_id, cat_ID';
$results = mysql_query($sql,$resB2) or die("Invalid query: " . mysql_error() . "
SQL : " . $sql);
if ($results) {
$cnt = 0;
$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
$newCatID = $id_result->Auto_increment;
// fake the blogs as top-level categories
while($b2blog = mysql_fetch_object($b2blogs))
{
if( $b2_lb ) import_linkblog( $b2blog->blog_links_blog_ID );
if( $b2_bac ) {
$b2blog->cat_name = $wpdb->escape(wp_specialchars($b2blog->blog_name));
if( sanitize_title($b2blog->blog_stub) == $b2blog->blog_stub )
$b2blog->cat_nicename = sanitize_title($b2blog->blog_stub);
else
$b2blog->cat_nicename = sanitize_title($b2blog->blog_shortname);
$b2blog->cat_description = $wpdb->escape( $b2blog->blog_description);
$b2blog->cat_parent_id = 0;
$b2blog->cat_blog_id = 0;
$b2blog->cat_ID = $b2blog->blog_ID * -1;
$arCat[$b2blog->cat_ID] = $newCatID ++;
$categories[$cnt ++] = $b2blog;
}
}
// echo 'categories (just blogs so far)';
// print_r ($categories);
// echo '
';
// now the "real" cats!
//$id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '$wpdb->categories'");
//$newCatID = $id_result->Auto_increment;
while ($result = mysql_fetch_object($results)) {
$result->cat_name = $wpdb->escape( wp_specialchars($result->cat_name));
$result->cat_nicename = sanitize_title($result->cat_name);
$result->cat_description = $wpdb->escape( wp_specialchars($result->cat_description) );
$arCat[$result->cat_ID] = $newCatID ++;
$categories[$cnt ++] = $result;
}
// remap category parent IDs and insert
foreach($categories as $result) {
if($b2_bac) {
if( !$result->cat_parent_id ) $result->cat_parent_id = $result->cat_blog_ID * -1;
}
$result->cat_parent_id = $arCat[$result->cat_parent_id];
$result->cat_ID = $arCat[$result->cat_ID];
$wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_parent, category_description) VALUES ('$result->cat_ID', '$result->cat_name', '$result->cat_nicename', '$result->cat_parent_id', '$result->cat_description')");
}
echo $cnt . " category record(s) imported!
";
} else {
echo "No category records found!
";
}
// get entries for blog
echo "Importing Entry records ...
";
$sql = 'SELECT * FROM `' . $b2_prefix . 'posts` INNER JOIN `' . $b2_prefix . 'categories` ON cat_ID = post_category';
echo "\nExact query is: $sql\nand b2_prefix = [$b2_prefix]\n";
if( $b2_blog != 'all' ) $sql .= ' WHERE cat_blog_ID IN (' . $b2_blog . ')';
$results = mysql_query($sql,$resB2) or die("Invalid query: " . mysql_error() . "
SQL : " . $sql);
if ($results) {
$cnt = 0;
$cntCom = 0;
$cntCat = 0;
while ($result = mysql_fetch_object($results)) {
// TODO: Import ALL posts with ANY cats in ANY selected blogs, not just main cat.
// check to make sure it's got an allowed category from the postcats table.
// if not, then continue.
// this will slow things down a bit, but no biggie.
// Comment out the 'where cat_blog_ID IN...' line above.
// author ID must be switched to new author ID
$post_author = $arUser[$result->post_author];
// category ID must be switched to new category ID
$post_cat = $arCat[$result->post_category];
if (!$cid) {
$cid = '1';
}
// status mapping
$stat = $result->post_status;
switch($stat) {
case 'published':
$stat='publish';
break;
case 'deprecated':
switch($b2_importdep) {
case 'private': $stat = 'private'; break;
case 'draft': $stat = 'draft'; break;
case 'abandon': continue; break;
case 'publish': $stat = 'publish';
}
break;
case 'published': $stat = 'publish'; break;
case 'protected': $stat = 'private'; break;
case 'draft': $stat = 'draft'; break;
}
// update urls in the post content
// IZS - removed this bit. I'm keeping my files where they are!
// $post_content = str_replace($filepath_b2, $filepath_wp, $result->post_content);
$post_content = $result->post_content;
// if there is a post_URL, then put it at the start of the post in its own paragraph.
$dometa = false;
$metaurl = '';
if( $result->post_url && $b2_posturl != 'ignore' ) {
if( $b2_posturl == 'meta' || $b2_posturl == 'postandmeta' ) {
$dometa = true;
$metaurl = $wpdb->escape($result->post_url);
// echo 'url: [' . $metaurl . ']';
}
if( $b2_posturl == 'post' || $b2_posturl == 'postandmeta' ) {
$link = '' . "\n\n";
$post_content = $link . $post_content;
}
}
$post_excerpt = $post_content;
/* IZS: Removed Post_excerpt stuff. Comment the next line to bring it back: */
$post_excerpt = '';
if( strpos(strtolower($post_excerpt), '') !== false ) {
$post_excerpt = preg_replace('//i','',$post_excerpt);
}
if( strpos(strtolower($post_excerpt), '') !== false ) {
//had a weird problem here with vs vs etc.
//that's why the next line with the /i switch, which looks like it does nothing.
//actually, this is LCase-ing all the stuff.
$post_excerpt = preg_replace('//i','',$post_excerpt);
$post_excerpt = explode('', $post_excerpt);
$post_excerpt = $post_excerpt[0];
}
$metaimg = '';
if( preg_match('#
]+>#', $result->post_title, $matches) )
{
// title has an image in the title. - i do this sometimes, and it fubars the importer a little.
// echo 'image found!$matches: ';
// print_r($matches);
// echo '
';
$metaimg = $matches[1];
$dometa = true;
$result->post_title = strip_tags($result->post_title);
if(!$result->post_title) $result->post_title = '[Image]';
}
$post_content = $wpdb->escape($post_content);
$post_excerpt = $wpdb->escape($post_excerpt);
$post_title = $wpdb->escape($result->post_title);
$post_name = sanitize_title($result->post_urltitle);
$post_date_gmt = get_gmt_from_date($result->post_issue_date);
$post_moddate_gmt = get_gmt_from_date($result->post_mod_date);
$sqlquery = "INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_title, post_type,
post_status, post_name, post_excerpt, post_modified, post_modified_gmt)
VALUES
('$post_author', '$result->post_issue_date', '$post_date_gmt', '$post_content',
'$post_title', 'post', '$stat', '$post_name', '$post_excerpt',
'$result->post_mod_date', '$post_moddate_gmt')";
echo "\nRunning SQL: $sqlquery\n";
$wpdb->query($sqlquery);
echo "Inserted '$post_title'
";
$id = $wpdb->insert_id;
$eid = $result->ID;
$cnt = $cnt + 1;
// get comments for entry
$sql = 'SELECT ' .
'CASE WHEN u.ID IS NULL THEN c.comment_author ELSE ' .
'CASE u.user_idmode ' .
"WHEN 'nickname' THEN u.user_nickname " .
"WHEN 'login' THEN u.user_login " .
"WHEN 'namefl' THEN CONCAT(u.user_firstname, ' ', u.user_lastname) " .
"WHEN 'namelf' THEN CONCAT(u.user_lastname, ' ', u.user_firstname) " .
"WHEN 'firstname' THEN u.user_firstname " .
"WHEN 'lastname' THEN u.user_lastname " .
'ELSE u.user_nickname END ' .
"END AS 'author', " .
"CASE WHEN u.ID IS NULL THEN c.comment_author_email ELSE u.user_email END AS 'author_email', " .
"CASE WHEN u.ID IS NULL THEN c.comment_author_url ELSE u.user_url END AS 'author_url', " .
'comment_author_IP, comment_content, comment_post_ID, comment_date, comment_karma, comment_type, comment_author_ID ' .
'FROM `' . $b2_prefix . 'comments` as c ' .
'LEFT JOIN `' . $b2_prefix . 'users` as u ON ' .
'u.ID = c.comment_author_id ' .
'WHERE comment_post_ID=' . $eid;
$subResults = mysql_query($sql, $resB2) or die("Invalid query: " . mysql_error() . "
SQL : " . $sql);
if ($subResults) {
while ($result = mysql_fetch_object($subResults)) {
if ($b2_importtb || ($result->comment_type != 'trackback')) {
$author = $wpdb->escape( apply_filters('pre_comment_author_name', $result->author));
$email = $wpdb->escape( apply_filters('pre_comment_author_email', $result->author_email));
$url = $wpdb->escape( apply_filters('pre_comment_author_url', $result->author_url));
$comment = apply_filters('pre_comment_content', $result->comment_content);
$comment = apply_filters('post_comment_text', $comment);
$comment = apply_filters('comment_content_presave', $comment);
$user_ip = apply_filters('pre_comment_user_ip', $result->comment_author_IP);
$comment_date_gmt = get_gmt_from_date("$result->comment_date");
// author ID must be switched to new author ID
$post_author = $arUser[$result->comment_author_ID];
$wpdb->query(
"INSERT INTO $wpdb->comments (
comment_post_ID, comment_author, comment_author_email, comment_author_url,
comment_author_IP, comment_date, comment_date_gmt, comment_content,
comment_karma, comment_type, user_id)
VALUES (
'$id', '$author', '$email', '$url', '$user_ip', '$result->comment_date',
'$comment_date_gmt', '$comment', '$result->comment_karma', '$result->comment_type', '$post_author')");
//
// djl
//
$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = '$id' AND comment_approved = '1'");
$wpdb->query( "UPDATE $wpdb->posts SET comment_count = $count WHERE ID = '$id'" );
$cntCom = $cntCom + 1;
}
}
}
// do meta for url if requested
if( $dometa ) {
if($metaurl) {
$metasql = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES (" .
$id . ", 'URL', '" . $metaurl . "')";
$wpdb->query($metasql);
}
if($metaimg) {
$metasql = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES (" .
$id . ", 'IMG', '" . $metaimg . "')";
$wpdb->query($metasql);
}
}
// get categories for entry
$cntTmp = 0;
$sql = "SELECT * FROM `" . $b2_prefix . "postcats` WHERE postcat_post_ID=" . $eid;
$subResults = mysql_query($sql, $resB2) or die("Invalid query: " . mysql_error() . "
SQL : " . $sql);
if ($subResults) {
while ($result = mysql_fetch_object($subResults)) {
$cid = $arCat[$result->postcat_cat_ID];
$wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ('$id', '$cid')");
$cntCat = $cntCat + 1;
$cntTmp = $cntTmp + 1;
}
}
if ($cntTmp == 0) {
// No categories defined in b2evo - put it in the default category
$wpdb->query("INSERT INTO $wpdb->post2cat (post_id, category_id) VALUES ('$id', '1')");
$cntCat = $cntCat + 1;
}
}
echo "$cnt entry record(s) imported!
";
echo "$cntCom comment record(s) imported!
";
echo "$cntCat entry category record(s) imported!
";
} else {
echo "No entry records found!
";
}
mysql_close($resB2);
echo "That's all folks!";
break;
}
?>