root/tags/1.5.1/wp-admin/import-greymatter.php

Revision 2565, 10.2 kB (checked in by ryan, 4 years ago)

Make greymatter importer less ugly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. You must install WordPress before you import any entries.");
3
4 require_once('../wp-config.php');
5 require('upgrade-functions.php');
6
7 $wpvarstoreset = array('action', 'gmpath', 'archivespath', 'lastentry');
8 for ($i=0; $i<count($wpvarstoreset); $i += 1) {
9     $wpvar = $wpvarstoreset[$i];
10     if (!isset($$wpvar)) {
11         if (empty($_POST["$wpvar"])) {
12             if (empty($_GET["$wpvar"])) {
13                 $$wpvar = '';
14             } else {
15                 $$wpvar = $_GET["$wpvar"];
16             }
17         } else {
18             $$wpvar = $_POST["$wpvar"];
19         }
20     }
21 }
22
23 header( 'Content-Type: text/html; charset=utf-8' );
24 ?>
25
26 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
27 <html xmlns="http://www.w3.org/1999/xhtml">
28 <title>WordPress &rsaquo; Import from GreyMatter</title>
29 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
30 <style media="screen" type="text/css">
31     body {
32         font-family: Georgia, "Times New Roman", Times, serif;
33         margin-left: 20%;
34         margin-right: 20%;
35     }
36     #logo {
37         margin: 0;
38         padding: 0;
39         background-image: url(http://wordpress.org/images/logo.png);
40         background-repeat: no-repeat;
41         height: 60px;
42         border-bottom: 4px solid #333;
43     }
44     #logo a {
45         display: block;
46         text-decoration: none;
47         text-indent: -100em;
48         height: 60px;
49     }
50     p {
51         line-height: 140%;
52     }
53     #authors li     {
54         padding:3px;
55         border: 1px solid #ccc;
56         width: 40%;
57         margin-bottom:2px;
58     }
59     </style>
60 </head><body>
61 <h1 id="logo"><a href="http://wordpress.org">WordPress</a></h1>
62
63 <?php
64 switch ($action) {
65
66 case "step1":
67
68     function gm2autobr($string) { // transforms GM's |*| into wp's <br />\n
69         $string = str_replace("|*|","<br />\n",$string);
70         return($string);
71     }
72
73     if (!@chdir($archivespath))
74         die("Wrong path, $archivespath\ndoesn't exist\non the server");
75
76     if (!@chdir($gmpath))
77         die("Wrong path, $gmpath\ndoesn't exist\non the server");
78 ?>
79
80 <p>The importer is running...</p>
81 <ul>
82 <li>importing users... <ul><?php
83
84     chdir($gmpath);
85     $userbase = file("gm-authors.cgi");
86
87     foreach($userbase as $user) {
88         $userdata=explode("|", $user);
89
90         $user_ip="127.0.0.1";
91         $user_domain="localhost";
92         $user_browser="server";
93
94         $s=$userdata[4];
95         $user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";
96
97         $user_login=addslashes($userdata[0]);
98         $pass1=addslashes($userdata[1]);
99         $user_nickname=addslashes($userdata[0]);
100         $user_email=addslashes($userdata[2]);
101         $user_url=addslashes($userdata[3]);
102         $user_joindate=addslashes($user_joindate);
103
104         $loginthere = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_login = '$user_login'");
105         if ($loginthere) {
106             echo "<li>user <i>$user_login</i>... <b>Already exists</b></li>";
107             continue;
108         }
109
110         $query = "INSERT INTO $wpdb->users (user_login,user_pass,user_nickname,user_email,user_url,user_ip,user_domain,user_browser,user_registered,user_level,user_idmode) VALUES ('$user_login','$pass1','$user_nickname','$user_email','$user_url','$user_ip','$user_domain','$user_browser','$user_joindate','1','nickname')";
111         $result = $wpdb->query($query);
112         if ($result==false) {
113             die ("<strong>ERROR</strong>: couldn't register an user!");
114         }
115         echo "<li>user <i>$user_login</i>... <b>Done</b></li>";
116
117     }
118
119 ?></ul><b>Done</b></li>
120 <li>importing posts, comments, and karma...<br /><ul><?php
121
122     chdir($archivespath);
123     
124     for($i = 0; $i <= $lastentry; $i = $i + 1) {
125         
126         $entryfile = "";
127         
128         if ($i<10000000) {
129             $entryfile .= "0";
130             if ($i<1000000) {
131                 $entryfile .= "0";
132                 if ($i<100000) {
133                     $entryfile .= "0";
134                     if ($i<10000) {
135                         $entryfile .= "0";
136                         if ($i<1000) {
137                             $entryfile .= "0";
138                             if ($i<100) {
139                                 $entryfile .= "0";
140                                 if ($i<10) {
141                                     $entryfile .= "0";
142         }}}}}}}
143
144         $entryfile .= "$i";
145
146         if (is_file($entryfile.".cgi")) {
147
148             $entry=file($entryfile.".cgi");
149             echo "<li>entry # $entryfile ";
150             $postinfo=explode("|",$entry[0]);
151             $postmaincontent=gm2autobr($entry[2]);
152             $postmorecontent=gm2autobr($entry[3]);
153
154             $post_author=trim(addslashes($postinfo[1]));
155             // we'll check the author is registered, or if it's a deleted author
156             $sql = "SELECT * FROM $wpdb->users WHERE user_login = '$post_author'";
157             $result = $wpdb->query($sql);
158             if (! $result) { // if deleted from GM, we register the author as a level 0 user in wp
159                 $user_ip="127.0.0.1";
160                 $user_domain="localhost";
161                 $user_browser="server";
162                 $user_joindate="1979-06-06 00:41:00";
163                 $user_login=addslashes($post_author);
164                 $pass1=addslashes("password");
165                 $user_nickname=addslashes($post_author);
166                 $user_email=addslashes("user@deleted.com");
167                 $user_url=addslashes("");
168                 $user_joindate=addslashes($user_joindate);
169                 $query = "INSERT INTO $wpdb->users (user_login,user_pass,user_nickname,user_email,user_url,user_ip,user_domain,user_browser,user_registered,user_level,user_idmode) VALUES ('$user_login','$pass1','$user_nickname','$user_email','$user_url','$user_ip','$user_domain','$user_browser','$user_joindate','0','nickname')";
170                 $result = $wpdb->query($query);
171                 if ($result==false) {
172                     die ("<strong>ERROR</strong>: couldn't register an user!");
173                 }
174                 echo ": registered deleted user <i>$user_login</i> at level 0 ";
175             }
176
177             $sql = "SELECT ID FROM $wpdb->users WHERE user_login = '$post_author'";
178             $post_author_ID = $wpdb->get_var($sql);
179
180             $post_title=gm2autobr($postinfo[2]);
181             $post_title=addslashes($post_title);
182
183             $postyear=$postinfo[6];
184             $postmonth=zeroise($postinfo[4],2);
185             $postday=zeroise($postinfo[5],2);
186             $posthour=zeroise($postinfo[7],2);
187             $postminute=zeroise($postinfo[8],2);
188             $postsecond=zeroise($postinfo[9],2);
189
190             if (($postinfo[10]=="PM") && ($posthour!="12"))
191                 $posthour=$posthour+12;
192
193             $post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
194
195             $post_content=$postmaincontent;
196             if (strlen($postmorecontent)>3)
197                 $post_content .= "<!--more--><br /><br />".$postmorecontent;
198             $post_content=addslashes($post_content);
199
200             $post_karma=$postinfo[12];
201
202             $query = "INSERT INTO $wpdb->posts (post_author,post_date,post_content,post_title) VALUES ('$post_author_ID','$post_date','$post_content','$post_title')";
203             $result = $wpdb->query($query);
204
205             if (!$result)
206                 die ("Error in posting...");
207             
208             $query = "SELECT ID FROM $wpdb->posts ORDER BY ID DESC LIMIT 1";
209             $post_ID = $wpdb->get_var($query);
210
211             // Grab a default category.
212             $post_category = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories LIMIT 1");
213
214             // Update the post2cat table.
215             $exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_ID AND category_id = $post_category");
216               
217             if (!$exists) {
218               $wpdb->query("
219                     INSERT INTO $wpdb->post2cat
220                     (post_id, category_id)
221                     VALUES
222                     ($post_ID, $post_category)
223                     ");
224             }
225
226             $c=count($entry);
227             if ($c>4) {
228                 for ($j=4;$j<$c;$j++) {
229                     $entry[$j]=gm2autobr($entry[$j]);
230                     $commentinfo=explode("|",$entry[$j]);
231                     $comment_post_ID=$post_ID;
232                     $comment_author=addslashes($commentinfo[0]);
233                     $comment_author_email=addslashes($commentinfo[2]);
234                     $comment_author_url=addslashes($commentinfo[3]);
235                     $comment_author_IP=addslashes($commentinfo[1]);
236
237                     $commentyear=$commentinfo[7];
238                     $commentmonth=zeroise($commentinfo[5],2);
239                     $commentday=zeroise($commentinfo[6],2);
240                     $commenthour=zeroise($commentinfo[8],2);
241                     $commentminute=zeroise($commentinfo[9],2);
242                     $commentsecond=zeroise($commentinfo[10],2);
243                     if (($commentinfo[11]=="PM") && ($commenthour!="12"))
244                         $commenthour=$commenthour+12;
245                     $comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
246
247                     $comment_content=addslashes($commentinfo[12]);
248
249                     $sql3 = "INSERT INTO $wpdb->comments (comment_post_ID,comment_author,comment_author_email,comment_author_url,comment_author_IP,comment_date,comment_content) VALUES ('$comment_post_ID','$comment_author','$comment_author_email','$comment_author_url','$comment_author_IP','$comment_date','$comment_content')";
250                     $result3 = $wpdb->query($sql3);
251                     if (!$result3)
252                         die ("There is an error with the database, it can't store your comment..");
253                 }
254                 $comments=$c-4;
255                 echo ": imported $comments comment";
256                 if ($comments>1)
257                     echo "s";
258             }
259             echo "... <b>Done</b></li>";
260         }
261     }
262     upgrade_all();
263     ?>
264 </ul><b>Done</b></li></ul>
265 <p>&nbsp;</p>
266 <p>Completed GM 2 WordPress import !</p>
267 <p>Now you can go and <a href="wp-login.php">log in</a>, have fun !</p>
268     <?php
269     break;
270
271 default:
272 ?>
273
274 <p>This is a basic GreyMatter to WordPress import script.</p>
275 <p>What it does:</p>
276 <ul>
277 <li>parses gm-authors.cgi to import authors: everyone is imported at level 1</li>
278 <li>parses the entries cgi files to import posts, comments, and karma on posts (although karma is not used on WordPress); if authors are found not to be in gm-authors.cgi, imports them at level 0</li>
279 </ul>
280 <p>What it does not:</p>
281 <ul>
282 <li>parse gm-counter.cgi (what's the use of that file ?), gm-banlist.cgi, gm-cplog.cgi (you can make a CP log hack if you really feel like it, but I question the need of a CP log)</li>
283 <li>import gm-templates. you'll start with the basic template wp.php</li>
284 <li>doesn't keep entries on top</li>
285 </ul>
286
287 <h3>First step: Install WordPress</h3>
288 <p>Install the WordPress blog as explained in the <a href="../readme.html" target="_blank">ReadMe</a>, then immediately come back here.</p>
289
290 <form name="stepOne" method="get">
291 <input type="hidden" name="action" value="step1" />
292 <h3>Second step: Provide GreyMatter details</h3>
293 <table cellpadding="0">
294 <tr>
295 <td>Path to GM files:</td>
296 <td><input type="text" style="width:300px" name="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td>
297 </tr>
298 <tr>
299 <td>Path to GM entries:</td>
300 <td><input type="text" style="width:300px" name="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td>
301 </tr>
302 </table>
303
304 <p>This importer will search for files 00000001.cgi to 000-whatever.cgi, so you need to enter the number of the last GM post here. (If you don't know that number, just log into your FTP and look it up in the entries' folder)</p>
305
306 <table>
307 <tr>
308 <td>Last entry's number:</td>
309 <td><input type="text" name="lastentry" value="00000001" /></td>
310 </tr>
311 </table>
312
313 <p>When you're ready, click OK to start importing: <input type="submit" name="submit" value="OK" class="search" /></p>
314 </form>
315
316 </body>
317 </html>
318     <?php
319     break;
320 }
321
322 ?>
Note: See TracBrowser for help on using the browser.