Make WordPress Core


Ignore:
Timestamp:
10/07/2005 05:43:43 PM (21 years ago)
Author:
ryan
Message:

Blogger updates from skeltoac. * No more SSL * No more fopen wrappers required (all CURL) * Better FTP handling * Better duplicate handling * Strings marked for translation * Post IDs are cronological * Coffee stays fresher longer. fixes #1737

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import/blogger.php

    r2928 r2939  
    22
    33class Blogger_Import {
    4        
     4
     5        var $lump_authors = false;
    56        var $import = array();
    6         var $template = '<br />
    7 <br />
    8 <br />
    9 <MainPage>
    10 <p>Are you looking for %title%? It is temporarily out of service. Please try again in an hour. Meanwhile, discover <a href="http://wordpress.org">a better blogging tool</a>.</p>
    11 <BloggerArchives>
    12 <a class="archive" href="<$BlogArchiveURL$>"><$BlogArchiveName$></a><br />
    13 </BloggerArchives>
    14 </MainPage>
    15 <ArchivePage>
    16 <Blogger><wordpresspost><$BlogItemDateTime$>|W|P|<$BlogItemAuthorNickname$>|W|P|<$BlogItemBody$>|W|P|<$BlogItemNumber$>|W|P|<$BlogItemTitle$>|W|P|<$BlogItemAuthorEmail$><BlogItemCommentsEnabled><BlogItemComments><wordpresscomment><$BlogCommentDateTime$>|W|P|<$BlogCommentAuthor$>|W|P|<$BlogCommentBody$></BlogItemComments></BlogItemCommentsEnabled></Blogger>
    17 </ArchivePage>';
    187
    198        // Shows the welcome screen and the magic iframe.
    209        function greet() {
    21                 echo '<div class="wrap">';
    22                 echo '<h2>'.__('Import Blogger').'</h2>';
    23                 _e("<p>Howdy! This importer allows you to import posts and comments from your Blogger account into your WordPress blog.</p>
    24 <p>Before you get started, you may want to back up your Blogger template by copying and pasting it into a text file on your computer. This script has to modify your template and other Blogger settings so it can get your posts and comments. It should restore everything afterwards but if you have put a lot of work into your template, it would be a good idea to make your own backup first.</p>
    25 <p>When you are ready to begin, enter your Blogger username and password below and click Start. Do not close this window until the process is complete.</p>");
    26                 echo "<iframe src='admin.php?import=blogger&noheader=true' height='350px' width = '99%'></iframe>";
    27                 echo "<p><a href='admin.php?import=blogger&amp;restart=true&amp;noheader=true' onclick='return confirm(\"This will delete everything saved by the Blogger importer. Are you sure you want to do this?\")'>Reset this importer</a></p>";
    28                 echo '</div>';
     10                $title = __('Import Blogger');
     11                $welcome = __('Howdy! This importer allows you to import posts and comments from your Blogger account into your WordPress blog.');
     12                $noiframes = __('This feature requires iframe support.');
     13                $warning = __('This will delete everything saved by the Blogger importer except your posts and comments. Are you sure you want to do this?');
     14                $reset = __('Reset this importer');
     15                echo "<div class='wrap'><h2>$title</h2><p>$welcome</p><iframe src='admin.php?import=blogger&amp;noheader=true' height='350px' width = '99%'>$noiframes</iframe><p><a href='admin.php?import=blogger&amp;restart=true&amp;noheader=true' onclick='return confirm(\"$warning\")'>$reset</a></p></div>\n";
    2916        }
    3017
    … …  
    3825        // Generates a string that will make the page reload in a specified interval.
    3926        function refresher($msec) {
    40                 return "<html><head><script type='text/javascript'>window.onload=setInterval('window.location.reload()', $msec);</script>\n</head>\n<body>";
     27                if ( $msec )
     28                        return "<html><head><script type='text/javascript'>window.onload=setTimeout('window.location.reload()', $msec);</script>\n</head>\n<body>\n";
     29                else
     30                        return "<html><head><script type='text/javascript'>window.onload=window.location.reload();</script>\n</head>\n<body>\n";
    4131        }
    4232
    … …  
    6757        // Prints a form for the user to enter Blogger creds.
    6858        function login_form($text='') {
    69                 echo "<h1>Log in to Blogger</h1>\n<p>$text</p>\n";
    70                 die("<form method='post' action='admin.php?import=blogger&amp;noheader=true&amp;step=0'><input type='text' name='user' /><input type='password' name='pass' /><input type='submit' value='Start' /></form>");
     59                echo '<h1>' . __('Log in to Blogger') . "</h1>\n$text\n";
     60                echo '<form method="post" action="admin.php?import=blogger&amp;noheader=true&amp;step=0"><table><tr><td>' . __('Username') . ':</td><td><input type="text" name="user" /></td></tr><tr><td>' . __('Password') . ':</td><td><input type="password" name="pass" /></td><td><input type="submit" value="' . __('Start') . '" /></td></tr></table></form>';
     61                die;
    7162        }
    7263
    … …  
    7970                curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
    8071                curl_setopt($ch, CURLOPT_URL,$_url);
    81                 curl_setopt($ch, CURLOPT_USERAGENT, 'Developing Blogger Exporter');
     72                curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter');
    8273                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    8374                curl_setopt($ch, CURLOPT_HEADER,1);
    … …  
    8677
    8778                $response = $this->parse_response($response);
     79
     80                sleep(1);
    8881
    8982                return $response['cookies'];
    … …  
    9790                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    9891                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    99                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    10092                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     93                curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter');
    10194                curl_setopt($ch, CURLOPT_HEADER,1);
    10295                if (is_array($header)) curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    … …  
    125118                if ($user && $pass) $params .= "username=$user&password=$pass";
    126119                $params = trim($params,'&');
    127         //echo addslashes(print_r(array('url'=>$url,'header'=>$header,'params'=>$params),1));
    128120                $ch = curl_init();
    129121                curl_setopt($ch, CURLOPT_POST,1);
    … …  
    131123                if ($user && $pass) curl_setopt($ch, CURLOPT_USERPWD,"{$user}:{$pass}");
    132124                curl_setopt($ch, CURLOPT_URL,$url);
    133                 curl_setopt($ch, CURLOPT_USERAGENT, 'Developing Blogger Exporter');
     125                curl_setopt($ch, CURLOPT_USERAGENT, 'Blogger Exporter');
    134126                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    135127                curl_setopt($ch, CURLOPT_HEADER,$parse);
    … …  
    150142        function show_blogs() {
    151143                global $import;
    152                 echo "<h1>Selecting a Blog</h1>\n<ul>";
     144                echo '<h1>' . __('Selecting a Blog') . "</h1>\n<ul>";
    153145                foreach ( $this->import['blogs'] as $blog ) {
    154146                        if (9 == $blog['nextstep']) $status = "100%";
    … …  
    166158
    167159        // Publishes.
    168         function publish_blogger($i) {
    169                 if ( ! $this->import['blogs'][$_GET['blog']]['publish'][$i] ) {
    170                         // First call. Start the publish process.
     160        function publish_blogger($i, $text) {
     161                $head = $this->refresher(2000) . "<h1>$text</h1>\n";
     162                if ( ! strstr($this->import['blogs'][$_GET['blog']]['publish'][$i], 'http') ) {
     163                        // First call. Start the publish process with a fresh set of cookies.
     164                        $this->import['cookies'] = $this->login_blogger($this->import['user'], $this->import['pass']);
     165                        update_option('import-blogger', $this->import);
    171166                        $paramary = array('blogID' => $_GET['blog'], 'all' => '1', 'republishAll' => 'Republish Entire Blog', 'publish' => '1', 'redirectUrl' => "/publish.do?blogID={$_GET['blog']}&inprogress=true");
     167
    172168                        $response = $this->post_blogger("http://www.blogger.com/publish.do?blogID={$_GET['blog']}", $this->import['cookies'], $paramary);
    173169                        if ( $response['code'] == '302' ) {
    … …  
    176172                                update_option('import-blogger', $this->import);
    177173                                $response = $this->get_blogger($url, $this->import['cookies']);
    178                                 if ( preg_match('#<p class="progressIndicator">.*</p>#U', $response['body'], $matches) ) {
    179                                         $progress = $matches[0];
    180                                         die($progress);
    181                                 } else {
    182                                         echo "matches:<pre>" . print_r($matches,1) . "</pre>\n";
    183                                 }
     174                                preg_match('#<p class="progressIndicator">.*</p>#U', $response['body'], $matches);
     175                                $progress = $matches[0];
     176                                die($head . $progress);
    184177                        } else {
    185                                 echo "<h1>Publish error: No 302</h1><p>Please tell the devs.</p><pre>" . addslashes(print_r($response,1)) . "</pre>\n";
    186                         }
    187                         die();
     178                                $this->import['blogs'][$_GET['blog']]['publish'][$i] = false;
     179                                update_option('import-blogger', $this->import);
     180                                die($head);
     181                        }
    188182                } else {
    189183                        // Subsequent call. Keep checking status until Blogger reports publish complete.
    … …  
    192186                        if ( preg_match('#<p class="progressIndicator">.*</p>#U', $response['body'], $matches) ) {
    193187                                $progress = $matches[0];
    194                                 if ( strstr($progress, '100%') )
     188                                if ( strstr($progress, '100%') ) {
    195189                                        $this->set_next_step($i);
    196                                 die($progress);
     190                                        $progress .= '<p>'.__('Moving on...').'</p>';
     191                                }
     192                                die($head . $progress);
    197193                        } else {
    198                                 echo "<h1>Publish error: No matches</h1><p>Please tell the devs.</p><pre>" . print_r($matches,1) . "</pre>\n";
     194                                $this->import['blogs'][$_GET['blog']]['publish'][$i] = false;
     195                                update_option('import-blogger', $this->import);
     196                                die("$head<p>" . __('Trying again...') . '</p>');
    199197                        }
    200198                }
    … …  
    213211        }
    214212
    215         // Step 0: Do Blogger login, get userid and blogid/title pairs.
     213        // Step 0: Do Blogger login, get blogid/title pairs.
    216214        function do_login() {
    217215                if ( ( ! $this->import['user'] && ! is_array($this->import['cookies']) ) ) {
    218216                        // The user must provide a Blogger username and password.
    219217                        if ( ! ( $_POST['user'] && $_POST['pass'] ) ) {
    220                                 $this->login_form("Please log in using your Blogger credentials.");
     218                                $this->login_form(__('The script will log into your Blogger account, change some settings so it can read your blog, and restore the original settings when it\'s done. Here\'s what you do:</p><ol><li>Back up your Blogger template.</li><li>Back up any other Blogger settings you might need later.</li><li>Log out of Blogger</li><li>Log in <em>here</em> with your Blogger username and password.</li><li>On the next screen, click one of your Blogger blogs.</li><li>Do not close this window or navigate away until the process is complete.</li></ol>'));
    221219                        }
    222220               
    … …  
    231229                        $this->import['user'] = $_POST['user'];
    232230
    233                         // Get the blog numbers so we can give the user a choice.
    234                         $response = $this->get_blogger('https://www.blogger.com/atom','',$_POST['user'],$_POST['pass']);
    235                         $blogger = $response['body'];
    236 
    237                         // Save the userID if we got one from Blogger, otherwise re-login.
    238                         $useridary = array();
    239                         preg_match('#>(\d+)</userid#', $blogger, $useridary);
    240 
    241                         if (! $this->import['userid'] = $useridary[1]) $this->login_form("Login failed. Please re-enter your username and password.");
    242                         unset($useridary);
    243 
    244                         // Save the blogs
     231                        // Get the Blogger welcome page and scrape the blog numbers and names from it
     232                        $response = $this->get_blogger('http://www.blogger.com/home', $this->import['cookies']);
     233                        if (! stristr($response['body'], 'signed in as') ) $this->login_form("Login failed. Please re-enter your username and password.");
    245234                        $blogsary = array();
    246                         preg_match_all('#/atom/(\d+)" rel\="service.feed" title="([^"]+)"#', $blogger, $blogsary);
    247                         if ( ! is_array( $blogsary[1] ) )
    248                                 die('No blogs found for this user.');
     235                        preg_match_all('#posts\.g\?blogID=(\d+)">([^<]+)</a>#U', $response['body'], $blogsary);
     236                        if ( ! count( $blogsary[1] < 1 ) )
     237                                die(__('No blogs found for this user.'));
    249238                        $this->import['blogs'] = array();
     239                        $template = '<MainPage><br /><br /><br /><p>'.__('Are you looking for %title%? It is temporarily out of service. Please try again in a few minutes. Meanwhile, discover <a href="http://wordpress.org">a better blogging tool</a>.').'</p><BloggerArchives><a class="archive" href="<$BlogArchiveURL$>"><$BlogArchiveName$></a><br /></BloggerArchives></MainPage><ArchivePage><Blogger><wordpresspost><$BlogItemDateTime$>|W|P|<$BlogItemAuthorNickname$>|W|P|<$BlogItemBody$>|W|P|<$BlogItemNumber$>|W|P|<$BlogItemTitle$>|W|P|<$BlogItemAuthorEmail$><BlogItemCommentsEnabled><BlogItemComments><wordpresscomment><$BlogCommentDateTime$>|W|P|<$BlogCommentAuthor$>|W|P|<$BlogCommentBody$></BlogItemComments></BlogItemCommentsEnabled></Blogger></ArchivePage>';
    250240                        foreach ( $blogsary[1] as $key => $id ) {
    251241                                // Define the required Blogger options.
    252242                                $blog_opts = array(
    253243                                        'blog-options-basic' => false,
     244                                        'blog-options-archiving' => array('archiveFrequency' => 'm'),
     245                                        'blog-publishing' => array('publishMode'=>'0', 'blogID' => "$id", 'subdomain' => mt_rand().mt_rand(), 'pingWeblogs' => 'false'),
    254246                                        'blog-formatting' => array('timeStampFormat' => '0', 'encoding'=>'UTF-8', 'convertLineBreaks'=>'false', 'floatAlignment'=>'false'),
    255247                                        'blog-comments' => array('commentsTimeStampFormat' => '0'),
    256                                         'blog-options-archiving' => array('archiveFrequency' => 'm'),
    257                                         'template-edit' => array( 'templateText' =>  str_replace('%title%', $blogsary[2][$key], $this->template) ),
    258                                         'blog-publishing' => array('publishMode'=>'0', 'blogID' => "$id", 'subdomain' => mt_rand().mt_rand(), 'pingWeblogs' => 'false')
     248                                        'template-edit' => array( 'templateText' =>  str_replace('%title%', trim($blogsary[2][$key]), $template) )
    259249                                );
    260250
    … …  
    265255                                $this->import['blogs']["$id"] = array(
    266256                                        'id' => $id,
    267                                         'title' => $blogsary[2][$key],
     257                                        'title' => trim($blogsary[2][$key]),
    268258                                        'options' => $new_opts,
    269259                                        'url' => false,
    … …  
    271261                                        'published' => false,
    272262                                        'archives' => false,
     263                                        'lump_authors' => false,
    273264                                        'newusers' => array(),
    274                                         'lump_authors' => false,
    275                                         'newusers' => 0,
    276265                                        'nextstep' => 2
    277266                                );
    … …  
    299288                foreach ($this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary) {
    300289                        if ( $blog_opt == $_GET['form'] ) {
    301                                 // Make sure not to overwrite any existing backups!
    302                                 if ( $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['backup'] ) continue;
    303 
    304290                                // Save the posted form data
    305291                                $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['backup'] = $_POST;
    … …  
    310296                                        $posturl = "http://www.blogger.com/{$blog_opt}.do";
    311297                                        $headers = array_merge($this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['cookies'], $this->import['cookies']);
    312                                         switch ($blog_opt) {
    313                                         case 'blog-publishing':
     298                                        if ( 'blog-publishing' == $blog_opt ) {
    314299                                                if ( $_POST['publishMode'] > 0 ) {
    315300                                                        $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode=0", $headers);
    316301                                                        if ( $response['code'] >= 400 )
    317                                                                 die('<h2>Failed attempt to change publishMode:</h2><pre>' . addslashes(print_r($headers, 1)) . addslashes(print_r($response, 1)) . '</pre>');
     302                                                                die('<h2>'.__('Failed attempt to change publish mode from FTP to BlogSpot.').'</h2><pre>' . addslashes(print_r($headers, 1)) . addslashes(print_r($response, 1)) . '</pre>');
    318303                                                        $this->import['blogs'][$_GET['blog']]['url'] = 'http://' . $optary['modify']['subdomain'] . '.blogspot.com/';
     304                                                        sleep(2);
    319305                                                } else {
    320306                                                        $this->import['blogs'][$_GET['blog']]['url'] = 'http://' . $_POST['subdomain'] . '.blogspot.com/';
    321307                                                        update_option('import-blogger', $this->import);
    322                                                         continue 2;
     308                                                        $output .= "<del><p>$blog_opt</p></del>\n";
     309                                                        continue;
    323310                                                }
    324311                                                $paramary = $optary['modify'];
    325                                                 break;
    326                                         case 'template-edit':
    327                                                 $this->import['blogs'][$_GET['blog']]['publish_cookies'] = $headers;
    328                                                 update_option('import-blogger', $this->import);
    329                                         default:
     312                                        } else {
    330313                                                $paramary = array_merge($_POST, $optary['modify']);
    331314                                        }
    332315                                        $response = $this->post_blogger($posturl, $headers, $paramary);
    333316                                        if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') )
    334                                                 die( "<p>Error on form submission. Refresh/Reload and click YES to resubmit.</p>" . addslashes(print_r($response, 1)));
    335                                         //header("Location: admin.php?import=blogger&noheader=true&step=2&blog={$_GET['blog']}");
    336                                         //die();
     317                                                die('<p>'.__('Error on form submission. Retry or reset the importer.').'</p>' . addslashes(print_r($response, 1)));
    337318                                }
    338319                                $output .= "<del><p>$blog_opt</p></del>\n";
    … …  
    362343                        die($output . $form);
    363344
    364                 $this->set_next_step(3);
     345                $this->set_next_step(4);
    365346                $this->do_next_step();
    366347        }
    367348
    368         // Step 3: Publish with the new template and settings.
     349        // Step 3: Cancelled :-)
     350
     351        // Step 4: Publish with the new template and settings.
    369352        function publish_blog() {
    370                 echo $this->refresher(2400) . "<h1>Publishing with new template and options</h1>\n";
    371                 $this->publish_blogger(5);
    372         }
    373 
    374         // Step 4: Deprecated. :-D
     353                $this->publish_blogger(5, __('Publishing with new template and options'));
     354        }
    375355
    376356        // Step 5: Get the archive URLs from the new blog.
    377357        function get_archive_urls() {
    378                 $bloghtml = file($this->import['blogs'][$_GET['blog']]['url']);
    379                 if (! is_array($bloghtml) ) die('Trouble connecting to your blog. Try reloading this page.');
    380                 preg_match_all('#<a class="archive" href="([^"]*)"#', implode('',$bloghtml), $archives);
    381                 if ( count($archives[1]) == 0 ) die ("That's odd. No archives were found. Does <a href='{$import['blogs'][$_GET['blog']]['url']}'>your blog</a> say Temporary Template?");
     358                $bloghtml = $this->get_blogger($this->import['blogs'][$_GET['blog']]['url']);
     359                if (! strstr($bloghtml['body'], '<a class="archive"') )
     360                        die(__('Your Blogger blog did not take the new template or did not respond.'));
     361                preg_match_all('#<a class="archive" href="([^"]*)"#', $bloghtml['body'], $archives);
    382362                foreach ($archives[1] as $archive) {
    383363                        $this->import['blogs'][$_GET['blog']]['archives'][$archive] = false;
    … …  
    390370        function get_archive() {
    391371                global $wpdb;
    392                 $output = "<h2>Importing Blogger archives into WordPress</h2>\n";
     372                $output = '<h2>'.__('Importing Blogger archives into WordPress').'</h2>';
    393373                $did_one = false;
     374                $post_array = $posts = array();
    394375                foreach ( $this->import['blogs'][$_GET['blog']]['archives'] as $url => $status ) {
    395376                        $archivename = substr(basename($url),0,7);
    … …  
    403384                                $commentcount = 0;
    404385                                $skippedcommentcount = 0;
    405                                 $status = '';
    406                                 $archive = implode('',file($url));
    407        
    408                                 $posts = explode('<wordpresspost>', $archive);
     386                                $status = __('in progress...');
     387                                $this->import['blogs'][$_GET['blog']]['archives']["$url"] = $status;
     388                                update_option('import-blogger', $import);
     389                                $archive = $this->get_blogger($url);
     390                                if ( $archive['code'] > 200 )
     391                                        continue;       
     392                                $posts = explode('<wordpresspost>', $archive['body']);
    409393                                for ($i = 1; $i < count($posts); $i = $i + 1) {
    410394                                        $postparts = explode('<wordpresscomment>', $posts[$i]);
    … …  
    417401                                        //$post_number = $postinfo[3];
    418402                                        $post_title = ( $postinfo[4] != '' ) ? $postinfo[4] : $postinfo[3];
    419                                         $post_author = trim($wpdb->escape($postinfo[1]));
    420                                         $post_author_name = trim(addslashes($postinfo[1]));
    421                                         $post_author_email = $postinfo[5] ? $postinfo[5] : 'no@email.com';
    422        
    423                                         if ( $this->import['blogs'][$_GET['blog']]['lump_authors'] ) {
     403                                        $post_author_name = $wpdb->escape(trim($postinfo[1]));
     404                                        $post_author_email = $postinfo[5] ? $postinfo[5] : 'user@wordpress.org';
     405       
     406                                        if ( $this->lump_authors ) {
    424407                                                // Ignore Blogger authors. Use the current user_ID for all posts imported.
    425                                                 $post_author = $GLOBALS['user_ID']; // For WPMU only. Assume all posts belong to the current user.
     408                                                $post_author = $GLOBALS['user_ID'];
    426409                                        } else {
    427410                                                // Add a user for each new author encountered.
    428411                                                if (! username_exists($post_author_name) ) {
    429                                                         $user_joindate = '1979-06-06 00:41:00'; // that's my birthdate (gmt+1) - I could choose any other date. You could change the date too. Just remember the year must be >=1970 or the world would just randomly fall on your head (everything might look fine, and then blam! major headache!)
    430412                                                        $user_login = $wpdb->escape($post_author_name);
    431                                                         $pass1 = $wpdb->escape('password');
    432413                                                        $user_email = $wpdb->escape($post_author_email);
    433                                                         $user_url = $wpdb->escape('');
    434                                                         $user_joindate = $wpdb->escape($user_joindate);
    435414                                                        $user_password = substr(md5(uniqid(microtime())), 0, 6);
    436415                                                        $result = wp_create_user( $user_login, $user_password, $user_email );
    437416                                                        $status.= "Registered user <strong>$user_login</strong>. ";
    438                                                         ++$this->import['blogs'][$_GET['blog']]['newusers'];
     417                                                        $this->import['blogs'][$_GET['blog']]['newusers'][] = $user_login;
    439418                                                }
    440419                                                $userdata = get_userdatabylogin( $post_author_name );
    … …  
    459438       
    460439                                        $post_content = addslashes($post_content);
    461                                         $post_content = str_replace('<br>', '<br />', $post_content); // the XHTML touch... ;)
     440                                        $post_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $post_content); // the XHTML touch... ;)
    462441       
    463442                                        $post_title = addslashes($post_title);
    … …  
    465444                                        $post_status = 'publish';
    466445       
    467                                         if ( post_exists($post_title, '', $post_date) ) {
     446                                        if ( $ID = post_exists($post_title, '', $post_date) ) {
     447                                                $post_array[$i]['ID'] = $ID;
    468448                                                $skippedpostcount++;
    469                                                 $comment_post_ID = $dupcheck[0]['ID'];
    470449                                        } else {
    471                                                 $post_array = compact('post_author', 'post_content', 'post_title', 'post_category', 'post_author', 'post_date', 'post_status');
    472                                                 $comment_post_ID = wp_insert_post($post_array);
     450                                                $post_array[$i]['post'] = compact('post_author', 'post_content', 'post_title', 'post_category', 'post_author', 'post_date', 'post_status');
     451                                                $post_array[$i]['comments'] = false;
    473452                                        }
    474                                        
     453
    475454                                        // Import any comments attached to this post.
    476455                                        if ($postparts[1]) :
    … …  
    491470                                                        $commenthour = '00';
    492471                                                $comment_date = "$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
    493                                                 $comment_author = addslashes(strip_tags(html_entity_decode($commentinfo[1]))); // Believe it or not, Blogger allows a user to call himself "Mr. Hell's Kitchen" which, as a string, really confuses SQL.
     472                                                $comment_author = addslashes(strip_tags(html_entity_decode($commentinfo[1])));
    494473                                                if ( strpos($commentinfo[1], 'a href') ) {
    495474                                                        $comment_author_parts = explode('&quot;', htmlentities($commentinfo[1]));
    496475                                                        $comment_author_url = $comment_author_parts[1];
    497476                                                } else $comment_author_url = '';
    498                                                 $comment_content = addslashes($commentinfo[2]);
    499                                                 $comment_content = str_replace('<br>', '<br />', $comment_content);
    500                                                 if ( $comment_post_ID == comment_exists($comment_author, $comment_date) ) {
     477                                                $comment_content = $commentinfo[2];
     478                                                $comment_content = str_replace(array('<br>','<BR>','<br/>','<BR/>','<br />','<BR />'), "\n", $comment_content);
     479                                                $comment_approved = 1;
     480                                                if ( comment_exists($comment_author, $comment_date) ) {
    501481                                                        $skippedcommentcount++;
    502482                                                } else {
    503                                                         $result = $wpdb->query("
    504                                                         INSERT INTO $wpdb->comments
    505                                                         (comment_post_ID,comment_author,comment_author_url,comment_date,comment_content)
    506                                                         VALUES
    507                                                                 ('$comment_post_ID','$comment_author','$comment_author_url','$comment_date','$comment_content')
    508                                                 ");
     483                                                        $comment = compact('comment_author', 'comment_author_url', 'comment_date', 'comment_content', 'comment_approved');
     484                                                        $post_array[$i]['comments'][$j] = wp_filter_comment($comment);
    509485                                                }
    510486                                                $commentcount++;
    … …  
    513489                                        $postcount++;
    514490                                }
    515                                 $status = "$postcount post(s) parsed, $skippedpostcount skipped... $commentcount comment(s) parsed, $skippedcommentcount skipped... <strong>Done</strong>";
     491                                if ( count($post_array) ) {
     492                                        krsort($post_array);
     493                                        foreach($post_array as $post) {
     494                                                if ( ! $comment_post_ID = $post['ID'] )
     495                                                        $comment_post_ID = wp_insert_post($post['post']);
     496                                                if ( $post['comments'] ) {
     497                                                        foreach ( $post['comments'] as $comment ) {
     498                                                                $comment['comment_post_ID'] = $comment_post_ID;
     499                                                                wp_insert_comment($comment);
     500                                                        }
     501                                                }
     502                                        }
     503                                }
     504                                $status = "$postcount ".__('post(s) parsed,')." $skippedpostcount ".__('skipped...')." $commentcount ".__('comment(s) parsed,')." $skippedcommentcount ".__('skipped...').' <strong>'.__('Done').'</strong>';
    516505                                $import = $this->import;
    517506                                $import['blogs'][$_GET['blog']]['archives']["$url"] = $status;
    … …  
    523512                if ( ! $did_one )
    524513                        $this->set_next_step(7);
    525                 die( $this->refresher(5000) . $output );
     514                die( $this->refresher(1000) . $output );
    526515        }
    527516
    528517        // Step 7: Restore the backed-up settings to Blogger
    529518        function restore_settings() {
    530                 $output = "<h1>Restoring your Blogger options</h1>\n";
     519                $output = '<h1>'.__('Restoring your Blogger options')."</h1>\n";
    531520                $did_one = false;
     521                // Restore options in reverse order.
     522                if ( ! $this->import['reversed'] ) {
     523                        $this->import['blogs'][$_GET['blog']]['options'] = array_reverse($this->import['blogs'][$_GET['blog']]['options'], true);
     524                        $this->import['reversed'] = true;
     525                        update_option('import-blogger', $this->import);
     526                }
    532527                foreach ( $this->import['blogs'][$_GET['blog']]['options'] as $blog_opt => $optary ) {
    533528                        if ( $did_one ) {
    534529                                $output .= "<p>$blog_opt</p>\n";
    535                         } elseif ( $optary['restored'] ) {
    536                                 $output .= "<p><del>$blog_opt</del></p>\n";
    537                         } elseif ( ! $optary['modify'] ) {
     530                        } elseif ( $optary['restored'] || ! $optary['modify'] ) {
    538531                                $output .= "<p><del>$blog_opt</del></p>\n";
    539532                        } else {
    … …  
    543536                                        if ( $optary['backup']['publishMode'] > 0 ) {
    544537                                                $response = $this->get_blogger("http://www.blogger.com/blog-publishing.g?blogID={$_GET['blog']}&publishMode={$optary['backup']['publishMode']}", $headers);
     538                                                sleep(2);
    545539                                                if ( $response['code'] >= 400 )
    546540                                                        die('<h1>Error restoring publishMode.</h1><p>Please tell the devs.</p>' . addslashes(print_r($response, 1)) );
    … …  
    549543                                if ( $optary['backup'] != $optary['modify'] ) {
    550544                                        $response = $this->post_blogger($posturl, $headers, $optary['backup']);
    551                                         if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') )
    552                                                 die( "<h1>Error on form submission in Step 7.</h1><p>Please tell the devs.</p>" . addslashes(print_r($response, 1)));
     545                                        if ( $response['code'] >= 400 || strstr($response['body'], 'There are errors on this form') ) {
     546                                                $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['error'] = true;
     547                                                update_option('import-blogger', $this->import);
     548                                                $output .= "<p><strong>$blog_opt</strong> ".__('failed. Trying again.').'</p>';
     549                                        } else {
     550                                                $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['restored'] = true;
     551                                                update_option('import-blogger', $this->import);
     552                                                $output .= "<p><strong>$blog_opt</strong> ".__('restored.').'</p>';
     553                                        }
    553554                                }
    554                                 $this->import['blogs'][$_GET['blog']]['options']["$blog_opt"]['restored'] = true;
    555                                 update_option('import-blogger', $this->import);
    556555                                $did_one = true;
    557                                 $output .= "<p><strong>$blog_opt</strong> in progress. Please wait...";
    558556                        }
    559557                }
    … …  
    561559                if ( $did_one ) {
    562560                        die( $this->refresher(1000) . $output );
    563                 }
    564 
    565                 $this->set_next_step(8);
     561                } elseif ( $this->import['blogs'][$_GET['blog']]['options']['blog-publishing']['backup']['publishMode'] > 0 ) {
     562                        $this->set_next_step(9);
     563                } else {
     564                        $this->set_next_step(8);
     565                }
     566
    566567                $this->do_next_step();
    567568        }
    … …  
    569570        // Step 8: Republish, all back to normal
    570571        function republish_blog() {
    571                 echo $this->refresher(2400) . "<h1>Publishing with original template and options</h1>\n";
    572                 $this->publish_blogger(9);
     572                $this->publish_blogger(9, __('Publishing with original template and options'));
    573573        }
    574574
    575575        // Step 9: Congratulate the user
    576576        function congrats() {
    577                 echo "<h1>Congratulations!</h1>
    578 <p>Now that you have imported your Blogger blog into WordPress, what are you going to do? Here are some suggestions:</p>
    579 <ul>
    580 <li>That was hard work! Take a break.</li>
    581 ";
     577                echo '<h1>'.__('Congratulations!').'</h1><p>'.__('Now that you have imported your Blogger blog into WordPress, what are you going to do? Here are some suggestions:').'</p><ul><li>'.__('That was hard work! Take a break.').'</li>';
    582578                if ( count($this->import['blogs']) > 1 )
    583                         echo "<li>In case you haven't done it already, you can import the posts from any other blogs you may have:" . $this->show_blogs() . "</li>\n";
     579                        echo '<li>'.__('In case you haven\'t done it already, you can import the posts from your other blogs:'). $this->show_blogs() . '</li>';
    584580                if ( $n = count($this->import['blogs'][$_GET['blog']]['newusers']) )
    585                         echo "<li>Since we had to create $n new users, you probably want to go to <a href='users.php' target='_parent'>Authors & Users</a>, where you can give them new passwords or delete them. If you want to make all of the imported posts yours, you will be given that option when you delete the new authors.</li>\n";
    586                
    587                 echo "\n<ul>";
     581                        echo '<li>'.__('Go to <a href="users.php" target="_parent">Authors & Users</a>, where you can modify the new user(s) or delete them. If you want to make all of the imported posts yours, you will be given that option when you delete the new authors.').'</li>';
     582                echo '<li>'.__('For security, click the link below to reset this importer. That will clear your Blogger credentials and options from the database.').'</li>';
     583                echo '</ul>';
    588584        }
    589585
    … …  
    597593                        $this->import = get_settings('import-blogger');
    598594
    599                         if ( isset($_GET['step']) ) {
     595                        if ( false === $this->import ) {
     596                                $step = 0;
     597                        } elseif ( isset($_GET['step']) ) {
    600598                                $step = (int) $_GET['step'];
    601                         } elseif ( isset($_GET['blog']) ) {
     599                        } elseif ( isset($_GET['blog']) && isset($this->import['blogs'][$_GET['blog']]['nextstep']) ) {
    602600                                $step = $this->import['blogs'][$_GET['blog']]['nextstep'];
    603601                        } elseif ( is_array($this->import['blogs']) ) {
    … …  
    606604                                $step = 0;
    607605                        }
     606//echo "Step $step.";
     607//die('<pre>'.print_r($this->import,1).'</pre');
    608608                        switch ($step) {
    609609                                case 0 :
    … …  
    617617                                        break;
    618618                                case 3 :
     619                                        $this->wait_for_blogger();
     620                                        break;
     621                                case 4 :
    619622                                        $this->publish_blog();
    620623                                        break;
    621                                 case 4 :
    622624                                case 5 :
    623625                                        $this->get_archive_urls();
    … …  
    636638                                        break;
    637639                        }
    638 
    639640                        die;
    640 
     641                       
    641642                } else {
    642643                        $this->greet();
    … …  
    652653
    653654register_importer('blogger', 'Blogger', __('Import posts and comments from a Blogger account'), array ($blogger_import, 'start'));
     655
    654656?>
Note: See TracChangeset for help on using the changeset viewer.