<?php
/*
 * Nucleus 2.0 -> WordPress 1.2 Import script
 * @author: James Sasitorn
 * @email  camus@onegoodcookie.com
 *
 * This file should be put in wp-admin/ 
 *
 * 1. I assume Nucleus and WordPress share the same mysql database
 * 2. It allows you to only import one nucleus blog at a time
 * 3. It imports users, posts, and comments based on unique titles.
 * 4. It doesnt correctly import  blog settings (not a big deal)
 * 5. User names are imported as Nicknames (doesnt fill in First/last)
 * 6. It doesnt handle Nucleus karma.
 *
 *
 * 2005.02.14: added table prefix listed below $table_prefix for 
 * nueclues table prefix.. Would be better to put this in the gui
 *
 * * also cleaned up a typo in the insertion of categories..
 * * stopped that pesky 'We're done. have fun' from being on all screens
 * * stored blog_id from nucleus as session_var.
 *  Should put table_prefix as a html option
 *
 * 2005.03.11: Integrate changes by  Naoki Haga (kyodai.com)
 *  * Changes improve user mapping and comment handling
 *  * Story details includes both the excerpt \n\n and the content
 *  * Set default user level to 0
 */
if (!file_exists('../wp-config.php')) die("There doesn't seem to be a wp-config.php file. Double check that you updated wp-config-sample.php with the proper database connection information and renamed it to wp-config.php.");
require_once(
'../wp-config.php');
require(
'upgrade-functions.php');

/* configuration.. nucleus table prefix.. blah_nucleus_.. */
//$tbl_prefix="ta_";
$tbl_prefix="";

$step $_GET['step'];
if (!
$step$step 0;

if (isset (
$_POST['import_blog_id'])) {
   
$import_blog_id $_POST['import_blog_id'];
   
session_register('import_blog_id');
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <title>WordPress &#8212; Nucleus Conversion</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style media="screen" type="text/css">
    body {
        font-family: Georgia, "Times New Roman", Times, serif;
        margin-left: 15%;
        margin-right: 15%;
    }
    #logo {
        margin: 0;
        padding: 0;
        background-image: url(http://wordpress.org/images/wordpress.gif);
        background-repeat: no-repeat;
        height: 60px;
        border-bottom: 4px solid #333;
    }
    #logo a {
        display: block;
        height: 60px;
    }
    #logo a span {
        display: none;
    }
    p, li {
        line-height: 140%;
    }
    </style>
