source: trunk/www.guidonia.net/wp/wp-admin/import/textpattern.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 20.3 KB
Line 
1<?php
2/**
3 * TextPattern Importer
4 *
5 * @package WordPress
6 * @subpackage Importer
7 */
8
9if(!function_exists('get_comment_count'))
10{
11 /**
12 * Get the comment count for posts.
13 *
14 * @package WordPress
15 * @subpackage Textpattern_Import
16 *
17 * @param int $post_ID Post ID
18 * @return int
19 */
20 function get_comment_count($post_ID)
21 {
22 global $wpdb;
23 return $wpdb->get_var( $wpdb->prepare("SELECT count(*) FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
24 }
25}
26
27if(!function_exists('link_exists'))
28{
29 /**
30 * Check whether link already exists.
31 *
32 * @package WordPress
33 * @subpackage Textpattern_Import
34 *
35 * @param string $linkname
36 * @return int
37 */
38 function link_exists($linkname)
39 {
40 global $wpdb;
41 return $wpdb->get_var( $wpdb->prepare("SELECT link_id FROM $wpdb->links WHERE link_name = %s", $linkname) );
42 }
43}
44
45/**
46 * TextPattern Importer Class
47 *
48 * @since unknown
49 */
50class Textpattern_Import {
51
52 function header()
53 {
54 echo '<div class="wrap">';
55 screen_icon();
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>';
58 }
59
60 function footer()
61 {
62 echo '</div>';
63 }
64
65 function greet() {
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&amp;step=1" method="post">';
71 wp_nonce_field('import-textpattern');
72 $this->db_form();
73 echo '<p class="submit"><input type="submit" name="submit" class="button" value="'.esc_attr__('Import').'" /></p>';
74 echo '</form>';
75 echo '</div>';
76 }
77
78 function get_txp_cats()
79 {
80 global $wpdb;
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');
85
86 // Get Categories
87 return $txpdb->get_results('SELECT
88 id,
89 name,
90 title
91 FROM '.$prefix.'txp_category
92 WHERE type = "article"',
93 ARRAY_A);
94 }
95
96 function get_txp_users()
97 {
98 global $wpdb;
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');
103
104 // Get Users
105
106 return $txpdb->get_results('SELECT
107 user_id,
108 name,
109 RealName,
110 email,
111 privs
112 FROM '.$prefix.'txp_users', ARRAY_A);
113 }
114
115 function get_txp_posts()
116 {
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');
121
122 // Get Posts
123 return $txpdb->get_results('SELECT
124 ID,
125 Posted,
126 AuthorID,
127 LastMod,
128 Title,
129 Body,
130 Excerpt,
131 Category1,
132 Category2,
133 Status,
134 Keywords,
135 url_title,
136 comments_count
137 FROM '.$prefix.'textpattern
138 ', ARRAY_A);
139 }
140
141 function get_txp_comments()
142 {
143 global $wpdb;
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');
148
149 // Get Comments
150 return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A);
151 }
152
153 function get_txp_links()
154 {
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');
159
160 return $txpdb->get_results('SELECT
161 id,
162 date,
163 category,
164 url,
165 linkname,
166 description
167 FROM '.$prefix.'txp_link',
168 ARRAY_A);
169 }
170
171 function cat2wp($categories='')
172 {
173 // General Housekeeping
174 global $wpdb;
175 $count = 0;
176 $txpcat2wpcat = array();
177 // Do the Magic
178 if(is_array($categories))
179 {
180 echo '<p>'.__('Importing Categories...').'<br /><br /></p>';
181 foreach ($categories as $category)
182 {
183 $count++;
184 extract($category);
185
186
187 // Make Nice Variables
188 $name = $wpdb->escape($name);
189 $title = $wpdb->escape($title);
190
191 if($cinfo = category_exists($name))
192 {
193 $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title));
194 }
195 else
196 {
197 $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title));
198 }
199 $txpcat2wpcat[$id] = $ret_id;
200 }
201
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>';
205 return true;
206 }
207 echo __('No Categories to Import!');
208 return false;
209 }
210
211 function users2wp($users='')
212 {
213 // General Housekeeping
214 global $wpdb;
215 $count = 0;
216 $txpid2wpid = array();
217
218 // Midnight Mojo
219 if(is_array($users))
220 {
221 echo '<p>'.__('Importing Users...').'<br /><br /></p>';
222 foreach($users as $user)
223 {
224 $count++;
225 extract($user);
226
227 // Make Nice Variables
228 $name = $wpdb->escape($name);
229 $RealName = $wpdb->escape($RealName);
230
231 if($uinfo = get_userdatabylogin($name))
232 {
233
234 $ret_id = wp_insert_user(array(
235 'ID' => $uinfo->ID,
236 'user_login' => $name,
237 'user_nicename' => $RealName,
238 'user_email' => $email,
239 'user_url' => 'http://',
240 'display_name' => $name)
241 );
242 }
243 else
244 {
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)
251 );
252 }
253 $txpid2wpid[$user_id] = $ret_id;
254
255 // Set Textpattern-to-WordPress permissions translation
256 $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0');
257
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'); }
267
268 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] );
269 update_usermeta( $ret_id, 'rich_editing', 'false');
270 }// End foreach($users as $user)
271
272 // Store id translation array for future use
273 add_option('txpid2wpid',$txpid2wpid);
274
275
276 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>';
277 return true;
278 }// End if(is_array($users)
279
280 echo __('No Users to Import!');
281 return false;
282
283 }// End function user2wp()
284
285 function posts2wp($posts='')
286 {
287 // General Housekeeping
288 global $wpdb;
289 $count = 0;
290 $txpposts2wpposts = array();
291 $cats = array();
292
293 // Do the Magic
294 if(is_array($posts))
295 {
296 echo '<p>'.__('Importing Posts...').'<br /><br /></p>';
297 foreach($posts as $post)
298 {
299 $count++;
300 extract($post);
301
302 // Set Textpattern-to-WordPress status translation
303 $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish');
304
305 //Can we do this more efficiently?
306 $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1;
307 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ;
308
309 $Title = $wpdb->escape($Title);
310 $Body = $wpdb->escape($Body);
311 $Excerpt = $wpdb->escape($Excerpt);
312 $post_status = $stattrans[$Status];
313
314 // Import Post data into WordPress
315
316 if($pinfo = post_exists($Title,$Body))
317 {
318 $ret_id = wp_insert_post(array(
319 'ID' => $pinfo,
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)
331 );
332 if ( is_wp_error( $ret_id ) )
333 return $ret_id;
334 }
335 else
336 {
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)
349 );
350 if ( is_wp_error( $ret_id ) )
351 return $ret_id;
352 }
353 $txpposts2wpposts[$ID] = $ret_id;
354
355 // Make Post-to-Category associations
356 $cats = array();
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; }
363
364 if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); }
365 }
366 }
367 // Store ID translation for later use
368 add_option('txpposts2wpposts',$txpposts2wpposts);
369
370 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>';
371 return true;
372 }
373
374 function comments2wp($comments='')
375 {
376 // General Housekeeping
377 global $wpdb;
378 $count = 0;
379 $txpcm2wpcm = array();
380 $postarr = get_option('txpposts2wpposts');
381
382 // Magic Mojo
383 if(is_array($comments))
384 {
385 echo '<p>'.__('Importing Comments...').'<br /><br /></p>';
386 foreach($comments as $comment)
387 {
388 $count++;
389 extract($comment);
390
391 // WordPressify Data
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);
399
400 if($cinfo = comment_exists($name, $posted))
401 {
402 // Update comments
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)
412 );
413 }
414 else
415 {
416 // Insert comments
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)
426 );
427 }
428 $txpcm2wpcm[$comment_ID] = $ret_id;
429 }
430 // Store Comment ID translation for future use
431 add_option('txpcm2wpcm', $txpcm2wpcm);
432
433 // Associate newly formed categories with posts
434 get_comment_count($ret_id);
435
436
437 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>';
438 return true;
439 }
440 echo __('No Comments to Import!');
441 return false;
442 }
443
444 function links2wp($links='')
445 {
446 // General Housekeeping
447 global $wpdb;
448 $count = 0;
449
450 // Deal with the links
451 if(is_array($links))
452 {
453 echo '<p>'.__('Importing Links...').'<br /><br /></p>';
454 foreach($links as $link)
455 {
456 $count++;
457 extract($link);
458
459 // Make nice vars
460 $category = $wpdb->escape($category);
461 $linkname = $wpdb->escape($linkname);
462 $description = $wpdb->escape($description);
463
464 if($linfo = link_exists($linkname))
465 {
466 $ret_id = wp_insert_link(array(
467 'link_id' => $linfo,
468 'link_url' => $url,
469 'link_name' => $linkname,
470 'link_category' => $category,
471 'link_description' => $description,
472 'link_updated' => $date)
473 );
474 }
475 else
476 {
477 $ret_id = wp_insert_link(array(
478 'link_url' => $url,
479 'link_name' => $linkname,
480 'link_category' => $category,
481 'link_description' => $description,
482 'link_updated' => $date)
483 );
484 }
485 $txplinks2wplinks[$link_id] = $ret_id;
486 }
487 add_option('txplinks2wplinks',$txplinks2wplinks);
488 echo '<p>';
489 printf(_n('Done! <strong>%s</strong> link imported', 'Done! <strong>%s</strong> links imported', $count), $count);
490 echo '<br /><br /></p>';
491 return true;
492 }
493 echo __('No Links to Import!');
494 return false;
495 }
496
497 function import_categories()
498 {
499 // Category Import
500 $cats = $this->get_txp_cats();
501 $this->cat2wp($cats);
502 add_option('txp_cats', $cats);
503
504
505
506 echo '<form action="admin.php?import=textpattern&amp;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'));
509 echo '</form>';
510
511 }
512
513 function import_users()
514 {
515 // User Import
516 $users = $this->get_txp_users();
517 $this->users2wp($users);
518
519 echo '<form action="admin.php?import=textpattern&amp;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'));
522 echo '</form>';
523 }
524
525 function import_posts()
526 {
527 // Post Import
528 $posts = $this->get_txp_posts();
529 $result = $this->posts2wp($posts);
530 if ( is_wp_error( $result ) )
531 return $result;
532
533 echo '<form action="admin.php?import=textpattern&amp;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'));
536 echo '</form>';
537 }
538
539 function import_comments()
540 {
541 // Comment Import
542 $comments = $this->get_txp_comments();
543 $this->comments2wp($comments);
544
545 echo '<form action="admin.php?import=textpattern&amp;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'));
548 echo '</form>';
549 }
550
551 function import_links()
552 {
553 //Link Import
554 $links = $this->get_txp_links();
555 $this->links2wp($links);
556 add_option('txp_links', $links);
557
558 echo '<form action="admin.php?import=textpattern&amp;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'));
561 echo '</form>';
562 }
563
564 function cleanup_txpimport()
565 {
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');
578 $this->tips();
579 }
580
581 function tips()
582 {
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&#8217;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&#8217;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&#8217;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>';
592 echo '<ul>';
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>';
596 echo '</ul>';
597 echo '<p>'.sprintf(__('That&#8217;s it! What are you waiting for? Go <a href="%1$s">login</a>!'), get_bloginfo( 'wpurl' ) . '/wp-login.php').'</p>';
598 }
599
600 function db_form()
601 {
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):'));
608 echo '</table>';
609 }
610
611 function dispatch()
612 {
613
614 if (empty ($_GET['step']))
615 $step = 0;
616 else
617 $step = (int) $_GET['step'];
618 $this->header();
619
620 if ( $step > 0 )
621 {
622 check_admin_referer('import-textpattern');
623
624 if($_POST['dbuser'])
625 {
626 if(get_option('txpuser'))
627 delete_option('txpuser');
628 add_option('txpuser', sanitize_user($_POST['dbuser'], true));
629 }
630 if($_POST['dbpass'])
631 {
632 if(get_option('txppass'))
633 delete_option('txppass');
634 add_option('txppass', sanitize_user($_POST['dbpass'], true));
635 }
636
637 if($_POST['dbname'])
638 {
639 if(get_option('txpname'))
640 delete_option('txpname');
641 add_option('txpname', sanitize_user($_POST['dbname'], true));
642 }
643 if($_POST['dbhost'])
644 {
645 if(get_option('txphost'))
646 delete_option('txphost');
647 add_option('txphost', sanitize_user($_POST['dbhost'], true));
648 }
649 if($_POST['dbprefix'])
650 {
651 if(get_option('tpre'))
652 delete_option('tpre');
653 add_option('tpre', sanitize_user($_POST['dbprefix']));
654 }
655
656
657 }
658
659 switch ($step)
660 {
661 default:
662 case 0 :
663 $this->greet();
664 break;
665 case 1 :
666 $this->import_categories();
667 break;
668 case 2 :
669 $this->import_users();
670 break;
671 case 3 :
672 $result = $this->import_posts();
673 if ( is_wp_error( $result ) )
674 echo $result->get_error_message();
675 break;
676 case 4 :
677 $this->import_comments();
678 break;
679 case 5 :
680 $this->import_links();
681 break;
682 case 6 :
683 $this->cleanup_txpimport();
684 break;
685 }
686
687 $this->footer();
688 }
689
690 function Textpattern_Import()
691 {
692 // Nothing.
693 }
694}
695
696$txp_import = new Textpattern_Import();
697
698register_importer('textpattern', __('Textpattern'), __('Import categories, users, posts, comments, and links from a Textpattern blog.'), array ($txp_import, 'dispatch'));
699
700?>
Note: See TracBrowser for help on using the repository browser.