PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

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

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