<?php
set_time_limit
(0);
/*
 * Import punbb forum into bbpress forum
 *
 * REPURPOSED for PUNBB BY: Brianna Privett <brianna@utopian.net>
 * ORIGINALLY DEVELOPED BY: Bruno Torres <http://brunotorres.net>
 *
 * USE WITH FRESH BBPRESS INSTALL. BACKUP YOUR DATA.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.  This program is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License at
 * http://www.gnu.org/copyleft/gpl.html for more details.
 */

/* The database connection for existing punbb */
$punbb_db['host'] = 'localhost';
$punbb_db['username'] = 'user';
$punbb_db['password'] = 'pass';
$punbb_db['database'] = 'dbname';
$punbb_db['tableprefix'] = 'punbb_';

/* The database connection for new bbpress */
$bbpress_db['host'] = 'localhost';
$bbpress_db['username'] = 'user';
$bbpress_db['password'] = 'pass';
$bbpress_db['database'] = 'dbname';
$bbpress_db['tableprefix'] = 'bb_';

$save_to_file TRUE//Change to FALSE to suppress writing queries to file.
$filename '/tmp/punbb_imported.sql'//Ensure the file is writable by PHP.

$do_import FALSE//Change to TRUE if you want the script to perform the import. 
//If FALSE, you will need to use the generated SQL file to do the import.


//You don't need to edit the rest of this file

$punbb_tables['forums'] = $punbb_db['tableprefix'] . 'forums';
$punbb_tables['categories'] = $punbb_db['tableprefix'] . 'categories';
$punbb_tables['users'] = $punbb_db['tableprefix'] . 'users';
$punbb_tables['topics'] = $punbb_db['tableprefix'] . 'topics';
$punbb_tables['posts'] = $punbb_db['tableprefix'] . 'posts';

$bbpress_tables['forums'] = $bbpress_db['tableprefix'] . 'forums';
$bbpress_tables['users'] = 'wp_users';
$bbpress_tables['usermeta'] = 'wp_usermeta';
$bbpress_tables['topics'] = $bbpress_db['tableprefix'] . 'topics';
$bbpress_tables['posts'] = $bbpress_db['tableprefix'] . 'posts';

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Import punbb to bbPress</title>
<style>
  body{ font:normal 1em verdana, arial, "bitstream vera sans", helvetica, sans-serif; }
</style>
<div id="content">
<?php

//Connecting to the old forum database
@mysql_connect($punbb_db['host'], $punbb_db['username'], $punbb_db['password']) or die("Connect to punbb database failed.<br>\nMySQL output:<br>\n<pre>" mysql_error() . "</pre>");

echo 
"Connected to punbb database host.<br>\n"flush();

