[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-admin/import/ -> mt.php (source)

   1  <?php
   2  /**
   3   * Movable Type and TypePad Importer
   4   *
   5   * @package WordPress
   6   * @subpackage Importer
   7   */
   8  
   9  /**
  10   * Moveable Type and TypePad Importer class
  11   *
  12   * Upload your exported Movable Type or TypePad entries into WordPress.
  13   *
  14   * @since unknown
  15   */
  16  class MT_Import {
  17  
  18      var $posts = array ();
  19      var $file;
  20      var $id;
  21      var $mtnames = array ();
  22      var $newauthornames = array ();
  23      var $j = -1;
  24  
  25  	function header() {
  26          echo '<div class="wrap">';
  27          screen_icon();
  28          echo '<h2>'.__('Import Movable Type or TypePad').'</h2>';
  29      }
  30  
  31  	function footer() {
  32          echo '</div>';
  33      }
  34  
  35  	function greet() {
  36          $this->header();
  37  ?>
  38  <div class="narrow">
  39  <p><?php _e( 'Howdy! We are about to begin importing all of your Movable Type or TypePad entries into WordPress. To begin, either choose a file to upload and click &#8220;Upload file and import&#8221;, 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 &#8220;Import mt-export.txt&#8221;.' ); ?></p>
  40  
  41  <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
  42  <form method="post" action="<?php echo esc_attr(add_query_arg('step', 1)); ?>" class="import-upload-form">
  43  
  44  <?php wp_nonce_field('import-upload'); ?>
  45  <p>
  46      <input type="hidden" name="upload_type" value="ftp" />
  47  <?php _e('Or use <code>mt-export.txt</code> in your <code>/wp-content/</code> directory'); ?></p>
  48  <p class="submit">
  49  <input type="submit" class="button" value="<?php esc_attr_e('Import mt-export.txt'); ?>" />
  50  </p>
  51  </form>
  52  <p><?php _e('The importer is smart enough not to import duplicates, so you can run this multiple times without worry if&#8212;for whatever reason&#8212;it doesn&#8217;t finish. If you get an <strong>out of memory</strong> error try splitting up the import file into pieces.'); ?> </p>
  53  </div>
  54  <?php
  55          $this->footer();
  56      }
  57  
  58  	function users_form($n) {
  59          $users = get_users_of_blog();
  60  ?><select name="userselect[<?php echo $n; ?>]">
  61      <option value="#NONE#"><?php _e('&mdash; Select &mdash;') ?></option>
  62      <?php
  63          foreach ( $users as $user )
  64              echo '<option value="' . $user->user_login . '">' . $user->user_login . '</option>';
  65      ?>
  66      </select>
  67      <?php
  68      }
  69  
  70  	function has_gzip() {
  71          return is_callable('gzopen');
  72      }
  73  
  74  	function fopen($filename, $mode='r') {
  75          if ( $this->has_gzip() )
  76              return gzopen($filename, $mode);
  77          return fopen($filename, $mode);
  78      }
  79  
  80  	function feof($fp) {
  81          if ( $this->has_gzip() )
  82              return gzeof($fp);
  83          return feof($fp);
  84      }
  85  
  86  	function fgets($fp, $len=8192) {
  87          if ( $this->has_gzip() )
  88              return gzgets($fp, $len);
  89          return fgets($fp, $len);
  90      }
  91  
  92  	function fclose($fp) {
  93          if ( $this->has_gzip() )
  94              return gzclose($fp);
  95          return fclose($fp);
  96       }
  97  
  98      //function to check the authorname and do the mapping
  99  	function checkauthor($author) {
 100          //mtnames is an array with the names in the mt import file
 101          $pass = wp_generate_password();
 102          if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
 103              ++ $this->j;
 104              $this->mtnames[$this->j] = $author; //add that new mt author name to an array
 105              $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
 106              if (!$user_id) { //banging my head against the desk now.
 107                  if ($this->newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
 108                      $user_id = wp_create_user($author, $pass);
 109                      $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
 110                  } else {
 111                      $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
 112                  }
 113              } else {
 114                  return $user_id; // return pre-existing wp username if it exists
 115              }
 116          } else {
 117              $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
 118              $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
 119          }
 120  
 121          return $user_id;
 122      }
 123  
 124  	function get_mt_authors() {
 125          $temp = array();
 126          $authors = array();
 127  
 128          $handle = $this->fopen($this->file, 'r');
 129          if ( $handle == null )
 130              return false;
 131  
 132          $in_comment = false;
 133          while ( $line = $this->fgets($handle) ) {
 134              $line = trim($line);
 135  
 136              if ( 'COMMENT:' == $line )
 137                  $in_comment = true;
 138              else if ( '-----' == $line )
 139                  $in_comment = false;
 140  
 141              if ( $in_comment || 0 !== strpos($line,"AUTHOR:") )
 142                  continue;
 143  
 144              $temp[] = trim( substr($line, strlen("AUTHOR:")) );
 145          }
 146  
 147          //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.
 148          $authors[0] = array_shift($temp);
 149          $y = count($temp) + 1;
 150          for ($x = 1; $x < $y; $x ++) {
 151              $next = array_shift($temp);
 152              if (!(in_array($next, $authors)))
 153                  array_push($authors, $next);
 154          }
 155  
 156          $this->fclose($handle);
 157  
 158          return $authors;
 159      }
 160  
 161  	function get_authors_from_post() {
 162          $formnames = array ();
 163          $selectnames = array ();
 164  
 165          foreach ($_POST['user'] as $key => $line) {
 166              $newname = trim(stripslashes($line));
 167              if ($newname == '')
 168                  $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.
 169              array_push($formnames, $newname);
 170          } // $formnames is the array with the form entered names
 171  
 172          foreach ($_POST['userselect'] as $user => $key) {
 173              $selected = trim(stripslashes($key));
 174              array_push($selectnames, $selected);
 175          }
 176  
 177          $count = count($formnames);
 178          for ($i = 0; $i < $count; $i ++) {
 179              if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
 180                  array_push($this->newauthornames, "$selectnames[$i]");
 181              } else {
 182                  array_push($this->newauthornames, "$formnames[$i]");
 183              }
 184          }
 185      }
 186  
 187  	function mt_authors_form() {
 188  ?>
 189  <div class="wrap">
 190  <?php screen_icon(); ?>
 191  <h2><?php _e('Assign Authors'); ?></h2>
 192  <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&#8217;s entries.'); ?></p>
 193  <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>
 194  <p><?php _e('If a new user is created by WordPress, a password will be randomly generated. Manually change the user&#8217;s details if necessary.'); ?></p>
 195      <?php
 196  
 197  
 198          $authors = $this->get_mt_authors();
 199          echo '<ol id="authors">';
 200          echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
 201          wp_nonce_field('import-mt');
 202          $j = -1;
 203          foreach ($authors as $author) {
 204              ++ $j;
 205              echo '<li><label>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'. esc_attr($author) .'" name="'.'user[]'.'" maxlength="30"> <br />');
 206              $this->users_form($j);
 207              echo '</label></li>';
 208          }
 209  
 210          echo '<p class="submit"><input type="submit" class="button" value="'.esc_attr__('Submit').'"></p>'.'<br />';
 211          echo '</form>';
 212          echo '</ol></div>';
 213  
 214      }
 215  
 216  	function select_authors() {
 217          if ( $_POST['upload_type'] === 'ftp' ) {
 218              $file['file'] = WP_CONTENT_DIR . '/mt-export.txt';
 219              if ( !file_exists($file['file']) )
 220                  $file['error'] = __('<code>mt-export.txt</code> does not exist');
 221          } else {
 222              $file = wp_import_handle_upload();
 223          }
 224          if ( isset($file['error']) ) {
 225              $this->header();
 226              echo '<p>'.__('Sorry, there has been an error').'.</p>';
 227              echo '<p><strong>' . $file['error'] . '</strong></p>';
 228              $this->footer();
 229              return;
 230          }
 231          $this->file = $file['file'];
 232          $this->id = (int) $file['id'];
 233  
 234          $this->mt_authors_form();
 235      }
 236  
 237  	function save_post(&$post, &$comments, &$pings) {
 238          $post = get_object_vars($post);
 239          $post = add_magic_quotes($post);
 240          $post = (object) $post;
 241  
 242          if ( $post_id = post_exists($post->post_title, '', $post->post_date) ) {
 243              echo '<li>';
 244              printf(__('Post <em>%s</em> already exists.'), stripslashes($post->post_title));
 245          } else {
 246              echo '<li>';
 247              printf(__('Importing post <em>%s</em>...'), stripslashes($post->post_title));
 248  
 249              if ( '' != trim( $post->extended ) )
 250                      $post->post_content .= "\n<!--more-->\n$post->extended";
 251  
 252              $post->post_author = $this->checkauthor($post->post_author); //just so that if a post already exists, new users are not created by checkauthor
 253              $post_id = wp_insert_post($post);
 254              if ( is_wp_error( $post_id ) )
 255                  return $post_id;
 256  
 257              // Add categories.
 258              if ( 0 != count($post->categories) ) {
 259                  wp_create_categories($post->categories, $post_id);
 260              }
 261  
 262               // Add tags or keywords
 263              if ( 1 < strlen($post->post_keywords) ) {
 264                   // Keywords exist.
 265                  printf('<br />'.__('Adding tags <em>%s</em>...'), stripslashes($post->post_keywords));
 266                  wp_add_post_tags($post_id, $post->post_keywords);
 267              }
 268          }
 269  
 270          $num_comments = 0;
 271          foreach ( $comments as $comment ) {
 272              $comment = get_object_vars($comment);
 273              $comment = add_magic_quotes($comment);
 274  
 275              if ( !comment_exists($comment['comment_author'], $comment['comment_date'])) {
 276                  $comment['comment_post_ID'] = $post_id;
 277                  $comment = wp_filter_comment($comment);
 278                  wp_insert_comment($comment);
 279                  $num_comments++;
 280              }
 281          }
 282  
 283          if ( $num_comments )
 284              printf(' '._n('(%s comment)', '(%s comments)', $num_comments), $num_comments);
 285  
 286          $num_pings = 0;
 287          foreach ( $pings as $ping ) {
 288              $ping = get_object_vars($ping);
 289              $ping = add_magic_quotes($ping);
 290  
 291              if ( !comment_exists($ping['comment_author'], $ping['comment_date'])) {
 292                  $ping['comment_content'] = "<strong>{$ping['title']}</strong>\n\n{$ping['comment_content']}";
 293                  $ping['comment_post_ID'] = $post_id;
 294                  $ping = wp_filter_comment($ping);
 295                  wp_insert_comment($ping);
 296                  $num_pings++;
 297              }
 298          }
 299  
 300          if ( $num_pings )
 301              printf(' '._n('(%s ping)', '(%s pings)', $num_pings), $num_pings);
 302  
 303          echo "</li>";
 304          //ob_flush();flush();
 305      }
 306  
 307  	function process_posts() {
 308          global $wpdb;
 309  
 310          $handle = $this->fopen($this->file, 'r');
 311          if ( $handle == null )
 312              return false;
 313  
 314          $context = '';
 315          $post = new StdClass();
 316          $comment = new StdClass();
 317          $comments = array();
 318          $ping = new StdClass();
 319          $pings = array();
 320  
 321          echo "<div class='wrap'><ol>";
 322  
 323          while ( $line = $this->fgets($handle) ) {
 324              $line = trim($line);
 325  
 326              if ( '-----' == $line ) {
 327                  // Finishing a multi-line field
 328                  if ( 'comment' == $context ) {
 329                      $comments[] = $comment;
 330                      $comment = new StdClass();
 331                  } else if ( 'ping' == $context ) {
 332                      $pings[] = $ping;
 333                      $ping = new StdClass();
 334                  }
 335                  $context = '';
 336              } else if ( '--------' == $line ) {
 337                  // Finishing a post.
 338                  $context = '';
 339                  $result = $this->save_post($post, $comments, $pings);
 340                  if ( is_wp_error( $result ) )
 341                      return $result;
 342                  $post = new StdClass;
 343                  $comment = new StdClass();
 344                  $ping = new StdClass();
 345                  $comments = array();
 346                  $pings = array();
 347              } else if ( 'BODY:' == $line ) {
 348                  $context = 'body';
 349              } else if ( 'EXTENDED BODY:' == $line ) {
 350                  $context = 'extended';
 351              } else if ( 'EXCERPT:' == $line ) {
 352                  $context = 'excerpt';
 353              } else if ( 'KEYWORDS:' == $line ) {
 354                  $context = 'keywords';
 355              } else if ( 'COMMENT:' == $line ) {
 356                  $context = 'comment';
 357              } else if ( 'PING:' == $line ) {
 358                  $context = 'ping';
 359              } else if ( 0 === strpos($line, "AUTHOR:") ) {
 360                  $author = trim( substr($line, strlen("AUTHOR:")) );
 361                  if ( '' == $context )
 362                      $post->post_author = $author;
 363                  else if ( 'comment' == $context )
 364                       $comment->comment_author = $author;
 365              } else if ( 0 === strpos($line, "TITLE:") ) {
 366                  $title = trim( substr($line, strlen("TITLE:")) );
 367                  if ( '' == $context )
 368                      $post->post_title = $title;
 369                  else if ( 'ping' == $context )
 370                      $ping->title = $title;
 371              } else if ( 0 === strpos($line, "STATUS:") ) {
 372                  $status = trim( strtolower( substr($line, strlen("STATUS:")) ) );
 373                  if ( empty($status) )
 374                      $status = 'publish';
 375                  $post->post_status = $status;
 376              } else if ( 0 === strpos($line, "ALLOW COMMENTS:") ) {
 377                  $allow = trim( substr($line, strlen("ALLOW COMMENTS:")) );
 378                  if ( $allow == 1 )
 379                      $post->comment_status = 'open';
 380                  else
 381                      $post->comment_status = 'closed';
 382              } else if ( 0 === strpos($line, "ALLOW PINGS:") ) {
 383                  $allow = trim( substr($line, strlen("ALLOW PINGS:")) );
 384                  if ( $allow == 1 )
 385                      $post->ping_status = 'open';
 386                  else
 387                      $post->ping_status = 'closed';
 388              } else if ( 0 === strpos($line, "CATEGORY:") ) {
 389                  $category = trim( substr($line, strlen("CATEGORY:")) );
 390                  if ( '' != $category )
 391                      $post->categories[] = $category;
 392              } else if ( 0 === strpos($line, "PRIMARY CATEGORY:") ) {
 393                  $category = trim( substr($line, strlen("PRIMARY CATEGORY:")) );
 394                  if ( '' != $category )
 395                      $post->categories[] = $category;
 396              } else if ( 0 === strpos($line, "DATE:") ) {
 397                  $date = trim( substr($line, strlen("DATE:")) );
 398                  $date = strtotime($date);
 399                  $date = date('Y-m-d H:i:s', $date);
 400                  $date_gmt = get_gmt_from_date($date);
 401                  if ( '' == $context ) {
 402                      $post->post_modified = $date;
 403                      $post->post_modified_gmt = $date_gmt;
 404                      $post->post_date = $date;
 405                      $post->post_date_gmt = $date_gmt;
 406                  } else if ( 'comment' == $context ) {
 407                      $comment->comment_date = $date;
 408                  } else if ( 'ping' == $context ) {
 409                      $ping->comment_date = $date;
 410                  }
 411              } else if ( 0 === strpos($line, "EMAIL:") ) {
 412                  $email = trim( substr($line, strlen("EMAIL:")) );
 413                  if ( 'comment' == $context )
 414                      $comment->comment_author_email = $email;
 415                  else
 416                      $ping->comment_author_email = '';
 417              } else if ( 0 === strpos($line, "IP:") ) {
 418                  $ip = trim( substr($line, strlen("IP:")) );
 419                  if ( 'comment' == $context )
 420                      $comment->comment_author_IP = $ip;
 421                  else
 422                      $ping->comment_author_IP = $ip;
 423              } else if ( 0 === strpos($line, "URL:") ) {
 424                  $url = trim( substr($line, strlen("URL:")) );
 425                  if ( 'comment' == $context )
 426                      $comment->comment_author_url = $url;
 427                  else
 428                      $ping->comment_author_url = $url;
 429              } else if ( 0 === strpos($line, "BLOG NAME:") ) {
 430                  $blog = trim( substr($line, strlen("BLOG NAME:")) );
 431                  $ping->comment_author = $blog;
 432              } else {
 433                  // Processing multi-line field, check context.
 434  
 435                  if( !empty($line) )
 436                      $line .= "\n";
 437  
 438                  if ( 'body' == $context ) {
 439                      $post->post_content .= $line;
 440                  } else if ( 'extended' ==  $context ) {
 441                      $post->extended .= $line;
 442                  } else if ( 'excerpt' == $context ) {
 443                      $post->post_excerpt .= $line;
 444                  } else if ( 'keywords' == $context ) {
 445                      $post->post_keywords .= $line;
 446                  } else if ( 'comment' == $context ) {
 447                      $comment->comment_content .= $line;
 448                  } else if ( 'ping' == $context ) {
 449                      $ping->comment_content .= $line;
 450                  }
 451              }
 452          }
 453  
 454          $this->fclose($handle);
 455  
 456          echo '</ol>';
 457  
 458          wp_import_cleanup($this->id);
 459          do_action('import_done', 'mt');
 460  
 461          echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
 462      }
 463  
 464  	function import() {
 465          $this->id = (int) $_GET['id'];
 466          if ( $this->id == 0 )
 467              $this->file = WP_CONTENT_DIR . '/mt-export.txt';
 468          else
 469              $this->file = get_attached_file($this->id);
 470          $this->get_authors_from_post();
 471          $result = $this->process_posts();
 472          if ( is_wp_error( $result ) )
 473              return $result;
 474      }
 475  
 476  	function dispatch() {
 477          if (empty ($_GET['step']))
 478              $step = 0;
 479          else
 480              $step = (int) $_GET['step'];
 481  
 482          switch ($step) {
 483              case 0 :
 484                  $this->greet();
 485                  break;
 486              case 1 :
 487                  check_admin_referer('import-upload');
 488                  $this->select_authors();
 489                  break;
 490              case 2:
 491                  check_admin_referer('import-mt');
 492                  set_time_limit(0);
 493                  $result = $this->import();
 494                  if ( is_wp_error( $result ) )
 495                      echo $result->get_error_message();
 496                  break;
 497          }
 498      }
 499  
 500  	function MT_Import() {
 501          // Nothing.
 502      }
 503  }
 504  
 505  $mt_import = new MT_Import();
 506  
 507  register_importer('mt', __('Movable Type and TypePad'), __('Import posts and comments from a Movable Type or TypePad blog.'), array ($mt_import, 'dispatch'));
 508  ?>


Generated: Thu May 20 22:30:08 2010 Cross-referenced by PHPXref 0.7