<?php
//
// b2evoTOwp.php V0.3 (ex migrate.php :-)
// b2evolution -> wordpress migrator
//
// This script was originally found on (http://www.nocblog.com/software/2006/01/23/migrator-b2evolution-wordpress/)
//
// Created by Justin Mazzi (http://r00tshell.com)
// Modified by TuMahler (http://www.tumahler.com) 4/3/2006
// Bugfixed and extended by Sebastian Dietzold (http://sebastian.dietzold.de) 27/8/2006/
//
// 4/3/2006
//   Updated to allow migration from B2evolution version 0.9.0.12 to Wordpress 2.02
//   Also added transfer of comments and fixed a bug in the creation of posts in multiple categories
// 29/8/2006
//   updated to convert all category relations
//   fixed for correct utf-8 handling
//   used to migrate from B2evolution version 0.9.0.11 to Wordpress 2.04
//
// *** WARNING ***
// This will wipe out your wordpress posts, categories etc before importing your
// b2evolution blog.
//
// *** WARNING ***
// 1) Install wordpress in the same db as your b2evolution blog.
// 2) Edit the connection info below (hostname, username, password, database)
// 3) Run migrate.php (this script)
// 4) Check over your posts, users, categories etc.
// 5) Enjoy your new wordpress blog
//


# CHANGE THIS
mysql_connect("localhost","","");
mysql_select_db("wordpress");


print 
"<pre>";

print 
"Removing Comments\n";
mysql_query("DELETE FROM wp_comments");
mysql_query("ALTER TABLE wp_comments AUTO_INCREMENT=1");

print 
"Removing Posts\n";
mysql_query("DELETE FROM wp_posts");
mysql_query("ALTER TABLE wp_posts AUTO_INCREMENT=1");

print 
"Removing Categories\n";
mysql_query("DELETE FROM wp_categories");
mysql_query("ALTER TABLE wp_categories AUTO_INCREMENT=1");

print 
"Removing Users\n";
mysql_query("DELETE FROM wp_users");

print 
"Removing User Meta date\n";
mysql_query("DELETE FROM wp_usermeta");

print 
"Remove Post 2 cat\n";
mysql_query("DELETE FROM wp_post2cat");
mysql_query("ALTER TABLE wp_post2cat AUTO_INCREMENT=1");

print 
"\n";

$query mysql_query("SELECT ID, user_login, user_pass, user_nickname as user_nicename, user_email, user_url, dateYMDhour as user_registered, concat(user_firstname, ' ', user_lastname) AS display_name FROM evo_users");
if(
mysql_error()) {
    print 
mysql_error() . "\n";
}

print 
"Processing Users And Users metadata\n\n";
while(
$i mysql_fetch_array($query)) {
    
$query2 "INSERT INTO wp_users ( ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, display_name, user_status) ";
    
$query2 .= "VALUES ( '$i[ID]','$i[user_login]', '$i[user_pass]', '$i[user_nicename]', '$i[user_email]', '$i[user_url]', '$i[user_registered]', '$i[display_name]', '0')";
     
mysql_query($query2);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }

    
$query3 "INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES ('$i[ID]', 'wp_user_level', '10')";
    
mysql_query($query3);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }

    
$query4 "INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES ('$i[ID]', 'wp_capabilities', 'a:1:{s:13:\"administrator\";b:1;}')";
    
mysql_query($query4);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }

    
$query5 "INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES ('$i[ID]', 'nickname', '$i[display_name]')";
    
mysql_query($query5);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }

 
$query6 "INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES ('$i[ID]', 'rich_editing', 'true')";
    
mysql_query($query6);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }
 print 
"Importing: $i[user_login]\n";
}
print 
"\n";

print 
"Processing Categories\n\n";
$query mysql_query("SELECT cat_ID, cat_name, cat_description AS category_description, cat_parent_ID as category_parent FROM evo_categories");
if(
mysql_error()) {
    print 
mysql_error() . "\n";
}

while(
$i mysql_fetch_array($query)) {
    
$nicename str_replace(" ""_"$i['cat_name']);
    
$nicename str_replace("&""and"$nicename);
    
$nicename strtolower($nicename);
 
    
$query2 "INSERT INTO wp_categories (cat_ID, cat_name, category_description, category_parent, category_nicename) VALUES ('$i[cat_ID]', '$i[cat_name]', '$i[category_description]', '$i[category_parent]', '$nicename')";
    
mysql_query($query2);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }
    print 
"Importing: $i[cat_name]\n";
}
print 
"\n";

