| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 Add These Functions to make our lives easier 4 **/ 5 if(!function_exists('get_catbynicename')) 6 { 7 function get_catbynicename($category_nicename) 8 { 9 global $wpdb; 10 11 $cat_id -= 0; // force numeric 12 $name = $wpdb->get_var('SELECT cat_ID FROM '.$wpdb->categories.' WHERE category_nicename="'.$category_nicename.'"'); 13 14 return $name; 15 } 16 } 17 18 if(!function_exists('get_comment_count')) 19 { 20 function get_comment_count($post_ID) 21 { 22 global $wpdb; 23 return $wpdb->get_var('SELECT count(*) FROM '.$wpdb->comments.' WHERE comment_post_ID = '.$post_ID); 24 } 25 } 26 27 if(!function_exists('link_exists')) 28 { 29 function link_exists($linkname) 30 { 31 global $wpdb; 32 return $wpdb->get_var('SELECT link_id FROM '.$wpdb->links.' WHERE link_name = "'.$wpdb->escape($linkname).'"'); 33 } 34 } 35 36 /** 37 The Main Importer Class 38 **/ 39 class Textpattern_Import { 40 41 function header() 42 { 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>'; 46 } 47 48 function footer() 49 { 50 echo '</div>'; 51 } 52 53 function greet() 54 { 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">'; 58 $this->db_form(); 59 echo '<input type="submit" name="submit" value="'.__('Import Categories').'" />'; 60 echo '</form>'; 61 } 62 63 function get_txp_cats() 64 { 65 global $wpdb; 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'); 70 71 // Get Categories 72 return $txpdb->get_results('SELECT 73 id, 74 name, 75 title 76 FROM '.$prefix.'txp_category 77 WHERE type = "article"', 78 ARRAY_A); 79 } 80 81 function get_txp_users() 82 { 83 global $wpdb; 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'); 88 89 // Get Users 90 91 return $txpdb->get_results('SELECT 92 user_id, 93 name, 94 RealName, 95 email, 96 privs 97 FROM '.$prefix.'txp_users', ARRAY_A); 98 } 99 100 function get_txp_posts() 101 { 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'); 106 107 // Get Posts 108 return $txpdb->get_results('SELECT 109 ID, 110 Posted, 111 AuthorID, 112 LastMod, 113 Title, 114 Body, 115 Excerpt, 116 Category1, 117 Category2, 118 Status, 119 Keywords, 120 url_title, 121 comments_count 122 FROM '.$prefix.'textpattern 123 ', ARRAY_A); 124 } 125 126 function get_txp_comments() 127 { 128 global $wpdb; 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'); 133 134 // Get Comments 135 return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A); 136 } 137 138 function get_txp_links() 139 { 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'); 144 145 return $txpdb->get_results('SELECT 146 id, 147 date, 148 category, 149 url, 150 linkname, 151 description 152 FROM '.$prefix.'txp_link', 153 ARRAY_A); 154 } 155 156 function cat2wp($categories='') 157 { 158 // General Housekeeping 159 global $wpdb; 160 $count = 0; 161 $txpcat2wpcat = array(); 162 // Do the Magic 163 if(is_array($categories)) 164 { 165 echo '<p>'.__('Importing Categories...').'<br /><br /></p>'; 166 foreach ($categories as $category) 167 { 168 $count++; 169 extract($category); 170 171 172 // Make Nice Variables 173 $name = $wpdb->escape($name); 174 $title = $wpdb->escape($title); 175 176 if($cinfo = category_exists($name)) 177 { 178 $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title)); 179 } 180 else 181 { 182 $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title)); 183 } 184 $txpcat2wpcat[$id] = $ret_id; 185 } 186 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>'; 190 return true; 191 } 192 echo __('No Categories to Import!'); 193 return false; 194 } 195 196 function users2wp($users='') 197 { 198 // General Housekeeping 199 global $wpdb; 200 $count = 0; 201 $txpid2wpid = array(); 202 203 // Midnight Mojo 204 if(is_array($users)) 205 { 206 echo '<p>'.__('Importing Users...').'<br /><br /></p>'; 207 foreach($users as $user) 208 { 209 $count++; 210 extract($user); 211 212 // Make Nice Variables 213 $name = $wpdb->escape($name); 214 $RealName = $wpdb->escape($RealName); 215 216 if($uinfo = get_userdatabylogin($name)) 217 { 218 219 $ret_id = wp_insert_user(array( 220 'ID' => $uinfo->ID, 221 'user_login' => $name, 222 'user_nicename' => $RealName, 223 'user_email' => $email, 224 'user_url' => 'http://', 225 'display_name' => $name) 226 ); 227 } 228 else 229 { 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) 236 ); 237 } 238 $txpid2wpid[$user_id] = $ret_id; 239 240 // Set Textpattern-to-WordPress permissions translation 241 $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0'); 242 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'); } 252 253 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] ); 254 update_usermeta( $ret_id, 'rich_editing', 'false'); 255 }// End foreach($users as $user) 256 257 // Store id translation array for future use 258 add_option('txpid2wpid',$txpid2wpid); 259 260 261 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>'; 262 return true; 263 }// End if(is_array($users) 264 265 echo __('No Users to Import!'); 266 return false; 267 268 }// End function user2wp() 269 270 function posts2wp($posts='') 271 { 272 // General Housekeeping 273 global $wpdb; 274 $count = 0; 275 $txpposts2wpposts = array(); 276 $cats = array(); 277 278 // Do the Magic 279 if(is_array($posts)) 280 { 281 echo '<p>'.__('Importing Posts...').'<br /><br /></p>'; 282 foreach($posts as $post) 283 { 284 $count++; 285 extract($post); 286 287 // Set Textpattern-to-WordPress status translation 288 $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish'); 289 290 //Can we do this more efficiently? 291 $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1; 292 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ; 293 294 $Title = $wpdb->escape($Title); 295 $Body = $wpdb->escape($Body); 296 $Excerpt = $wpdb->escape($Excerpt); 297 $post_status = $stattrans[$Status]; 298 299 // Import Post data into WordPress 300 301 if($pinfo = post_exists($Title,$Body)) 302 { 303 $ret_id = wp_insert_post(array( 304 'ID' => $pinfo, 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) 316 ); 317 } 318 else 319 { 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) 332 ); 333 } 334 $txpposts2wpposts[$ID] = $ret_id; 335 336 // Make Post-to-Category associations 337 $cats = array(); 338 if($cat1 = get_catbynicename($Category1)) { $cats[1] = $cat1; } 339 if($cat2 = get_catbynicename($Category2)) { $cats[2] = $cat2; } 340 341 if(!empty($cats)) { wp_set_post_cats('', $ret_id, $cats); } 342 } 343 } 344 // Store ID translation for later use 345 add_option('txpposts2wpposts',$txpposts2wpposts); 346 347 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>'; 348 return true; 349 } 350 351 function comments2wp($comments='') 352 { 353 // General Housekeeping 354 global $wpdb; 355 $count = 0; 356 $txpcm2wpcm = array(); 357 $postarr = get_option('txpposts2wpposts'); 358 359 // Magic Mojo 360 if(is_array($comments)) 361 { 362 echo '<p>'.__('Importing Comments...').'<br /><br /></p>'; 363 foreach($comments as $comment) 364 { 365 $count++; 366 extract($comment); 367 368 // WordPressify Data 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); 376 377 if($cinfo = comment_exists($name, $posted)) 378 { 379 // Update comments 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) 389 ); 390 } 391 else 392 { 393 // Insert comments 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) 403 ); 404 } 405 $txpcm2wpcm[$comment_ID] = $ret_id; 406 } 407 // Store Comment ID translation for future use 408 add_option('txpcm2wpcm', $txpcm2wpcm); 409 410 // Associate newly formed categories with posts 411 get_comment_count($ret_id); 412 413 414 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>'; 415 return true; 416 } 417 echo __('No Comments to Import!'); 418 return false; 419 } 420 421 function links2wp($links='') 422 { 423 // General Housekeeping 424 global $wpdb; 425 $count = 0; 426 427 // Deal with the links 428 if(is_array($links)) 429 { 430 echo '<p>'.__('Importing Links...').'<br /><br /></p>'; 431 foreach($links as $link) 432 { 433 $count++; 434 extract($link); 435 436 // Make nice vars 437 $category = $wpdb->escape($category); 438 $linkname = $wpdb->escape($linkname); 439 $description = $wpdb->escape($description); 440 441 if($linfo = link_exists($linkname)) 442 { 443 $ret_id = wp_insert_link(array( 444 'link_id' => $linfo, 445 'link_url' => $url, 446 'link_name' => $linkname, 447 'link_category' => $category, 448 'link_description' => $description, 449 'link_updated' => $date) 450 ); 451 } 452 else 453 { 454 $ret_id = wp_insert_link(array( 455 'link_url' => $url, 456 'link_name' => $linkname, 457 'link_category' => $category, 458 'link_description' => $description, 459 'link_updated' => $date) 460 ); 461 } 462 $txplinks2wplinks[$link_id] = $ret_id; 463 } 464 add_option('txplinks2wplinks',$txplinks2wplinks); 465 echo '<p>'; 466 printf(__('Done! <strong>%s</strong> Links imported'), $count); 467 echo '<br /><br /></p>'; 468 return true; 469 } 470 echo __('No Links to Import!'); 471 return false; 472 } 473 474 function import_categories() 475 { 476 // Category Import 477 $cats = $this->get_txp_cats(); 478 $this->cat2wp($cats); 479 add_option('txp_cats', $cats); 480 481 482 483 echo '<form action="admin.php?import=textpattern&step=2" method="post">'; 484 printf('<input type="submit" name="submit" value="%s" />', __('Import Users')); 485 echo '</form>'; 486 487 } 488 489 function import_users() 490 { 491 // User Import 492 $users = $this->get_txp_users(); 493 $this->users2wp($users); 494 495 echo '<form action="admin.php?import=textpattern&step=3" method="post">'; 496 printf('<input type="submit" name="submit" value="%s" />', __('Import Posts')); 497 echo '</form>'; 498 } 499 500 function import_posts() 501 { 502 // Post Import 503 $posts = $this->get_txp_posts(); 504 $this->posts2wp($posts); 505 506 echo '<form action="admin.php?import=textpattern&step=4" method="post">'; 507 printf('<input type="submit" name="submit" value="%s" />', __('Import Comments')); 508 echo '</form>'; 509 } 510 511 function import_comments() 512 { 513 // Comment Import 514 $comments = $this->get_txp_comments(); 515 $this->comments2wp($comments); 516 517 echo '<form action="admin.php?import=textpattern&step=5" method="post">'; 518 printf('<input type="submit" name="submit" value="%s" />', __('Import Links')); 519 echo '</form>'; 520 } 521 522 function import_links() 523 { 524 //Link Import 525 $links = $this->get_txp_links(); 526 $this->links2wp($links); 527 add_option('txp_links', $links); 528 529 echo '<form action="admin.php?import=textpattern&step=6" method="post">'; 530 printf('<input type="submit" name="submit" value="%s" />', __('Finish')); 531 echo '</form>'; 532 } 533 534 function cleanup_txpimport() 535 { 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'); 547 $this->tips(); 548 } 549 550 function tips() 551 { 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>'; 561 echo '<ul>'; 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>'; 565 echo '</ul>'; 566 echo '<p>'.sprintf(__('That\'s it! What are you waiting for? Go <a href="%1$s">login</a>!'), '/wp-login.php').'</p>'; 567 } 568 569 function db_form() 570 { 571 echo '<ul>'; 572 printf('<li><label for="dbuser">%s</label> <input type="text" name="dbuser" id="dbuser" /></li>', __('Textpattern Database User:')); 573 printf('<li><label for="dbpass">%s</label> <input type="password" name="dbpass" id="dbpass" /></li>', __('Textpattern Database Password:')); 574 printf('<li><label for="dbname">%s</label> <input type="text" id="dbname" name="dbname" /></li>', __('Textpattern Database Name:')); 575 printf('<li><label for="dbhost">%s</label> <input type="text" id="dbhost" name="dbhost" value="localhost" /></li>', __('Textpattern Database Host:')); 576 printf('<li><label for="dbprefix">%s</label> <input type="text" name="dbprefix" id="dbprefix" /></li>', __('Textpattern Table prefix (if any):')); 577 echo '</ul>'; 578 } 579 580 function dispatch() 581 { 582 583 if (empty ($_GET['step'])) 584 $step = 0; 585 else 586 $step = (int) $_GET['step']; 587 $this->header(); 588 589 if ( $step > 0 ) 590 { 591 if($_POST['dbuser']) 592 { 593 if(get_option('txpuser')) 594 delete_option('txpuser'); 595 add_option('txpuser',$_POST['dbuser']); 596 } 597 if($_POST['dbpass']) 598 { 599 if(get_option('txppass')) 600 delete_option('txppass'); 601 add_option('txppass',$_POST['dbpass']); 602 } 603 604 if($_POST['dbname']) 605 { 606 if(get_option('txpname')) 607 delete_option('txpname'); 608 add_option('txpname',$_POST['dbname']); 609 } 610 if($_POST['dbhost']) 611 { 612 if(get_option('txphost')) 613 delete_option('txphost'); 614 add_option('txphost',$_POST['dbhost']); 615 } 616 if($_POST['dbprefix']) 617 { 618 if(get_option('tpre')) 619 delete_option('tpre'); 620 add_option('tpre',$_POST['dbprefix']); 621 } 622 623 624 } 625 626 switch ($step) 627 { 628 default: 629 case 0 : 630 $this->greet(); 631 break; 632 case 1 : 633 $this->import_categories(); 634 break; 635 case 2 : 636 $this->import_users(); 637 break; 638 case 3 : 639 $this->import_posts(); 640 break; 641 case 4 : 642 $this->import_comments(); 643 break; 644 case 5 : 645 $this->import_links(); 646 break; 647 case 6 : 648 $this->cleanup_txpimport(); 649 break; 650 } 651 652 $this->footer(); 653 } 654 655 function Textpattern_Import() 656 { 657 // Nothing. 658 } 659 } 660 661 $txp_import = new Textpattern_Import(); 662 register_importer('textpattern', 'Textpattern', __('Import posts from a Textpattern Blog'), array ($txp_import, 'dispatch')); 663 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |