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) // 2b) For wpmu create the blogs and the user first // 3) Run migrate.php (this script) // 4) Check over your posts, users, categories etc. // 5) Enjoy your new wordpress blog // ************************************************************************* // CONFIG CHANGE THIS // type of wordpress single or multi $wpmu = true; // jlt: always true until the code is corrected again for wordpress single blog // blog categorie in b2evolution // import of one specific category (blog) from B2Evolution // in wpmu each blog has its tables: id ($b2_cat_blog_ID) -> blog ('wp_i_*') if ($_GET['b2_blog_ID']) { $b2_blog_ID = $_GET['b2_blog_ID']; } else { $b2_blog_ID = 21; } if ($_GET['wpmu_t_prefix']) { $wpmu_t_prefix = $_GET['wpmu_t_prefix']; } else { $wpmu_t_prefix = 'wp_2_'; } if ($_GET['wpmu_t_authorid']) { $wpmu_t_authorid = $_GET['wpmu_t_authorid']; } else { $wpmu_t_authorid = '2'; } // status matching $b2Towpmu_status ['published'] = 'publish'; $b2Towpmu_status ['protected'] = 'private'; $b2Towpmu_status ['private'] = 'private'; $b2Towpmu_status ['draft'] = 'draft'; mysql_connect("server", "user", "pwd"); mysql_select_db("db"); // CONFIG END print "
";

print "Removing Comments\n";
mysql_query("DELETE FROM " . $wpmu_t_prefix . "comments");
mysql_query("ALTER TABLE " . $wpmu_t_prefix . "comments AUTO_INCREMENT=1");

print "Removing Posts\n";
mysql_query("DELETE FROM " . $wpmu_t_prefix . "posts");
mysql_query("ALTER TABLE " . $wpmu_t_prefix . "posts AUTO_INCREMENT=1");

if (!$wpmu) {
    print "Removing Categories\n";
    mysql_query("DELETE FROM " . $wpmu_t_prefix . "categories");
    mysql_query("ALTER TABLE " . $wpmu_t_prefix . "categories AUTO_INCREMENT=1");

    print "Removing Users\n";
    mysql_query("DELETE FROM " . $wpmu_t_prefix . "users");

    print "Removing User Meta date\n";
    mysql_query("DELETE FROM " . $wpmu_t_prefix . "usermeta");
}
print "Remove Post 2 cat\n";
mysql_query("DELETE FROM " . $wpmu_t_prefix . "post2cat");
mysql_query("ALTER TABLE " . $wpmu_t_prefix . "post2cat AUTO_INCREMENT=1");

print "\n";
// no users in wp_i, users are managed in wpmu main tables
if (!$wpmu) {
    $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 " . $wpmu_t_prefix . "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 " . $wpmu_t_prefix . "usermeta (user_id, meta_key, meta_value) VALUES ('$i[ID]', '" . $wpmu_t_prefix . "user_level', '10')";
        mysql_query($query3);
        if (mysql_error()) {
            print mysql_error() . "\n";
        }
        $query4 = "INSERT INTO " . $wpmu_t_prefix . "usermeta (user_id, meta_key, meta_value) VALUES ('$i[ID]', '" . $wpmu_t_prefix . "capabilities', 'a:1:{s:13:\"administrator\";b:1;}')";
        mysql_query($query4);
        if (mysql_error()) {
            print mysql_error() . "\n";
        }

        $query5 = "INSERT INTO " . $wpmu_t_prefix . "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 " . $wpmu_t_prefix . "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";
    }
} // end if (!$wpmu)
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 WHERE cat_blog_ID=$b2_blog_ID");
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);
    // nte jlt filter categories from the specific blog
    $b2_blog_categories[] = $i['cat_ID'];
    print 'b2 categories: ' . $i['cat_ID'];

    $query2 = "INSERT INTO " . $wpmu_t_prefix . "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";

foreach($b2_blog_categories as $b2_cat) {
    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 WHERE post_category=$b2_cat 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];
        // matching categories
        $cattt = $i[post_category];
        $wpmuCat = $b2Towpmu_t_catid[$cattt];
        // matching post_status b2->wpmu
        $post_status = $b2Towpmu_status["$i[post_status]"]?$b2Towpmu_status["$i[post_status]"]:'publish';

        $query2 = "INSERT INTO " . $wpmu_t_prefix . "posts (ID, post_author, post_date, post_date_gmt, post_content, post_title, post_category, post_status, post_name,";
        $query2 .= "post_modified_gmt, post_modified, comment_count) VALUES ('$i[ID]', '$wpmu_t_authorid', '$i[post_date]', '$i[post_date]', '$i[post_content]', '$i[post_name]',";
        $query2 .= "'$i[post_category]', '$post_status', '$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 " . $wpmu_t_prefix . "posts");
    // mysql_query("ALTER TABLE ".$wpmu_t_prefix."posts AUTO_INCREMENT=206");
    print "\n";

    print "Processing Comments\n\n";
    if ($wpmu) {
        $query = mysql_query("SELECT evo_comments.comment_post_ID, evo_comments.comment_author, evo_comments.comment_author_email, evo_comments.comment_author_url, evo_comments.comment_author_IP, evo_comments.comment_date, evo_comments.comment_content, evo_comments.comment_karma FROM evo_comments, evo_posts WHERE evo_comments.comment_post_ID = evo_posts.ID AND evo_posts.post_category = $b2_cat");
    } else {
        $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 " . $wpmu_t_prefix . "comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, 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_date]', '$i[comment_content]', '$i[comment_karma]')";
        mysql_query($query2);
        if (mysql_error()) {
            print mysql_error() . "\n";
        }
    }
} // end foreach for b2 categories
print "\n";

print "Filling category post relations\n\n";
$query = mysql_query("SELECT ID, post_category FROM " . $wpmu_t_prefix . "posts");
while ($i = mysql_fetch_array($query)) {
    mysql_query("INSERT INTO " . $wpmu_t_prefix . "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 " . $wpmu_t_prefix . "post2cat (post_id, category_id) VALUES ('$i[ID]','$cats[category_id]')");
    }
}

print "Processing Category Counts\n\n";
$query = mysql_query("SELECT * FROM " . $wpmu_t_prefix . "categories");
if (mysql_error()) {
    print mysql_error() . "\n";
} while ($i = mysql_fetch_array($query)) {
    if ($wpmu) {
        $query2 = mysql_query("SELECT count(*) FROM " . $wpmu_t_prefix . "post2cat WHERE category_id = $i[cat_ID]");
    } else {
        // $query2 = mysql_query("SELECT count(*) FROM ".$wpmu_t_prefix."posts WHERE post_category = $i[cat_ID]");
    }

    if (mysql_error()) {
        print mysql_error() . "\n";
    }
    $result = mysql_fetch_array($query2);
    mysql_query("UPDATE " . $wpmu_t_prefix . "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";
}

?>