| [ Index ] |
|
Code source de WordPress 2.1.2 |
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 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 $this->db_form(); 60 echo '<p class="submit"><input type="submit" name="submit" value="'.__('Import Categories').' »" /></p>'; 61 echo '</form>'; 62 echo '</div>'; 63 } 64 65 function get_txp_cats() 66 { 67 global $wpdb; 68 // General Housekeeping 69 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); 70 set_magic_quotes_runtime(0); 71 $prefix = get_option('tpre'); 72 73 // Get Categories 74 return $txpdb->get_results('SELECT 75 id, 76 name, 77 title 78 FROM '.$prefix.'txp_category 79 WHERE type = "article"', 80 ARRAY_A); 81 } 82 83 function get_txp_users() 84 { 85 global $wpdb; 86 // General Housekeeping 87 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); 88 set_magic_quotes_runtime(0); 89 $prefix = get_option('tpre'); 90 91 // Get Users 92 93 return $txpdb->get_results('SELECT 94 user_id, 95 name, 96 RealName, 97 email, 98 privs 99 FROM '.$prefix.'txp_users', ARRAY_A); 100 } 101 102 function get_txp_posts() 103 { 104 // General Housekeeping 105 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); 106 set_magic_quotes_runtime(0); 107 $prefix = get_option('tpre'); 108 109 // Get Posts 110 return $txpdb->get_results('SELECT 111 ID, 112 Posted, 113 AuthorID, 114 LastMod, 115 Title, 116 Body, 117 Excerpt, 118 Category1, 119 Category2, 120 Status, 121 Keywords, 122 url_title, 123 comments_count 124 FROM '.$prefix.'textpattern 125 ', ARRAY_A); 126 } 127 128 function get_txp_comments() 129 { 130 global $wpdb; 131 // General Housekeeping 132 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); 133 set_magic_quotes_runtime(0); 134 $prefix = get_option('tpre'); 135 136 // Get Comments 137 return $txpdb->get_results('SELECT * FROM '.$prefix.'txp_discuss', ARRAY_A); 138 } 139 140 function get_txp_links() 141 { 142 //General Housekeeping 143 $txpdb = new wpdb(get_option('txpuser'), get_option('txppass'), get_option('txpname'), get_option('txphost')); 144 set_magic_quotes_runtime(0); 145 $prefix = get_option('tpre'); 146 147 return $txpdb->get_results('SELECT 148 id, 149 date, 150 category, 151 url, 152 linkname, 153 description 154 FROM '.$prefix.'txp_link', 155 ARRAY_A); 156 } 157 158 function cat2wp($categories='') 159 { 160 // General Housekeeping 161 global $wpdb; 162 $count = 0; 163 $txpcat2wpcat = array(); 164 // Do the Magic 165 if(is_array($categories)) 166 { 167 echo '<p>'.__('Importing Categories...').'<br /><br /></p>'; 168 foreach ($categories as $category) 169 { 170 $count++; 171 extract($category); 172 173 174 // Make Nice Variables 175 $name = $wpdb->escape($name); 176 $title = $wpdb->escape($title); 177 178 if($cinfo = category_exists($name)) 179 { 180 $ret_id = wp_insert_category(array('cat_ID' => $cinfo, 'category_nicename' => $name, 'cat_name' => $title)); 181 } 182 else 183 { 184 $ret_id = wp_insert_category(array('category_nicename' => $name, 'cat_name' => $title)); 185 } 186 $txpcat2wpcat[$id] = $ret_id; 187 } 188 189 // Store category translation for future use 190 add_option('txpcat2wpcat',$txpcat2wpcat); 191 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> categories imported.'), $count).'<br /><br /></p>'; 192 return true; 193 } 194 echo __('No Categories to Import!'); 195 return false; 196 } 197 198 function users2wp($users='') 199 { 200 // General Housekeeping 201 global $wpdb; 202 $count = 0; 203 $txpid2wpid = array(); 204 205 // Midnight Mojo 206 if(is_array($users)) 207 { 208 echo '<p>'.__('Importing Users...').'<br /><br /></p>'; 209 foreach($users as $user) 210 { 211 $count++; 212 extract($user); 213 214 // Make Nice Variables 215 $name = $wpdb->escape($name); 216 $RealName = $wpdb->escape($RealName); 217 218 if($uinfo = get_userdatabylogin($name)) 219 { 220 221 $ret_id = wp_insert_user(array( 222 'ID' => $uinfo->ID, 223 'user_login' => $name, 224 'user_nicename' => $RealName, 225 'user_email' => $email, 226 'user_url' => 'http://', 227 'display_name' => $name) 228 ); 229 } 230 else 231 { 232 $ret_id = wp_insert_user(array( 233 'user_login' => $name, 234 'user_nicename' => $RealName, 235 'user_email' => $email, 236 'user_url' => 'http://', 237 'display_name' => $name) 238 ); 239 } 240 $txpid2wpid[$user_id] = $ret_id; 241 242 // Set Textpattern-to-WordPress permissions translation 243 $transperms = array(1 => '10', 2 => '9', 3 => '5', 4 => '4', 5 => '3', 6 => '2', 7 => '0'); 244 245 // Update Usermeta Data 246 $user = new WP_User($ret_id); 247 if('10' == $transperms[$privs]) { $user->set_role('administrator'); } 248 if('9' == $transperms[$privs]) { $user->set_role('editor'); } 249 if('5' == $transperms[$privs]) { $user->set_role('editor'); } 250 if('4' == $transperms[$privs]) { $user->set_role('author'); } 251 if('3' == $transperms[$privs]) { $user->set_role('contributor'); } 252 if('2' == $transperms[$privs]) { $user->set_role('contributor'); } 253 if('0' == $transperms[$privs]) { $user->set_role('subscriber'); } 254 255 update_usermeta( $ret_id, 'wp_user_level', $transperms[$privs] ); 256 update_usermeta( $ret_id, 'rich_editing', 'false'); 257 }// End foreach($users as $user) 258 259 // Store id translation array for future use 260 add_option('txpid2wpid',$txpid2wpid); 261 262 263 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> users imported.'), $count).'<br /><br /></p>'; 264 return true; 265 }// End if(is_array($users) 266 267 echo __('No Users to Import!'); 268 return false; 269 270 }// End function user2wp() 271 272 function posts2wp($posts='') 273 { 274 // General Housekeeping 275 global $wpdb; 276 $count = 0; 277 $txpposts2wpposts = array(); 278 $cats = array(); 279 280 // Do the Magic 281 if(is_array($posts)) 282 { 283 echo '<p>'.__('Importing Posts...').'<br /><br /></p>'; 284 foreach($posts as $post) 285 { 286 $count++; 287 extract($post); 288 289 // Set Textpattern-to-WordPress status translation 290 $stattrans = array(1 => 'draft', 2 => 'private', 3 => 'draft', 4 => 'publish', 5 => 'publish'); 291 292 //Can we do this more efficiently? 293 $uinfo = ( get_userdatabylogin( $AuthorID ) ) ? get_userdatabylogin( $AuthorID ) : 1; 294 $authorid = ( is_object( $uinfo ) ) ? $uinfo->ID : $uinfo ; 295 296 $Title = $wpdb->escape($Title); 297 $Body = $wpdb->escape($Body); 298 $Excerpt = $wpdb->escape($Excerpt); 299 $post_status = $stattrans[$Status]; 300 301 // Import Post data into WordPress 302 303 if($pinfo = post_exists($Title,$Body)) 304 { 305 $ret_id = wp_insert_post(array( 306 'ID' => $pinfo, 307 'post_date' => $Posted, 308 'post_date_gmt' => $post_date_gmt, 309 'post_author' => $authorid, 310 'post_modified' => $LastMod, 311 'post_modified_gmt' => $post_modified_gmt, 312 'post_title' => $Title, 313 'post_content' => $Body, 314 'post_excerpt' => $Excerpt, 315 'post_status' => $post_status, 316 'post_name' => $url_title, 317 'comment_count' => $comments_count) 318 ); 319 } 320 else 321 { 322 $ret_id = wp_insert_post(array( 323 'post_date' => $Posted, 324 'post_date_gmt' => $post_date_gmt, 325 'post_author' => $authorid, 326 'post_modified' => $LastMod, 327 'post_modified_gmt' => $post_modified_gmt, 328 'post_title' => $Title, 329 'post_content' => $Body, 330 'post_excerpt' => $Excerpt, 331 'post_status' => $post_status, 332 'post_name' => $url_title, 333 'comment_count' => $comments_count) 334 ); 335 } 336 $txpposts2wpposts[$ID] = $ret_id; 337 338 // Make Post-to-Category associations 339 $cats = array(); 340 if($cat1 = get_catbynicename($Category1)) { $cats[1] = $cat1; } 341 if($cat2 = get_catbynicename($Category2)) { $cats[2] = $cat2; } 342 343 if(!empty($cats)) { wp_set_post_categories($ret_id, $cats); } 344 } 345 } 346 // Store ID translation for later use 347 add_option('txpposts2wpposts',$txpposts2wpposts); 348 349 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> posts imported.'), $count).'<br /><br /></p>'; 350 return true; 351 } 352 353 function comments2wp($comments='') 354 { 355 // General Housekeeping 356 global $wpdb; 357 $count = 0; 358 $txpcm2wpcm = array(); 359 $postarr = get_option('txpposts2wpposts'); 360 361 // Magic Mojo 362 if(is_array($comments)) 363 { 364 echo '<p>'.__('Importing Comments...').'<br /><br /></p>'; 365 foreach($comments as $comment) 366 { 367 $count++; 368 extract($comment); 369 370 // WordPressify Data 371 $comment_ID = ltrim($discussid, '0'); 372 $comment_post_ID = $postarr[$parentid]; 373 $comment_approved = (1 == $visible) ? 1 : 0; 374 $name = $wpdb->escape($name); 375 $email = $wpdb->escape($email); 376 $web = $wpdb->escape($web); 377 $message = $wpdb->escape($message); 378 379 if($cinfo = comment_exists($name, $posted)) 380 { 381 // Update comments 382 $ret_id = wp_update_comment(array( 383 'comment_ID' => $cinfo, 384 'comment_post_ID' => $comment_post_ID, 385 'comment_author' => $name, 386 'comment_author_email' => $email, 387 'comment_author_url' => $web, 388 'comment_date' => $posted, 389 'comment_content' => $message, 390 'comment_approved' => $comment_approved) 391 ); 392 } 393 else 394 { 395 // Insert comments 396 $ret_id = wp_insert_comment(array( 397 'comment_post_ID' => $comment_post_ID, 398 'comment_author' => $name, 399 'comment_author_email' => $email, 400 'comment_author_url' => $web, 401 'comment_author_IP' => $ip, 402 'comment_date' => $posted, 403 'comment_content' => $message, 404 'comment_approved' => $comment_approved) 405 ); 406 } 407 $txpcm2wpcm[$comment_ID] = $ret_id; 408 } 409 // Store Comment ID translation for future use 410 add_option('txpcm2wpcm', $txpcm2wpcm); 411 412 // Associate newly formed categories with posts 413 get_comment_count($ret_id); 414 415 416 echo '<p>'.sprintf(__('Done! <strong>%1$s</strong> comments imported.'), $count).'<br /><br /></p>'; 417 return true; 418 } 419 echo __('No Comments to Import!'); 420 return false; 421 } 422 423 function links2wp($links='') 424 { 425 // General Housekeeping 426 global $wpdb; 427 $count = 0; 428 429 // Deal with the links 430 if(is_array($links)) 431 { 432 echo '<p>'.__('Importing Links...').'<br /><br /></p>'; 433 foreach($links as $link) 434 { 435 $count++; 436 extract($link); 437 438 // Make nice vars 439 $category = $wpdb->escape($category); 440 $linkname = $wpdb->escape($linkname); 441 $description = $wpdb->escape($description); 442 443 if($linfo = link_exists($linkname)) 444 { 445 $ret_id = wp_insert_link(array( 446 'link_id' => $linfo, 447 'link_url' => $url, 448 'link_name' => $linkname, 449 'link_category' => $category, 450 'link_description' => $description, 451 'link_updated' => $date) 452 ); 453 } 454 else 455 { 456 $ret_id = wp_insert_link(array( 457 'link_url' => $url, 458 'link_name' => $linkname, 459 'link_category' => $category, 460 'link_description' => $description, 461 'link_updated' => $date) 462 ); 463 } 464 $txplinks2wplinks[$link_id] = $ret_id; 465 } 466 add_option('txplinks2wplinks',$txplinks2wplinks); 467 echo '<p>'; 468 printf(__('Done! <strong>%s</strong> Links imported'), $count); 469 echo '<br /><br /></p>'; 470 return true; 471 } 472 echo __('No Links to Import!'); 473 return false; 474 } 475 476 function import_categories() 477 { 478 // Category Import 479 $cats = $this->get_txp_cats(); 480 $this->cat2wp($cats); 481 add_option('txp_cats', $cats); 482 483 484 485 echo '<form action="admin.php?import=textpattern&step=2" method="post">'; 486 printf('<input type="submit" name="submit" value="%s" />', __('Import Users')); 487 echo '</form>'; 488 489 } 490 491 function import_users() 492 { 493 // User Import 494 $users = $this->get_txp_users(); 495 $this->users2wp($users); 496 497 echo '<form action="admin.php?import=textpattern&step=3" method="post">'; 498 printf('<input type="submit" name="submit" value="%s" />', __('Import Posts')); 499 echo '</form>'; 500 } 501 502 function import_posts() 503 { 504 // Post Import 505 $posts = $this->get_txp_posts(); 506 $this->posts2wp($posts); 507 508 echo '<form action="admin.php?import=textpattern&step=4" method="post">'; 509 printf('<input type="submit" name="submit" value="%s" />', __('Import Comments')); 510 echo '</form>'; 511 } 512 513 function import_comments() 514 { 515 // Comment Import 516 $comments = $this->get_txp_comments(); 517 $this->comments2wp($comments); 518 519 echo '<form action="admin.php?import=textpattern&step=5" method="post">'; 520 printf('<input type="submit" name="submit" value="%s" />', __('Import Links')); 521 echo '</form>'; 522 } 523 524 function import_links() 525 { 526 //Link Import 527 $links = $this->get_txp_links(); 528 $this->links2wp($links); 529 add_option('txp_links', $links); 530 531 echo '<form action="admin.php?import=textpattern&step=6" method="post">'; 532 printf('<input type="submit" name="submit" value="%s" />', __('Finish')); 533 echo '</form>'; 534 } 535 536 function cleanup_txpimport() 537 { 538 delete_option('tpre'); 539 delete_option('txp_cats'); 540 delete_option('txpid2wpid'); 541 delete_option('txpcat2wpcat'); 542 delete_option('txpposts2wpposts'); 543 delete_option('txpcm2wpcm'); 544 delete_option('txplinks2wplinks'); 545 delete_option('txpuser'); 546 delete_option('txppass'); 547 delete_option('txpname'); 548 delete_option('txphost'); 549 $this->tips(); 550 } 551 552 function tips() 553 { 554 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>'; 555 echo '<h3>'.__('Users').'</h3>'; 556 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>'; 557 echo '<h3>'.__('Preserving Authors').'</h3>'; 558 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>'; 559 echo '<h3>'.__('Textile').'</h3>'; 560 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>'; 561 echo '<h3>'.__('WordPress Resources').'</h3>'; 562 echo '<p>'.__('Finally, there are numerous WordPress resources around the internet. Some of them are:').'</p>'; 563 echo '<ul>'; 564 echo '<li>'.__('<a href="http://www.wordpress.org">The official WordPress site</a>').'</li>'; 565 echo '<li>'.__('<a href="http://wordpress.org/support/">The WordPress support forums</a>').'</li>'; 566 echo '<li>'.__('<a href="http://codex.wordpress.org">The Codex (In other words, the WordPress Bible)</a>').'</li>'; 567 echo '</ul>'; 568 echo '<p>'.sprintf(__('That\'s it! What are you waiting for? Go <a href="%1$s">login</a>!'), '/wp-login.php').'</p>'; 569 } 570 571 function db_form() 572 { 573 echo '<table class="editform">'; 574 printf('<tr><th scope="row"><label for="dbuser">%s</label></th><td><input type="text" name="dbuser" id="dbuser" /></td></tr>', __('Textpattern Database User:')); 575 printf('<tr><th scope="row"><label for="dbpass">%s</label></th><td><input type="password" name="dbpass" id="dbpass" /></td></tr>', __('Textpattern Database Password:')); 576 printf('<tr><th scope="row"><label for="dbname">%s</label></th><td><input type="text" id="dbname" name="dbname" /></td></tr>', __('Textpattern Database Name:')); 577 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:')); 578 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):')); 579 echo '</table>'; 580 } 581 582 function dispatch() 583 { 584 585 if (empty ($_GET['step'])) 586 $step = 0; 587 else 588 $step = (int) $_GET['step']; 589 $this->header(); 590 591 if ( $step > 0 ) 592 { 593 if($_POST['dbuser']) 594 { 595 if(get_option('txpuser')) 596 delete_option('txpuser'); 597 add_option('txpuser',$_POST['dbuser']); 598 } 599 if($_POST['dbpass']) 600 { 601 if(get_option('txppass')) 602 delete_option('txppass'); 603 add_option('txppass',$_POST['dbpass']); 604 } 605 606 if($_POST['dbname']) 607 { 608 if(get_option('txpname')) 609 delete_option('txpname'); 610 add_option('txpname',$_POST['dbname']); 611 } 612 if($_POST['dbhost']) 613 { 614 if(get_option('txphost')) 615 delete_option('txphost'); 616 add_option('txphost',$_POST['dbhost']); 617 } 618 if($_POST['dbprefix']) 619 { 620 if(get_option('tpre')) 621 delete_option('tpre'); 622 add_option('tpre',$_POST['dbprefix']); 623 } 624 625 626 } 627 628 switch ($step) 629 { 630 default: 631 case 0 : 632 $this->greet(); 633 break; 634 case 1 : 635 $this->import_categories(); 636 break; 637 case 2 : 638 $this->import_users(); 639 break; 640 case 3 : 641 $this->import_posts(); 642 break; 643 case 4 : 644 $this->import_comments(); 645 break; 646 case 5 : 647 $this->import_links(); 648 break; 649 case 6 : 650 $this->cleanup_txpimport(); 651 break; 652 } 653 654 $this->footer(); 655 } 656 657 function Textpattern_Import() 658 { 659 // Nothing. 660 } 661 } 662 663 $txp_import = new Textpattern_Import(); 664 register_importer('textpattern', __('Textpattern'), __('Import categories, users, posts, comments, and links from a Textpattern blog'), array ($txp_import, 'dispatch')); 665 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |