3 Add These Functions to make our lives easier
5 if(!function_exists('get_catbynicename'))
7 function get_catbynicename($category_nicename)
11 $cat_id -= 0; // force numeric
12 $name = $wpdb->get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"');
18 if(!function_exists('get_comment_count'))
20 function get_comment_count($post_ID)
23 return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID);
27 if(!function_exists('link_exists'))
29 function link_exists($linkname)
32 return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"');
37 The Main Importer Class
39 class Textpattern_Import {
43 echo '<div class="wrap">';
44 echo '<h2>'.__('Import Textpattern').'</h2>';
45 echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
55 echo '<p>'.__('Howdy! This importer allows you to extract posts from any Textpattern 4.0.2+ into your blog. This has not been tested on previous versions of Textpattern. Mileage may vary.').'</p>';
56 echo '<p>'.__('Your Textpattern Configuration settings are as follows:').'</p>';
57 echo '<form action="admin.php?import=textpattern&step=1" method="post">';
59 echo '<input type="submit" name="submit" value="'.__('Import Categories').'" />';
63 function get_txp_cats()
66 // General Housekeeping
67 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
68 set_magic_quotes_runtime(0);
69 $prefix = get_option('tpre');
72 return $txpdb->get_results('SELECT
76 FROM '.$prefix.'txp_category
77 WHERE type = "article"',
81 function get_txp_users()
84 // General Housekeeping
85 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
86 set_magic_quotes_runtime(0);
87 $prefix = get_option('tpre');
91 return $txpdb->get_results('SELECT
97 FROM '.$prefix.'txp_users', ARRAY_A);
100 function get_txp_posts()
102 // General Housekeeping
103 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
104 set_magic_quotes_runtime(0);
105 $prefix = get_option('tpre');
108 return $txpdb->get_results('SELECT
122 FROM '.$prefix.'textpattern
126 function get_txp_comments()
129 // General Housekeeping
130 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
131 set_magic_quotes_runtime(0);
132 $prefix = get_option('tpre');
135 return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
138 function get_txp_links()
140 //General Housekeeping
141 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
142 set_magic_quotes_runtime(0);
143 $prefix = get_option('tpre');
145 return $txpdb->get_results('SELECT
152 FROM '.$prefix.'txp_link',
156 function cat2wp($categories='')
158 // General Housekeeping
161 $txpcat2wpcat = array();
163 if(is_array($categories))
165 echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
166 foreach ($categories as $category)
172 // Make Nice Variables
173 $name = $wpdb->escape($name);
174 $title = $wpdb->escape($title);
176 if($cinfo = category_exists($name))
178 $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
182 $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
184 $txpcat2wpcat[$id] = $ret_id;
187 // Store category translation for future use
188 add_option('txpcat2wpcat',$txpcat2wpcat);
189 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
192 echo __('No Categories to Import!');
196 function users2wp($users='')
198 // General Housekeeping
201 $txpid2wpid = array();
206 echo '<p>'.__('Importing Users...').'<br /><br /></p>';
207 foreach($users as $user)
212 // Make Nice Variables
213 $name = $wpdb->escape($name);
214 $RealName = $wpdb->escape($RealName);
216 if($uinfo = get_userdatabylogin($name))
219 $ret_id = wp_insert_user(array(
221 'user_login' => $name,
222 'user_nicename' => $RealName,
223 'user_email' => $email,
224 'user_url' => 'http://',
225 'display_name' => $name)
230 $ret_id = wp_insert_user(array(
231 'user_login' => $name,
232 'user_nicename' => $RealName,
233 'user_email' => $email,
234 'user_url' => 'http://',
235 'display_name' => $name)
238 $txpid2wpid[$user_id] = $ret_id;
240 // Set Textpattern-to-WordPress permissions translation
241 $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0');
243 // Update Usermeta Data
244 $user = new WP_User($ret_id);
245 if('10' == $transperms[$privs]) { $user->set_role('administrator'); }
246 if('9' == $transperms[$privs]) { $user->set_role('editor'); }
247 if('5' == $transperms[$privs]) { $user->set_role('editor'); }
248 if('4' == $transperms[$privs]) { $user->set_role('author'); }
249 if('3' == $transperms[$privs]) { $user->set_role('contributor'); }
250 if('2' == $transperms[$privs]) { $user->set_role('contributor'); }
251 if('0' == $transperms[$privs]) { $user->set_role('subscriber'); }
253 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] );
254 update_usermeta( $ret_id, 'rich_editing', 'false');
255 }// End foreach($users as $user)
257 // Store id translation array for future use
258 add_option('txpid2wpid',$txpid2wpid);
261 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
263 }// End if(is_array($users)
265 echo __('No Users to Import!');
268 }// End function user2wp()
270 function posts2wp($posts='')
272 // General Housekeeping
275 $txpposts2wpposts = array();
281 echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
282 foreach($posts as $post)
287 // Set Textpattern-to-WordPress status translation
288 $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish');
290 //Can we do this more efficiently?
291 $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1;
292 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
294 $Title = $wpdb->escape($Title);
295 $Body = $wpdb->escape($Body);
296 $Excerpt = $wpdb->escape($Excerpt);
297 $post_status = $stattrans[$Status];
299 // Import Post data into WordPress
301 if($pinfo = post_exists($Title,$Body))
303 $ret_id = wp_insert_post(array(
305 'post_date' => $Posted,
306 'post_date_gmt' => $post_date_gmt,
307 'post_author' => $authorid,
308 'post_modified' => $LastMod,
309 'post_modified_gmt' => $post_modified_gmt,
310 'post_title' => $Title,
311 'post_content' => $Body,
312 'post_excerpt' => $Excerpt,
313 'post_status' => $post_status,
314 'post_name' => $url_title,
315 'comment_count' => $comments_count)
320 $ret_id = wp_insert_post(array(
321 'post_date' => $Posted,
322 'post_date_gmt' => $post_date_gmt,
323 'post_author' => $authorid,
324 'post_modified' => $LastMod,
325 'post_modified_gmt' => $post_modified_gmt,
326 'post_title' => $Title,
327 'post_content' => $Body,
328 'post_excerpt' => $Excerpt,
329 'post_status' => $post_status,
330 'post_name' => $url_title,
331 'comment_count' => $comments_count)
334 $txpposts2wpposts[$ID] = $ret_id;
336 // Make Post-to-Category associations
338 if($cat1 = get_catbynicename($Category1)) { $cats[1] = $cat1; }
339 if($cat2 = get_catbynicename($Category2)) { $cats[2] = $cat2; }
341 if(!empty($cats)) { wp_set_post_cats('', $ret_id, $cats); }
344 // Store ID translation for later use
345 add_option('txpposts2wpposts',$txpposts2wpposts);
347 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
351 function comments2wp($comments='')
353 // General Housekeeping
356 $txpcm2wpcm = array();
357 $postarr = get_option('txpposts2wpposts');
360 if(is_array($comments))
362 echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
363 foreach($comments as $comment)
369 $comment_ID = ltrim($discussid, '0');
370 $comment_post_ID = $postarr[$parentid];
371 $comment_approved = (1 == $visible) ? 1 : 0;
372 $name = $wpdb->escape($name);
373 $email = $wpdb->escape($email);
374 $web = $wpdb->escape($web);
375 $message = $wpdb->escape($message);
377 if($cinfo = comment_exists($name, $posted))
380 $ret_id = wp_update_comment(array(
381 'comment_ID' => $cinfo,
382 'comment_post_ID' => $comment_post_ID,
383 'comment_author' => $name,
384 'comment_author_email' => $email,
385 'comment_author_url' => $web,
386 'comment_date' => $posted,
387 'comment_content' => $message,
388 'comment_approved' => $comment_approved)
394 $ret_id = wp_insert_comment(array(
395 'comment_post_ID' => $comment_post_ID,
396 'comment_author' => $name,
397 'comment_author_email' => $email,
398 'comment_author_url' => $web,
399 'comment_author_IP' => $ip,
400 'comment_date' => $posted,
401 'comment_content' => $message,
402 'comment_approved' => $comment_approved)
405 $txpcm2wpcm[$comment_ID] = $ret_id;
407 // Store Comment ID translation for future use
408 add_option('txpcm2wpcm', $txpcm2wpcm);
410 // Associate newly formed categories with posts
411 get_comment_count($ret_id);
414 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
417 echo __('No Comments to Import!');
421 function links2wp($links='')
423 // General Housekeeping
427 // Deal with the links
430 echo '<p>'.__('Importing Links...').'<br /><br /></p>';
431 foreach($links as $link)
437 $category = $wpdb->escape($category);
438 $linkname = $wpdb->escape($linkname);
439 $description = $wpdb->escape($description);
441 if($linfo = link_exists($linkname))
443 $ret_id = wp_insert_link(array(
446 'link_name' => $linkname,
447 'link_category' => $category,
448 'link_description' => $description,
449 'link_updated' => $date)
454 $ret_id = wp_insert_link(array(
456 'link_name' => $linkname,
457 'link_category' => $category,
458 'link_description' => $description,
459 'link_updated' => $date)
462 $txplinks2wplinks[$link_id] = $ret_id;
464 add_option('txplinks2wplinks',$txplinks2wplinks);
466 printf(__('Done! <strong>%s</strong> Links imported'), $count);
467 echo '<br /><br /></p>';
470 echo __('No Links to Import!');
474 function import_categories()
477 $cats = $this->get_txp_cats();
478 $this->cat2wp($cats);
479 add_option('txp_cats', $cats);
483 echo '<form action="admin.php?import=textpattern&step=2" method="post">';
484 printf('<input type="submit" name="submit" value="%s" />', __('Import Users'));
489 function import_users()
492 $users = $this->get_txp_users();
493 $this->users2wp($users);
495 echo '<form action="admin.php?import=textpattern&step=3" method="post">';
496 printf('<input type="submit" name="submit" value="%s" />', __('Import Posts'));
500 function import_posts()
503 $posts = $this->get_txp_posts();
504 $this->posts2wp($posts);
506 echo '<form action="admin.php?import=textpattern&step=4" method="post">';
507 printf('<input type="submit" name="submit" value="%s" />', __('Import Comments'));
511 function import_comments()
514 $comments = $this->get_txp_comments();
515 $this->comments2wp($comments);
517 echo '<form action="admin.php?import=textpattern&step=5" method="post">';
518 printf('<input type="submit" name="submit" value="%s" />', __('Import Links'));
522 function import_links()
525 $links = $this->get_txp_links();
526 $this->links2wp($links);
527 add_option('txp_links', $links);
529 echo '<form action="admin.php?import=textpattern&step=6" method="post">';
530 printf('<input type="submit" name="submit" value="%s" />', __('Finish'));
534 function cleanup_txpimport()
536 delete_option('tpre');
537 delete_option('txp_cats');
538 delete_option('txpid2wpid');
539 delete_option('txpcat2wpcat');
540 delete_option('txpposts2wpposts');
541 delete_option('txpcm2wpcm');
542 delete_option('txplinks2wplinks');
543 delete_option('txpuser');
544 delete_option('txppass');
545 delete_option('txpname');
546 delete_option('txphost');
552 echo '<p>'.__('Welcome to WordPress. We hope (and expect!) that you will find this platform incredibly rewarding! As a new WordPress user coming from Textpattern, there are some things that we would like to point out. Hopefully, they will help your transition go as smoothly as possible.').'</p>';
553 echo '<h3>'.__('Users').'</h3>';
554 echo '<p>'.sprintf(__('You have already setup WordPress and have been assigned an administrative login and password. Forget it. You didn\'t have that login in Textpattern, why should you have it here? Instead we have taken care to import all of your users into our system. Unfortunately there is one downside. Because both WordPress and Textpattern uses a strong encryption hash with passwords, it is impossible to decrypt it and we are forced to assign temporary passwords to all your users. <strong>Every user has the same username, but their passwords are reset to password123.</strong> So <a href="%1$s">Login</a> and change it.'), '/wp-login.php').'</p>';
555 echo '<h3>'.__('Preserving Authors').'</h3>';
556 echo '<p>'.__('Secondly, we have attempted to preserve post authors. If you are the only author or contributor to your blog, then you are safe. In most cases, we are successful in this preservation endeavor. However, if we cannot ascertain the name of the writer due to discrepancies between database tables, we assign it to you, the administrative user.').'</p>';
557 echo '<h3>'.__('Textile').'</h3>';
558 echo '<p>'.__('Also, since you\'re coming from Textpattern, you probably have been using Textile to format your comments and posts. If this is the case, we recommend downloading and installing <a href="http://www.huddledmasses.org/2004/04/19/wordpress-plugin-textile-20/">Textile for WordPress</a>. Trust me... You\'ll want it.').'</p>';
559 echo '<h3>'.__('WordPress Resources').'</h3>';
560 echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>';
562 echo '<li>'.__('<a href="http://www.wordpress.org">The official WordPress site</a>').'</li>';
563 echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums').'</li>';
564 echo '<li>'.__('<a href="http://codex.wordpress.org">The Codex (In other words, the WordPress Bible)</a>').'</li>';
566 echo '<p>'.sprintf(__('That\'s it! What are you waiting for? Go <a href="%1$s">login</a>!'), '/wp-login.php').'</p>';
572 printf('<li><label for="dbuser">%s</label> <input type="text" name="dbuser" /></li>', __('Textpattern Database User:'));
573 printf('<li><label for="dbpass">%s</label> <input type="password" name="dbpass" /></li>', __('Textpattern Database Password:'));
574 printf('<li><label for="dbname">%s</label> <input type="text" name="dbname" /></li>', __('Textpattern Database Name:'));
575 printf('<li><label for="dbhost">%s</label> <input type="text" name="dbhost" value="localhost" /></li>', __('Textpattern Database Host:'));
576 printf('<li><label for="dbprefix">%s</label> <input type="text" name="dbprefix" /></li>', __('Textpattern Table prefix (if any):'));
583 if (empty ($_GET['step']))
586 $step = (int) $_GET['step'];
593 if(get_option('txpuser'))
594 delete_option('txpuser');
595 add_option('txpuser',$_POST['dbuser']);
599 if(get_option('txppass'))
600 delete_option('txppass');
601 add_option('txppass',$_POST['dbpass']);
606 if(get_option('txpname'))
607 delete_option('txpname');
608 add_option('txpname',$_POST['dbname']);
612 if(get_option('txphost'))
613 delete_option('txphost');
614 add_option('txphost',$_POST['dbhost']);
616 if($_POST['dbprefix'])
618 if(get_option('tpre'))
619 delete_option('tpre');
620 add_option('tpre',$_POST['dbprefix']);
633 $this->import_categories();
636 $this->import_users();
639 $this->import_posts();
642 $this->import_comments();
645 $this->import_links();
648 $this->cleanup_txpimport();
655 function Textpattern_Import()
661 $txp_import = new Textpattern_Import();
662 register_importer('textpattern', 'Textpattern', __('Import posts from a Textpattern Blog'), array ($txp_import, 'dispatch'));