<?php
// For security reasons, fill in the connection details to your Textpattern database below:

$tp_database_name 'textpattern';
$tp_database_username 'username';
$tp_database_password 'password';
$tp_database_host 'localhost';

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(
'../wp-config.php');
require(
'upgrade-functions.php');

$step $_GET['step'];
if (!
$step$step 0;
?>
<!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 &rsaquo; Textpattern Import</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;
        text-decoration: none;
        text-indent: -100em;
        height: 60px;
    }
    p {
        line-height: 140%;
    }
    </style>
</head><body> 
<h1 id="logo"><a href="http://wordpress.org">WordPress</a></h1> 
<?php
switch($step) {

    case 
0:
?> 
<p>This script imports your entries from Textpattern into WordPress. It should be relatively painless, and we hope you're happy with the result.</p>
<p>To run this, you first need to edit this file (<code>import-textpattern.php</code>) and enter your Textpattern database connection details. Let's check if the database connection information works...</p>
<?php
$connection 
= @mysql_connect($tp_database_host$tp_database_username$tp_database_password);
$database = @mysql_select_db($tp_database_name);
if (
$connection && $database) {
?>
<p>Everything seems dandy so far, <a href="?step=1">let's get started</a>!</p>
<?php
} else {
?>
<p><em>It looks like your database information is incorrect. Please re-edit this file and double-check all the settings.</em></p>
<?php
}
    break;
    
    case 
1:
?> 
<h1>Step 1</h1> 
<p>First let's get posts and comments.</p> 
<?php
// For people running this on .72
$query "ALTER TABLE `$tableposts` ADD `post_name` VARCHAR(200) NOT NULL";
maybe_add_column($tableposts'post_name'$query);

// Create post_name field
$connection = @mysql_connect($tp_database_host$tp_database_username$tp_database_password);
$database = @mysql_select_db($tp_database_name);



// For now we're going to give everything the same author and same category
$author $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_level = 10 LIMIT 1");
//$category = $wpdb->get_var("SELECT cat_ID FROM $tablecategories LIMIT 1");

$posts mysql_query('SELECT * FROM textpattern'$connection);

while (
$post mysql_fetch_array($posts)) {
    
//  ID, AuthorID, LastMod, LastModID, Posted, Title, Body, Body_html, Abstract, Category1, Category2, Annotate, AnnotateInvite, Status, Listing1, Listing2, Section
    
$posted $post['Posted'];
    
$content $post['Body'];
    
$title $post['Title'];
    
$post_name sanitize_title($title);
    
$id $post['ID'];
    unset(
$post_categories);
    if(!empty(
$post['Category1'])) $post_categories[] = $post['Category1'];
    if(!empty(
$post['Category2'])) $post_categories[] = $post['Category2'];
        
    
$wpdb->query("INSERT INTO $tableposts
        (ID, post_author, post_date, post_content, post_title, post_name, post_status)
        VALUES
        ('$id', '$author', '$posted', '$content', '$title', '$post_name', 'publish')"
);

    if (
!= count($post_categories)) {
        foreach (
$post_categories as $post_category) {
            
// See if the category exists yet
            
$cat_id $wpdb->get_var("SELECT cat_ID from $tablecategories WHERE cat_name = '$post_category'");
            if (!
$cat_id && '' != trim($post_category)) {
                
$cat_nicename sanitize_title($post_category);
                
$wpdb->query("INSERT INTO $tablecategories (cat_name, category_nicename) VALUES ('$post_category', '$cat_nicename')");
                
$cat_id $wpdb->get_var("SELECT cat_ID from $tablecategories WHERE cat_name = '$post_category'");
            }
            if (
'' == trim($post_category)) $cat_id 1;
            
// Double check it's not there already
            
$exists $wpdb->get_row("SELECT * FROM $tablepost2cat WHERE post_id = $id AND category_id = $cat_id");
            if (!
$exists) { 
                
$wpdb->query("
                INSERT INTO $tablepost2cat
                (post_id, category_id)
                VALUES
                ($id, $cat_id)
                "
);
            }
        } 
// end category loop
    
} else {
        
$exists $wpdb->get_row("SELECT * FROM $tablepost2cat WHERE post_id = $id AND category_id = 1");
        if (!
$exists$wpdb->query("INSERT INTO $tablepost2cat (post_id, category_id) VALUES ($id, 1) ");
    }        

    
$comments mysql_query("SELECT * FROM txp_discuss WHERE parentid = $id");
    if (
$comments) {
        while(
$comment mysql_fetch_object($comments)) {
            
//  discussid, parentid, name, email, web, ip, posted, message
            // For some reason here "posted" is a real MySQL date, so we don't have to do anything about it
            //  comment_post_ID       comment_author       comment_author_email       comment_author_url       comment_author_IP       comment_date       comment_content       comment_karma
            
$wpdb->query("INSERT INTO $tablecomments
                (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_content)
                VALUES
                ($id, '$comment->name', '$comment->email', '$comment->web', '$comment->ip', '$comment->posted', '$comment->message')"
);
        }
    }
}

$links mysql_query('SELECT * FROM txp_link'$connection);
while (
$link mysql_fetch_object($links)) {
    
//id  date  category  url  linkname  linksort description 
    
    // See if the category exists yet
    
$linkcat_id $wpdb->get_var("SELECT cat_id from $tablelinkcategories WHERE cat_name = '$link->category'");
    if (!
$linkcat_id && '' != trim($link->category)) {
        
$wpdb->query("INSERT INTO $tablelinkcategories (cat_name) VALUES ('$link->category')");
        
$linkcat_id $wpdb->get_var("SELECT cat_ID from $tablelinkcategories WHERE cat_name = '$link->category'");
    }
    if (
'' == trim($link->category)) $linkcat_id 1;
    
    
$wpdb->query("INSERT INTO $tablelinks
        (link_url, link_name, link_category, link_description, link_updated)
        VALUES
        ('$link->url', '$link->linkname', $linkcat_id, '$link->description', '$link->date')"
);
}



upgrade_all();
?>
<p><strong>All done.</strong> Wasn&#8217;t that fun? <a href="../">Have fun</a>.</p> 
<?php
break;
}
?> 

</body>
</html>