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>';
54 echo '<div class="narrow">';
55 echo '<p>'.__('Howdy! This imports categories, users, posts, comments, and links from any Textpattern 4.0.2+ into this blog.').'</p>';
56 echo '<p>'.__('This has not been tested on previous versions of Textpattern. Mileage may vary.').'</p>';
57 echo '<p>'.__('Your Textpattern Configuration settings are as follows:').'</p>';
58 echo '<form action="admin.php?import=textpattern&step=1" method="post">';
59 wp_nonce_field('import-textpattern');
61 echo '<p class="submit"><input type="submit" name="submit" value="'.attribute_escape(__('Import Categories »')).'" /></p>';
66 function get_txp_cats()
69 // General Housekeeping
70 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
71 set_magic_quotes_runtime(0);
72 $prefix = get_option('tpre');
75 return $txpdb->get_results('SELECT
79 FROM '.$prefix.'txp_category
80 WHERE type = "article"',
84 function get_txp_users()
87 // General Housekeeping
88 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
89 set_magic_quotes_runtime(0);
90 $prefix = get_option('tpre');
94 return $txpdb->get_results('SELECT
100 FROM '.$prefix.'txp_users', ARRAY_A);
103 function get_txp_posts()
105 // General Housekeeping
106 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
107 set_magic_quotes_runtime(0);
108 $prefix = get_option('tpre');
111 return $txpdb->get_results('SELECT
125 FROM '.$prefix.'textpattern
129 function get_txp_comments()
132 // General Housekeeping
133 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
134 set_magic_quotes_runtime(0);
135 $prefix = get_option('tpre');
138 return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
141 function get_txp_links()
143 //General Housekeeping
144 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost'));
145 set_magic_quotes_runtime(0);
146 $prefix = get_option('tpre');
148 return $txpdb->get_results('SELECT
155 FROM '.$prefix.'txp_link',
159 function cat2wp($categories='')
161 // General Housekeeping
164 $txpcat2wpcat = array();
166 if(is_array($categories))
168 echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
169 foreach ($categories as $category)
175 // Make Nice Variables
176 $name = $wpdb->escape($name);
177 $title = $wpdb->escape($title);
179 if($cinfo = category_exists($name))
181 $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
185 $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
187 $txpcat2wpcat[$id] = $ret_id;
190 // Store category translation for future use
191 add_option('txpcat2wpcat',$txpcat2wpcat);
192 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>';
195 echo __('No Categories to Import!');
199 function users2wp($users='')
201 // General Housekeeping
204 $txpid2wpid = array();
209 echo '<p>'.__('Importing Users...').'<br /><br /></p>';
210 foreach($users as $user)
215 // Make Nice Variables
216 $name = $wpdb->escape($name);
217 $RealName = $wpdb->escape($RealName);
219 if($uinfo = get_userdatabylogin($name))
222 $ret_id = wp_insert_user(array(
224 'user_login' => $name,
225 'user_nicename' => $RealName,
226 'user_email' => $email,
227 'user_url' => 'http://',
228 'display_name' => $name)
233 $ret_id = wp_insert_user(array(
234 'user_login' => $name,
235 'user_nicename' => $RealName,
236 'user_email' => $email,
237 'user_url' => 'http://',
238 'display_name' => $name)
241 $txpid2wpid[$user_id] = $ret_id;
243 // Set Textpattern-to-WordPress permissions translation
244 $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0');
246 // Update Usermeta Data
247 $user = new WP_User($ret_id);
248 if('10' == $transperms[$privs]) { $user->set_role('administrator'); }
249 if('9' == $transperms[$privs]) { $user->set_role('editor'); }
250 if('5' == $transperms[$privs]) { $user->set_role('editor'); }
251 if('4' == $transperms[$privs]) { $user->set_role('author'); }
252 if('3' == $transperms[$privs]) { $user->set_role('contributor'); }
253 if('2' == $transperms[$privs]) { $user->set_role('contributor'); }
254 if('0' == $transperms[$privs]) { $user->set_role('subscriber'); }
256 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] );
257 update_usermeta( $ret_id, 'rich_editing', 'false');
258 }// End foreach($users as $user)
260 // Store id translation array for future use
261 add_option('txpid2wpid',$txpid2wpid);
264 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
266 }// End if(is_array($users)
268 echo __('No Users to Import!');
271 }// End function user2wp()
273 function posts2wp($posts='')
275 // General Housekeeping
278 $txpposts2wpposts = array();
284 echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
285 foreach($posts as $post)
290 // Set Textpattern-to-WordPress status translation
291 $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish');
293 //Can we do this more efficiently?
294 $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1;
295 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
297 $Title = $wpdb->escape($Title);
298 $Body = $wpdb->escape($Body);
299 $Excerpt = $wpdb->escape($Excerpt);
300 $post_status = $stattrans[$Status];
302 // Import Post data into WordPress
304 if($pinfo = post_exists($Title,$Body))
306 $ret_id = wp_insert_post(array(
308 'post_date' => $Posted,
309 'post_date_gmt' => $post_date_gmt,
310 'post_author' => $authorid,
311 'post_modified' => $LastMod,
312 'post_modified_gmt' => $post_modified_gmt,
313 'post_title' => $Title,
314 'post_content' => $Body,
315 'post_excerpt' => $Excerpt,
316 'post_status' => $post_status,
317 'post_name' => $url_title,
318 'comment_count' => $comments_count)
323 $ret_id = wp_insert_post(array(
324 'post_date' => $Posted,
325 'post_date_gmt' => $post_date_gmt,
326 'post_author' => $authorid,
327 'post_modified' => $LastMod,
328 'post_modified_gmt' => $post_modified_gmt,
329 'post_title' => $Title,
330 'post_content' => $Body,
331 'post_excerpt' => $Excerpt,
332 'post_status' => $post_status,
333 'post_name' => $url_title,
334 'comment_count' => $comments_count)
337 $txpposts2wpposts[$ID] = $ret_id;
339 // Make Post-to-Category associations
341 if($cat1 = get_catbynicename($Category1)) { $cats[1] = $cat1; }
342 if($cat2 = get_catbynicename($Category2)) { $cats[2] = $cat2; }
344 if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
347 // Store ID translation for later use
348 add_option('txpposts2wpposts',$txpposts2wpposts);
350 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
354 function comments2wp($comments='')
356 // General Housekeeping
359 $txpcm2wpcm = array();
360 $postarr = get_option('txpposts2wpposts');
363 if(is_array($comments))
365 echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
366 foreach($comments as $comment)
372 $comment_ID = ltrim($discussid, '0');
373 $comment_post_ID = $postarr[$parentid];
374 $comment_approved = (1 == $visible) ? 1 : 0;
375 $name = $wpdb->escape($name);
376 $email = $wpdb->escape($email);
377 $web = $wpdb->escape($web);
378 $message = $wpdb->escape($message);
380 if($cinfo = comment_exists($name, $posted))
383 $ret_id = wp_update_comment(array(
384 'comment_ID' => $cinfo,
385 'comment_post_ID' => $comment_post_ID,
386 'comment_author' => $name,
387 'comment_author_email' => $email,
388 'comment_author_url' => $web,
389 'comment_date' => $posted,
390 'comment_content' => $message,
391 'comment_approved' => $comment_approved)
397 $ret_id = wp_insert_comment(array(
398 'comment_post_ID' => $comment_post_ID,
399 'comment_author' => $name,
400 'comment_author_email' => $email,
401 'comment_author_url' => $web,
402 'comment_author_IP' => $ip,
403 'comment_date' => $posted,
404 'comment_content' => $message,
405 'comment_approved' => $comment_approved)
408 $txpcm2wpcm[$comment_ID] = $ret_id;
410 // Store Comment ID translation for future use
411 add_option('txpcm2wpcm', $txpcm2wpcm);
413 // Associate newly formed categories with posts
414 get_comment_count($ret_id);
417 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
420 echo __('No Comments to Import!');
424 function links2wp($links='')
426 // General Housekeeping
430 // Deal with the links
433 echo '<p>'.__('Importing Links...').'<br /><br /></p>';
434 foreach($links as $link)
440 $category = $wpdb->escape($category);
441 $linkname = $wpdb->escape($linkname);
442 $description = $wpdb->escape($description);
444 if($linfo = link_exists($linkname))
446 $ret_id = wp_insert_link(array(
449 'link_name' => $linkname,
450 'link_category' => $category,
451 'link_description' => $description,
452 'link_updated' => $date)
457 $ret_id = wp_insert_link(array(
459 'link_name' => $linkname,
460 'link_category' => $category,
461 'link_description' => $description,
462 'link_updated' => $date)
465 $txplinks2wplinks[$link_id] = $ret_id;
467 add_option('txplinks2wplinks',$txplinks2wplinks);
469 printf(__('Done! <strong>%s</strong> Links imported'), $count);
470 echo '<br /><br /></p>';
473 echo __('No Links to Import!');
477 function import_categories()
480 $cats = $this->get_txp_cats();
481 $this->cat2wp($cats);
482 add_option('txp_cats', $cats);
486 echo '<form action="admin.php?import=textpattern&step=2" method="post">';
487 wp_nonce_field('import-textpattern');
488 printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Users')));
493 function import_users()
496 $users = $this->get_txp_users();
497 $this->users2wp($users);
499 echo '<form action="admin.php?import=textpattern&step=3" method="post">';
500 wp_nonce_field('import-textpattern');
501 printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Posts')));
505 function import_posts()
508 $posts = $this->get_txp_posts();
509 $this->posts2wp($posts);
511 echo '<form action="admin.php?import=textpattern&step=4" method="post">';
512 wp_nonce_field('import-textpattern');
513 printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Comments')));
517 function import_comments()
520 $comments = $this->get_txp_comments();
521 $this->comments2wp($comments);
523 echo '<form action="admin.php?import=textpattern&step=5" method="post">';
524 wp_nonce_field('import-textpattern');
525 printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Import Links')));
529 function import_links()
532 $links = $this->get_txp_links();
533 $this->links2wp($links);
534 add_option('txp_links', $links);
536 echo '<form action="admin.php?import=textpattern&step=6" method="post">';
537 wp_nonce_field('import-textpattern');
538 printf('<input type="submit" name="submit" value="%s" />', attribute_escape(__('Finish')));
542 function cleanup_txpimport()
544 delete_option('tpre');
545 delete_option('txp_cats');
546 delete_option('txpid2wpid');
547 delete_option('txpcat2wpcat');
548 delete_option('txpposts2wpposts');
549 delete_option('txpcm2wpcm');
550 delete_option('txplinks2wplinks');
551 delete_option('txpuser');
552 delete_option('txppass');
553 delete_option('txpname');
554 delete_option('txphost');
560 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>';
561 echo '<h3>'.__('Users').'</h3>';
562 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>';
563 echo '<h3>'.__('Preserving Authors').'</h3>';
564 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>';
565 echo '<h3>'.__('Textile').'</h3>';
566 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>';
567 echo '<h3>'.__('WordPress Resources').'</h3>';
568 echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>';
570 echo '<li>'.__('<a href="http://www.wordpress.org">The official WordPress site</a>').'</li>';
571 echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums</a>').'</li>';
572 echo '<li>'.__('<a href="http://codex.wordpress.org">The Codex (In other words, the WordPress Bible)</a>').'</li>';
574 echo '<p>'.sprintf(__('That\'s it! What are you waiting for? Go <a href="%1$s">login</a>!'), '/wp-login.php').'</p>';
579 echo '<table class="editform">';
580 printf('<tr><th scope="row"><label for="dbuser">%s</label></th><td><input type="text" name="dbuser" id="dbuser" /></td></tr>', __('Textpattern Database User:'));
581 printf('<tr><th scope="row"><label for="dbpass">%s</label></th><td><input type="password" name="dbpass" id="dbpass" /></td></tr>', __('Textpattern Database Password:'));
582 printf('<tr><th scope="row"><label for="dbname">%s</label></th><td><input type="text" id="dbname" name="dbname" /></td></tr>', __('Textpattern Database Name:'));
583 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:'));
584 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):'));
591 if (empty ($_GET['step']))
594 $step = (int) $_GET['step'];
599 check_admin_referer('import-textpattern');
603 if(get_option('txpuser'))
604 delete_option('txpuser');
605 add_option('txpuser', sanitize_user($_POST['dbuser'], true));
609 if(get_option('txppass'))
610 delete_option('txppass');
611 add_option('txppass', sanitize_user($_POST['dbpass'], true));
616 if(get_option('txpname'))
617 delete_option('txpname');
618 add_option('txpname', sanitize_user($_POST['dbname'], true));
622 if(get_option('txphost'))
623 delete_option('txphost');
624 add_option('txphost', sanitize_user($_POST['dbhost'], true));
626 if($_POST['dbprefix'])
628 if(get_option('tpre'))
629 delete_option('tpre');
630 add_option('tpre', sanitize_user($_POST['dbprefix']));
643 $this->import_categories();
646 $this->import_users();
649 $this->import_posts();
652 $this->import_comments();
655 $this->import_links();
658 $this->cleanup_txpimport();
665 function Textpattern_Import()
671 $txp_import = new Textpattern_Import();
672 register_importer('textpattern', __('Textpattern'), __('Import categories, users, posts, comments, and links from a Textpattern blog'), array ($txp_import, 'dispatch'));