Import Scripts Converting VBCMS Articles to WordPress

Home
Uncategorized
Import Scripts Converting VBCMS Articles to Wordpress

Import Scripts Converting VBCMS Articles to WordPress

There are no tags

This post is part of my description of the migration from VBulletin CMS to WordPress.

As with the rest of the import, this is not for the non-technical. You will need to be able to look in the database, check and cure errors, modify my scripts, deal with regular expressions and perhaps change PHP parameters (execution time and so on), and generally fiddle at a level that needs a good working knowledge of basic PHP programming and MySQL.

Include script (converter_inc.php)

Complete the details for your VBulletin database at the top. Note that table prefix is not included as a variable. You may need to change the prefix manually in other import scripts. Call the script converter_inc.php and put it in your WordPress route directory.

The bbcode_to_html function is taken from the internet, and is shown in various places, but I can’t find the original author to give credit. I have not VBulletin-ized it by stripping out non-VBulletin tags, but rather just changed and added as I needed for the contents of my articles. You will need to edit the [attachment] lines with your own VBulletin url – the WordPress CMS will continue to use the VBulletin attachments. Then after running the import scripts browse through your articles and look for problems and correct them by modifying the function.

I would note that you might not want to convert your BB tags. There are WordPress plugins that allow the use of BB tags and may therefore allow you to leave your articles exactly as they are. I didn’t try this route as I didn’t want to use a plugin of that sort where not strictly necessary.

<?php
$DBHost = '';
$DBUser = '';
$DBPassword = '';
$DBName = '';

