Drupal 4.7 Migrator // //February 24th, 2006 // //If you have questions or bug reports contact me via:// //http://www.quillem.com/nuke2drupal // //Karthik Kumar // //Modifications by: // //Alexis Bellido // //////////////////////////////////////////////////////// // Slight modifications by: // Bogdan Tokovenko (http://www.bogdan.org.ua/contact/) // * added options for custom phpnuke table prefixes // * default name is now = uname (login), not 'temp_name' // * replaced links to 'migrate.php' with links to $_SERVER['PHP_SELF'] // * now forum topics should not be promoted to the main page (changed 1 to 0 as hinted by Alexis) error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', true); ini_set('max_execution_time', '600'); ///////////////////////////////////// //This is what you edit. //Database config $host = "localhost"; //Host address for MySQL $username = "root"; //Username to connect to MySQL $password = ""; //Password to connect to MySQL $nuke = "bioua_old"; //Name of the Nuke database $drupal = "bioua"; //Name of the Drupal database // PHPNuke database tables prefixes $nuke_prefix = "bioua"; //Prefix used for PHPNuke tables $nuke_secret = "secret"; // 'Secret' prefix used for PHPNuke 'users' table //Users already added [anonymous should always be present] $user_array = array("anonymous", "admin", "bogdan"); ///////////////////////////////////// //You shouldn't have to modify anything below this. //Tables $n_users = "{$nuke}.{$nuke_secret}_users"; $n_stories = "{$nuke}.{$nuke_prefix}_stories"; $n_fora = "{$nuke}.{$nuke_prefix}_bbforums"; $n_posts = "{$nuke}.{$nuke_prefix}_bbposts"; $n_posts_text = "{$nuke}.{$nuke_prefix}_bbposts_text"; $n_topics = "{$nuke}.{$nuke_prefix}_bbtopics"; $d_users = $drupal.".users"; $d_roles = $drupal.".users_roles"; $d_nodes = $drupal.".node"; $d_nodes_revisions = $drupal.".node_revisions"; $d_sequences = $drupal.".sequences"; $d_fora = $drupal.".forum"; $d_comments = $drupal.".comments"; $d_comment_statistics = $drupal.".node_comment_statistics"; $d_term_data = $drupal.".term_data"; $d_term_node = $drupal.".term_node"; $d_term_hierarchy = $drupal.".term_hierarchy"; $d_variables = $drupal.".variable"; $d_vocabulary = $drupal.".vocabulary"; $role_string = 'a:1:{s:5:"roles";a:1:{i:0;s:1:"2";}}'; $comment_user_string = 'a:1:{i:0;i:0;}'; //Story HTML format - //1 = Basic HTML 2 = PHP Allowed 3 = All HTML (3 - recommended) $format = 3; $promote = 1; $comment = 2; $node_stype = "story"; $node_ftype = "forum"; $forum_container = "Archived Nuke Fora"; $container_description = "Nuke Fora transferred to drupal. Will need to be reorganised."; ///////////////////////////////// //Functions //Gets the vid for a field (forums) from the vocabulary table function get_vid($name) { //message("Getting current vid for $name"); $d_vocabulary = $GLOBALS["d_vocabulary"]; $query = "SELECT vid FROM $d_vocabulary where name='$name'"; $result = mysql_query($query); if ($row = mysql_fetch_object($result)) { //success("vid for $name retrieved!"); return($row->vid); } else { failure("MySQL Error: vid for $name could not be retrieved"); exit(); } } //Gets the max_id from the sequences table for a particular id. //This is because the tables don't/shouldn't really use the auto_increment featureof MySQL/pgSQL //as it is not ANSI compliant. So Drupal stores the next auto_increment value in a separate table. function get_max_id($field) { //message("Getting Max ID for $field"); $d_sequences = $GLOBALS["d_sequences"]; $query = "SELECT id FROM $d_sequences WHERE name='$field'"; $result = mysql_query($query); if ($row = mysql_fetch_object($result)) { //success("Max ID for $field retrieved!"); return($row->id); } else { failure("MySQL Error: Max ID for $field could not be retrieved"); exit(); } } //Updates the above mentioned field once a row(s) is inserted. function set_max_id($field, $value) { //message("Updating Max ID for $field"); $d_sequences = $GLOBALS["d_sequences"]; $query = "UPDATE $d_sequences SET id='$value' WHERE name='$field'"; $result = mysql_query($query); if (!$result) { failure("MySQL Error: Max ID for $field could not be updated"); exit(); } //success("Max ID for $field updated!"); } //Convert Hex IP to standard octet form function get_ip($hexip) { $ip = array(); for ($i = 0; $i < 4; $i++) { $octal = substr($hexip, $i * 2, 2); $ip[$i] = hexdec($octal); } return(implode(".", $ip)); } //Display functions //Displays page headings function mainheading($str) { echo "

