9 if(!function_exists('get_comment_count'))
12 * Get the comment count for posts.
15 * @subpackage Textpattern_Import
17 * @param int $post_ID Post ID
20 function get_comment_count($post_ID)
23 return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
27 if(!function_exists('link_exists'))
30 * Check whether link already exists.
33 * @subpackage Textpattern_Import
35 * @param string $linkname
38 function link_exists($linkname)
41 return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
46 * TextPattern Importer Class
50 class Textpattern_Import {
54 echo '<div class="wrap">';
56 echo '<h2>'.__('Import Textpattern').'</h2>';
57 echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'</p>';
66 echo '<div class="narrow">';
67 echo '<p>'.__('Howdy! This imports categories, users, posts, comments, and links from any Textpattern 4.0.2+ into this blog.').'</p>';
68 echo '<p>'.__('This has not been tested on previous versions of Textpattern. Mileage may vary.').'</p>';
69 echo '<p>'.__('Your Textpattern Configuration settings are as follows:').'</p>';
70 echo '<form action="admin.php?import=textpattern&step=1" method="post">';
71 wp_nonce_field('import-textpattern');
73 echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Import').'" /></p>';
78 function get_txp_cats()
81 // General Housekeeping
82 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
83 set_magic_quotes_runtime(0);
84 $prefix = get_option('tpre');
87 return $txpdb->get_results('SELECT
91 FROM '.$prefix.'txp_category
92 WHERE type = "article"',
96 function get_txp_users()
99 // General Housekeeping
100 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
101 set_magic_quotes_runtime(0);
102 $prefix = get_option('tpre');
106 return $txpdb->get_results('SELECT
112 FROM '.$prefix.'txp_users', ARRAY_A);
115 function get_txp_posts()
117 // General Housekeeping
118 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
119 set_magic_quotes_runtime(0);
120 $prefix = get_option('tpre');
123 return $txpdb->get_results('SELECT
137 FROM '.$prefix.'textpattern
141 function get_txp_comments()
144 // General Housekeeping
145 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
146 set_magic_quotes_runtime(0);
147 $prefix = get_option('tpre');
150 return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
153 function get_txp_links()
155 //General Housekeeping
156 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
157 set_magic_quotes_runtime(0);
158 $prefix = get_option('tpre');
160 return $txpdb->get_results('SELECT
167 FROM '.$prefix.'txp_link',
171 function cat2wp($categories='')
173 // General Housekeeping
176 $txpcat2wpcat = array();
178 if(is_array($categories))
180 echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
181 foreach ($categories as $category)
187 // Make Nice Variables
188 $name = $wpdb->escape($name);
189 $title = $wpdb->escape($title);
191 if($cinfo = category_exists($name))
193 $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
197 $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
199 $txpcat2wpcat[$id] = $ret_id;
202 // Store category translation for future use
203 add_option('txpcat2wpcat',$txpcat2wpcat);
204 echo '<p>'.sprintf(_n('Done! <strong>%1$s</strong> category imported.', 'Done! <strong>%1$s</strong> categories imported.', $count), $count).'<br /><br /></p>';
207 echo __('No Categories to Import!');
211 function users2wp($users='')
213 // General Housekeeping
216 $txpid2wpid = array();
221 echo '<p>'.__('Importing Users...').'<br /><br /></p>';
222 foreach($users as $user)
227 // Make Nice Variables
228 $name = $wpdb->escape($name);
229 $RealName = $wpdb->escape($RealName);
231 if($uinfo = get_userdatabylogin($name))
234 $ret_id = wp_insert_user(array(
236 'user_login' => $name,
237 'user_nicename' => $RealName,
238 'user_email' => $email,
239 'user_url' => 'http://',
240 'display_name' => $name)
245 $ret_id = wp_insert_user(array(
246 'user_login' => $name,
247 'user_nicename' => $RealName,
248 'user_email' => $email,
249 'user_url' => 'http://',
250 'display_name' => $name)
253 $txpid2wpid[$user_id] = $ret_id;
255 // Set Textpattern-to-WordPress permissions translation
256 $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0');
258 // Update Usermeta Data
259 $user = new WP_User($ret_id);
260 if('10' == $transperms[$privs]) { $user->set_role('administrator'); }
261 if('9' == $transperms[$privs]) { $user->set_role('editor'); }
262 if('5' == $transperms[$privs]) { $user->set_role('editor'); }
263 if('4' == $transperms[$privs]) { $user->set_role('author'); }
264 if('3' == $transperms[$privs]) { $user->set_role('contributor'); }
265 if('2' == $transperms[$privs]) { $user->set_role('contributor'); }
266 if('0' == $transperms[$privs]) { $user->set_role('subscriber'); }
268 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] );
269 update_usermeta( $ret_id, 'rich_editing', 'false');
270 }// End foreach($users as $user)
272 // Store id translation array for future use
273 add_option('txpid2wpid',$txpid2wpid);
276 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
278 }// End if(is_array($users)
280 echo __('No Users to Import!');
283 }// End function user2wp()
285 function posts2wp($posts='')
287 // General Housekeeping
290 $txpposts2wpposts = array();
296 echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
297 foreach($posts as $post)
302 // Set Textpattern-to-WordPress status translation
303 $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish');
305 //Can we do this more efficiently?
306 $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1;
307 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
309 $Title = $wpdb->escape($Title);
310 $Body = $wpdb->escape($Body);
311 $Excerpt = $wpdb->escape($Excerpt);
312 $post_status = $stattrans[$Status];
314 // Import Post data into WordPress
316 if($pinfo = post_exists($Title,$Body))
318 $ret_id = wp_insert_post(array(
320 'post_date' => $Posted,
321 'post_date_gmt' => $post_date_gmt,
322 'post_author' => $authorid,
323 'post_modified' => $LastMod,
324 'post_modified_gmt' => $post_modified_gmt,
325 'post_title' => $Title,
326 'post_content' => $Body,
327 'post_excerpt' => $Excerpt,
328 'post_status' => $post_status,
329 'post_name' => $url_title,
330 'comment_count' => $comments_count)
332 if ( is_wp_error( $ret_id ) )
337 $ret_id = wp_insert_post(array(
338 'post_date' => $Posted,
339 'post_date_gmt' => $post_date_gmt,
340 'post_author' => $authorid,
341 'post_modified' => $LastMod,
342 'post_modified_gmt' => $post_modified_gmt,
343 'post_title' => $Title,
344 'post_content' => $Body,
345 'post_excerpt' => $Excerpt,
346 'post_status' => $post_status,
347 'post_name' => $url_title,
348 'comment_count' => $comments_count)
350 if ( is_wp_error( $ret_id ) )
353 $txpposts2wpposts[$ID] = $ret_id;
355 // Make Post-to-Category associations
357 $category1 = get_category_by_slug($Category1);
358 $category1 = $category1->term_id;
359 $category2 = get_category_by_slug($Category2);
360 $category2 = $category2->term_id;
361 if($cat1 = $category1) { $cats[1] = $cat1; }
362 if($cat2 = $category2) { $cats[2] = $cat2; }
364 if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
367 // Store ID translation for later use
368 add_option('txpposts2wpposts',$txpposts2wpposts);
370 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
374 function comments2wp($comments='')
376 // General Housekeeping
379 $txpcm2wpcm = array();
380 $postarr = get_option('txpposts2wpposts');
383 if(is_array($comments))
385 echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
386 foreach($comments as $comment)
392 $comment_ID = ltrim($discussid, '0');
393 $comment_post_ID = $postarr[$parentid];
394 $comment_approved = (1 == $visible) ? 1 : 0;
395 $name = $wpdb->escape($name);
396 $email = $wpdb->escape($email);
397 $web = $wpdb->escape($web);
398 $message = $wpdb->escape($message);
400 if($cinfo = comment_exists($name, $posted))
403 $ret_id = wp_update_comment(array(
404 'comment_ID' => $cinfo,
405 'comment_post_ID' => $comment_post_ID,
406 'comment_author' => $name,
407 'comment_author_email' => $email,
408 'comment_author_url' => $web,
409 'comment_date' => $posted,
410 'comment_content' => $message,
411 'comment_approved' => $comment_approved)
417 $ret_id = wp_insert_comment(array(
418 'comment_post_ID' => $comment_post_ID,
419 'comment_author' => $name,
420 'comment_author_email' => $email,
421 'comment_author_url' => $web,
422 'comment_author_IP' => $ip,
423 'comment_date' => $posted,
424 'comment_content' => $message,
425 'comment_approved' => $comment_approved)
428 $txpcm2wpcm[$comment_ID] = $ret_id;
430 // Store Comment ID translation for future use
431 add_option('txpcm2wpcm', $txpcm2wpcm);
433 // Associate newly formed categories with posts
434 get_comment_count($ret_id);
437 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
440 echo __('No Comments to Import!');
444 function links2wp($links='')
446 // General Housekeeping
450 // Deal with the links
453 echo '<p>'.__('Importing Links...').'<br /><br /></p>';
454 foreach($links as $link)
460 $category = $wpdb->escape($category);
461 $linkname = $wpdb->escape($linkname);
462 $description = $wpdb->escape($description);
464 if($linfo = link_exists($linkname))
466 $ret_id = wp_insert_link(array(
469 'link_name' => $linkname,
470 'link_category' => $category,
471 'link_description' => $description,
472 'link_updated' => $date)
477 $ret_id = wp_insert_link(array(
479 'link_name' => $linkname,
480 'link_category' => $category,
481 'link_description' => $description,
482 'link_updated' => $date)
485 $txplinks2wplinks[$link_id] = $ret_id;
487 add_option('txplinks2wplinks',$txplinks2wplinks);
489 printf(_n('Done! <strong>%s</strong> link imported', 'Done! <strong>%s</strong> links imported', $count), $count);
490 echo '<br /><br /></p>';
493 echo __('No Links to Import!');
497 function import_categories()
500 $cats = $this->get_txp_cats();
501 $this->cat2wp($cats);
502 add_option('txp_cats', $cats);
506 echo '<form action="admin.php?import=textpattern&step=2" method="post">';
507 wp_nonce_field('import-textpattern');
508 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Users'));
513 function import_users()
516 $users = $this->get_txp_users();
517 $this->users2wp($users);
519 echo '<form action="admin.php?import=textpattern&step=3" method="post">';
520 wp_nonce_field('import-textpattern');
521 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Posts'));
525 function import_posts()
528 $posts = $this->get_txp_posts();
529 $result = $this->posts2wp($posts);
530 if ( is_wp_error( $result ) )
533 echo '<form action="admin.php?import=textpattern&step=4" method="post">';
534 wp_nonce_field('import-textpattern');
535 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Comments'));
539 function import_comments()
542 $comments = $this->get_txp_comments();
543 $this->comments2wp($comments);
545 echo '<form action="admin.php?import=textpattern&step=5" method="post">';
546 wp_nonce_field('import-textpattern');
547 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Links'));
551 function import_links()
554 $links = $this->get_txp_links();
555 $this->links2wp($links);
556 add_option('txp_links', $links);
558 echo '<form action="admin.php?import=textpattern&step=6" method="post">';
559 wp_nonce_field('import-textpattern');
560 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Finish'));
564 function cleanup_txpimport()
566 delete_option('tpre');
567 delete_option('txp_cats');
568 delete_option('txpid2wpid');
569 delete_option('txpcat2wpcat');
570 delete_option('txpposts2wpposts');
571 delete_option('txpcm2wpcm');
572 delete_option('txplinks2wplinks');
573 delete_option('txpuser');
574 delete_option('txppass');
575 delete_option('txpname');
576 delete_option('txphost');
577 do_action('import_done', 'textpattern');
583 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>';
584 echo '<h3>'.__('Users').'</h3>';
585 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.'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
586 echo '<h3>'.__('Preserving Authors').'</h3>';
587 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>';
588 echo '<h3>'.__('Textile').'</h3>';
589 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/category/development/wordpress/textile/">Textile for WordPress</a>. Trust me... You’ll want it.').'</p>';
590 echo '<h3>'.__('WordPress Resources').'</h3>';
591 echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>';
593 echo '<li>'.__('<a href="http://www.wordpress.org">The official WordPress site</a>').'</li>';
594 echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums</a>').'</li>';
595 echo '<li>'.__('<a href="http://codex.wordpress.org">The Codex (In other words, the WordPress Bible)</a>').'</li>';
597 echo '<p>'.sprintf(__('That’s it! What are you waiting for? Go <a href="%1$s">login</a>!'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
602 echo '<table class="form-table">';
603 printf('<tr><th scope="row"><label for="dbuser">%s</label></th><td><input type="text" name="dbuser" id="dbuser" /></td></tr>', __('Textpattern Database User:'));
604 printf('<tr><th scope="row"><label for="dbpass">%s</label></th><td><input type="password" name="dbpass" id="dbpass" /></td></tr>', __('Textpattern Database Password:'));
605 printf('<tr><th scope="row"><label for="dbname">%s</label></th><td><input type="text" id="dbname" name="dbname" /></td></tr>', __('Textpattern Database Name:'));
606 printf('<tr><th scope="row"><label for="dbhost">%s</label></th><td><input type="text" id="dbhost" name="dbhost" value="localhost" /></td></tr>', __('Textpattern Database Host:'));
607 printf('<tr><th scope="row"><label for="dbprefix">%s</label></th><td><input type="text" name="dbprefix" id="dbprefix" /></td></tr>', __('Textpattern Table prefix (if any):'));
614 if (empty ($_GET['step']))
617 $step = (int) $_GET['step'];
622 check_admin_referer('import-textpattern');
626 if(get_option('txpuser'))
627 delete_option('txpuser');
628 add_option('txpuser', sanitize_user($_POST['dbuser'], true));
632 if(get_option('txppass'))
633 delete_option('txppass');
634 add_option('txppass', sanitize_user($_POST['dbpass'], true));
639 if(get_option('txpname'))
640 delete_option('txpname');
641 add_option('txpname', sanitize_user($_POST['dbname'], true));
645 if(get_option('txphost'))
646 delete_option('txphost');
647 add_option('txphost', sanitize_user($_POST['dbhost'], true));
649 if($_POST['dbprefix'])
651 if(get_option('tpre'))
652 delete_option('tpre');
653 add_option('tpre', sanitize_user($_POST['dbprefix']));
666 $this->import_categories();
669 $this->import_users();
672 $result = $this->import_posts();
673 if ( is_wp_error( $result ) )
674 echo $result->get_error_message();
677 $this->import_comments();
680 $this->import_links();
683 $this->cleanup_txpimport();
690 function Textpattern_Import()
696 $txp_import = new Textpattern_Import();
698 register_importer('textpattern', __('Textpattern'), __('Import categories, users, posts, comments, and links from a Textpattern blog.'), array ($txp_import, 'dispatch'));