summaryrefslogtreecommitdiffstats
path: root/wp-inst/wp-admin/import-greymatter.php
blob: 4d96a82c0e27dfe66b3f785d3c55474fecbfa01f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
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.");

require_once('../wp-config.php');
require_once('upgrade-functions.php');

$wpvarstoreset = array('action', 'gmpath', 'archivespath', 'lastentry');
for ($i=0; $i<count($wpvarstoreset); $i += 1) {
	$wpvar = $wpvarstoreset[$i];
	if (!isset($$wpvar)) {
		if (empty($_POST["$wpvar"])) {
			if (empty($_GET["$wpvar"])) {
				$$wpvar = '';
			} else {
				$$wpvar = $_GET["$wpvar"];
			}
		} else {
			$$wpvar = $_POST["$wpvar"];
		}
	}
}

header( 'Content-Type: text/html; charset=utf-8' );
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>WordPress &rsaquo; Import from GreyMatter</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style media="screen" type="text/css">
	body {
		font-family: Georgia, "Times New Roman", Times, serif;
		margin-left: 20%;
		margin-right: 20%;
	}
	#logo {
		margin: 0;
		padding: 0;
		background-image: url(http://wordpress.org/images/logo.png);
		background-repeat: no-repeat;
		height: 60px;
		border-bottom: 4px solid #333;
	}
	#logo a {
		display: block;
		text-decoration: none;
		text-indent: -100em;
		height: 60px;
	}
	p {
		line-height: 140%;
	}
	#authors li 	{
		padding:3px;
		border: 1px solid #ccc;
		width: 40%;
		margin-bottom:2px;
	}
	</style>
</head><body> 
<h1 id="logo"><a href="http://wordpress.org">WordPress</a></h1> 

