Changeset 3546

Show
Ignore:
Timestamp:
02/18/06 00:06:33 (3 years ago)
Author:
ryan
Message:

greymatter importer from bungeman. fixes #2464

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/import/greymatter.php

    r3096 r3546  
     1<?php 
     2 
     3class GM_Import { 
     4 
     5    var $gmnames = array (); 
     6 
     7    function header() { 
     8        echo '<div class="wrap">'; 
     9        echo '<h2>'.__('Import Graymatter').'</h2>'; 
     10    } 
     11 
     12    function footer() { 
     13        echo '</div>'; 
     14    } 
     15 
     16    function greet() { 
     17        $this->header(); 
     18?> 
     19<p>This is a basic GreyMatter to WordPress import script.</p> 
     20<p>What it does:</p> 
     21<ul> 
     22<li>Parses gm-authors.cgi to import (new) authors. Everyone is imported at level 1.</li> 
     23<li>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>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>What it does not:</p> 
     27<ul> 
     28<li>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>Import gm-templates.</li> 
     30<li>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<h3>Second step: GreyMatter details:</h3> 
     38<p><table cellpadding="0"> 
     39<tr> 
     40<td>Path to GM files:</td> 
     41<td><input type="text" style="width:300px" name="gmpath" value="/home/my/site/cgi-bin/greymatter/" /></td> 
     42</tr> 
     43<tr> 
     44<td>Path to GM entries:</td> 
     45<td><input type="text" style="width:300px" name="archivespath" value="/home/my/site/cgi-bin/greymatter/archives/" /></td> 
     46</tr> 
     47<tr> 
     48<td colspan="2"><br />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> 
     49</tr> 
     50<tr> 
     51<td>Last entry's number:</td> 
     52<td><input type="text" name="lastentry" value="00000001" /></td> 
     53</tr> 
     54</table> 
     55</p> 
     56<p>When you're ready, click OK to start importing: <input type="submit" name="submit" value="OK" class="search" /></p> 
     57</form> 
     58<p>&nbsp</p> 
     59<?php 
     60        $this->footer(); 
     61    } 
     62 
     63 
     64 
     65    function gm2autobr($string) { // transforms GM's |*| into b2's <br />\n 
     66        $string = str_replace("|*|","<br />\n",$string); 
     67        return($string); 
     68    } 
     69     
     70    function import() { 
     71        global $wpdb; 
     72     
     73        $wpvarstoreset = array('gmpath', 'archivespath', 'lastentry'); 
     74        for ($i=0; $i<count($wpvarstoreset); $i += 1) { 
     75            $wpvar = $wpvarstoreset[$i]; 
     76            if (!isset($$wpvar)) { 
     77                if (empty($_POST["$wpvar"])) { 
     78                    if (empty($_GET["$wpvar"])) { 
     79                        $$wpvar = ''; 
     80                    } else { 
     81                        $$wpvar = $_GET["$wpvar"]; 
     82                    } 
     83                } else { 
     84                    $$wpvar = $_POST["$wpvar"]; 
     85                } 
     86            } 
     87        } 
     88 
     89        if (!chdir($archivespath)) 
     90            die("Wrong path, $archivespath\ndoesn't exist\non the server"); 
     91 
     92        if (!chdir($gmpath)) 
     93            die("Wrong path, $gmpath\ndoesn't exist\non the server"); 
     94             
     95        $this->header(); 
     96?> 
     97<p>The importer is running...</p> 
     98<ul> 
     99<li>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            echo "<li>user <i>$user_login</i>... <b>Already exists</b></li>"; 
     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        echo "<li>user <i>$user_login</i>... <b>Done</b></li>"; 
     133 
     134    } 
     135 
     136?></ul><b>Done</b></li> 
     137<li>importing posts, comments, and karma...<br /><ul><?php 
     138 
     139    chdir($archivespath); 
     140     
     141    for($i = 0; $i <= $lastentry; $i = $i + 1) { 
     142         
     143        $entryfile = ""; 
     144         
     145        if ($i<10000000) { 
     146            $entryfile .= "0"; 
     147            if ($i<1000000) { 
     148                $entryfile .= "0"; 
     149                if ($i<100000) { 
     150                    $entryfile .= "0"; 
     151                    if ($i<10000) { 
     152                        $entryfile .= "0"; 
     153                        if ($i<1000) { 
     154                            $entryfile .= "0"; 
     155                            if ($i<100) { 
     156                                $entryfile .= "0"; 
     157                                if ($i<10) { 
     158                                    $entryfile .= "0"; 
     159        }}}}}}} 
     160 
     161        $entryfile .= "$i"; 
     162 
     163        if (is_file($entryfile.".cgi")) { 
     164 
     165            $entry=file($entryfile.".cgi"); 
     166            echo "<li>entry # $entryfile "; 
     167            $postinfo=explode("|",$entry[0]); 
     168            $postmaincontent=$this->gm2autobr($entry[2]); 
     169            $postmorecontent=$this->gm2autobr($entry[3]); 
     170 
     171            $post_author=trim($wpdb->escape($postinfo[1])); 
     172 
     173            $post_title=$this->gm2autobr($postinfo[2]); 
     174            echo " : $post_title : by $postinfo[1]"; 
     175            $post_title=$wpdb->escape($post_title); 
     176 
     177            $postyear=$postinfo[6]; 
     178            $postmonth=zeroise($postinfo[4],2); 
     179            $postday=zeroise($postinfo[5],2); 
     180            $posthour=zeroise($postinfo[7],2); 
     181            $postminute=zeroise($postinfo[8],2); 
     182            $postsecond=zeroise($postinfo[9],2); 
     183 
     184            if (($postinfo[10]=="PM") && ($posthour!="12")) 
     185                $posthour=$posthour+12; 
     186 
     187            $post_date="$postyear-$postmonth-$postday $posthour:$postminute:$postsecond"; 
     188 
     189            $post_content=$postmaincontent; 
     190            if (strlen($postmorecontent)>3) 
     191                $post_content .= "<!--more--><br /><br />".$postmorecontent; 
     192            $post_content=$wpdb->escape($post_content); 
     193 
     194            $post_karma=$postinfo[12]; 
     195 
     196            $post_status = 'publish'; //in greymatter, there are no drafts 
     197            $comment_status = 'open'; 
     198            $ping_status = 'closed'; 
     199             
     200            if ($post_ID = post_exists($post_title, '', $post_date)) { 
     201                echo ' (already exists)'; 
     202            } else { 
     203                //just so that if a post already exists, new users are not created by checkauthor 
     204                // we'll check the author is registered, or if it's a deleted author 
     205                $user_id = username_exists($post_author); 
     206                if (!$user_id) {    // if deleted from GM, we register the author as a level 0 user 
     207                    $user_ip="127.0.0.1"; 
     208                    $user_domain="localhost"; 
     209                    $user_browser="server"; 
     210                    $user_joindate="1979-06-06 00:41:00"; 
     211                    $user_login=$wpdb->escape($post_author); 
     212                    $pass1=$wpdb->escape("password"); 
     213                    $user_nickname=$wpdb->escape($post_author); 
     214                    $user_email=$wpdb->escape("user@deleted.com"); 
     215                    $user_url=$wpdb->escape(""); 
     216                    $user_joindate=$wpdb->escape($user_joindate); 
     217                     
     218                    $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"); 
     219                    $user_id = wp_insert_user($user_info); 
     220                    $this->gmnames[$postinfo[1]] = $user_id; 
     221                     
     222                    echo ": registered deleted user <i>$user_login</i> at level 0 "; 
     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            } 
     234 
     235            $c=count($entry); 
     236            if ($c>4) { 
     237                $numAddedComments = 0; 
     238                $numComments = 0; 
     239                for ($j=4;$j<$c;$j++) { 
     240                    $entry[$j]=$this->gm2autobr($entry[$j]); 
     241                    $commentinfo=explode("|",$entry[$j]); 
     242                    $comment_post_ID=$post_ID; 
     243                    $comment_author=$wpdb->escape($commentinfo[0]); 
     244                    $comment_author_email=$wpdb->escape($commentinfo[2]); 
     245                    $comment_author_url=$wpdb->escape($commentinfo[3]); 
     246                    $comment_author_IP=$wpdb->escape($commentinfo[1]); 
     247 
     248                    $commentyear=$commentinfo[7]; 
     249                    $commentmonth=zeroise($commentinfo[5],2); 
     250                    $commentday=zeroise($commentinfo[6],2); 
     251                    $commenthour=zeroise($commentinfo[8],2); 
     252                    $commentminute=zeroise($commentinfo[9],2); 
     253                    $commentsecond=zeroise($commentinfo[10],2); 
     254                    if (($commentinfo[11]=="PM") && ($commenthour!="12")) 
     255                        $commenthour=$commenthour+12; 
     256                    $comment_date="$commentyear-$commentmonth-$commentday $commenthour:$commentminute:$commentsecond"; 
     257 
     258                    $comment_content=$wpdb->escape($commentinfo[12]); 
     259 
     260                    if (!comment_exists($comment_author, $comment_date)) { 
     261                        $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_content', 'comment_approved'); 
     262                        $commentdata = wp_filter_comment($commentdata); 
     263                        wp_insert_comment($commentdata); 
     264                        $numAddedComments++; 
     265                    } 
     266                    $numComments++; 
     267                } 
     268                if ($numAddedComments > 0) { 
     269                    echo ": imported $numAddedComments comment"; 
     270                    if ($numAddedComments != 1) 
     271                        echo "s"; 
     272                } 
     273                $preExisting = $numComments - numAddedComments; 
     274                if ($preExisting > 0) 
     275                    echo " (ignored $preExisting pre-existing comments)"; 
     276            } 
     277            echo "... <b>Done</b></li>"; 
     278        } 
     279    } 
     280    ?> 
     281</ul><b>Done</b></li></ul> 
     282<p>&nbsp;</p> 
     283<p>Completed Greymatter import !</p> 
     284<?php 
     285    $this->footer(); 
     286    } 
     287 
     288    function dispatch() { 
     289        if (empty ($_GET['step'])) 
     290            $step = 0; 
     291        else 
     292            $step = (int) $_GET['step']; 
     293 
     294        switch ($step) { 
     295            case 0 : 
     296                $this->greet(); 
     297                break; 
     298            case 1: 
     299                $this->import(); 
     300                break; 
     301        } 
     302    } 
     303 
     304    function GM_Import() { 
     305        // Nothing.  
     306    } 
     307} 
     308 
     309$gm_import = new GM_Import(); 
     310 
     311register_importer('greymatter', 'Greymatter', __('Import posts and comments from your Greymatter blog'), array ($gm_import, 'dispatch')); 
     312?>