Import b2evolution blog to wordpress

A few months ago I decided that it’s time to say goodbye to b2evolution. I liked the platform, and I’ve been using it for almost 5 years, but development seemed to have stalled, and while I compensated the stillstand for a while by developing my own tools, I grew tired of the fact that everything I wanted is just a click away in WordPress.

All I had to do now was to find a way to feed my b2evolution data into the WordPress scheme. It took a good while until I found someone with a similar task who was willing to share. And to my shame, I have to admit that I don’t know anymore where I got the script from. It proved to be broken anyway, probably because it wasn’t up to date. So I based my importing script on that script, and it worked well. Needless to say that there’s no guarantee that this will still work. And you should (in any case) be sure that you have backed up everything.

What you will need for this setup:

1. A running b2evolution installation
2. A running WordPress installation
3. WordPress Counterize Plugin
4. Database access parameters (see first 20 lines in script)

The script could look nicer, but I wanted to limit it to just one file, so this is what I came up with (again, based on someone else’s work):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
#variables that require your attention: b2evolution

$b2_db = 'B2EVOLUTION_DATABASE';
$b2_usr = 'B2EVOLUTION_DATABASE_USER';
$b2_pwd = 'B2EVOLUTION_DATABASE_PASSWORD';
$b2_host = 'localhost';	//usually ok as is
$b2_blog = 'B2EVOLUTION_BLOG_NUMBER';
$b2_startfrom = '1'; // put in number from your first proper blog entry
$b2_oldimagepath = 'path/to/image/folder';	//ignore if images and other media files will stay in same directory
$b2_newimagepath = 'path/to/image/folder';	//ignore if images and other media files will stay in same directory
 
#variables that require your attention: wordpress

$wp_db = 'WORDPRESS_DATABASE';
$wp_usr = 'WORDPRESS_DATABASE_USER';
$wp_pwd = 'WORDPRESS_DATABASE_PASSWORD';
$wp_host = 'localhost';	//usually ok as is
$wp_path = '/path/to/wp/'; //don't forget first and trailing slash
$wp_pref = 'wp_'; //set when installing Wordpress. Default is wp_ - change if you aren't using the default
 
 
 
// connect to the b2evo database
$resB2 = mysql_connect($b2_host,$b2_usr,$b2_pwd);
if (!$resB2) {
	exit("Connection failed! host: $b2_host, user: $b2_usr, pass: $b2_pwd");
}
if (!mysql_select_db($b2_db,$resB2)) {
	exit("Couldn't select database: $b2_db");
}            
// connect to the WP database
$resWP = mysql_connect($wp_host,$wp_usr,$wp_pwd,TRUE);
if (!$resWP) {
	exit("Connection failed! host: $wp_host, user: $wp_usr, pass: $wp_pwd");
}
if (!mysql_select_db($wp_db,$resWP)) {
	exit("Couldn't select database: $wp_db");
}
 
 
$arUser = array();
$arCat = array();
$arUser[1] = 1;
 
// get authors for blog
echo "Importing User records ... <BR />";
$sql = "SELECT DISTINCT evo_users.* 
		FROM evo_users, evo_posts 
		WHERE evo_users.user_ID=post_creator_user_ID 
		AND user_login<>'admin'";
$result = mysql_query($sql,$resB2) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
if ($result) {
	$cnt = 0;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$sql = trim(str_replace("\n","","INSERT INTO `". $wp_pref ."users` ".
			"(`user_login`,`user_pass`,`user_firstname`,`user_lastname`,`user_nickname`,`user_icq`," .
			"`user_email`,`user_url`,`user_ip`,`user_domain`,`user_browser`,`dateYMDhour`,`user_level`," .
			"`user_aim`,`user_msn`,`user_yim`,`user_idmode`)" .
			" VALUES ('".$row['user_login']."','".$row['user_pass']."','".$row['user_firstname']."','".$row['user_lastname']."','".$row['user_nickname']."','".$row['user_icq']."','" .
			$row['user_email']."','".$row['user_url']."','".$row['user_ip']."','".$row['user_domain']."','".$row['user_browser']."','".$row['dateYMDhour']."','".$row['user_level']."','".
			$row['user_aim']."','".$row['user_msn']."','".$row['user_yim']."','".$row['user_idmode']."');"));
		$q = mysql_query($sql, $resWP) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
		$id = mysql_insert_id($resWP);
		$arUser[$row['ID']] = $id;
		$cnt = $cnt + 1;
	}
	echo $cnt . " User record(s) imported! <BR />";
} else {
	echo "No User records found!<BR />";
}
 
 
// get categories
echo "Importing Category records ... <BR />";
$sql = "SELECT * FROM evo_categories";
$result = mysql_query($sql,$resB2) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
if ($result) {
	$cnt = 0;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$sql = trim(str_replace("\n","","INSERT INTO `". $wp_pref ."categories` ".
			"(`cat_name`,`category_nicename`,`category_description`)" .
			" VALUES ('". mysql_escape_string($row['cat_name']) ."','".mysql_escape_string($row['cat_description'])."','".mysql_escape_string($row['cat_longdesc'])."');"));
		$q = mysql_query($sql, $resWP) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
		$id = mysql_insert_id($resWP);
		$arCat[$row['cat_ID']] = $id;
		$cnt = $cnt + 1;
	}
	echo $cnt . " Category record(s) imported! <BR />";
} else {
	echo "No Category records found!<BR />";
}
 
 
// get entries for blog
echo "Importing Entry records ... <BR />";
$sql = "SELECT * 
		FROM evo_posts 
		WHERE post_ID>".$b2_startfrom;
