[ Index ]
 

Code source de WordPress 2.1.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

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


Généré le : Fri Mar 30 19:41:27 2007 par Balluche grâce à PHPXref 0.7