@
mysql_select_db($punbb_db['database']) or die("Cannot select punbb database. MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

echo 
"Selected punbb database.<br><br>\n"flush();
//First, let's import the forum parents/categories

$export_sql "SELECT * FROM " $punbb_tables['categories'];
$export_result mysql_query($export_sql) or die("Can't retrieve the data from the punbb forums table. The query used was:<br>\n<pre>" $export_sql "</pre><br>\n MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

$import_sql "#Exporting Forums Category data: \n";
while(
$export_row mysql_fetch_object($export_result)){
  
$import_sql .= "INSERT INTO " $bbpress_tables['forums'] . " (forum_id, forum_name, forum_order) VALUES (" $export_row->id ", '" addslashes($export_row->cat_name) . "', " $export_row->disp_position ");\n";
}

echo 
"Forum parents/categories exported - OK.<br>\n"flush();


//Next, let's import the forums themselves

$export_sql "SELECT id, forum_name, forum_desc, disp_position, num_topics, num_posts, cat_id FROM " $punbb_tables['forums'];
$export_result mysql_query($export_sql) or die("Can't retrieve data from the punbb forums table. The query used was:<br>\n<pre>" $export_sql "</pre><br>\n and the server threw the following error:<br>\n<pre>" mysql_error() . "</pre>");

$import_sql .= "#Exporting Forums data: \n";
while(
$export_row mysql_fetch_object($export_result)){
  
$import_sql .= "INSERT INTO " $bbpress_tables['forums'] . " (forum_id, forum_name, forum_desc, forum_order, forum_parent, topics, posts) VALUES (" $export_row->id ", '" addslashes($export_row->forum_name) . "', '" addslashes($export_row->forum_desc) . "', " $export_row->disp_position ", " $export_row->cat_id ", " $export_row->num_topics ", " $export_row->num_posts ");\n";
}
echo 
"Forums exported - OK.<br>\n"flush();

//Now, to the users

$export_sql "SELECT id, username, password, email, url, registered FROM " $punbb_tables['users'];
$export_result mysql_query($export_sql) or die("Can't retrieve data from the punbb users table. The query used was:<br>\n<pre>" $export_sql "</pre><br>\n MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

$import_sql .= "#Exporting Users data: \n";
while(
$export_row mysql_fetch_object($export_result)){
  
$user_id $export_row->id;
  
//Punbb allows apostrophes in login names - this replaces first possessives, then apostrophes, with a dash.
  
$posname str_replace("'s ","s-"$export_row->username);
  
$username str_replace("'","-"$posname);
  
//User ID = -1 seem to cause such a confusion. So we'll change it to a very big number
  
if ($user_id == -1$user_id 999999998;
  
$regdate date("Y-m-d H:i:s"$export_row->registered);
  
$import_sql .= "INSERT INTO " $bbpress_tables['users'] . " (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_status, display_name) VALUES (" $user_id ", '" $username "', '" $export_row->password "', '', '" $export_row->email "', '" $export_row->url "', '$regdate', 0, '" $username "');\n";
}

echo 
"Users exported - OK.<br>\n"flush();

//Users meta data
//Obs: All users imported will be flagged as "members"
//You can change this later from bbPress Admin Panel
//using your already registered admin user

$export_sql "SELECT id, location FROM " $punbb_tables['users'];
$export_result mysql_query($export_sql) or die("Can't retrieve metadata from punbb users table. The query used was:<br>\n<pre>" $export_sql "</pre><br>\n MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

$import_sql .= "#Exporting Users meta data: \n";
while(
$export_row mysql_fetch_object($export_result)){
  
$user_id $export_row->id;
  
//User ID = -1 seem to cause such a confusion. So we'll change it to a very big number
  
if ($user_id == -1$user_id 999999998;
  
//capabilities
  
$import_sql .= "INSERT INTO " $bbpress_tables['usermeta'] . "(user_id, meta_key, meta_value) VALUES (" $user_id ", 'bb_capabilities', 'a:1:{s:6:\"member\";b:1;}');\n";
  
//From
  
$import_sql .= "INSERT INTO " $bbpress_tables['usermeta'] . "(user_id, meta_key, meta_value) VALUES (" $user_id ", 'from', '" addslashes($export_row->location) . "');\n";
  
//Occupation
}

echo 
"Users meta data exported - OK.<br>\n"flush();

//Importing topics

$export_sql "SELECT * FROM " $punbb_tables['topics'];
$export_result mysql_query($export_sql) or die("Can't retrieve data from the punbb topics table. The query used was:<br>\n<pre>" $export_sql "</pre><br>\n MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

$import_sql .= "#Exporting Topics: \n";
while(
$export_row mysql_fetch_object($export_result)){
  
$topic_poster_name $export_row->poster;
  
$topic_last_poster_name $export_row->last_poster;
  
$postdate date("Y-m-d H:i:s"$export_row->posted);
  
$lastpost date("Y-m-d H:i:s"$export_row->last_post);
  
$import_sql .= "INSERT INTO " $bbpress_tables['topics'] . " (topic_id, topic_title, topic_poster, topic_poster_name, topic_last_poster_name, topic_start_time, topic_time, forum_id, topic_status, topic_open, topic_last_post_id, topic_sticky, topic_posts, tag_count) VALUES (
  " 
$export_row->id ",
  '" 
addslashes($export_row->subject) . "',
  '" 
$export_row->poster "',
  '" 
$topic_poster_name "',
  '" 
$topic_last_poster_name "',
  '" 
$postdate "',
  '" 
$lastpost "',
  " 
$export_row->forum_id ",
  0,
  1,
  " 
$export_row->last_post_id ",
  " 
$export_row->sticky ",
  " 
. ($export_row->num_replies 1) . ", 0);\n";
}

echo 
"Topics exported - OK.<br>\n"flush();

//Importing posts

$export_sql "SELECT * FROM " $punbb_tables['posts'];  
             
$export_result mysql_query($export_sql) or die("Can't retrieve data from punbb poste table. The query used was:<br>\n<pre>" $export_sql "</pre><br>\n MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

$import_sql .= "#Exporting Posts: \n";
while(
$export_row mysql_fetch_object($export_result)){
  
$post_time date("Y-m-d H:i:s"$export_row->posted);
  
$import_sql .= "INSERT INTO " $bbpress_tables['posts'] . " (post_id, topic_id, poster_id, post_text, post_time, poster_ip, post_status, post_position) VALUES (
  " 
$export_row->id ",
  " 
$export_row->topic_id ",
  " 
$export_row->poster_id ",
  '" 
addslashes($export_row->message) . "',
  '" 
$post_time "',
  '" 
$export_row->poster_ip "',
  0,
  1
  );\n"
;
}

echo 
"Posts exported.<br><br>\n"flush();

$sql_file $_SERVER['DOCUMENT_ROOT']."/ifri-test/wpmu/forum".$filename;

if (
$save_to_file){
  if (
file_put_contents($sql_fileutf8_encode($import_sql))){
    echo 
"The SQL queries were successfully written to the file: ".$sql_file."<br><br>\n"flush();
  } else {
    echo 
"Could not write file. Check file/directory permissions."flush();
  }
}

if (
$do_import){
    @
mysql_connect($bbpress_db['host'], $bbpress_db['username'], $bbpress_db['password']) or die("Can't connect to bbpress database.<br>\nMySQL output:<br>\n<pre>" mysql_error() . "</pre>");

    @
mysql_select_db($bbpress_db['database']) or die("Can't select the bbpress database. MySQL output:<br>\n<pre>" mysql_error() . "</pre>");

  
/* Cleanup; admin user is given high ID to avoid the ID already issued to punbb users */
  
@mysql_query ("UPDATE " $bbpress_tables['users'] . " SET ID=999999999 WHERE ID=1");
  @
mysql_query ("UPDATE " $bbpress_tables['usermeta'] . " SET user_id=999999999 WHERE user_id=1");
  @
mysql_query("TRUNCATE TABLE " $bbpress_tables['forums']);
  @
mysql_query("TRUNCATE TABLE " $bbpress_tables['topics']);
  @
mysql_query("TRUNCATE TABLE " $bbpress_tables['posts']);

  
/* Break SQL into an array of single INSERTs and apply each */
  
$import_sql utf8_encode($import_sql);
  
$import_sql_array explode(";\n"$import_sql);

  echo 
"Starting the import...<br>\n"flush();
  
$ok 0;
  
$error 0;
  foreach (
$import_sql_array as $s){
    if (!@
mysql_query($s)){
      echo 
"There was an error trying to execute the query:<br>\n<pre>$s</pre><br>\nMySQLoutput:<br>\n<pre>" mysql_error() . "</pre>\n"flush();
      
$error++;
    } else {
      
$ok++;
    }
  }
  echo 
"Import complete.<br>\nStatistics:<br>\n$ok successful, $error errors"flush();
}

?>
</div>