</head>
<body>
<h1 id="logo"><a href="http://wordpress.org"><span>WordPress</span></a></h1>
<?php
switch($step) {

    case 
0:
?>
<p>Welcome to WordPress. Since you&#8217;re upgrading from Nucelus thins will change a bit.
   Here are some notes on upgrading:</p>
<ul>
  <li>If you&#8217;re using an older version of Nucleus, it's probably a good idea to upgrade 
    to at least 2.0 before making the leap to WordPress.</li>
  <li>The templates are so much better, and there is so much more going on than 
    before it&#8217;s probably worth it to start from scratch and work back to your 
    design.</li>
  <li>You need to transfer some of your settings from your old <code>config.php</code>
    to <code>wp-config.php</code> file.</li>
  <li>WordPress issues should be discussed in our <a href="http://wordpress.org/support/">support 
    forums</a>.</li>
  <li><strong>Back up</strong> your database before you do anything. Yes, you.</li>
</ul>
<p>Have you looked at the <a href="../readme.html">readme</a>? If 
  you&#8217;re all ready, <a href="import-nucleus.php?step=1">let&#8217;s go</a>!</p>
<?php
    
break;
    
    case 
1:
?>
<h1>Step 1</h1>
<p>Okay first we&#8217;re going to set up the links database. You will need to choose which blog you will be importing.
We will import the categories, posts, members and comments from this blog. We will not import any blog settings. You will
need to do that manually.</p>


<p>Checking for tables...</p>
<?php
$result 
mysql_list_tables(DB_NAME);
if (!
$result) {
    print 
"DB Error, could not list tables\n";
    print 
'MySQL Error: ' mysql_error();
    exit;
}
?>
<p>Select the blog to import.</p>
<?php
    
echo '<form action="?step=2" method="post">';
$sql "SELECT * from ${tbl_prefix}nucleus_blog order by bnumber";
$result $wpdb->get_results($sql) or print ("Can't query ${tbl_prefix}nucleus_blog <br />" .$sql ."<br />" mysql_error());
    
?><select name="import_blog_id">

    <?php foreach($result as $blog) {
        echo 
'<option value="'.$blog->bnumber.'">'.$blog->bname.'</option><br />';
        } 
?>
    </select>


<input type="submit" value="Go to Step 2"><br/>
</form>

<?php
    
break;
    case 
2:
?>
<h1>Step 2</h1>

<p>Deleting older posts...</p>
<?
      $query 
"TRUNCATE $tablecategories"; echo $query ."<br />"$wpdb->query($query);
      
$query "TRUNCATE $tablecomments"; echo $query ."<br />"$wpdb->query($query);
      
$query "TRUNCATE $tableposts"; echo $query ."<br />"$wpdb->query($query);
      
$query "TRUNCATE $tablepost2cat"; echo $query ."<br />"$wpdb->query($query);
      
$query "DELETE FROM $tableusers WHERE ID>1"; echo $query ."<br />"$wpdb->query($query);
      
$query "ALTER TABLE $tableusers AUTO_INCREMENT=2"; echo $query ."<br />"$wpdb->query($query);
?>

<p>First we&#8217;re going to import categories.</p>

<?php
$did_category
=0;
$did_members=0;
$did_posts=0;
$did_comments=0;
$category_map = array();
$username_map = array();
$usermail_map = array();
$user_map = array();
$post_map = array();


$sql "SELECT * from ${tbl_prefix}nucleus_category where cblog=$import_blog_id order by catid";
$result $wpdb->get_results($sql);
foreach (
$result as $category) {
   
// See if the category exists yet
   
$category->cdesc str_replace("'""\\'"$category->cdesc);
   
$cat_id $wpdb->get_var("SELECT cat_ID from $tablecategories WHERE cat_name = '{$category->cname}'");
   if (
$cat_id) {
      echo 
".. '{$category->cname}' already exists <br>\n";
    } else if (!
$cat_id) { //lowest is 1
      
$cat_nicename sanitize_title($category->cname);

      
$query "INSERT INTO $tablecategories(cat_name,category_nicename,category_description) VALUES
             ('{$category->cname}', '$cat_nicename', '{$category->cdesc}')"
;
      echo 
$query ."<br />";
      echo 
".. Adding '{$category->cname}' <br>\n";
      
$wpdb->query($query);

      
//now map the category for later
      
$cat_id $wpdb->get_var("SELECT cat_ID from $tablecategories WHERE cat_name = '{$category->cname}'");
      
//TODO: insert in map
   
}
   
$category_map[$category->catid] = $cat_id;
   
$did_category 1;
}
//print_r($category_map);
//die;
?>

<p>Next we&#8217;re going to import nucleus members.</p>
<?

$user_map
[1] = 1//map admin user

$sql "SELECT nucleus_member.* from ${tbl_prefix}nucleus_member as nucleus_member,${tbl_prefix}nucleus_team as nucleus_team WHERE " .
    
" nucleus_team.tmember=nucleus_member.mnumber AND "  .
    
" nucleus_team.tblog={$import_blog_id} ";

$result $wpdb->get_results($sql);
foreach (
$result as $user) {
   
// See if the category exists yet
   
$user_id $wpdb->get_var("SELECT ID from $tableusers WHERE user_login = '{$user->mname}'");

   if (
$user_id != 1) {
      
//lowest is 1 and we auto map that to admin

      
$user->mnotes str_replace("'""\\'"$user->mnotes);
      
$query "INSERT INTO $tableusers (user_login, user_nickname, user_pass,user_email, user_url, ".
               
" user_description, user_domain, user_ip, user_level, user_idmode) VALUES ".
            
"  ('{$user->mname}', '{$user->mrealname}', '{$user->mpassword}','{$user->memail}', '{$user->murl}', ".
                
" '{$user->mnotes}', '127.0.0.1','127.0.0.1', 0, 'nickname'  )";
      
      
//echo $query ."<br />";
      
echo ".. Importing {$user->mname} <br />";
      
$wpdb->query($query);

      
//now map the user for later
      
$user_id $wpdb->get_var("SELECT ID from $tableusers WHERE user_login = '{$user->mname}'");
   }
   
$user_map[$user->mnumber] = $user_id;
   
$username_map[$user->mnumber] = $user->mname;
   
$usermail_map[$user->mnumber] = $user->memail;
   
$did_members 1;
}
?>
<p>Next we&#8217;re going to import nucleus posts.</p>

<?

$sql 
"SELECT * from ${tbl_prefix}nucleus_item WHERE iblog=$import_blog_id order by inumber";
$result $wpdb->get_results($sql);
foreach (
$result as $post) { 
   
$post_title addslashes($post->ititle);
   
$post_id $wpdb->get_var("SELECT ID from $tableposts WHERE post_title = '$post_title'");
   
$post_category $category_map[$post->icat];

   if (!
$post_id) {

      if (
trim($post->imore) != "") {
          
$post_excerpt addslashes($post->ibody);
          
$post_content $post_excerpt ."\n\n"addslashes($post->imore);
      } else {
          
$post_excerpt '';
          
$post_content addslashes($post->ibody);
      }      
            
      
$post_excerpt str_replace("<br />",""$post_excerpt);
      
$post_content str_replace("<br />",""$post_content);

      
$post_excerpt trim($post_excerpt);
      
$post_content trim($post_content);
    
      
$post_date strtotime($post->itime);
      
$post_date date('Y-m-d H:i:s'$post_date);
      
$post_date_gmt get_gmt_from_date("$post_date");
      if (
$post->iclosed) { 
         
$comment_status 'closed';
      } else { 
         
$comment_status 'open';
      }
   
      if (
$post->idraft) {
         
$post_status 'draft';
      } else {
         
$post_status 'publish';
      }
      
$post_author $user_map[$post->iauthor];
      
$post_name sanitize_title($post->ititle);
//echo "--- $post_content <br>\n";
      
$query =<<<EOF
INSERT INTO $tableposts(post_title, post_excerpt, post_content,post_author,
                     post_date, post_date_gmt,post_modified,post_modified_gmt,
                     comment_status, post_status, post_category, post_name)
 VALUES('$post_title','$post_excerpt', '$post_content','$post_author',
       '$post_date', '$post_date_gmt', '$post_date', '$post_date_gmt',
       '$comment_status', '$post_status', $post_category, '$post_name')
     
EOF;
   
//echo "$query <br /> ";
   
$wpdb->query($query);

   
$post_id $wpdb->get_var("SELECT ID from $tableposts WHERE post_title = '$post_title'");
   }
   
$post_map[$post->inumber] = $post_id;


   
//make sure post is linked
   
$exists $wpdb->get_row("SELECT * FROM $tablepost2cat WHERE post_id = $post_id AND category_id = $post_category");
   if (!
$exists) {
      
$wpdb->query("INSERT INTO $tablepost2cat
                (post_id, category_id)
                VALUES
                ($post_id, $post_category)  "
);
   }



}

   
//foreach post also add comments
$sql "SELECT * from ${tbl_prefix}nucleus_comment WHERE cblog=$import_blog_id order by cnumber";
$result $wpdb->get_results($sql);
foreach (
$result as $comment) {
   
$comment_content addslashes($comment->cbody);

   
$comment_id $wpdb->get_var("SELECT comment_ID FROM $tablecomments WHERE comment_date = '{$comment->ctime}' AND comment_content = '$comment_content'");
   if (!
$comment_id) {

      
$comment_author       addslashes($comment->cuser);
      
$comment_author_email addslashes($comment->cmail);
      if (
$comment->cmember) {
         
$cmember    $user_map[$comment->cmember];
         
$comment_author $username_map[$comment->cmember];
         
$comment_author_email $usermail_map[$comment->cmember];
      } else {
         
$cmember 0;
      }

      
$comment_post_ID $post_map[$comment->citem];
      
$comment_date strtotime($comment->ctime);
      
$comment_date date('Y-m-d H:i:s'$comment_date);
      
$comment_date_gmt get_gmt_from_date("$comment_date");

      
$query =<<<EOF
INSERT INTO $tablecomments (comment_content,comment_author, comment_author_email, user_id,
comment_post_ID,comment_date, comment_date_gmt,
comment_author_IP) VALUES
('$comment_content', '$comment_author', '$comment_author_email','$cmember', '$comment_post_ID','$comment_date','$comment_date_gmt','
{$comment->cip}')
EOF;
      
//echo "$query  <br /> ";
      
$wpdb->query($query);
      
      
$comment_id $wpdb->get_var("SELECT comment_ID FROM $tablecomments WHERE comment_date = '{$comment->ctime}' AND comment_content = '$comment_content'"); //unused
   
}
      

}
echo 
"<p>Looks like we are done now. <a href=\"../\">Have fun</a>!</p>";
}
?>




</body>
</html>