10 * GreyMatter Importer class
12 * Basic GreyMatter to WordPress importer, will import posts, comments, and
19 var $gmnames = array ();
22 echo '<div class="wrap">';
24 echo '<h2>'.__('Import GreyMatter').'</h2>';
34 <p><?php _e('This is a basic GreyMatter to WordPress import script.') ?></p>
35 <p><?php _e('What it does:') ?></p>
37 <li><?php _e('Parses gm-authors.cgi to import (new) authors. Everyone is imported at level 1.') ?></li>
38 <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>
39 <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>
41 <p><?php _e('What it does not:') ?></p>
43 <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>
44 <li><?php _e('Import gm-templates.') ?></li>
45 <li><?php _e("Doesn't keep entries on top.")?></li>
49 <form name="stepOne" method="get" action="">
50 <input type="hidden" name="import" value="greymatter" />
51 <input type="hidden" name="step" value="1" />
52 <?php wp_nonce_field('import-greymatter'); ?>
53 <h3><?php _e('Second step: GreyMatter details:') ?></h3>
54 <table class="form-table">
56 <td><label for="gmpath"><?php _e('Path to GM files:') ?></label></td>
57 <td><input type="text" style="width:300px" name="gmpath" id="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td>
60 <td><label for="archivespath"><?php _e('Path to GM entries:') ?></label></td>
61 <td><input type="text" style="width:300px" name="archivespath" id="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td>
64 <td><label for="lastentry"><?php _e('Last entry’s number:') ?></label></td>
65 <td><input type="text" name="lastentry" id="lastentry" value="00000001" /><br />
66 <?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>
69 <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Start Importing') ?>" /></p>
77 function gm2autobr($string) { // transforms GM's |*| into b2's <br />\n
78 $string = str_replace("|*|","<br />\n",$string);
85 $wpvarstoreset = array('gmpath', 'archivespath', 'lastentry');
86 for ($i=0; $i<count($wpvarstoreset); $i += 1) {
87 $wpvar = $wpvarstoreset[$i];
88 if (!isset($$wpvar)) {
89 if (empty($_POST["$wpvar"])) {
90 if (empty($_GET["$wpvar"])) {
93 $$wpvar = $_GET["$wpvar"];
96 $$wpvar = $_POST["$wpvar"];
101 if (!chdir($archivespath))
102 wp_die(__("Wrong path, the path to the GM entries does not exist on the server"));
105 wp_die(__("Wrong path, the path to the GM files does not exist on the server"));
107 $lastentry = (int) $lastentry;
111 <p><?php _e('The importer is running...') ?></p>
113 <li><?php _e('importing users...') ?><ul><?php
116 $userbase = file("gm-authors.cgi");
118 foreach($userbase as $user) {
119 $userdata=explode("|", $user);
121 $user_ip="127.0.0.1";
122 $user_domain="localhost";
123 $user_browser="server";
126 $user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";
128 $user_login=$wpdb->escape($userdata[0]);
129 $pass1=$wpdb->escape($userdata[1]);
130 $user_nickname=$wpdb->escape($userdata[0]);
131 $user_email=$wpdb->escape($userdata[2]);
132 $user_url=$wpdb->escape($userdata[3]);
133 $user_joindate=$wpdb->escape($user_joindate);
135 $user_id = username_exists($user_login);
137 printf('<li>'.__('user %s').'<strong>'.__('Already exists').'</strong></li>', "<em>$user_login</em>");
138 $this->gmnames[$userdata[0]] = $user_id;
142 $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");
143 $user_id = wp_insert_user($user_info);
144 $this->gmnames[$userdata[0]] = $user_id;
146 printf('<li>'.__('user %s...').' <strong>'.__('Done').'</strong></li>', "<em>$user_login</em>");
149 ?></ul><strong><?php _e('Done') ?></strong></li>
150 <li><?php _e('importing posts, comments, and karma...') ?><br /><ul><?php
152 chdir($archivespath);
154 for($i = 0; $i <= $lastentry; $i = $i + 1) {
176 if (is_file($entryfile.".cgi")) {
178 $entry=file($entryfile.".cgi");
179 $postinfo=explode("|",$entry[0]);
180 $postmaincontent=$this->gm2autobr($entry[2]);
181 $postmorecontent=$this->gm2autobr($entry[3]);
183 $post_author=trim($wpdb->escape($postinfo[1]));
185 $post_title=$this->gm2autobr($postinfo[2]);
186 printf('<li>'.__('entry # %s : %s : by %s'), $entryfile, $post_title, $postinfo[1]);
187 $post_title=$wpdb->escape($post_title);
189 $postyear=$postinfo[6];
190 $postmonth=zeroise($postinfo[4],2);
191 $postday=zeroise($postinfo[5],2);
192 $posthour=zeroise($postinfo[7],2);
193 $postminute=zeroise($postinfo[8],2);
194 $postsecond=zeroise($postinfo[9],2);
196 if (($postinfo[10]=="PM") && ($posthour!="12"))
197 $posthour=$posthour+12;
199 $post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
201 $post_content=$postmaincontent;
202 if (strlen($postmorecontent)>3)
203 $post_content .= "<!--more--><br /><br />".$postmorecontent;
204 $post_content=$wpdb->escape($post_content);
206 $post_karma=$postinfo[12];
208 $post_status = 'publish'; //in greymatter, there are no drafts
209 $comment_status = 'open';
210 $ping_status = 'closed';
212 if ($post_ID = post_exists($post_title, '', $post_date)) {
214 _e('(already exists)');
216 //just so that if a post already exists, new users are not created by checkauthor
217 // we'll check the author is registered, or if it's a deleted author
218 $user_id = username_exists($post_author);
219 if (!$user_id) { // if deleted from GM, we register the author as a level 0 user
220 $user_ip="127.0.0.1";
221 $user_domain="localhost";
222 $user_browser="server";
223 $user_joindate="1979-06-06 00:41:00";
224 $user_login=$wpdb->escape($post_author);
225 $pass1=$wpdb->escape("password");
226 $user_nickname=$wpdb->escape($post_author);
227 $user_email=$wpdb->escape("user@deleted.com");
228 $user_url=$wpdb->escape("");
229 $user_joindate=$wpdb->escape($user_joindate);
231 $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");
232 $user_id = wp_insert_user($user_info);
233 $this->gmnames[$postinfo[1]] = $user_id;
236 printf(__('registered deleted user %s at level 0 '), "<em>$user_login</em>");
239 if (array_key_exists($postinfo[1], $this->gmnames)) {
240 $post_author = $this->gmnames[$postinfo[1]];
242 $post_author = $user_id;
245 $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');
246 $post_ID = wp_insert_post($postdata);
247 if ( is_wp_error( $post_ID ) )
253 $numAddedComments = 0;
255 for ($j=4;$j<$c;$j++) {
256 $entry[$j]=$this->gm2autobr($entry[$j]);
257 $commentinfo=explode("|",$entry[$j]);
258 $comment_post_ID=$post_ID;
259 $comment_author=$wpdb->escape($commentinfo[0]);
260 $comment_author_email=$wpdb->escape($commentinfo[2]);
261 $comment_author_url=$wpdb->escape($commentinfo[3]);
262 $comment_author_IP=$wpdb->escape($commentinfo[1]);
264 $commentyear=$commentinfo[7];
265 $commentmonth=zeroise($commentinfo[5],2);
266 $commentday=zeroise($commentinfo[6],2);
267 $commenthour=zeroise($commentinfo[8],2);
268 $commentminute=zeroise($commentinfo[9],2);
269 $commentsecond=zeroise($commentinfo[10],2);
270 if (($commentinfo[11]=="PM") && ($commenthour!="12"))
271 $commenthour=$commenthour+12;
272 $comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond";
274 $comment_content=$wpdb->escape($commentinfo[12]);
276 if (!comment_exists($comment_author, $comment_date)) {
277 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved');
278 $commentdata = wp_filter_comment($commentdata);
279 wp_insert_comment($commentdata);
284 if ($numAddedComments > 0) {
286 printf( _n('imported %s comment', 'imported %s comments', $numAddedComments) , $numAddedComments);
288 $preExisting = $numComments - numAddedComments;
289 if ($preExisting > 0) {
291 printf( _n( 'ignored %s pre-existing comment', 'ignored %s pre-existing comments', $preExisting ) , $preExisting);
294 echo '... <strong>'.__('Done').'</strong></li>';
297 do_action('import_done', 'greymatter');
299 </ul><strong><?php _e('Done') ?></strong></li></ul>
301 <p><?php _e('Completed GreyMatter import!') ?></p>
307 function dispatch() {
308 if (empty ($_GET['step']))
311 $step = (int) $_GET['step'];
318 check_admin_referer('import-greymatter');
319 $result = $this->import();
320 if ( is_wp_error( $result ) )
321 echo $result->get_error_message();
326 function GM_Import() {
331 $gm_import = new GM_Import();
333 register_importer('greymatter', __('GreyMatter'), __('Import users, posts, and comments from a Greymatter blog.'), array ($gm_import, 'dispatch'));