| [ 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