print 
"Processing Posts\n\n";
$query mysql_query("SELECT ID, post_author, post_issue_date as post_date, post_issue_date, post_content, post_title, post_urltitle, post_category, post_status, post_title as post_name, post_mod_date as post_modified_gmt, post_mod_date as post_modified FROM evo_posts ORDER BY ID ASC");
if(
mysql_error()) {
    print 
mysql_error() . "\n";
}
while(
$i mysql_fetch_array($query)) {
    
//$i['post_content'] = mysql_escape_string($i['post_content']);
    
$queryComment mysql_query("SELECT count(*) as comment_count FROM `evo_comments` WHERE comment_post_ID=$i[ID]");
    
$j mysql_fetch_array($queryComment);
    foreach(
$i as $key=>$val) {
        if (
$key == "post_content")
        {
            
$i[$key] = mysql_escape_string($val);
        }
        else
        {
            
$i[$key] = mysql_escape_string($val);
        }
    }
    
$i[post_content] = mb_convert_encoding($i[post_content],"UTF-8");
    
$i[post_content] = str_replace('\r\n' ,'\n'$i[post_content]);
    
$i[post_name] = mb_convert_encoding($i[post_name],"UTF-8");
    
#$i[post_content] = str_replace('\n' ,'', $i[post_content]);
    //$i[post_content] = $i[post_content];
    //$i[post_content] = preg_replace('/\\r\\n|\\r|\\n|\\n\\r/', 'hello', $i[post_content]);
    //$i[post_content] = preg_replace('/\\r\\n/', 'hello', $i[post_content]);
    //$i[post_content] = preg_replace('/\\n/', '', $i[post_content]);
    
print "Post_content: $i[post_content]\n";
    
$i[post_urltitle] = str_replace("aamp""and"$i[post_urltitle]);
    if(empty(
$i[post_name])) $i[post_name] = $i[post_urltitle];
 
    
$query2 "INSERT INTO wp_posts (ID, post_author, post_date, post_content, post_title, post_category, post_status, post_name,"
    
$query2 .= "post_modified_gmt, post_modified, comment_count) VALUES ('$i[ID]', '$i[post_author]', '$i[post_date]', '$i[post_content]', '$i[post_name]',";
    
$query2 .= "'$i[post_category]', 'publish', '$i[post_urltitle]', '$i[post_modified]', '$i[post_modified]', '$j[comment_count]')";
    
//print $query2 . "\n";
    
mysql_query($query2);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }

    print 
"Importing: $i[post_name] | $i[post_urltitle]\n";
    
$j++;
}
mysql_query("ALTER TABLE wp_posts AUTO_INCREMENT=206");
print 
"\n";

print 
"Processing Comments\n\n";
$query mysql_query("SELECT comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_karma FROM evo_comments");
if(
mysql_error()) {
    print 
mysql_error() . "\n";
}

while(
$i mysql_fetch_array($query)) {
    foreach(
$i as $key=>$val) {
        
$i[$key] = mysql_escape_string($val);
    }
    
$i[comment_content] = mb_convert_encoding($i[comment_content],"UTF-8");
    
$i[comment_author] = mb_convert_encoding($i[comment_author],"UTF-8");
    
$query2 "INSERT INTO wp_comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content, comment_karma) VALUES ('$i[comment_post_ID]', '$i[comment_author]', '$i[comment_author_email]', '$i[comment_author_url]', '$i[comment_author_IP]', '$i[comment_date]', '$i[comment_content]', '$i[comment_karma]')";
    
mysql_query($query2);
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }
}
print 
"\n";


print 
"Filling category post relations\n\n";
$query mysql_query("SELECT ID, post_category FROM wp_posts");
while(
$i mysql_fetch_array($query)) {
    
#mysql_query("INSERT INTO wp_post2cat (post_id, category_id) VALUES ('$i[ID]','$i[post_category]')");
    
$catQuery mysql_query("SELECT postcat_post_ID as post_id , postcat_cat_ID as category_id FROM evo_postcats WHERE postcat_post_ID='$i[ID]'");
    while(
$cats mysql_fetch_array($catQuery)) {
        
mysql_query("INSERT INTO wp_post2cat (post_id, category_id) VALUES ('$i[ID]','$cats[category_id]')");
    }
}

print 
"Processing Category Counts\n\n";
$query mysql_query("SELECT * FROM wp_categories");
if(
mysql_error()) {
    print 
mysql_error() . "\n";
}

while(
$i mysql_fetch_array($query)) {
    
#$query2 = mysql_query("SELECT count(*) FROM wp_posts WHERE post_category = $i[cat_ID]");
    
$query2 mysql_query("SELECT count(*) FROM wp_post2cat WHERE category_id = $i[cat_ID]");
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }
    
$result mysql_fetch_array($query2);
    
mysql_query("UPDATE wp_categories set category_count = '$result[0]' WHERE cat_ID = '$i[cat_ID]'");
    if(
mysql_error()) {
        print 
mysql_error() . "\n";
    }
    print 
"$i[cat_name]: $result[0]\n";
}

?>