I needed to move my blog from Xoops2 to MT4, however MTs importing format proved of little help. It seems to have major issues with page-breaks.

So I made a php script which I'd like to share. It's not plug-and-play, and I cannot be held responsible for any damages it may cause, however I think it can be a lot of help as it's been for me.


PS: On my system the Xoops2 mysql tables and MT mysql tables are in the same database. That has simplified things a great deal.


CODE
<?php

    function query($q_str,$verbose=0)
    /**
    *    This function gets a query-string and sends it to the current database
    */
    {
        /**
        Add your values to these parameters if they're not in vars.php
        $database =
        $dbhost =
        $db_user =
        $db_pass =  
         */
        require '../vars.php';
        
        $link = mysql_connect("$dbhost", "$db_user", "$db_pass")
            or die("Unable to connect\n".mysql_errno().":".mysql_error()."\n");

        mysql_select_db("$database")
            or die("Unable to select db ".mysql_error()."\n");    
        if ($verbose == 1)
            echo "$q_str<br>";
        

        $result = mysql_query($q_str); // consider wheather you want to post the error-mes to the users.
            if (mysql_errno() != 0) {
                die("<b>Query Failed:</b>". mysql_errno().": ".mysql_error()."<BR>$q_str");
            }

        return $result;
    }

    function xoopsCodeDecode($text)
    /**
     *    taken from Xoops own library
     */
    {
        $patterns = array();
        $replacements = array();

        $patterns[] = "/\[siteurl=(['\"]?)([^\"'<>]*)\\1](.*)\[\/siteurl\]/sU";
        $replacements[] = '<a href="'.XOOPS_URL.'/\\2" target="_blank">\\3</a>';

        $patterns[] = "/\[url=(['\"]?)(http[s]?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
        $replacements[] = '<a href="\\2" target="_blank">\\3</a>';

        $patterns[] = "/\[url=(['\"]?)(ftp?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
        $replacements[] = '<a href="\\2" target="_blank">\\3</a>';

        $patterns[] = "/\[url=(['\"]?)([^\"'<>]*)\\1](.*)\[\/url\]/sU";
        $replacements[] = '<a href="http://\\2" target="_blank">\\3</a>';

        $patterns[] = "/\[color=(['\"]?)([a-zA-Z0-9]*)\\1](.*)\[\/color\]/sU";
        $replacements[] = '<span style="color: #\\2;">\\3</span>';

        $patterns[] = "/\[size=(['\"]?)([a-z0-9-]*)\\1](.*)\[\/size\]/sU";
        $replacements[] = '<span style="font-size: \\2;">\\3</span>';

        $patterns[] = "/\[font=(['\"]?)([^;<>\*\(\)\"']*)\\1](.*)\[\/font\]/sU";
        $replacements[] = '<span style="font-family: \\2;">\\3</span>';

        $patterns[] = "/\[email]([^;<>\*\(\)\"']*)\[\/email\]/sU";
        $replacements[] = '<a href="mailto:\\1">\\1</a>';

        $patterns[] = "/\[b](.*)\[\/b\]/sU";
        $replacements[] = '<b>\\1</b>';

        $patterns[] = "/\[i](.*)\[\/i\]/sU";
        $replacements[] = '<i>\\1</i>';

        $patterns[] = "/\[u](.*)\[\/u\]/sU";
        $replacements[] = '<u>\\1</u>';

        $patterns[] = "/\[d](.*)\[\/d\]/sU";
        $replacements[] = '<del>\\1</del>';

        $patterns[] = "/\[img align=(['\"]?)(left|center|right)\\1]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";

        $patterns[] = "/\[img align=(['\"]?)(left|center|right)\\1 id=(['\"]?)([0-9]*)\\3]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";
        $patterns[] = "/\[img id=(['\"]?)([0-9]*)\\1]([^\"\(\)\?\&'<>]*)\[\/img\]/sU";

        $patterns[] = "/\[quote]/sU";
        $replacements[] = _QUOTEC.'<div class="xoopsQuote"><blockquote>';

        $patterns[] = "/\[\/quote]/sU";
        $replacements[] = '</blockquote></div>';

        $patterns[] = "/java script:/si";
        $replacements[] = "java script:";

        $patterns[] = "/about:/si";
        $replacements[] = "about :";

        return preg_replace($patterns, $replacements, $text);
    }

    function prep_text($text)
    {
        return addslashes(xoopsCodeDecode(utf8_encode($text)));
    }

    // retrieve the categories already in MT - this is done to avoid dublicates.
    $res = query("SELECT category_label, category_id
                    FROM `mt_category`");
        if (mysql_num_rows($res) > 0) {
            while ($row = mysql_fetch_array($res)) {
                $categories_tested[$row['category_label']] = $row['category_id'];
            }
        }
    // cancel these variables...
    $res = null;
    $row = null;

    //    echo "<pre>".print_r($categories_tested,1)."</pre>";
    
    // fetch the stories from Xoops along with user-name and topics..    
    $res = query("SELECT xu.name,
                         xs.title,
                         xt.topic_title,
                         xt.topic_id,
                         xs.storyid,
                         xs.created,
                         xs.published,
                         xs.hometext,
                         xs.comments
                    FROM `xoops2_stories` xs, `xoops2_users` xu, `xoops2_topics` xt
                   WHERE xs.uid = xu.uid AND
                         xs.topicid = xt.topic_id");

    while ($row = mysql_fetch_array($res)) {
        $org_storyid = $row['storyid'];
        $new_storyid = $row['storyid'] + 1000;

        /**
         *    fetch any comments related to the xoops-story.
         */
        if ((int) $row['comments'] > 0) {
            $comment_res = query("SELECT xxc.com_text,
                                         xxc.com_ip,
                                         xxc.com_created,
                                          xu.name,
                                         xxc.com_text
                                      FROM xoops2_xoopscomments xxc
                                     LEFT JOIN xoops2_users xu ON xxc.com_uid = xu.uid
                                     WHERE xxc.com_itemid = '".$org_storyid."'");
            
            if (mysql_num_rows($comment_res) > 0) {
                while($comment_row = mysql_fetch_array($comment_res)) {
                   // keep it in an array. Afterwards it'll be inserted.
                    if ($comment_row['name'] == "") {
                        $comment_row['name'] = "Un-registered";
                    }
                    $insert_mt_comment[$new_storyid][] = "INSERT INTO `mt_comment` SET
                            comment_author          = '".prep_text($comment_row['name'])."',
                            comment_blog_id         = 1,
                            comment_created_by         = 1,
                            comment_created_on         = '".date('Y-m-d H:i:s',$comment_row['created'])."',
                            comment_entry_id         = $new_storyid,
                            comment_ip                 = '".$comment_row['com_ip']."',
                            comment_last_moved_on     = '2000-01-01 00:00:00',
                            comment_modified_on     = '".date('Y-m-d H:i:s',$comment_row['created'])."',
                            comment_text             = '".prep_text($comment_row['com_text'])."',
                            comment_visible            = 1";
                }
            }
        }
        
        // this will deal with any categories in Xoops which has not already been transfered to MT
        if (empty($categories_tested[utf8_encode($row['topic_title'])])) {    
            query("INSERT INTO `mt_category` SET
                                category_allow_pings = 1,
                                category_author_id     = 1,
                                category_basename     = '".prep_text(str_replace(" ","_",$row['topic_title']))."',
                                category_blog_id     = 1,
                                category_class         = 'category',
                                category_created_by     = 1,
                                category_created_on  = '".date('Y-m-d H:i:s',$comment_row['created'])."',
                                category_label         = '".prep_text($row['topic_title'])."',
                                category_modified_on = '".date('Y-m-d H:i:s',$comment_row['created'])."',
                                category_parent         = 0");
            $categories_tested[utf8_encode($row['topic_title'])] = mysql_insert_id();
        }
        // insert an entry in mt_placement to link the category, entry and blog.
        query("INSERT INTO `mt_placement` SET
                            placement_blog_id        = 1,
                            placement_category_id    = ".$categories_tested[utf8_encode($row['topic_title'])].",
                            placement_entry_id         = $new_storyid,
                            placement_is_primary    = 1");        

        // create the actual sql-entry for the blog-post.
        $insert_mt_entry[$new_storyid] = "INSERT INTO `mt_entry` SET
                entry_id                 = $new_storyid,
                entry_allow_comments     = 1,
                entry_allow_pings         = 1,
                entry_atom_id            = 'tag:www.wickedpixel.net,".date('Y',$row['created']).":/blog//1.".$new_storyid."',
                entry_author_id            = '1',
                entry_authored_on        = '".date('Y-m-d H:i:s',$row['created'])."',
                entry_basename            = 'speciale_".$new_storyid."',
                entry_category_id        = ".$categories_tested[utf8_encode($row['topic_title'])].",
                entry_blog_id            = '1',
                entry_class                = 'entry',
                entry_convert_breaks    = 'richtext',
                entry_created_by        = '1',
                entry_created_on        = '".date('Y-m-d H:i:s',$row['published'])."',
                entry_modified_on        = '".date('Y-m-d H:i:s',$row['published'])."',
                entry_status            = '1',
                entry_text                = '".nl2br(prep_text($row['hometext']))."',
                entry_title                = '".prep_text($row['title'])."',
                entry_week_number        = '".date('YW',$row['created'])."'";
    }    

    // Now run the two insert-scripts

    while (list($entry_key,$entry_val) = each($insert_mt_entry)) {
        query($entry_val);
        echo "Successfully inserted Entry with id#$entry_key<br />";
        
        if (count($insert_mt_comment[$entry_key]) > 0) {
            while (list($comment_key,$comment_val) = each($insert_mt_comment[$entry_key])) {
                query($comment_val);
                echo "- Successfully inserted comment with id# for entry #$entry_key".mysql_insert_id()."<br />";
            }
        }
    }
?>