]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/greymatter.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / import / greymatter.php
1 <?php
2
3 class GM_Import {
4
5         var $gmnames = array ();
6
7         function header() {
8                 echo '<div class="wrap">';
9                 echo '<h2>'.__('Import GreyMatter').'</h2>';
10         }
11
12         function footer() {
13                 echo '</div>';
14         }
15
16         function greet() {
17                 $this->header();
18 ?>
19 <p><?php _e('This is a basic GreyMatter to WordPress import script.') ?></p>
20 <p><?php _e('What it does:') ?></p>
21 <ul>
22 <li><?php _e('Parses gm-authors.cgi to import (new) authors. Everyone is imported at level 1.') ?></li>
23 <li><?php _e('Parses the entries cgi files to import posts, comments, and karma on posts (although karma is not used on WordPress yet).<br />If authors are found not to be in gm-authors.cgi, imports them at level 0.') ?></li>
24 <li><?php _e("Detects duplicate entries or comments. If you don't import everything the first time, or this import should fail in the middle, duplicate entries will not be made when you try again.") ?></li>
25 </ul>
26 <p><?php _e('What it does not:') ?></p>
27 <ul>
28 <li><?php _e('Parse gm-counter.cgi, 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>
29 <li><?php _e('Import gm-templates.') ?></li>
30 <li><?php _e("Doesn't keep entries on top.")?></li>
31 </ul>
32 <p>&nbsp;</p>
33
34 <form name="stepOne" method="get">
35 <input type="hidden" name="import" value="greymatter" />
36 <input type="hidden" name="step" value="1" />
37 <?php wp_nonce_field('import-greymatter'); ?>
38 <h3><?php _e('Second step: GreyMatter details:') ?></h3>
39 <table class="form-table">
40 <tr>
41 <td><?php _e('Path to GM files:') ?></td>
42 <td><input type="text" style="width:300px" name="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td>
43 </tr>
44 <tr>
45 <td><?php _e('Path to GM entries:') ?></td>
46 <td><input type="text" style="width:300px" name="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td>
47 </tr>
48 <tr>
49 <td><?php _e("Last entry's number:") ?></td>
50 <td><input type="text" name="lastentry" value="00000001" /><br />
51         <?php _e("This importer will search for files 00000001.cgi to 000-whatever.cgi,<br />so you need to enter the number of the last GM post here.<br />(if you don't know that number, just log into your FTP and look it out<br />in the entries' folder)") ?></td>
52 </tr>
53 </table>
54 </p>
55 <p><input type="submit" name="submit" value="<?php _e('Start Importing') ?>" class="button" /></p>
56 </form>
57 <?php
58                 $this->footer();
59         }
60
61
62
63         function gm2autobr($string) { // transforms GM's |*| into b2's <br />\n
64                 $string = str_replace("|*|","<br />\n",$string);
65                 return($string);
66         }
67
68         function import() {
69                 global $wpdb;
70
71                 $wpvarstoreset = array('gmpath', 'archivespath', 'lastentry');
72                 for ($i=0; $i<count($wpvarstoreset); $i += 1) {
73                         $wpvar = $wpvarstoreset[$i];
74                         if (!isset($$wpvar)) {
75                                 if (empty($_POST["$wpvar"])) {
76                                         if (empty($_GET["$wpvar"])) {
77                                                 $$wpvar = '';
78                                         } else {
79                                                 $$wpvar = $_GET["$wpvar"];
80                                         }
81                                 } else {
82                                         $$wpvar = $_POST["$wpvar"];
83                                 }
84                         }
85                 }
86
87                 if (!chdir($archivespath))
88                         wp_die(__("Wrong path, the path to the GM entries does not exist on the server"));
89
90                 if (!chdir($gmpath))
91                         wp_die(__("Wrong path, the path to the GM files does not exist on the server"));
92
93                 $lastentry = (int) $lastentry;
94
95                 $this->header();
96 ?>
97 <p><?php _e('The importer is running...') ?></p>
98 <ul>
99 <li><?php _e('importing users...') ?><ul><?php
100
101         chdir($gmpath);
102         $userbase = file("gm-authors.cgi");
103
104         foreach($userbase as $user) {
105                 $userdata=explode("|", $user);
106
107                 $user_ip="127.0.0.1";
108                 $user_domain="localhost";
109                 $user_browser="server";
110
111                 $s=$userdata[4];
112                 $user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";
113
114                 $user_login=$wpdb->escape($userdata[0]);
115                 $pass1=$wpdb->escape($userdata[1]);
116                 $user_nickname=$wpdb->escape($userdata[0]);
117                 $user_email=$wpdb->escape($userdata[2]);
118                 $user_url=$wpdb->escape($userdata[3]);
119                 $user_joindate=$wpdb->escape($user_joindate);
120
121                 $user_id = username_exists($user_login);
122                 if ($user_id) {
123                         printf('<li>'.__('user %s').'<strong>'.__('Already exists').'</strong></li>', "<em>$user_login</em>");
124                         $this->gmnames[$userdata[0]] = $user_id;
125                         continue;
126                 }
127
128                 $user_info = array("user_login"=>"$user_login", "user_pass"=>"$pass1", "user_nickname"=>"$user_nickname", "user_email"=>"$user_email", "user_url"=>"$user_url", "user_ip"=>"$user_ip", "user_domain"=>"$user_domain", "user_browser"=>"$user_browser", "dateYMDhour"=>"$user_joindate", "user_level"=>"1", "user_idmode"=>"nickname");
129                 $user_id = wp_insert_user($user_info);
130                 $this->gmnames[$userdata[0]] = $user_id;
131
132                 printf('<li>'.__('user %s...').' <strong>'.__('Done').'</strong></li>', "<em>$user_login</em>");
133         }
134
135 ?></ul><strong><?php _e('Done') ?></strong></li>
136 <li><?php _e('importing posts, comments, and karma...') ?><br /><ul><?php
137
138         chdir($archivespath);
139
140         for($i = 0; $i <= $lastentry; $i = $i + 1) {
141
142                 $entryfile = "";
143
144                 if ($i<10000000) {
145                         $entryfile .= "0";
146                         if ($i<1000000) {
147                                 $entryfile .= "0";
148                                 if ($i<100000) {
149                                         $entryfile .= "0";
150                                         if ($i<10000) {
151                                                 $entryfile .= "0";
152                                                 if ($i<1000) {
153                                                         $entryfile .= "0";
154                                                         if ($i<100) {
155                                                                 $entryfile .= "0";
156                                                                 if ($i<10) {
157                                                                         $entryfile .= "0";
158                 }}}}}}}
159
160                 $entryfile .= "$i";
161
162                 if (is_file($entryfile.".cgi")) {
163
164                         $entry=file($entryfile.".cgi");
165                         $postinfo=explode("|",$entry[0]);
166                         $postmaincontent=$this->gm2autobr($entry[2]);
167                         $postmorecontent=$this->gm2autobr($entry[3]);
168
169                         $post_author=trim($wpdb->escape($postinfo[1]));
170
171                         $post_title=$this->gm2autobr($postinfo[2]);
172                         printf('<li>'.__('entry # %s : %s : by %s'), $entryfile, $post_title, $postinfo[1]);
173                         $post_title=$wpdb->escape($post_title);
174
175                         $postyear=$postinfo[6];
176                         $postmonth=zeroise($postinfo[4],2);
177                         $postday=zeroise($postinfo[5],2);
178                         $posthour=zeroise($postinfo[7],2);
179                         $postminute=zeroise($postinfo[8],2);
180                         $postsecond=zeroise($postinfo[9],2);
181
182                         if (($postinfo[10]=="PM") && ($posthour!="12"))
183                                 $posthour=$posthour+12;
184
185                         $post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
186
187                         $post_content=$postmaincontent;
188                         if (strlen($postmorecontent)>3)
189                                 $post_content .= "<!--more--><br /><br />".$postmorecontent;
190                         $post_content=$wpdb->escape($post_content);
191
192                         $post_karma=$postinfo[12];
193
194                         $post_status = 'publish'; //in greymatter, there are no drafts
195                         $comment_status = 'open';
196                         $ping_status = 'closed';
197
198                         if ($post_ID = post_exists($post_title, '', $post_date)) {
199                                 echo ' ';
200                                 _e('(already exists)');
201                         } else {
202                                 //just so that if a post already exists, new users are not created by checkauthor
203                                 // we'll check the author is registered, or if it's a deleted author
204                                 $user_id = username_exists($post_author);
205                                 if (!$user_id) {        // if deleted from GM, we register the author as a level 0 user
206                                         $user_ip="127.0.0.1";
207                                         $user_domain="localhost";
208                                         $user_browser="server";
209                                         $user_joindate="1979-06-06 00:41:00";
210                                         $user_login=$wpdb->escape($post_author);
211                                         $pass1=$wpdb->escape("password");
212                                         $user_nickname=$wpdb->escape($post_author);
213                                         $user_email=$wpdb->escape("user@deleted.com");
214                                         $user_url=$wpdb->escape("");
215                                         $user_joindate=$wpdb->escape($user_joindate);
216
217                                         $user_info = array("user_login"=>$user_login, "user_pass"=>$pass1, "user_nickname"=>$user_nickname, "user_email"=>$user_email, "user_url"=>$user_url, "user_ip"=>$user_ip, "user_domain"=>$user_domain, "user_browser"=>$user_browser, "dateYMDhour"=>$user_joindate, "user_level"=>0, "user_idmode"=>"nickname");
218                                         $user_id = wp_insert_user($user_info);
219                                         $this->gmnames[$postinfo[1]] = $user_id;
220
221                                         echo ': ';
222                                         printf(__('registered deleted user %s at level 0 '), "<em>$user_login</em>");
223                                 }
224
225                                 if (array_key_exists($postinfo[1], $this->gmnames)) {
226                                         $post_author = $this->gmnames[$postinfo[1]];
227                                 } else {
228                                         $post_author = $user_id;
229                                 }
230
231                                 $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt');
232                                 $post_ID = wp_insert_post($postdata);
233                                 if ( is_wp_error( $post_ID ) )
234                                         return $post_ID;
235                         }
236
237                         $c=count($entry);
238                         if ($c>4) {
239                                 $numAddedComments = 0;
240                                 $numComments = 0;
241                                 for ($j=4;$j<$c;$j++) {
242                                         $entry[$j]=$this->gm2autobr($entry[$j]);
243                                         $commentinfo=explode("|",$entry[$j]);
244                                         $comment_post_ID=$post_ID;
245                                         $comment_author=$wpdb->escape($commentinfo[0]);
246                                         $comment_author_email=$wpdb->escape($commentinfo[2]);
247                                         $comment_author_url=$wpdb->escape($commentinfo[3]);
248                                         $comment_author_IP=$wpdb->escape($commentinfo[1]);
249
250                                         $commentyear=$commentinfo[7];
251                                         $commentmonth=zeroise($commentinfo[5],2);
252                                         $commentday=zeroise($commentinfo[6],2);
253                                         $commenthour=zeroise($commentinfo[8],2);
254                                         $commentminute=zeroise($commentinfo[9],2);
255                                         $commentsecond=zeroise($commentinfo[10],2);
256                                         if (($commentinfo[11]=="PM") && ($commenthour!="12"))
257                                                 $commenthour=$commenthour+12;
258                                         $comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
259
260                                         $comment_content=$wpdb->escape($commentinfo[12]);
261
262                                         if (!comment_exists($comment_author, $comment_date)) {
263                                                 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved');
264                                                 $commentdata = wp_filter_comment($commentdata);
265                                                 wp_insert_comment($commentdata);
266                                                 $numAddedComments++;
267                                         }
268                                         $numComments++;
269                                 }
270                                 if ($numAddedComments > 0) {
271                                         echo ': ';
272                                 printf( __ngettext('imported %s comment', 'imported %s comments', $numAddedComments) , $numAddedComments);
273                                 }
274                                 $preExisting = $numComments - numAddedComments;
275                                 if ($preExisting > 0) {
276                                         echo ' ';
277                                         printf( __ngettext( 'ignored %s pre-existing comment', 'ignored %s pre-existing comments', $preExisting ) , $preExisting);
278                                 }
279                         }
280                         echo '... <strong>'.__('Done').'</strong></li>';
281                 }
282         }
283         do_action('import_done', 'greymatter');
284         ?>
285 </ul><strong><?php _e('Done') ?></strong></li></ul>
286 <p>&nbsp;</p>
287 <p><?php _e('Completed GreyMatter import!') ?></p>
288 <?php
289         $this->footer();
290         return;
291         }
292
293         function dispatch() {
294                 if (empty ($_GET['step']))
295                         $step = 0;
296                 else
297                         $step = (int) $_GET['step'];
298
299                 switch ($step) {
300                         case 0 :
301                                 $this->greet();
302                                 break;
303                         case 1:
304                                 check_admin_referer('import-greymatter');
305                                 $result = $this->import();
306                                 if ( is_wp_error( $result ) )
307                                         echo $result->get_error_message();
308                                 break;
309                 }
310         }
311
312         function GM_Import() {
313                 // Nothing.
314         }
315 }
316
317 $gm_import = new GM_Import();
318
319 register_importer('greymatter', __('GreyMatter'), __('Import users, posts, and comments from a Greymatter blog.'), array ($gm_import, 'dispatch'));
320 ?>