Last updated December 27, 2005. Created on December 27, 2005.
Edited by sepeck, Suuch. Log in to edit this page.

<?php
/*
 * dcforumintegration.module
 * From: DCForum+ User Accounts and Forum
 * To Drupal forum and flatforum module.
 * Passwords are migrated by modifying user.module
 * Remember to search and replace "$my_dcf_dbase" with the name of the dcforum+ mysql database.
 */


function dcforumintegration_menu($may_cache)
{
    global $user;
  $items = array();
  $admin_access = user_access('administer site configuration');
  $items[] = array('path' => 'dcf', 'title' => t('migrate dcf'),
      'callback' => 'actions', 'access' => $admin_access);
    $items[] = array('path' => 'dcf/users', 'title' => t('migrate dcf users'),
      'callback' => 'actions', 'access' => $admin_access);
     $items[] = array('path' => 'dcf/forums', 'title' => t('migrate dcf forums'),
      'callback' => 'actions', 'access' => $admin_access, weight=> '1');
  return $items;
}


//------------HELPER FUNCTIONS------------------//



function actions()
{
    switch(arg(1))
    {
        case 'users':
            $output = migrate_users();
        break;
        case 'forums':
        {
            if(arg(2) == "toplevel")
            {
                $output = migrate_containers();
                $output.= migrate_forums();
            }
            else if(arg(2) == "undotoplevel")
            {
                $output = unmigrate_containers();
                $output.= unmigrate_forums();
            }               
            else if(arg(2) == "topposts")
                $output = migrate_topposts();
            else if(arg(2) == "undotopposts")
                $output = unmigrate_topposts();
               
               
        }
        break;
        default:
        $output = "<li>".l('Migrate Users', 'dcf/users')."</li><br/><b>Note that all users except the root user will be deleted from the database</b>";
        $output .= "<li>".l('Migrate Forums', 'dcf/forums')."</li><br/><b>Perform the following in order
        <br/>Remember to augment the node, comment tables with the two fields from $my_dcf_dbase</b>";
        $output .= "<ul><li>".l('Migrate Top Level Forums', 'dcf/forums/toplevel')." [".l('Undo', 'dcf/forums/undotoplevel')."]</li>";
        $output .= "<ul><li>".l('Migrate Top Level Posts + comments/responses)', 'dcf/forums/topposts')." [".l('Undo', 'dcf/forums/undotopposts')."]</li>";
    break;
    }
   
    print theme('page',$output);
}

function migrate_users()
{
    # Step 1: Migrate Users


    db_query("DELETE FROM {users} WHERE uid!=1");
    db_query("INSERT INTO {users} (uid) VALUES(0);");
    db_query('INSERT INTO {users} (uid, name, pass, mail, signature, mode, status, init, data, created, changed) SELECT id, username, password, email, pk, 0, 1, email, "N;", reg_date, last_date FROM $my_dcf_dbase.dcuser WHERE id!=1;');
    db_query("DELETE FROM {users_roles};");
    db_query("INSERT INTO {users_roles}(uid, rid) VALUES(0, 1);");
    db_query("INSERT INTO {users_roles}(uid, rid) VALUES(1, 2);");
    db_query("INSERT INTO {users_roles} (uid, rid) SELECT uid, 2 FROM {users} WHERE uid != 1 AND uid !=0;");
   
    # Set the user sequence!
    db_next_id("user");
   
    watchdog('dcfmigration', "All users deleted, populated with dcuser entries", 1);
    return "All users deleted, populated with dcuser entries".l('View Imported Users', 'admin/user');
}

