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);
401 'comment_post_ID' => $comment_post_ID,
402 'comment_author' => $name,
403 'comment_author_IP' => $ip,
404 'comment_author_email' => $email,
405 'comment_author_url' => $web,
406 'comment_date' => $posted,
407 'comment_content' => $message,
408 'comment_approved' => $comment_approved);
409 $comment = wp_filter_comment($comment);
411 if ( $cinfo = comment_exists($name, $posted) ) {
413 $comment['comment_ID'] = $cinfo;
414 $ret_id = wp_update_comment($comment);
417 $ret_id = wp_insert_comment($comment);
419 $txpcm2wpcm[$comment_ID] = $ret_id;
421 // Store Comment ID translation for future use
422 add_option('txpcm2wpcm', $txpcm2wpcm);
424 // Associate newly formed categories with posts
425 get_comment_count($ret_id);
428 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
431 echo __('No Comments to Import!');
435 function links2wp($links='')
437 // General Housekeeping
441 // Deal with the links
444 echo '<p>'.__('Importing Links...').'<br /><br /></p>';
445 foreach($links as $link)
451 $category = $wpdb->escape($category);
452 $linkname = $wpdb->escape($linkname);
453 $description = $wpdb->escape($description);
455 if($linfo = link_exists($linkname))
457 $ret_id = wp_insert_link(array(
460 'link_name' => $linkname,
461 'link_category' => $category,
462 'link_description' => $description,
463 'link_updated' => $date)
468 $ret_id = wp_insert_link(array(
470 'link_name' => $linkname,
471 'link_category' => $category,
472 'link_description' => $description,
473 'link_updated' => $date)
476 $txplinks2wplinks[$link_id] = $ret_id;
478 add_option('txplinks2wplinks',$txplinks2wplinks);
480 printf(_n('Done! <strong>%s</strong> link imported', 'Done! <strong>%s</strong> links imported', $count), $count);
481 echo '<br /><br /></p>';
484 echo __('No Links to Import!');
488 function import_categories()
491 $cats = $this->get_txp_cats();
492 $this->cat2wp($cats);
493 add_option('txp_cats', $cats);
497 echo '<form action="admin.php?import=textpattern&step=2" method="post">';
498 wp_nonce_field('import-textpattern');
499 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Users'));
504 function import_users()
507 $users = $this->get_txp_users();
508 $this->users2wp($users);
510 echo '<form action="admin.php?import=textpattern&step=3" method="post">';
511 wp_nonce_field('import-textpattern');
512 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Posts'));
516 function import_posts()
519 $posts = $this->get_txp_posts();
520 $result = $this->posts2wp($posts);
521 if ( is_wp_error( $result ) )
524 echo '<form action="admin.php?import=textpattern&step=4" method="post">';
525 wp_nonce_field('import-textpattern');
526 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Comments'));
530 function import_comments()
533 $comments = $this->get_txp_comments();
534 $this->comments2wp($comments);
536 echo '<form action="admin.php?import=textpattern&step=5" method="post">';
537 wp_nonce_field('import-textpattern');
538 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Import Links'));
542 function import_links()
545 $links = $this->get_txp_links();
546 $this->links2wp($links);
547 add_option('txp_links', $links);
549 echo '<form action="admin.php?import=textpattern&step=6" method="post">';
550 wp_nonce_field('import-textpattern');
551 printf('<p class="submit"><input type="submit" name="submit" class="button" value="%s" /></p>', esc_attr__('Finish'));
555 function cleanup_txpimport()
557 delete_option('tpre');
558 delete_option('txp_cats');
559 delete_option('txpid2wpid');
560 delete_option('txpcat2wpcat');
561 delete_option('txpposts2wpposts');
562 delete_option('txpcm2wpcm');
563 delete_option('txplinks2wplinks');
564 delete_option('txpuser');
565 delete_option('txppass');
566 delete_option('txpname');
567 delete_option('txphost');
568 do_action('import_done', 'textpattern');
574 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>';
575 echo '<h3>'.__('Users').'</h3>';
576 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">log in</a> and change it.'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
577 echo '<h3>'.__('Preserving Authors').'</h3>';
578 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>';
579 echo '<h3>'.__('Textile').'</h3>';
580 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>';
581 echo '<h3>'.__('WordPress Resources').'</h3>';
582 echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>';
584 echo '<li>'.__('<a href="http://www.wordpress.org">The official WordPress site</a>').'</li>';
585 echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums</a>').'</li>';
586 echo '<li>'.__('<a href="http://codex.wordpress.org">The Codex (In other words, the WordPress Bible)</a>').'</li>';
588 echo '<p>'.sprintf(__('That’s it! What are you waiting for? Go <a href="%1$s">log in</a>!'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
593 echo '<table class="form-table">';
594 printf('<tr><th scope="row"><label for="dbuser">%s</label></th><td><input type="text" name="dbuser" id="dbuser" /></td></tr>', __('Textpattern Database User:'));
595 printf('<tr><th scope="row"><label for="dbpass">%s</label></th><td><input type="password" name="dbpass" id="dbpass" /></td></tr>', __('Textpattern Database Password:'));
596 printf('<tr><th scope="row"><label for="dbname">%s</label></th><td><input type="text" id="dbname" name="dbname" /></td></tr>', __('Textpattern Database Name:'));
597 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:'));
598 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):'));
605 if (empty ($_GET['step']))
608 $step = (int) $_GET['step'];
613 check_admin_referer('import-textpattern');
617 if(get_option('txpuser'))
618 delete_option('txpuser');
619 add_option('txpuser', sanitize_user($_POST['dbuser'], true));
623 if(get_option('txppass'))
624 delete_option('txppass');
625 add_option('txppass', sanitize_user($_POST['dbpass'], true));
630 if(get_option('txpname'))
631 delete_option('txpname');
632 add_option('txpname', sanitize_user($_POST['dbname'], true));
636 if(get_option('txphost'))
637 delete_option('txphost');
638 add_option('txphost', sanitize_user($_POST['dbhost'], true));
640 if($_POST['dbprefix'])
642 if(get_option('tpre'))
643 delete_option('tpre');
644 add_option('tpre', sanitize_user($_POST['dbprefix']));
657 $this->import_categories();
660 $this->import_users();
663 $result = $this->import_posts();
664 if ( is_wp_error( $result ) )
665 echo $result->get_error_message();
668 $this->import_comments();
671 $this->import_links();
674 $this->cleanup_txpimport();
681 function Textpattern_Import()
687 $txp_import = new Textpattern_Import();
689 register_importer('textpattern', __('Textpattern'), __('Import categories, users, posts, comments, and links from a Textpattern blog.'), array ($txp_import, 'dispatch'));