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