[ Index ]

WordPress Source Cross Reference

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 and Typepad').'</h2>';
  15      }
  16  
  17  	function footer() {
  18          echo '</div>';
  19      }
  20  
  21  	function greet() {
  22          $this->header();
  23  ?>
  24  <p><?php _e('Howdy! We&#8217;re about to begin the process to import all of your Movable Type entries into WordPress. To begin, select a file to upload and click Import.'); ?></p>
  25  <?php wp_import_upload_form( add_query_arg('step', 1) ); ?>
  26      <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>
  27  <?php
  28          $this->footer();
  29      }
  30  
  31  	function users_form($n) {
  32          global $wpdb, $testing;
  33          $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
  34  ?><select name="userselect[<?php echo $n; ?>]">
  35      <option value="#NONE#">- Select -</option>
  36      <?php
  37  
  38  
  39          foreach ($users as $user) {
  40              echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
  41          }
  42  ?>
  43      </select>
  44      <?php
  45  
  46  
  47      }
  48  
  49      //function to check the authorname and do the mapping
  50  	function checkauthor($author) {
  51          global $wpdb;
  52          //mtnames is an array with the names in the mt import file
  53          $pass = 'changeme';
  54          if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
  55              ++ $this->j;
  56              $this->mtnames[$this->j] = $author; //add that new mt author name to an array 
  57              $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
  58              if (!$user_id) { //banging my head against the desk now. 
  59                  if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
  60                      $user_id = wp_create_user($author, $pass);
  61                      $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
  62                  } else {
  63                      $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
  64                  }
  65              } else {
  66                  return $user_id; // return pre-existing wp username if it exists
  67              }
  68          } else {
  69              $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
  70              $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
  71          }
  72  
  73          return $user_id;
  74      }
  75  
  76  	function get_entries() {
  77          set_magic_quotes_runtime(0);
  78          $importdata = file($this->file); // Read the file into an array
  79          $importdata = implode('', $importdata); // squish it
  80          $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
  81          $importdata = preg_replace("/\n--------\n/", "--MT-ENTRY--\n", $importdata);
  82          $this->posts = explode("--MT-ENTRY--", $importdata);
  83      }
  84  
  85  	function get_mt_authors() {
  86          $temp = array ();
  87          $i = -1;
  88          foreach ($this->posts as $post) {
  89              if ('' != trim($post)) {
  90                  ++ $i;
  91                  preg_match("|AUTHOR:(.*)|", $post, $thematch);
  92                  $thematch = trim($thematch[1]);
  93                  array_push($temp, "$thematch"); //store the extracted author names in a temporary array
  94              }
  95          }
  96  
  97          //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.
  98          $authors[0] = array_shift($temp);
  99          $y = count($temp) + 1;
 100          for ($x = 1; $x < $y; $x ++) {
 101              $next = array_shift($temp);
 102              if (!(in_array($next, $authors)))
 103                  array_push($authors, "$next");
 104          }
 105  
 106          return $authors;
 107      }
 108  
 109  	function get_authors_from_post() {
 110          $formnames = array ();
 111          $selectnames = array ();
 112  
 113          foreach ($_POST['user'] as $key => $line) {
 114              $newname = trim(stripslashes($line));
 115              if ($newname == '')
 116                  $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.
 117              array_push($formnames, "$newname");
 118          } // $formnames is the array with the form entered names
 119  
 120          foreach ($_POST['userselect'] as $user => $key) {
 121              $selected = trim(stripslashes($key));
 122              array_push($selectnames, "$selected");
 123          }
 124  
 125          $count = count($formnames);
 126          for ($i = 0; $i < $count; $i ++) {
 127              if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
 128                  array_push($this->newauthornames, "$selectnames[$i]");
 129              } else {
 130                  array_push($this->newauthornames, "$formnames[$i]");
 131              }
 132          }
 133      }
 134  
 135  	function mt_authors_form() {
 136  ?>
 137  <div class="wrap">
 138  <h2><?php _e('Assign Authors'); ?></h2>
 139  <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 <code>admin</code>s entries.'); ?></p>
 140  <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>
 141  <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
 142      <?php
 143  
 144  
 145          $authors = $this->get_mt_authors();
 146          echo '<ol id="authors">';
 147          echo '<form action="?import=mt&amp;step=2&amp;id=' . $this->id . '" method="post">';
 148          $j = -1;
 149          foreach ($authors as $author) {
 150              ++ $j;
 151              echo '<li>Current author: <strong>'.$author.'</strong><br />'.'Create user <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br /> or map to existing ';
 152              $this->users_form($j);
 153              echo '</li>';
 154          }
 155  
 156          echo '<input type="submit" value="Submit">'.'<br/>';
 157          echo '</form>';
 158          echo '</ol></div>';
 159  
 160      }
 161  
 162  	function select_authors() {
 163          $file = wp_import_handle_upload();
 164          if ( isset($file['error']) ) {
 165              $this->header();
 166              echo '<p>Sorry, there has been an error.</p>';
 167              echo '<p><strong>' . $file['error'] . '</strong></p>';
 168              $this->footer();
 169              return;
 170          }
 171          $this->file = $file['file'];
 172          $this->id = $file['id'];
 173  
 174          $this->get_entries();
 175          $this->mt_authors_form();
 176      }
 177  
 178  	function process_posts() {
 179          global $wpdb;
 180          $i = -1;
 181          echo "<div class='wrap'><ol>";
 182          foreach ($this->posts as $post) {
 183              if ('' != trim($post)) {
 184                  ++ $i;
 185                  unset ($post_categories);
 186  
 187                  // Take the pings out first
 188                  preg_match("|(-----\n\nPING:.*)|s", $post, $pings);
 189                  $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post);
 190  
 191                  // Then take the comments out
 192                  preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments);
 193                  $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post);
 194  
 195                  // We ignore the keywords
 196                  $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post);
 197  
 198                  // We want the excerpt
 199                  preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt);
 200                  $post_excerpt = $wpdb->escape(trim($excerpt[1]));
 201                  $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post);
 202  
 203                  // We're going to put extended body into main body with a more tag
 204                  preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended);
 205                  $extended = trim($extended[1]);
 206                  if ('' != $extended)
 207                      $extended = "\n<!--more-->\n$extended";
 208                  $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post);
 209  
 210                  // Now for the main body
 211                  preg_match("|-----\nBODY:(.*)|s", $post, $body);
 212                  $body = trim($body[1]);
 213                  $post_content = $wpdb->escape($body.$extended);
 214                  $post = preg_replace("|(-----\nBODY:.*)|s", '', $post);
 215  
 216                  // Grab the metadata from what's left
 217                  $metadata = explode("\n", $post);
 218                  foreach ($metadata as $line) {
 219                      preg_match("/^(.*?):(.*)/", $line, $token);
 220                      $key = trim($token[1]);
 221                      $value = trim($token[2]);
 222                      // Now we decide what it is and what to do with it
 223                      switch ($key) {
 224                          case '' :
 225                              break;
 226                          case 'AUTHOR' :
 227                              $post_author = $value;
 228                              break;
 229                          case 'TITLE' :
 230                              $post_title = $wpdb->escape($value);
 231                              break;
 232                          case 'STATUS' :
 233                              // "publish" and "draft" enumeration items match up; no change required
 234                              $post_status = $value;
 235                              if (empty ($post_status))
 236                                  $post_status = 'publish';
 237                              break;
 238                          case 'ALLOW COMMENTS' :
 239                              $post_allow_comments = $value;
 240                              if ($post_allow_comments == 1) {
 241                                  $comment_status = 'open';
 242                              } else {
 243                                  $comment_status = 'closed';
 244                              }
 245                              break;
 246                          case 'CONVERT BREAKS' :
 247                              $post_convert_breaks = $value;
 248                              break;
 249                          case 'ALLOW PINGS' :
 250                              $ping_status = trim($meta[2][0]);
 251                              if ($ping_status == 1) {
 252                                  $ping_status = 'open';
 253                              } else {
 254                                  $ping_status = 'closed';
 255                              }
 256                              break;
 257                          case 'PRIMARY CATEGORY' :
 258                              if (! empty ($value) )
 259                                  $post_categories[] = $wpdb->escape($value);
 260                              break;
 261                          case 'CATEGORY' :
 262                              if (! empty ($value) )
 263                                  $post_categories[] = $wpdb->escape($value);
 264                              break;
 265                          case 'DATE' :
 266                              $post_modified = strtotime($value);
 267                              $post_modified = date('Y-m-d H:i:s', $post_modified);
 268                              $post_modified_gmt = get_gmt_from_date("$post_modified");
 269                              $post_date = $post_modified;
 270                              $post_date_gmt = $post_modified_gmt;
 271                              break;
 272                          default :
 273                              // echo "\n$key: $value";
 274                              break;
 275                      } // end switch
 276                  } // End foreach
 277  
 278                  // Let's check to see if it's in already
 279                  if ($post_id = post_exists($post_title, '', $post_date)) {
 280                      echo '<li>';
 281                      printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
 282                  } else {
 283                      echo '<li>';
 284                      printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
 285  
 286                      $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
 287  
 288                      $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt');
 289                      $post_id = wp_insert_post($postdata);
 290                      // Add categories.
 291                      if (0 != count($post_categories)) {
 292                          wp_create_categories($post_categories, $post_id);
 293                      }
 294                  }
 295  
 296                  $comment_post_ID = $post_id;
 297                  $comment_approved = 1;
 298  
 299                  // Now for comments
 300                  $comments = explode("-----\nCOMMENT:", $comments[0]);
 301                  $num_comments = 0;
 302                  foreach ($comments as $comment) {
 303                      if ('' != trim($comment)) {
 304                          // Author
 305                          preg_match("|AUTHOR:(.*)|", $comment, $comment_author);
 306                          $comment_author = $wpdb->escape(trim($comment_author[1]));
 307                          $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment);
 308                          preg_match("|EMAIL:(.*)|", $comment, $comment_author_email);
 309                          $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
 310                          $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment);
 311  
 312                          preg_match("|IP:(.*)|", $comment, $comment_author_IP);
 313                          $comment_author_IP = trim($comment_author_IP[1]);
 314                          $comment = preg_replace('|(\n?IP:.*)|', '', $comment);
 315  
 316                          preg_match("|URL:(.*)|", $comment, $comment_author_url);
 317                          $comment_author_url = $wpdb->escape(trim($comment_author_url[1]));
 318                          $comment = preg_replace('|(\n?URL:.*)|', '', $comment);
 319  
 320                          preg_match("|DATE:(.*)|", $comment, $comment_date);
 321                          $comment_date = trim($comment_date[1]);
 322                          $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
 323                          $comment = preg_replace('|(\n?DATE:.*)|', '', $comment);
 324  
 325                          $comment_content = $wpdb->escape(trim($comment));
 326                          $comment_content = str_replace('-----', '', $comment_content);
 327                          // Check if it's already there
 328                          if (!comment_exists($comment_author, $comment_date)) {
 329                              $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved');
 330                              $commentdata = wp_filter_comment($commentdata);
 331                              wp_insert_comment($commentdata);
 332                              $num_comments++;
 333                          }
 334                      }
 335                  }
 336                  if ( $num_comments )
 337                      printf(__(' (%s comments)'), $num_comments);
 338  
 339                  // Finally the pings
 340                  // fix the double newline on the first one
 341                  $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]);
 342                  $pings = explode("-----\nPING:", $pings[0]);
 343                  $num_pings = 0;
 344                  foreach ($pings as $ping) {
 345                      if ('' != trim($ping)) {
 346                          // 'Author'
 347                          preg_match("|BLOG NAME:(.*)|", $ping, $comment_author);
 348                          $comment_author = $wpdb->escape(trim($comment_author[1]));
 349                          $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping);
 350  
 351                          preg_match("|IP:(.*)|", $ping, $comment_author_IP);
 352                          $comment_author_IP = trim($comment_author_IP[1]);
 353                          $ping = preg_replace('|(\n?IP:.*)|', '', $ping);
 354  
 355                          preg_match("|URL:(.*)|", $ping, $comment_author_url);
 356                          $comment_author_url = $wpdb->escape(trim($comment_author_url[1]));
 357                          $ping = preg_replace('|(\n?URL:.*)|', '', $ping);
 358  
 359                          preg_match("|DATE:(.*)|", $ping, $comment_date);
 360                          $comment_date = trim($comment_date[1]);
 361                          $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
 362                          $ping = preg_replace('|(\n?DATE:.*)|', '', $ping);
 363  
 364                          preg_match("|TITLE:(.*)|", $ping, $ping_title);
 365                          $ping_title = $wpdb->escape(trim($ping_title[1]));
 366                          $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping);
 367  
 368                          $comment_content = $wpdb->escape(trim($ping));
 369                          $comment_content = str_replace('-----', '', $comment_content);
 370  
 371                          $comment_content = "<strong>$ping_title</strong>\n\n$comment_content";
 372  
 373                          $comment_type = 'trackback';
 374  
 375                          // Check if it's already there
 376                          if (!comment_exists($comment_author, $comment_date)) {
 377                              $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_type', 'comment_approved');
 378                              $commentdata = wp_filter_comment($commentdata);
 379                              wp_insert_comment($commentdata);
 380                              $num_pings++;
 381                          }
 382                      }
 383                  }
 384                  if ( $num_pings )
 385                      printf(__(' (%s pings)'), $num_pings);
 386  
 387                  echo "</li>";
 388              }
 389          }
 390  
 391          echo '</ol>';
 392  
 393          wp_import_cleanup($this->id);
 394  
 395          echo '<h3>'.sprintf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')).'</h3></div>';
 396      }
 397  
 398  	function import() {
 399          $this->id = (int) $_GET['id'];
 400          
 401          $this->file = get_attached_file($this->id);
 402          $this->get_authors_from_post();
 403          $this->get_entries();
 404          $this->process_posts();
 405      }
 406  
 407  	function dispatch() {
 408          if (empty ($_GET['step']))
 409              $step = 0;
 410          else
 411              $step = (int) $_GET['step'];
 412