web/wp-admin/import/greymatter.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * GreyMatter Importer
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Importer
       
     7  */
       
     8 
       
     9 /**
       
    10  * GreyMatter Importer class
       
    11  *
       
    12  * Basic GreyMatter to WordPress importer, will import posts, comments, and
       
    13  * posts karma.
       
    14  *
       
    15  * @since unknown
       
    16  */
       
    17 class GM_Import {
       
    18 
       
    19 	var $gmnames = array ();
       
    20 
       
    21 	function header() {
       
    22 		echo '<div class="wrap">';
       
    23 		screen_icon();
       
    24 		echo '<h2>'.__('Import GreyMatter').'</h2>';
       
    25 	}
       
    26 
       
    27 	function footer() {
       
    28 		echo '</div>';
       
    29 	}
       
    30 
       
    31 	function greet() {
       
    32 		$this->header();
       
    33 ?>
       
    34 <p><?php _e('This is a basic GreyMatter to WordPress import script.') ?></p>
       
    35 <p><?php _e('What it does:') ?></p>
       
    36 <ul>
       
    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>
       
    40 </ul>
       
    41 <p><?php _e('What it does not:') ?></p>
       
    42 <ul>
       
    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>
       
    46 </ul>
       
    47 <p>&nbsp;</p>
       
    48 
       
    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">
       
    55 <tr>
       
    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>
       
    58 </tr>
       
    59 <tr>
       
    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>
       
    62 </tr>
       
    63 <tr>
       
    64 <td><label for="lastentry"><?php _e('Last entry&#8217;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&#8217;t know that number, just log into your FTP and look it out<br />in the entries&#8217; folder)') ?></td>
       
    67 </tr>
       
    68 </table>
       
    69 <p class="submit"><input type="submit" name="submit" class="button" value="<?php esc_attr_e('Start Importing') ?>" /></p>
       
    70 </form>
       
    71 <?php
       
    72 		$this->footer();
       
    73 	}
       
    74 
       
    75 
       
    76 
       
    77 	function gm2autobr($string) { // transforms GM's |*| into b2's <br />\n
       
    78 		$string = str_replace("|*|","<br />\n",$string);
       
    79 		return($string);
       
    80 	}
       
    81 
       
    82 	function import() {
       
    83 		global $wpdb;
       
    84 
       
    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"])) {
       
    91 						$$wpvar = '';
       
    92 					} else {
       
    93 						$$wpvar = $_GET["$wpvar"];
       
    94 					}
       
    95 				} else {
       
    96 					$$wpvar = $_POST["$wpvar"];
       
    97 				}
       
    98 			}
       
    99 		}
       
   100 
       
   101 		if (!chdir($archivespath))
       
   102 			wp_die(__("Wrong path, the path to the GM entries does not exist on the server"));
       
   103 
       
   104 		if (!chdir($gmpath))
       
   105 			wp_die(__("Wrong path, the path to the GM files does not exist on the server"));
       
   106 
       
   107 		$lastentry = (int) $lastentry;
       
   108 
       
   109 		$this->header();
       
   110 ?>
       
   111 <p><?php _e('The importer is running...') ?></p>
       
   112 <ul>
       
   113 <li><?php _e('importing users...') ?><ul><?php
       
   114 
       
   115 	chdir($gmpath);
       
   116 	$userbase = file("gm-authors.cgi");
       
   117 
       
   118 	foreach($userbase as $user) {
       
   119 		$userdata=explode("|", $user);
       
   120 
       
   121 		$user_ip="127.0.0.1";
       
   122 		$user_domain="localhost";
       
   123 		$user_browser="server";
       
   124 
       
   125 		$s=$userdata[4];
       
   126 		$user_joindate=substr($s,6,4)."-".substr($s,0,2)."-".substr($s,3,2)." 00:00:00";
       
   127 
       
   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);
       
   134 
       
   135 		$user_id = username_exists($user_login);
       
   136 		if ($user_id) {
       
   137 			printf('<li>'.__('user %s').'<strong>'.__('Already exists').'</strong></li>', "<em>$user_login</em>");
       
   138 			$this->gmnames[$userdata[0]] = $user_id;
       
   139 			continue;
       
   140 		}
       
   141 
       
   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;
       
   145 
       
   146 		printf('<li>'.__('user %s...').' <strong>'.__('Done').'</strong></li>', "<em>$user_login</em>");
       
   147 	}
       
   148 
       
   149 ?></ul><strong><?php _e('Done') ?></strong></li>
       
   150 <li><?php _e('importing posts, comments, and karma...') ?><br /><ul><?php
       
   151 
       
   152 	chdir($archivespath);
       
   153 
       
   154 	for($i = 0; $i <= $lastentry; $i = $i + 1) {
       
   155 
       
   156 		$entryfile = "";
       
   157 
       
   158 		if ($i<10000000) {
       
   159 			$entryfile .= "0";
       
   160 			if ($i<1000000) {
       
   161 				$entryfile .= "0";
       
   162 				if ($i<100000) {
       
   163 					$entryfile .= "0";
       
   164 					if ($i<10000) {
       
   165 						$entryfile .= "0";
       
   166 						if ($i<1000) {
       
   167 							$entryfile .= "0";
       
   168 							if ($i<100) {
       
   169 								$entryfile .= "0";
       
   170 								if ($i<10) {
       
   171 									$entryfile .= "0";
       
   172 		}}}}}}}
       
   173 
       
   174 		$entryfile .= "$i";
       
   175 
       
   176 		if (is_file($entryfile.".cgi")) {
       
   177 
       
   178 			$entry=file($entryfile.".cgi");
       
   179 			$postinfo=explode("|",$entry[0]);
       
   180 			$postmaincontent=$this->gm2autobr($entry[2]);
       
   181 			$postmorecontent=$this->gm2autobr($entry[3]);
       
   182 
       
   183 			$post_author=trim($wpdb->escape($postinfo[1]));
       
   184 
       
   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);
       
   188 
       
   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);
       
   195 
       
   196 			if (($postinfo[10]=="PM") && ($posthour!="12"))
       
   197 				$posthour=$posthour+12;
       
   198 
       
   199 			$post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond";
       
   200 
       
   201 			$post_content=$postmaincontent;
       
   202 			if (strlen($postmorecontent)>3)
       
   203 				$post_content .= "<!--more--><br /><br />".$postmorecontent;
       
   204 			$post_content=$wpdb->escape($post_content);
       
   205 
       
   206 			$post_karma=$postinfo[12];
       
   207 
       
   208 			$post_status = 'publish'; //in greymatter, there are no drafts
       
   209 			$comment_status = 'open';
       
   210 			$ping_status = 'closed';
       
   211 
       
   212 			if ($post_ID = post_exists($post_title, '', $post_date)) {
       
   213 				echo ' ';
       
   214 				_e('(already exists)');
       
   215 			} else {
       
   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);
       
   230 
       
   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;
       
   234 
       
   235 					echo ': ';
       
   236 					printf(__('registered deleted user %s at level 0 '), "<em>$user_login</em>");
       
   237 				}
       
   238 
       
   239 				if (array_key_exists($postinfo[1], $this->gmnames)) {
       
   240 					$post_author = $this->gmnames[$postinfo[1]];
       
   241 				} else {
       
   242 					$post_author = $user_id;
       
   243 				}
       
   244 
       
   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 ) )
       
   248 					return $post_ID;
       
   249 			}
       
   250 
       
   251 			$c=count($entry);
       
   252 			if ($c>4) {
       
   253 				$numAddedComments = 0;
       
   254 				$numComments = 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]);
       
   263 
       
   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";
       
   273 
       
   274 					$comment_content=$wpdb->escape($commentinfo[12]);
       
   275 
       
   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);
       
   280 						$numAddedComments++;
       
   281 					}
       
   282 					$numComments++;
       
   283 				}
       
   284 				if ($numAddedComments > 0) {
       
   285 					echo ': ';
       
   286 				printf( _n('imported %s comment', 'imported %s comments', $numAddedComments) , $numAddedComments);
       
   287 				}
       
   288 				$preExisting = $numComments - numAddedComments;
       
   289 				if ($preExisting > 0) {
       
   290 					echo ' ';
       
   291 					printf( _n( 'ignored %s pre-existing comment', 'ignored %s pre-existing comments', $preExisting ) , $preExisting);
       
   292 				}
       
   293 			}
       
   294 			echo '... <strong>'.__('Done').'</strong></li>';
       
   295 		}
       
   296 	}
       
   297 	do_action('import_done', 'greymatter');
       
   298 	?>
       
   299 </ul><strong><?php _e('Done') ?></strong></li></ul>
       
   300 <p>&nbsp;</p>
       
   301 <p><?php _e('Completed GreyMatter import!') ?></p>
       
   302 <?php
       
   303 	$this->footer();
       
   304 	return;
       
   305 	}
       
   306 
       
   307 	function dispatch() {
       
   308 		if (empty ($_GET['step']))
       
   309 			$step = 0;
       
   310 		else
       
   311 			$step = (int) $_GET['step'];
       
   312 
       
   313 		switch ($step) {
       
   314 			case 0 :
       
   315 				$this->greet();
       
   316 				break;
       
   317 			case 1:
       
   318 				check_admin_referer('import-greymatter');
       
   319 				$result = $this->import();
       
   320 				if ( is_wp_error( $result ) )
       
   321 					echo $result->get_error_message();
       
   322 				break;
       
   323 		}
       
   324 	}
       
   325 
       
   326 	function GM_Import() {
       
   327 		// Nothing.
       
   328 	}
       
   329 }
       
   330 
       
   331 $gm_import = new GM_Import();
       
   332 
       
   333 register_importer('greymatter', __('GreyMatter'), __('Import users, posts, and comments from a Greymatter blog.'), array ($gm_import, 'dispatch'));
       
   334 ?>