Make WordPress Core

Ticket #1123: import-blogger.php

File import-blogger.php, 11.2 KB (added by Skeltoac, 21 years ago)
Line 
1<?php
2$wpvarstoreset = array('action');
3for ($i=0; $i<count($wpvarstoreset); $i += 1) {
4        $wpvar = $wpvarstoreset[$i];
5        if (!isset($$wpvar)) {
6                if (empty($_POST["$wpvar"])) {
7                        if (empty($_GET["$wpvar"])) {
8                                $$wpvar = '';
9                        } else {
10                                $$wpvar = $_GET["$wpvar"];
11                        }
12                } else {
13                        $$wpvar = $_POST["$wpvar"];
14                }
15        }
16}
17        require_once('../wp-config.php');
18        require('upgrade-functions.php');
19switch ($action) {
20
21case "step1":
22?>
23<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
24<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
25        <title>Blogger to WordPress - Converting...</title>
26        <link rel="stylesheet" href="wp-admin.css" type="text/css">
27</head>
28<body>
29<div class="wrap">
30<h1>Blogger to <img src="../wp-images/wpminilogo.png" width="50" height="50" border="0" alt="WordPress" align="top" /></h1>
31<p>The importer is running...</p>
32<ul>
33        <li>Importing posts and users
34                <ul><?php
35
36        for($bgy=1999; $bgy<=(date('Y')); $bgy++) {
37                for($bgm=1; $bgm<13; $bgm++) {
38
39                $bgmm = zeroise($bgm,2);
40       
41                $archivefile = "../$bgy"."_"."$bgmm"."_01_wordpress.php";
42               
43                if (file_exists($archivefile)) {
44                        $postcount = 0;
45                        $skippedpostcount = 0;
46                        $commentcount = 0;
47                        $skippedcommentcount = 0;
48                        $f = fopen($archivefile,"r");
49                        $archive = fread($f, filesize($archivefile));
50                        fclose($f);
51                        echo "<li>$bgy/$bgmm ";
52
53                        $posts = explode('<wordpresspost>', $archive);
54
55                        for ($i = 1; $i < count($posts); $i = $i + 1) {
56
57                        $postparts = explode('<wordpresscomment>', $posts[$i]);
58                        $postinfo = explode('|||', $postparts[0]);
59                        $post_date = $postinfo[0];
60                        $post_content = $postinfo[2];
61                        // Don't try to re-use the original numbers
62                        // because the new, longer numbers are too
63                        // big to handle as ints.
64                        //$post_number = $postinfo[3];
65                        $post_title = $postinfo[4];
66
67                        $post_author = trim(addslashes($postinfo[1]));
68                        // we'll check the author is registered already
69                        $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$post_author'");
70                        if (!$user) { // seems s/he's not, so let's register
71                                $user_ip = '127.0.0.1';
72                                $user_domain = 'localhost';
73                                $user_browser = 'server';
74                                $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!)
75                                $user_login = addslashes($post_author);
76                                $pass1 = addslashes('password');
77                                $user_nickname = addslashes($post_author);
78                                $user_email = addslashes('user@wordpress.org');
79                                $user_url = addslashes('');
80                                $user_joindate = addslashes($user_joindate);
81                                $result = $wpdb->query("
82                                INSERT INTO $wpdb->users (
83                                        user_login,
84                                        user_pass,
85                                        user_nickname,
86                                        user_email,
87                                        user_url,
88                                        user_ip,
89                                        user_domain,
90                                        user_browser,
91                                        user_registered,
92                                        user_level,
93                                        user_idmode
94                                ) VALUES (
95                                        '$user_login',
96                                        '$pass1',
97                                        '$user_nickname',
98                                        '$user_email',
99                                        '$user_url',
100                                        '$user_ip',
101                                        '$user_domain',
102                                        '$user_browser',
103                                        '$user_joindate',
104                                        '1',
105                                        'nickname'
106                                )");
107
108                                echo ": Registered user <strong>$user_login</strong>";
109                        }
110
111                        $post_author_ID = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$post_author'");
112
113                        $post_date = explode(' ', $post_date);
114                        $post_date_Ymd = explode('/', $post_date[0]);
115                        $postyear = $post_date_Ymd[2];
116                        $postmonth = zeroise($post_date_Ymd[0], 2);
117                        $postday = zeroise($post_date_Ymd[1], 2);
118                        $post_date_His = explode(':', $post_date[1]);
119                        $posthour = zeroise($post_date_His[0], 2);
120                        $postminute = zeroise($post_date_His[1], 2);
121                        $postsecond = zeroise($post_date_His[2], 2);
122
123                        if (($post_date[2] == 'PM') && ($posthour != '12'))
124                                $posthour = $posthour + 12;
125                        else if (($post_date[2] == 'AM') && ($posthour == '12'))
126                                $posthour = '00';
127
128                        $post_date = "$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
129
130                        $post_content = addslashes($post_content);
131                        $post_content = str_replace('<br>', '<br />', $post_content); // the XHTML touch... ;)
132                       
133                        $post_title = addslashes($post_title);
134                       
135                        // Quick-n-dirty check for dups:
136                        $dupcheck = $wpdb->get_results("SELECT ID,post_date,post_title FROM $wpdb->posts WHERE post_date='$post_date' AND post_title='$post_title' LIMIT 1",ARRAY_A);
137                        if ($dupcheck[0]['ID']) {
138                                $skippedpostcount++;
139                                $comment_post_ID = $dupcheck[0]['ID'];
140                        } else {
141                                $result = $wpdb->query("
142                                INSERT INTO $wpdb->posts 
143                                        (post_author,post_date,post_content,post_title,post_category)
144                                VALUES
145                                        ('$post_author_ID','$post_date','$post_content','$post_title','1')
146                                ");
147                                $comment_post_ID = $wpdb->get_var("SELECT MAX(ID) FROM $wpdb->posts");
148                        }
149                        if ($postparts[1]) {
150                                for ($j = 1; $j < count($postparts); $j = $j + 1) {
151                                        $commentinfo = explode('|||', $postparts[$j]);
152                                        $comment_date = explode(' ', $commentinfo[0]);
153                                        $comment_date_Ymd = explode('/', $comment_date[0]);
154                                        $commentyear = $comment_date_Ymd[2];
155                                        $commentmonth = zeroise($comment_date_Ymd[0], 2);
156                                        $commentday = zeroise($comment_date_Ymd[1], 2);
157                                        $comment_date_His = explode(':', $comment_date[1]);
158                                        $commenthour = zeroise($comment_date_His[0], 2);
159                                        $commentminute = zeroise($comment_date_His[1], 2);
160                                        $commentsecond = '00';
161                                        if (($comment_date[2] == 'PM') && ($commenthour != '12'))
162                                                $commenthour = $commenthour + 12;
163                                        else if (($comment_date[2] == 'AM') && ($commenthour == '12'))
164                                                $commenthour = '00';
165                                        $comment_date = "$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
166                                        $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.
167                                        if ( strpos($commentinfo[1], 'a href') ) {
168                                                $comment_author_parts = explode('&quot;', htmlentities($commentinfo[1]));
169                                                $comment_author_url = $comment_author_parts[1];
170                                        } else $comment_author_url = '';
171                                        $comment_content = addslashes($commentinfo[2]);
172                                        $comment_content = str_replace('<br>', '<br />', $comment_content);
173                                        $dupcheck = $wpdb->get_results("SELECT comment_ID,comment_post_ID, comment_author, comment_date, comment_content FROM $wpdb->comments WHERE comment_post_ID='$comment_post_ID' AND comment_author='$comment_author' AND comment_date='$comment_date' AND comment_content='$comment_content' LIMIT 1",ARRAY_A);
174                                        if ( $dupcheck[0]['comment_ID'] ) {
175                                                $skippedcommentcount++;
176                                        } else {
177                                                $result = $wpdb->query("
178                                                INSERT INTO $wpdb->comments 
179                                                (comment_post_ID,comment_author,comment_author_url,comment_date,comment_content)
180                                                VALUES
181                                                        ('$comment_post_ID','$comment_author','$comment_author_url','$comment_date','$comment_content')
182                                                ");
183                                        }
184                                        $commentcount++; // Increment the monthly comment counter
185                                } // End of comment processing loop
186                        } // End of comment tester
187                        $postcount++;
188                        } // End of post processing loop
189                        echo "... $postcount post(s) parsed, $skippedpostcount skipped... $commentcount comment(s) parsed, $skippedcommentcount skipped... <strong>Done</strong></li>"; 
190                } // End of monthly archive processor
191                } // End of monthly loop
192        } // End of yearly loop
193upgrade_all();
194        ?>
195</ul>
196<strong>Done</strong>
197</li>
198</ul>
199<p>&nbsp;</p>
200<p>Completed Blogger to WordPress import!</p>
201<p><strong>Cleanup steps:</strong><br />Delete everything that Blogger put on your web server. Files like '2005_01_01_wordpress.php' just take up space and folders like '2005' can mess up your WordPress archives, so delete them.</p>
202<p>Now you can go and <a href="../wp-login.php">log in</a>, have fun!</p>
203</div>
204</body>
205</html>
206        <?php
207        break;
208
209default:
210
211        ?>
212<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
213<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
214        <title>Blogger to WordPress Import Utility</title>
215        <link rel="stylesheet" href="wp-admin.css" type="text/css">
216</head>
217
218<body>
219<div class="wrap">
220<h1>Blogger to <img src="../wp-images/wpminilogo.png" width="50" height="50" border="0" alt="WordPress" align="top" /></h1>
221<p>This is a complete Blogger to WordPress import script.</p>
222<p>What it does:</p>
223<ul>
224        <li>Parses your Blogger archives to retrieve your posts and comments.</li>
225        <li>Adds an author whenever it sees a new nickname, all authors are imported at level 1, with a default profile and the password 'password'</li>
226</ul>
227<p>What it does not:</p>
228<ul>
229        <li>It isn't very forgiving.
230        <li>It sucks at making coffee.</li>
231        <li>It always forgets to call back.</li>
232</ul>
233
234<h2>Follow these directions exactly, or else! You have been warned. :)</h2>
235<h3>First step: Install WordPress</h3>
236<p>Install the WordPress blog as explained in the <a href="../readme.html">read me</a>, then immediately come back here.</p>
237<h3>Second step: let's play with Blogger</h3>
238<ul><li>Log into your Blogger account. Go to Settings.</li>
239<li>In the Publishing tab, have Blogger publish your files in the directory where your WordPress resides. Save Settings.</li>
240<li>In the Formatting tab, set Timestamp Format to mm/dd/yyyy hh:mm:ss AM/PM (the first choice in the dropdown menu). Save Settings.</li>
241<li>In the Comments tab, set the Comments Timestamp Format just like you did in the previous step. Save Settings.</li>
242<li>In the Archives tab, set the frequency to 'monthly' and the archive filename to 'wordpress.php' (without the quotes), set the ftp archive path to make Blogger publish the archives in your WordPress directory. Save Settings.</li>
243<li>Go to your Template. Replace your existing template with this very long line (copy and paste, don't break it into lines):
244<code>&lt;Blogger>&lt;wordpresspost>&lt;$BlogItemDateTime$>|||&lt;$BlogItemAuthorNickname$>|||&lt;$BlogItemBody$>|||&lt;$BlogItemNumber$>|||&lt;$BlogItemSubject$>&lt;BlogItemCommentsEnabled>&lt;BlogItemComments>&lt;wordpresscomment>&lt;$BlogCommentDateTime$>|||&lt;$BlogCommentAuthor$>|||&lt;$BlogCommentBody$>&lt;/BlogItemComments>&lt;/BlogItemCommentsEnabled>&lt;/Blogger></code>
245Save Template Changes.</li>
246<li>Click 'Republish'. If you republish Index Only, it will not work. Sorry, you have to wait while Blogger does its thing.</li>
247<li>Check in your FTP that you've got the archive files published. They should look like this example: <code>2005_03_01_wordpress.php</code>. If they aren't there, redo the republish process.</li>
248</ul><p>You're done with the hard part. :)</p>
249<form name="stepOne" method="get">
250<input type="hidden" name="action" value="step1" />
251<h3>Third step: w00t, let's click OK:</h3>
252<p>When you're ready, click OK to start importing: <input type="submit" name="submit" value="OK" /><br /><br />
253<i><strong>Note:</strong> the script might take some time, like 2 seconds for every 100 entries (posts and comments)
254imported. DO NOT STOP IT or else you won't have a complete import.</i></p>
255</form>
256</div>
257</body>
258</html>
259        <?php
260        break;
261
262}
263
264?>