-
+ 3B5DF893BD2EC915E46237523169C3269AE2D414BAD0C77BD78E380882A7CFE08A399AE86988C1A6DB618810FE2A59039E9128CA98B6060F6708A1302A8C9AB6mp-wp/wp-admin/import/mt.php(0 . 0)(1 . 512)
19303 <?php
19304 /**
19305 * Movable Type and Typepad Importer
19306 *
19307 * @package WordPress
19308 * @subpackage Importer
19309 */
19310
19311 /**
19312 * Moveable Type and Typepad Importer class
19313 *
19314 * Upload your exported Movable Type or Typepad entries into WordPress.
19315 *
19316 * @since unknown
19317 */
19318 class MT_Import {
19319
19320 var $posts = array ();
19321 var $file;
19322 var $id;
19323 var $mtnames = array ();
19324 var $newauthornames = array ();
19325 var $j = -1;
19326
19327 function header() {
19328 echo '<div class="wrap">';
19329 screen_icon();
19330 echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
19331 }
19332
19333 function footer() {
19334 echo '</div>';
19335 }
19336
19337 function greet() {
19338 $this->header();
19339 ?>
19340 <div class="narrow">
19341 <p><?php _e('Howdy! We’re about to begin importing all of your Movable Type or Typepad entries into WordPress. To begin, either choose a file to upload and click "Upload file and import," or use FTP to upload your MT export file as <code>mt-export.txt</code> in your <code>/wp-content/</code> directory and then click "Import mt-export.txt"'); ?></p>
19342
19343 <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
19344 <form method="post" action="<?php echo add_query_arg('step', 1); ?>" class="import-upload-form">
19345
19346 <?php wp_nonce_field('import-upload'); ?>
19347 <p>
19348 <input type="hidden" name="upload_type" value="ftp" />
19349 <?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory'); ?></p>
19350 <p class="submit">
19351 <input type="submit" class="button" value="<?php echo attribute_escape(__('Import mt-export.txt')); ?>" />
19352 </p>
19353 </form>
19354 <p><?php _e('The importer is smart enough not to import duplicates, so you can run this multiple times without worry if—for whatever reason—it doesn\'t finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces.'); ?> </p>
19355 </div>
19356 <?php
19357 $this->footer();
19358 }
19359
19360 function users_form($n) {
19361 global $wpdb;
19362 $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
19363 ?><select name="userselect[<?php echo $n; ?>]">
19364 <option value="#NONE#"><?php _e('- Select -') ?></option>
19365 <?php
19366
19367
19368 foreach ($users as $user) {
19369 echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
19370 }
19371 ?>
19372 </select>
19373 <?php
19374
19375 }
19376
19377 function has_gzip() {
19378 return is_callable('gzopen');
19379 }
19380
19381 function fopen($filename, $mode='r') {
19382 if ( $this->has_gzip() )
19383 return gzopen($filename, $mode);
19384 return fopen($filename, $mode);
19385 }
19386
19387 function feof($fp) {
19388 if ( $this->has_gzip() )
19389 return gzeof($fp);
19390 return feof($fp);
19391 }
19392
19393 function fgets($fp, $len=8192) {
19394 if ( $this->has_gzip() )
19395 return gzgets($fp, $len);
19396 return fgets($fp, $len);
19397 }
19398
19399 function fclose($fp) {
19400 if ( $this->has_gzip() )
19401 return gzclose($fp);
19402 return fclose($fp);
19403 }
19404
19405 //function to check the authorname and do the mapping
19406 function checkauthor($author) {
19407 //mtnames is an array with the names in the mt import file
19408 $pass = wp_generate_password();
19409 if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
19410 ++ $this->j;
19411 $this->mtnames[$this->j] = $author; //add that new mt author name to an array
19412 $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
19413 if (!$user_id) { //banging my head against the desk now.
19414 if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
19415 $user_id = wp_create_user($author, $pass);
19416 $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
19417 } else {
19418 $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
19419 }
19420 } else {
19421 return $user_id; // return pre-existing wp username if it exists
19422 }
19423 } else {
19424 $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
19425 $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
19426 }
19427
19428 return $user_id;
19429 }
19430
19431 function get_mt_authors() {
19432 $temp = array();
19433 $authors = array();
19434
19435 $handle = $this->fopen($this->file, 'r');
19436 if ( $handle == null )
19437 return false;
19438
19439 $in_comment = false;
19440 while ( $line = $this->fgets($handle) ) {
19441 $line = trim($line);
19442
19443 if ( 'COMMENT:' == $line )
19444 $in_comment = true;
19445 else if ( '-----' == $line )
19446 $in_comment = false;
19447
19448 if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
19449 continue;
19450
19451 $temp[] = trim( substr($line, strlen("AUTHOR:")) );
19452 }
19453
19454 //we need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
19455 $authors[0] = array_shift($temp);
19456 $y = count($temp) + 1;
19457 for ($x = 1; $x < $y; $x ++) {
19458 $next = array_shift($temp);
19459 if (!(in_array($next, $authors)))
19460 array_push($authors, "$next");
19461 }
19462
19463 $this->fclose($handle);
19464
19465 return $authors;
19466 }
19467
19468 function get_authors_from_post() {
19469 $formnames = array ();
19470 $selectnames = array ();
19471
19472 foreach ($_POST['user'] as $key => $line) {
19473 $newname = trim(stripslashes($line));
19474 if ($newname == '')
19475 $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form.
19476 array_push($formnames, "$newname");
19477 } // $formnames is the array with the form entered names
19478
19479 foreach ($_POST['userselect'] as $user => $key) {
19480 $selected = trim(stripslashes($key));
19481 array_push($selectnames, "$selected");
19482 }
19483
19484 $count = count($formnames);
19485 for ($i = 0; $i < $count; $i ++) {
19486 if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
19487 array_push($this->newauthornames, "$selectnames[$i]");
19488 } else {
19489 array_push($this->newauthornames, "$formnames[$i]");
19490 }
19491 }
19492 }
19493
19494 function mt_authors_form() {
19495 ?>
19496 <div class="wrap">
19497 <?php screen_icon(); ?>
19498 <h2><?php _e('Assign Authors'); ?></h2>
19499 <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as admin\'s entries.'); ?></p>
19500 <p><?php _e('Below, you can see the names of the authors of the MovableType posts in <em>italics</em>. For each of these names, you can either pick an author in your WordPress installation from the menu, or enter a name for the author in the textbox.'); ?></p>
19501 <p><?php _e('If a new user is created by WordPress, a password will be randomly generated. Manually change the user\'s details if necessary.'); ?></p>
19502 <?php
19503
19504
19505 $authors = $this->get_mt_authors();
19506 echo '<ol id="authors">';
19507 echo '<form action="?import=mt&step=2&id=' . $this->id . '" method="post">';
19508 wp_nonce_field('import-mt');
19509 $j = -1;
19510 foreach ($authors as $author) {
19511 ++ $j;
19512 echo '<li><label>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br />');
19513 $this->users_form($j);
19514 echo '</label></li>';
19515 }
19516
19517 echo '<p class="submit"><input type="submit" class="button" value="'.__('Submit').'"></p>'.'<br />';
19518 echo '</form>';
19519 echo '</ol></div>';
19520
19521 }
19522
19523 function select_authors() {
19524 if ( $_POST['upload_type'] === 'ftp' ) {
19525 $file['file'] = WP_CONTENT_DIR . '/mt-export.txt';
19526 if ( !file_exists($file['file']) )
19527 $file['error'] = __('<code>mt-export.txt</code> does not exist');
19528 } else {
19529 $file = wp_import_handle_upload();
19530 }
19531 if ( isset($file['error']) ) {
19532 $this->header();
19533 echo '<p>'.__('Sorry, there has been an error').'.</p>';
19534 echo '<p><strong>' . $file['error'] . '</strong></p>';
19535 $this->footer();
19536 return;
19537 }
19538 $this->file = $file['file'];
19539 $this->id = (int) $file['id'];
19540
19541 $this->mt_authors_form();
19542 }
19543
19544 function save_post(&$post, &$comments, &$pings) {
19545 // Reset the counter
19546 set_time_limit(30);
19547 $post = get_object_vars($post);
19548 $post = add_magic_quotes($post);
19549 $post = (object) $post;
19550
19551 if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
19552 echo '<li>';
19553 printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title));
19554 } else {
19555 echo '<li>';
19556 printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title));
19557
19558 if ( '' != trim( $post->extended ) )
19559 $post->post_content .= "\n<!--more-->\n$post->extended";
19560
19561 $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
19562 $post_id = wp_insert_post($post);
19563 if ( is_wp_error( $post_id ) )
19564 return $post_id;
19565
19566 // Add categories.
19567 if ( 0 != count($post->categories) ) {
19568 wp_create_categories($post->categories, $post_id);
19569 }
19570
19571 // Add tags or keywords
19572 if ( 1 < strlen($post->post_keywords) ) {
19573 // Keywords exist.
19574 printf(__('<br />Adding tags <i>%s</i>...'), stripslashes($post->post_keywords));
19575 wp_add_post_tags($post_id, $post->post_keywords);
19576 }
19577 }
19578
19579 $num_comments = 0;
19580 foreach ( $comments as $comment ) {
19581 $comment = get_object_vars($comment);
19582 $comment = add_magic_quotes($comment);
19583
19584 if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
19585 $comment['comment_post_ID'] = $post_id;
19586 $comment = wp_filter_comment($comment);
19587 wp_insert_comment($comment);
19588 $num_comments++;
19589 }
19590 }
19591
19592 if ( $num_comments )
19593 printf(' '.__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
19594
19595 $num_pings = 0;
19596 foreach ( $pings as $ping ) {
19597 $ping = get_object_vars($ping);
19598 $ping = add_magic_quotes($ping);
19599
19600 if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
19601 $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
19602 $ping['comment_post_ID'] = $post_id;
19603 $ping = wp_filter_comment($ping);
19604 wp_insert_comment($ping);
19605 $num_pings++;
19606 }
19607 }
19608
19609 if ( $num_pings )
19610 printf(' '.__ngettext('(%s ping)', '(%s pings)', $num_pings), $num_pings);
19611
19612 echo "</li>";
19613 //ob_flush();flush();
19614 }
19615
19616 function process_posts() {
19617 global $wpdb;
19618
19619 $handle = $this->fopen($this->file, 'r');
19620 if ( $handle == null )
19621 return false;
19622
19623 $context = '';
19624 $post = new StdClass();
19625 $comment = new StdClass();
19626 $comments = array();
19627 $ping = new StdClass();
19628 $pings = array();
19629
19630 echo "<div class='wrap'><ol>";
19631
19632 while ( $line = $this->fgets($handle) ) {
19633 $line = trim($line);
19634
19635 if ( '-----' == $line ) {
19636 // Finishing a multi-line field
19637 if ( 'comment' == $context ) {
19638 $comments[] = $comment;
19639 $comment = new StdClass();
19640 } else if ( 'ping' == $context ) {
19641 $pings[] = $ping;
19642 $ping = new StdClass();
19643 }
19644 $context = '';
19645 } else if ( '--------' == $line ) {
19646 // Finishing a post.
19647 $context = '';
19648 $result = $this->save_post($post, $comments, $pings);
19649 if ( is_wp_error( $result ) )
19650 return $result;
19651 $post = new StdClass;
19652 $comment = new StdClass();
19653 $ping = new StdClass();
19654 $comments = array();
19655 $pings = array();
19656 } else if ( 'BODY:' == $line ) {
19657 $context = 'body';
19658 } else if ( 'EXTENDED BODY:' == $line ) {
19659 $context = 'extended';
19660 } else if ( 'EXCERPT:' == $line ) {
19661 $context = 'excerpt';
19662 } else if ( 'KEYWORDS:' == $line ) {
19663 $context = 'keywords';
19664 } else if ( 'COMMENT:' == $line ) {
19665 $context = 'comment';
19666 } else if ( 'PING:' == $line ) {
19667 $context = 'ping';
19668 } else if ( 0 === strpos($line, "AUTHOR:") ) {
19669 $author = trim( substr($line, strlen("AUTHOR:")) );
19670 if ( '' == $context )
19671 $post->post_author = $author;
19672 else if ( 'comment' == $context )
19673 $comment->comment_author = $author;
19674 } else if ( 0 === strpos($line, "TITLE:") ) {
19675 $title = trim( substr($line, strlen("TITLE:")) );
19676 if ( '' == $context )
19677 $post->post_title = $title;
19678 else if ( 'ping' == $context )
19679 $ping->title = $title;
19680 } else if ( 0 === strpos($line, "STATUS:") ) {
19681 $status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
19682 if ( empty($status) )
19683 $status = 'publish';
19684 $post->post_status = $status;
19685 } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
19686 $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
19687 if ( $allow == 1 )
19688 $post->comment_status = 'open';
19689 else
19690 $post->comment_status = 'closed';
19691 } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
19692 $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
19693 if ( $allow == 1 )
19694 $post->ping_status = 'open';
19695 else
19696 $post->ping_status = 'closed';
19697 } else if ( 0 === strpos($line, "CATEGORY:") ) {
19698 $category = trim( substr($line, strlen("CATEGORY:")) );
19699 if ( '' != $category )
19700 $post->categories[] = $category;
19701 } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
19702 $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
19703 if ( '' != $category )
19704 $post->categories[] = $category;
19705 } else if ( 0 === strpos($line, "DATE:") ) {
19706 $date = trim( substr($line, strlen("DATE:")) );
19707 $date = strtotime($date);
19708 $date = date('Y-m-d H:i:s', $date);
19709 $date_gmt = get_gmt_from_date($date);
19710 if ( '' == $context ) {
19711 $post->post_modified = $date;
19712 $post->post_modified_gmt = $date_gmt;
19713 $post->post_date = $date;
19714 $post->post_date_gmt = $date_gmt;
19715 } else if ( 'comment' == $context ) {
19716 $comment->comment_date = $date;
19717 } else if ( 'ping' == $context ) {
19718 $ping->comment_date = $date;
19719 }
19720 } else if ( 0 === strpos($line, "EMAIL:") ) {
19721 $email = trim( substr($line, strlen("EMAIL:")) );
19722 if ( 'comment' == $context )
19723 $comment->comment_author_email = $email;
19724 else
19725 $ping->comment_author_email = '';
19726 } else if ( 0 === strpos($line, "IP:") ) {
19727 $ip = trim( substr($line, strlen("IP:")) );
19728 if ( 'comment' == $context )
19729 $comment->comment_author_IP = $ip;
19730 else
19731 $ping->comment_author_IP = $ip;
19732 } else if ( 0 === strpos($line, "URL:") ) {
19733 $url = trim( substr($line, strlen("URL:")) );
19734 if ( 'comment' == $context )
19735 $comment->comment_author_url = $url;
19736 else
19737 $ping->comment_author_url = $url;
19738 } else if ( 0 === strpos($line, "BLOG NAME:") ) {
19739 $blog = trim( substr($line, strlen("BLOG NAME:")) );
19740 $ping->comment_author = $blog;
19741 } else {
19742 // Processing multi-line field, check context.
19743
19744 $line .= "\n";
19745 if ( 'body' == $context ) {
19746 $post->post_content .= $line;
19747 } else if ( 'extended' == $context ) {
19748 $post->extended .= $line;
19749 } else if ( 'excerpt' == $context ) {
19750 $post->post_excerpt .= $line;
19751 } else if ( 'keywords' == $context ) {
19752 $post->post_keywords .= $line;
19753 } else if ( 'comment' == $context ) {
19754 $comment->comment_content .= $line;
19755 } else if ( 'ping' == $context ) {
19756 $ping->comment_content .= $line;
19757 }
19758 }
19759 }
19760
19761 $this->fclose($handle);
19762
19763 echo '</ol>';
19764
19765 wp_import_cleanup($this->id);
19766 do_action('import_done', 'mt');
19767
19768 echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
19769 }
19770
19771 function import() {
19772 $this->id = (int) $_GET['id'];
19773 if ( $this->id == 0 )
19774 $this->file = WP_CONTENT_DIR . '/mt-export.txt';
19775 else
19776 $this->file = get_attached_file($this->id);
19777 $this->get_authors_from_post();
19778 $result = $this->process_posts();
19779 if ( is_wp_error( $result ) )
19780 return $result;
19781 }
19782
19783 function dispatch() {
19784 if (empty ($_GET['step']))
19785 $step = 0;
19786 else
19787 $step = (int) $_GET['step'];
19788
19789 switch ($step) {
19790 case 0 :
19791 $this->greet();
19792 break;
19793 case 1 :
19794 check_admin_referer('import-upload');
19795 $this->select_authors();
19796 break;
19797 case 2:
19798 check_admin_referer('import-mt');
19799 $result = $this->import();
19800 if ( is_wp_error( $result ) )
19801 echo $result->get_error_message();
19802 break;
19803 }
19804 }
19805
19806 function MT_Import() {
19807 // Nothing.
19808 }
19809 }
19810
19811 $mt_import = new MT_Import();
19812
19813 register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or Typepad blog.'), array ($mt_import, 'dispatch'));
19814 ?>