$str

"; } function heading($str) { echo "

$str:

"; } function message($str) { echo "
$str
"; } function success($str) { echo "
$str
"; } function failure($str) { echo "
$str
"; echo "
"; echo " Back To Main?"; echo "
"; } //Connect to MySQL $connected = true; $link = @mysql_connect($host, $username, $password); if (!$link) { failure("Connection to MySQL could not be made. Edit this file and make sure that the database settings are correct."); $connected = false; if (isset($_GET["section"])) { exit(); } } //////////////////////////////// ?> Nuke Migrator
$maxuid "; // alexis echo $query; $result = mysql_query($query, $link); if ($result) { success("User roles updated - all users registered as authenticated users (role = 2)."); } else { failure("Users Roles could not be added. It could be that there are duplicate usernames that are causing conflicts."); exit(); } } else { failure("Users could not be added."); exit(); } echo "
"; echo "Migrate Stories?"; echo " Back To Main?"; echo "
"; break; ////////////////////////////////////////////////////////////////////////////////////////// case "stories": heading("Migrating Stories"); $maxnid = get_max_id("node_nid"); $query = "SELECT $d_users.uid, $n_stories.title, UNIX_TIMESTAMP($n_stories.time) AS time, $n_stories.hometext, $n_stories.bodytext "; $query .= "FROM $n_stories, $d_users "; $query .= "WHERE $n_stories.aid=$d_users.name OR ($n_stories.aid='anonymous' AND $d_users.uid=0)"; message("Retrieving stories from PHP-Nuke"); // alexis echo $query; $result = mysql_query($query, $link); if ($result) { success("Retrieved Stories From Nuke"); message("Adding retrieved stories into Drupal. This may take a while.."); $nid = $maxnid + 1; while ($row = mysql_fetch_object($result)) { $row->hometext = addslashes($row->hometext); $row->bodytext = addslashes($row->bodytext); $row->title = addslashes($row->title); $query = "INSERT INTO $d_nodes(nid, vid, type, title, uid, status, created, changed, comment, promote) "; $query .= "VALUES ('$nid', '$nid', '$node_stype', '$row->title', '$row->uid', 1, '$row->time', '$row->time', '$comment', '$promote')"; $query_revisions = "INSERT INTO $d_nodes_revisions(nid, vid, uid, title, teaser, body, log, timestamp, format) "; $query_revisions .= "VALUES ('$nid', '$nid', '$row->uid', '$row->title', '$row->hometext', '".$row->hometext."

".$row->bodytext."

"."', '', '$row->time', '$format')"; // alexis echo $query . '
'; echo $query_revisions . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Error transferring story '$row->title'. ".($nid - $maxnid - 1)." stories transferred"); exit(); } else { //success("Story '$title' inserted"); } $result3 = mysql_query($query_revisions, $link); if (!$result3) { failure("Error transferring story '$row->title'. ".($nid - $maxnid - 1)." stories transferred"); exit(); } else { //success("Story '$title' inserted"); } //message("Inserting row into comment statistics table"); $query = "INSERT INTO $d_comment_statistics(nid, last_comment_timestamp, last_comment_uid, comment_count) "; $query .= "VALUES ('$nid', '$row->time', '$row->uid', '0')"; // alexis echo $query; $result2 = mysql_query($query, $link); if (!$result2) { failure("Post comments statistics registration failed"); exit(); } else { //success("Story '$title' comment statistics inserted"); } $nid++; } set_max_id("node_nid", $nid - 1); success(($nid - $maxnid - 1)." Stories transferred successfully!"); } else { failure("MySQL error. Nuke Stories could not be retrieved."); exit(); } echo "
"; echo "Migrate Fora?"; echo " Back To Main?"; echo "
"; break; ////////////////////////////////////////////////////////////////////////////////////////// case "fora": heading("Migrating Fora"); $maxtid = get_max_id("term_data_tid"); $vid = get_vid("Forums"); $maxtid++; //archive_id : To set parent forum later on $archive_id = $maxtid; $query = "INSERT INTO $d_term_data(tid, vid, name, description) "; $query .= "VALUES ('$maxtid', '$vid', '$forum_container', '$container_description')"; // alexis echo $query; //Insert Archive forum message("Insert Archive forum"); $result = mysql_query($query, $link); if (!$result) { failure("Could not create Parent Forum."); exit(); } success("Parent Forum Added"); ////////////////////////////// //Insert Archive Forum information into hierarchy table with parent as root (0) $query = "INSERT INTO $d_term_hierarchy(tid, parent) "; $query .= "VALUES ('$maxtid', '0')"; // alexis echo $query . '
'; message("Insert Archive Forum information into hierarchy table with parent as root (0)"); $result = mysql_query($query, $link); if (!$result) { exit("Could not set container hierarchy."); } success("Parent Forum Hierarchy Established"); //Copy forum names and descriptions over to drupal from nuke $query = "SELECT forum_id, forum_name, forum_desc FROM $n_fora"; message("Copy forum names and descriptions over to Drupal from Nuke"); $result = mysql_query($query, $link); if (!$result) { failure("Nuke Fora Could not be retrieved."); exit(); } success("Nuke Fora retrieved"); message("Inserting fora and associated posts into Drupal now. This may take a while.."); $counter = 1; while ($row = mysql_fetch_object($result)) { $maxtid++; $row->forum_name = addslashes($row->forum_name); $row->forum_desc = addslashes($row->forum_desc); $forum_id = $row->forum_id; //message("Creating $row->forum_name forum in Drupal"); $query = "INSERT INTO $d_term_data(tid, vid, name, description) "; $query .= "VALUES ('$maxtid', '$vid', '$row->forum_name', '$row->forum_desc')"; // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Error transferring forum titles.. Exiting.. "); exit(); } $counter++; success("Created '$row->forum_name' forum in Drupal"); //Insert Container information into hierarchy table with parent $query = "INSERT INTO $d_term_hierarchy(tid, parent) "; $query .= "VALUES ('$maxtid', '$archive_id')"; //message("Creating forum hierarchy for '$row->forum_name' forum in Drupal"); // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Could not set forum hierarchy."); exit(); } //success("Created forum hierarchy for '$row->forum_name' forum in Drupal."); //Select Posts $query = "SELECT $n_posts.topic_id AS tid, $d_users.uid AS uid, $d_users.name AS name, $n_posts_text.post_subject AS title, "; $query .= "$n_posts_text.post_text AS text, $n_posts.post_time AS time, $n_posts.poster_ip AS ip "; $query .= "FROM $n_posts_text, $n_posts, $n_users, $d_users "; $query .= "WHERE $n_users.uid=$n_posts.poster_id AND $n_posts.post_id=$n_posts_text.post_id "; $query .= "AND $n_posts.forum_id='$forum_id' AND ($d_users.name=$n_users.uname) "; $query .= "ORDER BY $n_posts.topic_id, $n_posts.post_id"; //message("Retrieving posts for $row->forum_name retrieved"); // alexis echo $query . '
'; $result3 = mysql_query($query, $link); if ($result3) { //success("Posts for $row->forum_name retrieved"); $maxnid = get_max_id("node_nid"); $maxcid = get_max_id("comments_cid"); $nid = $maxnid + 1; $cid = $maxcid + 1; //topic controls post threading $topic = -1; while ($row = mysql_fetch_object($result3)) { $row->title = addslashes($row->title); $row->text = addslashes($row->text); $row->ip = get_ip($row->ip); if ($topic != $row->tid) { $topic = $row->tid; $tid = $maxtid; $ptid = $nid; $threadid = 0; $counter++; $ptitle = $row->title; $query = "INSERT INTO $d_nodes(nid, vid, type, title, uid, status, created, changed, comment, promote) "; $promote = 0; // to avoid promoting forum topics to the main page $query .= "VALUES ('$nid', '$nid', '$node_ftype', '$row->title', '$row->uid', 1, '$row->time', '$row->time', '$comment', '$promote')"; $promote = 1; // restore value $query_revisions = "INSERT INTO $d_nodes_revisions(nid, vid, uid, title, teaser, body, log, timestamp, format) "; $query_revisions .= "VALUES ('$nid', '$nid', '$row->uid', '$row->title', '$row->title', '$row->text', '', '$row->time', '$format')"; // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Error inserting post '$row->title'. ".$counter." fora + posts transferred in total"); exit(); } echo $query_revisions . '
'; //message("Inserting parent post '$row->title' in Drupal"); $result2_revisions = mysql_query($query_revisions, $link); if (!$result2_revisions) { failure("Error inserting post revisions '$row->title'. ".$counter." fora + posts transferred in total"); exit(); } //success("Inserted parent post '$row->title' in Drupal"); $query = "INSERT INTO $d_comment_statistics(nid, last_comment_timestamp, last_comment_uid, comment_count) "; $query .= "VALUES ('$nid', '$row->time', '$row->uid', '0')"; //message("Registering comment statistic for post '$row->title' in Drupal"); // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Post comments statistics registration failed."); exit(); } //success("Registered comment statistic for post '$row->title' in Drupal"); $query = "INSERT INTO $d_fora(nid, vid, tid) "; $query .= "VALUES ('$nid', '$nid', '$tid')"; //message("Updating forum table in Drupal"); // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Updating forum table Failed. ".$counter." fora + posts transferred in total"); exit(); } $query = "INSERT INTO $d_term_node(nid, tid) "; $query .= "VALUES ('$nid', '$tid')"; //message("Updating term_node table for post '$row->title' in Drupal"); // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Updating term_node table failed. ".$counter." fora + posts transferred in total"); exit(); } $nid++; } else { $threadid++; $thread = $threadid."/"; if (strlen($row->title) <= 0) { $row->title = $ptitle; } $query = "INSERT INTO $d_comments(cid, nid, uid, subject, comment, hostname, timestamp, format, thread, users, name) "; $query .= "VALUES ('$cid', '$ptid', '$row->uid', '$row->title', '$row->text', '$row->ip', '$row->time', '$format', '$thread', '$comment_user_string', '$row->name')"; //message("Inserting comment post into comments table for post '$row->title' in Drupal"); // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Inserting comment post failed. ".$counter." fora + posts transferred in total"); exit(); } $counter++; //message("Inserted comment post into comments table for post '$row->title' in Drupal"); $query = "UPDATE $d_comment_statistics SET last_comment_timestamp='$row->time', last_comment_name='$row->title', last_comment_uid='$row->uid', comment_count='$threadid' "; $query .= "WHERE nid='$ptid'"; //message("Updating comment statistics for post '$row->title' in Drupal"); // alexis echo $query . '
'; $result2 = mysql_query($query, $link); if (!$result2) { failure("Post comments statistics registration failed."); exit(); } //message("Updated comment statistics for post '$row->title' in Drupal"); $cid++; } } set_max_id("node_nid", $nid - 1); set_max_id("comments_cid", $cid - 1); } else { failure("MySQL error. Nuke Posts could not be retrieved."); exit(); } } set_max_id("term_data_tid", $maxtid); success($counter." fora + posts transferred successfully!"); echo "
"; echo " Back To Main?"; echo "
"; break; default: heading("Overview"); if ($connected) { $query = "SELECT COUNT(*) AS usernum FROM $n_users"; // alexis echo $query; $result = mysql_query($query, $link); if ($row = mysql_fetch_object($result)) { $usernum_n = $row->usernum; } $query = "SELECT COUNT(*) AS usernum FROM $d_users"; // alexis echo $query . '
'; $result = mysql_query($query, $link); if ($row = mysql_fetch_object($result)) { $usernum_d = $row->usernum; } $query = "SELECT COUNT(*) AS storynum FROM $n_stories"; // alexis echo $query . '
'; $result = mysql_query($query, $link); if ($row = mysql_fetch_object($result)) { $storynum = $row->storynum; } } echo ""; if ($connected) { echo "
"; echo "Migrate Users?"; echo "
"; } break; ////////////////////////////////////////////////////////////////////////////////////////// } if ($connected) mysql_close($link); ?>