function bbcode_to_html($bbtext){
        $bbextended = array(
                        "/\[font=(.*?)\](.*?)\[\/font\]/i" => "$2",
                        "/\[size=(.*?)\](.*?)\[\/size\]/i" => "$2",
                        "/\[url](.*?)\[\/url]/i" => "<a href=\"http://$1\" title=\"$1\">$1</a>",
                        "/\[url=(.*?)\](.*?)\[\/url\]/i" => "<a href=\"$1\" title=\"$1\">$2</a>",
                        "/\[email=(.*?)\](.*?)\[\/email\]/i" => "<a href=\"mailto:$1\">$2</a>",
                        "/\[mail=(.*?)\](.*?)\[\/mail\]/i" => "<a href=\"mailto:$1\">$2</a>",
                        "/\[img\]([^[]*)\[\/img\]/i" => "<img src=\"$1\" alt=\" \" />",
                        "/\[image\]([^[]*)\[\/image\]/i" => "<img src=\"$1\" alt=\" \" />",
                        "/\[image_left\]([^[]*)\[\/image_left\]/i" => "<img src=\"$1\" alt=\" \" class=\"img_left\" />",
                        "/\[image_right\]([^[]*)\[\/image_right\]/i" => "<img src=\"$1\" alt=\" \" class=\"img_right\" />",
                        "/\[color=(.*?)\](.*?)\[\/color\]/i" => "<span style=\"color:$1\">$2</span>",
                        "/\[attach=(.*?)\](.*?)\[\/attach\]/i" => "<img src=\"http://www.yourforum.com/attachment.php?attachmentid=$2\" alt=\" \" />",
                        "/\[attach\](.*?)\[\/attach\]/i" => "<img src=\"http://www.yourforum.com/attachment.php?attachmentid=$1\" alt=\" \" />",

        );
        foreach($bbextended as $match=>$replacement){
                $bbtext = preg_replace($match, $replacement, $bbtext);
        }

        $bbtags = array(
                        '[prbreak][/prbreak]' => '<!--more-->',
                        '[QUOTE=;][/QUOTE]' => '',
                        '[heading1]' => '<h1>','[/heading1]' => '</h1>',
                        '[heading2]' => '<h2>','[/heading2]' => '</h2>',
                        '[heading3]' => '<h3>','[/heading3]' => '</h3>',
                        '
			

' => '<h1>','

' => '</h1>', '

' => '<h2>','

' => '</h2>', '

' => '<h3>','

' => '</h3>', '[paragraph]' => '<p>','[/paragraph]' => '</p>', '[para]' => '<p>','[/para]' => '</p>', '[p]' => '<p>','[/p]' => '</p>', '[left]' => '<p style="text-align:left;">','[/left]' => '</p>', '[right]' => '<p style="text-align:right;">','[/right]' => '</p>', '[center]' => '<p style="text-align:center;">','[/center]' => '</p>', '[justify]' => '<p style="text-align:justify;">','[/justify]' => '</p>', '[bold]' => '<span style="font-weight:bold;">','[/bold]' => '</span>', '[italic]' => '<span style="font-weight:bold;">','[/italic]' => '</span>', '[underline]' => '<span style="text-decoration:underline;">','[/underline]' => '</span>', '[b]' => '<span style="font-weight:bold;">','[/b]' => '</span>', '[i]' => '<span style="font-weight:bold;">','[/i]' => '</span>', '[u]' => '<span style="text-decoration:underline;">','[/u]' => '</span>', '[newline]' => '<br>', '[nl]' => '<br>', '[unordered_list]' => '<ul>','[/unordered_list]' => '</ul>', '[list]' => '<ul>','[/list]' => '</ul>', '[ul]' => '<ul>','[/ul]' => '</ul>', '[ordered_list]' => '<ol>','[/ordered_list]' => '</ol>', '[ol]' => '<ol>','[/ol]' => '</ol>', '[*]' => '<li>','[/*]' => '</li>', '""' => '"', ); $bbtext = str_ireplace(array_keys($bbtags), array_values($bbtags), $bbtext); return $bbtext; } ?>
Script 1 – Importing Articles

This is the big one. Before you use it, create your destination categories in WordPress and note their ‘slugs’ (the bit that appears in the SEO URL). You will convert articles from each VBulletin Section ID to the corresponding category in WordPress. VBulletin categories will be converted to tags in WordPress, which will be created automatically by the script. Edit the script near the top where the VBulletin ID and slug are set (see comments in code).

Users with characters such as ? in their username may cause problems. WordPress will strip out the character and the username will be incorrect, and the script will throw an error. You can change usernames to remove special characters, then try to reimport. If this will affect lots of users, then consider using this WordPress plugin before import (not tested).

If you table prefix is not vb_ then you will need to edit the queries.

Users are created by the script as needed.

Call the script converter.php and upload it to your WordPress root. Run it in your browser (www.yourwpsite.com/converter.php) and see what happens. You will probably end up with trial and error imports and to ‘reset’ after each one I suggest installing this bulk delete plugin.

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require("./converter_inc.php");
require('./wp-load.php');
echo "done the includes";
$conn =	mysql_connect($DBHost,$DBUser,$DBPassword);
if (!$conn) {
	die('Could not connect: ' . mysql_error());
}
mysql_select_db($DBName,$conn);
mysql_query("SET character_set_results=utf8", $conn);

//enter your section node ID from VBulletin, and your WordPress target category slug.  
// the node id is the number in the URL when you browse to the section concerned in VB.
$vb_parent_node = 251; 
// the slug is set manually or created automatically when you create a WordPress category.
$catname = "Books";
$post_categories = array(get_cat_ID($catname));
//print_r($post_categories);
if (empty($post_categories)) {
	echo "error - no WP category found for ". $catname;
	exit;
}

echo $query = "SELECT n.nodeid,i.title, pagetext,FROM_UNIXTIME(n.publishdate,'%Y-%m-%d %H:%i:%s') as publishdate, FROM_UNIXTIME(n.lastupdated,'%Y-%m-%d %H:%i:%s') as lastupdated, u.username,u.email,n.setpublish,n.comments_enabled FROM vb_cms_article a join vb_cms_node n on a.contentid = n.contentid join vb_cms_nodeinfo i on i.nodeid = n.nodeid join vb_user u on n.userid = u.userid where n.parentnode = $vb_parent_node and n.contenttypeid = 18";
$results = mysql_query($query);
if (!$results) {
	die('query error: ' . mysql_error());
}
$i = 0;
while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) {
	echo "got a result";
	$post = array();
	// publish state needs consideration.  WP has more states than VB.  
	// here we put unpublished posts in draft.  I can delete afterwards
	$post['post_status'] = $row['setpublish'] == 1 ? 'publish':'draft';
	// in the UK, so gmt used for all from VB's unix timestamp
	$post['post_date'] = $post['post_date_gmt'] = $row['publishdate'];
	$post['post_modified'] = $post['post_modified_gmt'] = $row['lastupdated'];
	$post['post_title'] = $row['title'];
	$post['post_content'] = bbcode_to_html(preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]",$row['pagetext']));
	$post['post_type'] = 'post';
	$post['import_id']=$row['nodeid'];
	$post['comment_status'] = $row['comments_enabled'] == 1 ? 'open': 'closed';
	$post['ping_status'] = 'closed';
	// VB articles have authors and users (as userids).  In my case always use the userid.
	$author = $row['username'];
	// create a WP user if necessary
	if (!$wp_user_id = username_exists($author)) {	
	if (!email_exists($row['email'])) {
		$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
		$wp_user_id = wp_create_user($author, $random_password, $row['email']);
		echo "<br/>creating user $author with email address" . $row['email']; 
	} else {
		echo "<br/>Error: User $author email exists, but not username - cannot create user in WP - exit";
		exit;
	}
	}
	$post['post_author'] = $wp_user_id;
	
	$post_ID = wp_insert_post($post,true);
	if (is_wp_error($post_ID)) {
		echo "<br/>post " . $row['title'] . "could not be inserted";
		echo '<pre>';
		print_r ($wp_error);
		echo '</pre>';
	}
	else { 
		echo "<br/>post " . $row['title'] . " was inserted and is ID $postID"; 
		wp_set_post_categories($post_ID,$post_categories); 
		$tags_query = "SELECT c.category FROM vb_cms_category c JOIN vb_cms_nodecategory nc on c.categoryid = nc.categoryid where nc.nodeid = " . $row['nodeid'];
		$tags_results = mysql_query($tags_query);
		if (!$tags_results) {
			echo "no tags for this post";
		}
		else {
			while ($tags_row = mysql_fetch_array($tags_results,MYSQL_ASSOC)) {
				wp_set_post_tags($post_ID, $tags_row['category'], true );
				echo "<br/>setting tag " . $tags_row['category'];
			}
		}
		
	}
	$i++;
		
}
?>
Script 2 – Comments Converter

Most of the comments above are also valid for this. You should run this after all articles are successfully imported.

Check through the script and make any modifications needed, then call the script what you like, upload it to your WP root and run it in your browser.

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require("./converter_inc.php");
require('./wp-load.php');

$conn =	mysql_connect($DBHost,$DBUser,$DBPassword);
if (!$conn) {
	die('Could not connect: ' . mysql_error());
}
mysql_select_db($DBName,$conn);
mysql_query("SET character_set_results=utf8", $conn);

$args = array('posts_per_page' => 2000);
$posts_array = get_posts($args);

foreach ($posts_array as $post) {
	echo "<hr/>";
	$ID = $post->ID;
	echo ":nbsp;";

	echo $query = "SELECT p.username,u.email,p.ipaddress,p.pagetext, FROM_UNIXTIME(p.dateline,'%Y-%m-%d %H:%i:%s') as commentdate FROM vb_post p join vb_user u on p.userid = u.userid where parentid = (select p2.postid from vb_post p2 join vb_cms_nodeinfo ni on p2.threadid = ni.associatedthreadid where ni.nodeid = " . $ID .  " and p2.parentid=0)";
	$results = mysql_query($query);
	if (!$results) {
		die('query error: ' . mysql_error());
	}
	$i = 0;
	while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) {
		$commentdata = array();
		$commentdata['comment_post_ID'] = $ID;
		$commentdata['comment_approved'] = 1;
		$commentdata['comment_date'] = $row['commentdate'];
		$commentdata['comment_content'] = bbcode_to_html(preg_replace("/\[(.*?)\]\s*(.*?)\s*\[\/(.*?)\]/", "[$1]$2[/$3]",$row['pagetext']));
		$commentdata['comment_type'] = '';
		$commentdata['comment_parent'] = 0;
		
		$author = $row['username'];
		// create a WP user if necessary
		if (!$wp_user_id = username_exists($author)) {	
			if (!email_exists($row['email'])) {
				$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
				$wp_user_id = wp_create_user($author, $random_password, $row['email']);
				echo "<br/>creating user $author with email address" . $row['email']; 
			} else {
				echo "<br/>Error: User $author email exists, but not username - cannot create user in WP - exit";
				break;
			}
		}
		$commentdata['comment_author'] = $row['username'];
		$commentdata['comment_author_email'] = $row['email'];
		$commentdata['comment_author_IP'] = $row['ipaddress'];
	
		$comment_ID = wp_insert_comment($commentdata);
		if (is_wp_error($comment_ID)) {
			echo "<br/>comment " . $ID . "could not be inserted";
			echo '<pre>';
			print_r ($wp_error);
			echo '</pre>';
		} 
		
	}
}
?>
More scripts

I used a few more scripts to do jobs specific to my CMS. For example one script strips out some review ratings that were manually inserted as images in a div call ‘rating’. Another script converted these ratings to custom fields in WordPress.

Following the model of the first two and BBCode to HTML script, plus the WordPress codex, you should be able to modify to suit your content.

Copyright 2013 Olive Net Ltd