<?php

/*
   pnconvert.php - a simple script to convert postnuke articles into WordPress
                   entries.
   Copyright 2004 Bryan Klingner (wares.b@overt.org)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
   
    
$hostname "localhost";
    
$username "user";
    
$password "password";

    
$sourcedb "postnuke"// your postnuke database
    
$sourcetable "nuke_stories"// your postnuke stories table
    
$sourcecat "nuke_topics"// your postnuke topics table
    
    
$destdb "wordpress"// your wordpress database
    
$desttable_prefix "test_"// you word press table prefix

    
$db_connect = @mysql_connect($hostname$username$password
                    or die(
"Fatal Error: ".mysql_error());
    
    
mysql_select_db($sourcedb$db_connect);
    
$srcresult mysql_query("select * from $sourcetable"$db_connect
                    or die(
"Fatal Error: ".mysql_error());

    
$srccatresult mysql_query("select * from $sourcecat"$db_connect)
                    or die(
"Fatal Error: ".mysql_error());
    
    
// sanitize function stolen from wordpress source
    // used to generate the dashed titles in the URLs
    
function sanitize($title) {
        
$title strtolower($title);
        
$title preg_replace('/&.+?;/'''$title); // kill entities
        
$title preg_replace('/[^a-z0-9 _-]/'''$title);
        
$title preg_replace('/\s+/'' '$title);
        
$title str_replace(' ''-'$title);
        
$title preg_replace('|-+|''-'$title);
        
$title trim($title'-');

        return 
$title;
    }
?>

<html>
<head>
    <title>Convert postnuke stories and comments to WordPress</title>
</head>
<body>

<?php
    
// copy over categories
    
$rownum 0;
    while (
$myrow mysql_fetch_array($srccatresult))
    {
        
$mytopictext mysql_escape_string($myrow['pn_topictext']);
        
$sql "INSERT INTO `" $desttable_prefix "categories`
               (
                 `cat_ID`,
                 `cat_name`,
                 `category_nicename`,
                 `category_description`
               )
               VALUES
               (
                 '$myrow[pn_topicid]',
                 '$myrow[pn_topicname]',
                 '$mytopictext',
                 ''
               );"
;
        print 
$sql;
        if (
$submit)
        {
            
mysql_select_db($destdb$db_connect);
            
mysql_query($sql$db_connect) or die("Fatal error: ".mysql_error());
        }
       

    }
               
?>
<table cellpadding="2" cellspacing="3" bgcolor="#DDDDDD">
    <thead>
        <td>pn_sid</td>
        <td>pn_title</td>
        <td>pn_time</td>
        <td>pn_hometext</td>
        <td>pn_topic</td>
        <td>pn_counter</td>
        <td>comments</td>
    </thead>
<?php
    $rownum 
0
    while (
$myrow mysql_fetch_array($srcresult))
    {
        
        
$myhometext mysql_escape_string($myrow['pn_hometext']);
        
$mytitle mysql_escape_string($myrow['pn_title']);
        
$myname mysql_escape_string(sanitize($mytitle));
        
$sql "INSERT INTO `" $desttable_prefix "posts` 
               ( 
                 `ID` ,
                 `post_author` ,
                 `post_date` ,
                 `post_date_gmt` ,
                 `post_content` ,
                 `post_title` ,
                 `post_name` ,
                 `post_category` ,
                 `post_excerpt` ,
                 `post_lat` ,
                 `post_lon` ,
                 `post_status` ,
                 `comment_status` ,
                 `ping_status` ,
                 `post_password` ,
                 `to_ping` ,
                 `pinged` ,
                 `post_modified` ,
                 `post_modified_gmt` ,
                 `post_content_filtered` ,
                 `post_parent` ) 
               VALUES 
               ( 
                 '',
                 '1',
                 '$myrow[pn_time]',
                 '0000-00-00 00:00:00',
                 '$myhometext',
                 '$mytitle',
                 '$myname',
                 '$myrow[pn_topic]',
                 '',
                 NULL ,
                 NULL ,
                 'publish',
                 'open',
                 'open',
                 '',
                 '',
                 '',
                 '$myrow[pn_time]',
                 '0000-00-00 00:00:00',
                 '',
                 '0' );"
;
      
        
// only really do insert if requested
        
if ($submit)
        {
            
mysql_select_db($destdb$db_connect);
            
mysql_query($sql$db_connect);
       
            
// now get the ID of the post we just added
            
$sql "select MAX(ID) from " $desttable_prefix "posts";
            
$getID mysql_query($sql$db_connect);
            
$currentID mysql_fetch_array($getID);
            
$currentID $currentID['MAX(ID)'];
            
printf("<h1>Just inserted ID %s</h1>\n"$currentID);
       
            
// add post2cat map... why does he have this table?
            
if ($myrow['pn_topic'] == 0)
                
$topicnum 1;
            else
                
$topicnum $myrow['pn_topic'];
            
$sql "insert into `" $desttable_prefix "post2cat` (`post_id`, `category_id`) VALUES ('$currentID', '$topicnum');";
            
$result mysql_query($sql$db_connect) or die("Fatal error: ".mysql_error());
        }
        
        
// print out source information
        
if ($rownum++ % == 0)
            
printf("<tr bgcolor=\"#BBBBBB\">");
        else
            
printf("<tr>\n");
        
printf("    <td>%s</td>\n
                    <td>%s</td>\n
                    <td>%s</td>\n
                    <td>%s</td>\n
                    <td>%s</td>\n
                    <td>%s</td>\n"

                
$myrow["pn_sid"], 
                
$myrow["pn_title"], 
                
$myrow["pn_time"], 
                
$myrow["pn_hometext"], 
                
$myrow["pn_topic"], 
                
$myrow["pn_counter"]);

        
// retreive all associated comments
        
$mysid $myrow["pn_sid"];
        
mysql_select_db($sourcedb$db_connect);
        
$comments mysql_query("select * from nuke_comments where pn_sid = $mysid"$db_connect);
        print 
"<td>";
        print 
"<table cellpadding=\"2\" cellspacing=\"3\" bgcolor=\"#DDDDDD\">";
        print 
"<thead>";
        print 
"    <td>pn_tid</td>";
        print 
"    <td>pn_sid</td>";
        print 
"    <td>pn_date</td>";
        print 
"    <td>pn_name</td>";
        print 
"    <td>pn_email</td>";
        print 
"    <td>pn_url</td>";
        print 
"    <td>pn_host_name</td>";
        print 
"    <td>pn_subject</td>";
        print 
"    <td>pn_comment</td>";
        print 
"</thead>";
            
        
$comrownum 0;
        while (
$comrow mysql_fetch_array($comments))
        {
            
            
$myname mysql_escape_string($comrow['pn_name']);
            
$myemail mysql_escape_string($comrow['pn_email']);
            
$myurl mysql_escape_string($comrow['pn_url']);
            
$myIP mysql_escape_string($comrow['pn_host_name']);
            
$mycomment mysql_escape_string($comrow['pn_comment']);
            
$sql "INSERT INTO `" $desttable_prefix "comments` 
                     ( 
                        `comment_ID` , 
                        `comment_post_ID` , 
                        `comment_author` , 
                        `comment_author_email` , 
                        `comment_author_url` , 
                        `comment_author_IP` , 
                        `comment_date` , 
                        `comment_date_gmt` , 
                        `comment_content` , 
                        `comment_karma` , 
                        `comment_approved` , 
                        `user_id` )
                     VALUES 
                     (
                        '',
                        '$currentID',
                        '$myname',
                        '$myemail',
                        '$myurl',
                        '$myIP',
                        '$comrow[pn_date]',
                        '0000-00-00 00:00:00',
                        '$mycomment',
                        '0',
                        '1',
                        '0'
                     );"
;
            
            if (
$submit)
            {
                
mysql_select_db($destdb$db_connect);
                
mysql_query($sql$db_connect);
            }
       
     
            if (
$comrownum++ %== 0)
                print 
"<tr bgcolor=\"#BBBBBB\">";
            else
                print 
"<tr bgcolor=\"#FFFFFF\">";
            
printf("    <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td></tr>\n"
,
                   
$comrow["pn_tid"],
                   
$comrow["pn_sid"],
                   
$comrow["pn_date"],
                   
$comrow["pn_name"],
                   
$comrow["pn_email"],
                   
$comrow["pn_url"],
                   
$comrow["pn_host_name"],
                   
$comrow["pn_subject"],
                   
$comrow["pn_comment"]);

        }
        print 
"</table>\n";
        print 
"</td>\n";
        print 
"</tr>\n";
    }
?>
</table>

<form method="post" action="<?php echo $PHP_SELF?>">
    <input type="submit" name="submit" value="Convert PN to Wordpress">
</form>


</body>
</html>