home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / import / mt.php < prev    next >
Encoding:
PHP Script  |  2008-05-27  |  14.8 KB  |  470 lines

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