<?php
switch ($action) {

case "step1":

	function gm2autobr($string) { // transforms GM's |*| into wp's <br />\n
		$string = str_replace("|*|","<br />\n",$string);
		return($string);
	}

	if (!@chdir($archivespath))
		die("Wrong path, $archivespath\ndoesn't exist\non the server");

	if (!@chdir($gmpath))
		die("Wrong path, $gmpath\ndoesn't exist\non the server");
?>

<p>The importer is running...</p>
<ul>
<li>importing users... <ul><?php

	chdir($gmpath);
	$userbase = file("gm-authors.cgi");

	foreach($userbase as $user) {
		$userdata=explode("|", $user);

		$s=$userdata[4];
		$user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";

		$user_login=$wpdb->escape($userdata[0]);
		$pass1=$wpdb->escape($userdata[1]);
		$user_nickname=$wpdb->escape($userdata[0]);
		$user_email=$wpdb->escape($userdata[2]);
		$user_url=$wpdb->escape($userdata[3]);
		$user_joindate=$wpdb->escape($user_joindate);

		$loginthere = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE user_login = '$user_login'");
		if ($loginthere) {
			echo "<li>user <i>$user_login</i>... <b>Already exists</b></li>";
			continue;
		}

		$query = "INSERT INTO $wpdb->users (user_login,user_pass,user_email,user_url,user_registered,user_level) VALUES ('$user_login','$pass1','$user_email','$user_url','$user_joindate','1')";
		$result = $wpdb->query($query);
		if ($result==false) {
			die ("<strong>ERROR</strong>: couldn't register an user!");
		}
		echo "<li>user <i>$user_login</i>... <b>Done</b></li>";

	}

?></ul><b>Done</b></li>
<li>importing posts, comments, and karma...<br /><ul><?php

	chdir($archivespath);
	
	for($i = 0; $i <= $lastentry; $i = $i + 1) {
		
		$entryfile = "";
		
		if ($i<10000000) {
			$entryfile .= "0";
			if ($i<1000000) {
				$entryfile .= "0";
				if ($i<100000) {
					$entryfile .= "0";
					if ($i<10000) {
						$entryfile .= "0";
						if ($i<1000) {
							$entryfile .= "0";
							if ($i<100) {
								$entryfile .= "0";
								if ($i<10) {
									$entryfile .= "0";
		}}}}}}}

		$entryfile .= "$i";

		if (is_file($entryfile.".cgi")) {

			$entry=file($entryfile.".cgi");
			echo "<li>entry # $entryfile ";
			$postinfo=explode("|",$entry[0]);
			$postmaincontent=gm2autobr($entry[2]);
			$postmorecontent=gm2autobr($entry[3]);

			$post_author=trim($wpdb->escape($postinfo[1]));
			// we'll check the author is registered, or if it's a deleted author
			$sql = "SELECT * FROM $wpdb->users WHERE user_login = '$post_author'";
			$result = $wpdb->query($sql);
			if (! $result) { // if deleted from GM, we register the author as a level 0 user in wp
				$user_joindate="1979-06-06 00:41:00";
				$user_login=$wpdb->escape($post_author);
				$pass1=$wpdb->escape("password");
				$user_nickname=$wpdb->escape($post_author);
				$user_email=$wpdb->escape("user@deleted.com");
				$user_url=$wpdb->escape("");
				$user_joindate=$wpdb->escape($user_joindate);
				$query = "INSERT INTO $wpdb->users (user_login,user_pass,user_email,user_url,user_registered,user_level) VALUES ('$user_login','$pass1','$user_email','$user_url','$user_joindate','0')";
				$result = $wpdb->query($query);
				if ($result==false) {
					die ("<strong>ERROR</strong>: couldn't register an user!");
				}
				echo ": registered deleted user <i>$user_login</i> at level 0 ";
			}

			$sql = "SELECT ID FROM $wpdb->users WHERE user_login = '$post_author'";
			$post_author_ID = $wpdb->get_var($sql);

			$post_title=gm2autobr($postinfo[2]);
			$post_title=$wpdb->escape($post_title);

			$postyear=$postinfo[6];
			$postmonth=zeroise($postinfo[4],2);
			$postday=zeroise($postinfo[5],2);
			$posthour=zeroise($postinfo[7],2);
			$postminute=zeroise($postinfo[8],2);
			$postsecond=zeroise($postinfo[9],2);

			if (($postinfo[10]=="PM") && ($posthour!="12"))
				$posthour=$posthour+12;

			$post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";

			$post_content=$postmaincontent;
			if (strlen($postmorecontent)>3)
				$post_content .= "<!--more--><br /><br />".$postmorecontent;
			$post_content=$wpdb->escape($post_content);

			$post_karma=$postinfo[12];

			$query = "INSERT INTO $wpdb->posts (post_author,post_date,post_content,post_title) VALUES ('$post_author_ID','$post_date','$post_content','$post_title')";
			$result = $wpdb->query($query);

			if (!$result)
				die ("Error in posting...");
			
			$query = "SELECT ID FROM $wpdb->posts ORDER BY ID DESC LIMIT 1";
			$post_ID = $wpdb->get_var($query);

			// Grab a default category.
			$post_category = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories LIMIT 1");

			// Update the post2cat table.
			$exists = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post_ID AND category_id = $post_category");
			  
			if (!$exists) {
			  $wpdb->query("
					INSERT INTO $wpdb->post2cat
					(post_id, category_id)
					VALUES
					($post_ID, $post_category)
					");
			}

			$c=count($entry);
			if ($c>4) {
				for ($j=4;$j<$c;$j++) {
					$entry[$j]=gm2autobr($entry[$j]);
					$commentinfo=explode("|",$entry[$j]);
					$comment_post_ID=$post_ID;
					$comment_author=$wpdb->escape($commentinfo[0]);
					$comment_author_email=$wpdb->escape($commentinfo[2]);
					$comment_author_url=$wpdb->escape($commentinfo[3]);
					$comment_author_IP=$wpdb->escape($commentinfo[1]);

					$commentyear=$commentinfo[7];
					$commentmonth=zeroise($commentinfo[5],2);
					$commentday=zeroise($commentinfo[6],2);
					$commenthour=zeroise($commentinfo[8],2);
					$commentminute=zeroise($commentinfo[9],2);
					$commentsecond=zeroise($commentinfo[10],2);
					if (($commentinfo[11]=="PM") && ($commenthour!="12"))
						$commenthour=$commenthour+12;
					$comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";

					$comment_content=$wpdb->escape($commentinfo[12]);

					$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')";
					$result3 = $wpdb->query($sql3);
					if (!$result3)
						die ("There is an error with the database, it can't store your comment..");
				}
				$comments=$c-4;
				echo ": imported $comments comment";
				if ($comments>1)
					echo "s";
			}
			echo "... <b>Done</b></li>";
		}
	} 
	upgrade_all();
	?>
</ul><b>Done</b></li></ul>
<p>&nbsp;</p>
<p>Completed GM 2 WordPress import !</p>
<p>Now you can go and <a href="wp-login.php">log in</a>, have fun !</p>
	<?php
	break;

default:
?>

<p>This is a basic GreyMatter to WordPress import script.</p>
<p>What it does:</p>
<ul>
<li>parses gm-authors.cgi to import authors: everyone is imported at level 1</li>
<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>
</ul>
<p>What it does not:</p>
<ul>
<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>
<li>import gm-templates. you'll start with the basic template wp.php</li>
<li>doesn't keep entries on top</li>
</ul>

<h3>First step: Install WordPress</h3>
<p>Install the WordPress blog as explained in the <a href="../readme.html" target="_blank">ReadMe</a>, then immediately come back here.</p>

<form name="stepOne" method="get">
<input type="hidden" name="action" value="step1" />
<h3>Second step: Provide GreyMatter details</h3>
<table cellpadding="0">
<tr>
<td>Path to GM files:</td>
<td><input type="text" style="width:300px" name="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td>
</tr>
<tr>
<td>Path to GM entries:</td>
<td><input type="text" style="width:300px" name="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td>
</tr>
</table>

<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>

<table>
<tr>
<td>Last entry's number:</td>
<td><input type="text" name="lastentry" value="00000001" /></td>
</tr>
</table>

<p>When you're ready, click OK to start importing: <input type="submit" name="submit" value="OK" class="search" /></p>
</form>

</body>
</html>
	<?php
	break;
}

?>