$result = mysql_query($sql,$resB2) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
if ($result) {
	$cnt = 0;
	$cntCom = 0;
	$cntCat = 0;
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		// author ID must be switched to new author ID
		$aid = $arUser[$row['post_author']];
		// category ID must be switched to new category ID
		$cid = $arCat[$row['post_category']];
		if (!$cid) {
			$cid = '1';
		}
 
		//change date to GMT:
		$posttime = strtotime($row['post_datestart']);
		$gwmt = $posttime-(9*60*60);
		$gwmtimestring = date("Y-m-d H:i:s",$gwmt);
 
		//change path for images
		$row['post_content'] = $b2_newimagepath!=$b2_oldimagepath ? str_replace($b2_oldimagepath,$b2_newimagepath,$row['post_content']) : $row['post_content'];
 
		$sql = trim(str_replace("\n","","INSERT INTO `". $wp_pref ."posts` ".
			"(post_author,post_date,post_modified,post_date_gmt,post_modified_gmt,post_content,post_title,post_name)" .
			" VALUES ('1','".$row['post_datestart']."','".$row['post_datestart']."','".$gwmtimestring."','".$gwmtimestring."','".mysql_escape_string($row['post_content'])."','".mysql_escape_string($row['post_title'])."','" .mysql_escape_string($row['post_urltitle'])."');"));
		$q = mysql_query($sql, $resWP) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
		$id = mysql_insert_id($resWP);
 
		//lets add the page views, given that we have "Counterize Pages" installed...
		$formattime = explode(" ",$row['post_datecreated']);
		$formattime2 = explode("-",$formattime[0]);
		$urlforwp = $wp_path.$formattime2[0]."/".$formattime2[1]."/".$formattime2[2]."/".$row['post_urltitle']."/";
		$sql = "INSERT INTO ".$wp_pref."Counterize_Pages (url,count,postID) VALUES 
						('".$urlforwp ."','".$row['post_views']."','".$id."');";
		$q = @mysql_query($sql, $resWP);
		//page views add end
 
 
		$eid = $row['post_ID'];
		$cnt = $cnt + 1;
		// get comments for entry
		$sql = "SELECT * 
				FROM evo_comments WHERE comment_post_ID='" . $eid."'";
		$res = mysql_query($sql, $resB2) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
 
		echo "$sql <br />";
 
		if ($res) {
			while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
				$sql = trim(str_replace("\n","","INSERT INTO `". $wp_pref ."comments` ".
					"(`comment_post_ID`,`comment_author`,`comment_author_email`," .
					"`comment_author_url`,`comment_author_IP`,`comment_date`," .
					"`comment_content`,`comment_karma`)" .
					" VALUES ('".$id."','" . mysql_escape_string($row['comment_author']) . "','" . mysql_escape_string($row['comment_author_email']) .
					"','" . mysql_escape_string($row['comment_author_url']) . "','" . $row['comment_author_IP'] . "','" . $row['comment_date'] .
					"','" . mysql_escape_string($row['comment_content'])."','". $row['comment_karma'] ."');"));
				$q = mysql_query($sql, $resWP) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
				$cntCom = $cntCom + 1;
			}
		}
		// get categories for entry
 
		$cntTmp = 0;
		$sql = "SELECT * FROM evo_postcats WHERE postcat_post_ID=" . $eid;
		$res = mysql_query($sql, $resB2) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
		if ($res) {
			while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
				$cid = $arCat[$row['postcat_cat_ID']];
				$sql = trim(str_replace("\n","","INSERT INTO `". $wp_pref ."post2cat` ".
					"(`post_id`,`category_id`)" .
					" VALUES ('" . $id . "','" . $cid . "');"));
				$q = mysql_query($sql, $resWP) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
				$cntCat = $cntCat + 1;
				$cntTmp = $cntTmp + 1;
			}
		}
		if ($cntTmp == 0) {
			// No categories defined in b2evo - put it in the default category
			$sql = trim(str_replace("\n","","INSERT INTO `". $wp_pref ."post2cat` ".
				"(`post_id`,`category_id`)" .
				" VALUES ('" . $id . "','1');"));
			$q = mysql_query($sql, $resWP) or die("Invalid query: " . mysql_error() . "<BR /> SQL : " . $sql);
			$cntCat = $cntCat + 1;
		}
	}
 
 
	//now put up comments count
$sql = "SELECT ID FROM ".$wp_pref."posts";
$result = mysql_query($sql, $resWP);
 
 if ($result) {
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$sql2 = "SELECT COUNT(*) 
				 FROM ".$wp_pref."comments 
				 WHERE comment_post_ID = '".$row['ID']."' 
				 AND comment_approved = '1'";
		$result2 = mysql_query($sql2, $resWP);
		$row2 = mysql_fetch_row($result2);
		echo $row2[0];
 
		if($row2[0]>0) {
			$sql3 = "UPDATE ".$wp_pref."posts 
					 SET comment_count='".$row2[0]."' 
					 WHERE ID='".$row['ID']."'";
			$result3 = mysql_query($sql3, $resWP);
				}
 
		}
 }
 
	echo $cnt . " Entry record(s) imported! <BR />";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $cntCom . "Comment record(s) imported! <BR />";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $cntCat . "Entry Category record(s) imported! <BR />";
} else {
	echo "No Entry records found!<BR />";
}
mysql_close($resB2);
mysql_close($resWP);
echo "Finished";
?>

Post a Comment

You must be logged in to post a comment.