function migrate_containers()
{
    # number of vocabulary groups I already have
    $numvocgroups = db_fetch_object(db_query("SELECT id FROM {sequences} WHERE name = 'vocabulary_vid'"));
    $numvocgroups = $numvocgroups->id;
   
    # Step 3: Migrate toplevel containers
    # Remember to clear forum* variables from the variables table
    db_query("DELETE FROM {variable} WHERE name LIKE 'forum%'");
    db_query("INSERT INTO {term_data} (tid, vid, name, description, weight)
            SELECT id, %d, name, description, forum_order FROM $my_dcf_dbase.dcforum WHERE parent_id='0';",$numvocgroups );
   
    $res = db_query("SELECT id FROM $my_dcf_dbase.dcforum WHERE parent_id='0'");
   
    #Make them containers
    $containers = variable_get('forum_containers', array());
    while($tid = db_fetch_object($res))
    {
        $containers[] = $tid->id;
    }
    variable_set('forum_containers', $containers);

db_query("INSERT INTO {term_hierarchy} (tid, parent)
        SELECT tid, 0 FROM {term_data;}");
db_query("REPLACE INTO {sequences} (id, name) SELECT max(tid), 'term_data_tid' FROM {term_data}"); # Set the term_data_id sequence!
        return "Top level containers ".l('View forums', 'forum');
       
   
}

function unmigrate_containers()
{
    $tids = db_query("SELECT id, name FROM $my_dcf_dbase.dcforum WHERE parent_id='0'");
    while($tid = db_fetch_array($tids))
    {
        db_query("DELETE FROM {term_data}  WHERE tid = %d    ", $tid);       
        db_query("DELETE FROM {term_hierarchy} WHERE tid = %d    ", $tid);       
        $output.= "<li>".$tid['name'];
    }
   
    variable_set('forum_containers', array());
    db_query("REPLACE INTO {sequences} (id, name) SELECT max(tid), 'term_data_tid' FROM {term_data}"); # Set the term_data_id sequence!

    return $output."<br><b>Top Level Containers Deleted</b>";
   
}

function migrate_forums()
{
    # number of vocabulary groups I already have
    $numvocgroups = db_fetch_object(db_query("SELECT id FROM {sequences} WHERE name = 'vocabulary_vid'"));
    $numvocgroups = $numvocgroups->id;
   
    # Step 3: Migrate forums (these have parent_id's > 0)
    db_query("INSERT INTO {term_data} (tid, vid, name, description, weight)
            SELECT id, %d, name, description, forum_order FROM $my_dcf_dbase.dcforum WHERE parent_id >0 ;",$numvocgroups );
   

    db_queryd("INSERT INTO {term_hierarchy} (tid, parent)
            SELECT id, parent_id FROM  $my_dcf_dbase.dcforum WHERE parent_id > 0");
    db_query("REPLACE INTO {sequences} (id, name) SELECT max(tid), 'term_data_tid' FROM {term_data}"); # Set the term_data_id sequence!
        return "Migrated Forums ";
       
   
}

function unmigrate_forums()
{
    $tids = db_query("SELECT id, name FROM $my_dcf_dbase.dcforum WHERE parent_id > 0");
    while($tid = db_fetch_array($tids))
    {
        db_query("DELETE FROM {term_data}  WHERE tid = %d    ", $tid);       
        db_query("DELETE FROM {term_hierarchy} WHERE tid = %d    ", $tid);       
        $output.= "<li>".$tid['name'];
    }
    db_query("REPLACE INTO {sequences} (id, name) SELECT max(tid), 'term_data_tid' FROM {term_data}"); # Set the term_data_id sequence!

    return $output."<br><b>Forums Deleted</b>";
   
}

function migrate_topposts()
{

    //Need to update 5 main tables: forum, node, node_comment_statistics, term_node, flat_forum
   
    db_query("INSERT INTO {flatforum} (posts, uid) SELECT num_topics, uid FROM $my_dcf_dbase.dcforum, {users}         WHERE {users}.name = last_author;");
   
       
    # number of toplevel posts I have
    $initnodeoffset = db_fetch_object(db_query("SELECT id FROM {sequences} WHERE name = 'node_nid';"));
    $initnodeoffset = $initnodeoffset->id;
    # add all the forum topics (posts become comments to these topics)
    $allforums = db_query("SELECT id FROM $my_dcf_dbase.dcforum WHERE parent_id > 0");
    //$allforums = db_query("SELECT id FROM $my_dcf_dbase.dcforum WHERE id = 5");

    while($eachforum = db_fetch_object($allforums))
    {        
        db_query('INSERT INTO {node} (forum_id, topic_id, type, title, uid, status, created, comment, promote, moderate,   teaser, body, changed, revisions)
        SELECT %d, t.id,
                   "forum",
               t.subject,
               t.author_id,
               1,
               unix_timestamp(t.mesg_date),
               2,
               0,
               0,
               t.subject,
               t.message ,
             unix_timestamp(t.last_date) ,
            ""
        FROM $my_dcf_dbase.%d_mesg t WHERE parent_id = 0', $eachforum->id,  $eachforum->id);
       
        #Update the forum table
        db_query("INSERT INTO {forum} (nid, tid)
            SELECT nid, %d FROM $my_dcf_dbase.%d_mesg t, {node} n WHERE n.topic_id = t.id", $eachforum->id, $eachforum->id, $initnodeoffset, $eachforum->id);
           
       
           
        #Update the term_node table
        db_query("INSERT INTO {term_node} (nid, tid)
            SELECT nid, %d FROM $my_dcf_dbase.%d_mesg t, {node} n WHERE n.topic_id = t.id", $eachforum->id, $eachforum->id, $initnodeoffset, $eachforum->id);

        #insert responses to posts as comments
        db_query("INSERT INTO {comments}
        (forum_id, topic_id, pid, nid, uid, subject, comment, hostname, timestamp)
            SELECT %d, t.id, n.topic_id,  n.nid,
           t.author_id,
           t.subject,
           t.message,
           'unknown',
           unix_timestamp(t.mesg_date)
    FROM {node} n, $my_dcf_dbase.%d_mesg t WHERE t.parent_id > 0 AND t.top_id = (n.topic_id)", $eachforum->id, $eachforum->id);
       
     #update node_comment_statistics
    db_query("REPLACE INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_uid)
    SELECT nid, changed, uid FROM {node}");
    /*
        db_query("INSERT INTO {node_comment_statistics}        (nid,  last_comment_timestamp,  last_comment_name,  last_comment_uid)
            SELECT DISTINCT(nid), unix_timestamp(t.last_date), last_author, n.uid FROM $my_dcf_dbase.%d_mesg t, {node} n WHERE n.topic_id = t.id", $eachforum->id, $initnodeoffset, $eachforum->id);
            */
   
    }

    #Update node_comment_statistics comment_count
    $allnodes = db_query("SELECT DISTINCT(nid) FROM {comments}");// WHERE nid=c.nid");
    while($eachnode = db_fetch_object($allnodes))
    {
        $no = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE nid = $eachnode->nid"));
        db_query("UPDATE {node_comment_statistics} SET comment_count = $no WHERE nid = %d", $eachnode->nid);
    }
   
   
   
   
   
   
    $node_seq = (array) db_fetch_object(db_query("SELECT max(nid) FROM {node}"));
    db_query("UPDATE  {sequences} SET id = %d WHERE name = 'node_nid'", $node_seq['max(nid)']); # Set the node sequence!
   
    $comments_seq = db_result(db_query("SELECT max(cid) FROM {comments}"));
    db_query("UPDATE  {sequences} SET id = %d  WHERE name = 'comments_cid'", $comments_seq); # Set the comments sequence!
   
    //update flatforums with users who have made posts
    $all_users = db_query("SELECT uid FROM {users}");
    while($uid = db_fetch_object($all_users))
    {
        $uid = $uid_>uid;
        $posts = db_result(db_query("SELECT COUNT(nid) FROM {node} n WHERE uid = %d", $uid));
        $posts += db_result(db_query("SELECT COUNT(cid) FROM {comments} c WHERE uid = %d", $uid));
        db_query('REPLACE INTO {flatforum} (uid, posts) VALUES (%d, %d) ', $uid, $posts);
    }
}

function unmigrate_topposts()
{
    $res = db_query("SELECT uid FROM {users} INNER JOIN $my_dcf_dbase.dcforum WHERE {users}.name = last_author");
    while($uid = db_fetch_object($res))
    {
        db_query("DELETE FROM {flatforum} WHERE uid = %d", $uid->uid);
    }
   
       
    # number of toplevel posts I have
    $initnodeoffset = db_fetch_object(db_query("SELECT id FROM {sequences} WHERE name = 'node_nid';"));
    $initnodeoffset = $initnodeoffset->id;
   
    # remove the forum topics (posts become comments to these topics)
    db_query('DELETE FROM {node} WHERE type="forum"');

    #Update the forum table
    db_query("DELETE FROM {forum}");
   
    #Update the term_node table
    db_query("DELETE FROM {term_node}");
   
    #update comments
    db_query("DELETE FROM {comments} WHERE forum_id!=0");

    #update the node_comment_statistics
    db_query("DELETE FROM {node_comment_statistics}");
       
    //update flatforums with users who have made posts
    $all_users = db_query("SELECT uid FROM {users}");
    while($uid = db_fetch_object($all_users))
    {   
        $uid = $uid->uid;
        $posts = db_result(db_query("SELECT COUNT(nid) FROM {node} n WHERE uid = %d", $uid));
        $posts += db_result(db_query("SELECT COUNT(cid) FROM {comments} c WHERE uid = %d", $uid));
        db_query('REPLACE INTO {flatforum} (uid, posts) VALUES (%d, %d) ', $uid, $posts);
    }
       
       
        $comments_seq = db_result(db_query("SELECT max(cid) FROM {comments}"));
    db_query("UPDATE  {sequences} SET id = %d  WHERE name = 'comments_cid'", $comments_seq); # Set the comments sequence!
   
        $node_seq = (array) db_fetch_object(db_query("SELECT max(nid) FROM {node}"));
        db_query("UPDATE  {sequences} SET id = %d WHERE name = 'node_nid'", $node_seq['max(nid)']); # Set the node sequence!
       

}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.