Page 1 of 1

Parsimony Converter

Posted: Sun May 22, 2005 8:05 pm
by alexsb
Hi, I finished writing my Parsimony Convertor. Thanks for the help!

Here is the code, probably it will help someone:

Code: Select all

<?
//******************************************************************************
// Title: parsimony_2_phpbb
// Usage: convert statical parsimony forums from its html backup to phpbb forum
// Filename: convert_forum.php
// Usage: enter the path to the database and the path to the old forum backups
//        and execute it
//
// Author: Lex Alexander
// e-Mail: alexander.lex@gmx.at
// Version: 1.0
//
// Licensed under the GNU GENERAL PUBLIC LICENSE Version 2
// see http://www.gnu.org/copyleft/gpl.html
//
//******************************************************************************

define("HOST","yourhost");
define("USER","username");
define("PW","password");
define("DBNAME","databasename");


define("POST_START", "<!-- Anfang BeitragsText -->");
define("POST_END", "<!-- Ende BeitragsText -->");
define("TITLE_BEGIN", "<!-- Ende BeitragsNavigationsLeisteOben -->");
define("RESPONSES", "(<!--responses:");

define("NUMBER_OF_THREADS", 1500);
// The Path to the parsimony forum
define("PATH_TO_FORUM", "./forum/");

function extract_new_thread($starting_file, $visited, $thread_counter, $post_counter)
{
 
  echo "<br><h2>New Thread</h2><br>";

  extract_message($starting_file, 1, &$thread_counter, &$post_counter, 0);

 
  // ====== extract the references ======
  $file_name = PATH_TO_FORUM.$starting_file.".htm";
  $response_first_part =  file_get_contents($file_name, "r");
  
  $count_replies=0;
  while(true)
  {
    $response_first_part = strstr($response_first_part, RESPONSES);
    if(!$response_first_part)
       break;

    $response = substr($response_first_part, 0, strpos($response_first_part, "-->"));
    $response_reference = substr($response, 16);
    $response_first_part = substr($response_first_part, 16);

    $visited[$response_reference] = 1;
    $count_replies++;
    extract_message($response_reference, 0, &$thread_counter, &$post_counter, &$count_replies);
  }
}

function extract_message($file_name, $new_thread, $thread_counter, $post_counter, $count_replies)
{

  $file_name = PATH_TO_FORUM.$file_name.".htm";
  $file = file_get_contents($file_name, "r");

  // ====== extract the title ======
  $message_title = strstr($file, "<h1>");
  $message_title = substr($message_title, 0, strpos($message_title, "</h1>"));
  $message_title = substr($message_title, 4);


  // ====== extract the message composed ====== 
  $message_sub_title = strstr($file, TITLE_BEGIN);
  $message_sub_title = substr($message_sub_title, 0, strpos($message_sub_title, POST_START));
  $message_sub_title = strip_tags($message_sub_title);
  $message_text = strstr($file, POST_START);
  $message_text = substr($message_text, 0, strpos($message_text, POST_END));

  // remove all html commands
  $message_text = strip_tags($message_text);

  // build the message by using the subtitle and the message itself
  $message_text = $message_sub_title.$message_text;

  // escape '
  $message_title = str_replace("'", "\'", $message_title);
  $message_text = str_replace("'", "\'", $message_text);

  $message_title = htmlentities($message_title);
  $message_text = htmlentities($message_text);

  //======== New Thread ===============
  if($new_thread == 1)
  {
    $thread_counter++;
    $post_counter++;
    $time = time();
   
    // topics_table 
    $sql="INSERT INTO phpbb_topics (topic_title, topic_poster, topic_time, forum_id, topic_status, topic_type, topic_vote, topic_first_post_id) VALUES ('$message_title', -1, $time, 1, 0, 0, 0, $post_counter)";
 
    echo "Topics Table Insert: <br> ".$sql."<br><br>";

    mysql_query($sql)
      or die("Inserting into Topics Table failed");

    // posts_table 
    $sql="INSERT INTO phpbb_posts (topic_id, forum_id, poster_id, post_username, post_time, poster_ip, enable_bbcode, enable_html, enable_smilies, enable_sig) VALUES ($thread_counter, 1, -1, '', $time, '3e2f1823', 1, 0, 1, 0)";

    echo "Posts Table Insert: <br> ".$sql."<br><br>";

    mysql_query($sql)
    or die("Inserting into posts table failed");   

    //  posts_text_table 
    $sql="INSERT INTO phpbb_posts_text (post_id, post_subject, bbcode_uid, post_text) VALUES ($post_counter, '$message_title', 'd36eb7da09', '$message_text')";

    echo "Posts Text Table Insert: <br> ".$sql."<br><br>";

    mysql_query($sql)
    or die("Inserting into posts text table failed");
  }
  //======== Reply ===============
  else
  {
    $post_counter++;
    $time = time();

    $sql="UPDATE `phpbb_topics` SET `topic_replies` = '$count_replies',
topic_last_post_id = '$post_counter' WHERE `topic_id` = '$thread_counter' LIMIT 1 ;";

    echo "Topics Table Uptdate: <br> ".$sql."<br><br>";

    mysql_query($sql)
      or die("Inserting into Topics Table failed");

    //posts_table 
    $sql="INSERT INTO phpbb_posts (topic_id, forum_id, poster_id, post_username, post_time, poster_ip, enable_bbcode, enable_html, enable_smilies, enable_sig) VALUES ($thread_counter, 1, -1, '', $time, '3e2f1823', 1, 0, 1, 0)";

    echo "Posts Table Insert: <br> ".$sql."<br><br>";

    mysql_query($sql)
    or die("Inserting into posts table failed");   

    //posts_text_table 
    $sql="INSERT INTO phpbb_posts_text (post_id, post_subject, bbcode_uid, post_text) VALUES ($post_counter, '$message_title', 'a3ababb17f', '$message_text')";

    echo "Posts Text Table Insert: <br> ".$sql."<br><br>";

    mysql_query($sql)
    or die("Inserting into posts text table failed");
  }

  echo "Thread Number: ".$thread_counter.", Post Number: ".$post_counter."<br><br>";
  echo $message_title;
  echo "<br><br>";
  echo $message_sub_title;
  echo "<br>";
  echo $message_text;
  echo "<br><hr><br>";

}

//******************************************
// =========== Main ========================

$thread_counter = 1;
$post_counter = 1;

$session = @mysql_connect(HOST, USER, PW)
  or die("could not connect to server");

@mysql_select_db(DBNAME, $session)
  or die("could not connect to db");


for($count = 0; $count < 1600; $count++)
{ 
 if($visited[$count] != 1) 
 {
   $visited[$count]=1;
   $file_name = PATH_TO_FORUM.$count.".htm";
   $file =  file_get_contents($file_name, "r");
   if($file)
   {
     extract_new_thread($count, &$visited, &$thread_counter, &$post_counter);
   } 
   else
   {
     echo "<br><b>no such thread</b></br>";
   }
 } 
}

// Update the Forum Statistics finally
$sql="UPDATE `phpbb_forums` SET `forum_posts` = '$post_counter',
`forum_topics` = '$thread_counter',
`forum_last_post_id` = '$post_counter',
`prune_next` = NULL WHERE `forum_id` = '1' LIMIT 1" ;

echo "Updateing the Forum Table: <br>".$sql;

mysql_query($sql)
  or die("Inserting into Forums Table failed");
?>

Posted: Fri Sep 09, 2005 11:18 pm
by alexsb
Here is an update, that fixes some problems, use this instead:

edit: sorry, no update available, read on for an explanation

both the same?!

Posted: Sat Sep 10, 2005 12:36 am
by twitek
Hi,

i just run fc (file compare) on both sourcecodes - they are identic!
Could you post the update (1.x?) again, please?

Best regards,
Thomas

Posted: Sat Sep 10, 2005 3:55 pm
by alexsb
Hey, you are right, they are identical - and I can not find the latest version - I am sorry. I thought I copied it to my backup directory, but it seems like I did not. I remeber deleting it from the server as well. The problem with this version was that no user names are inserted, and that the postings are not in the correct order. The former is easy to achieve as soon as you have the username, which you can parse from the message in the parsimony forum, the latter is a bug in extract_message, I think, but I do not remeber exactly what it was. Something about the right order of parsing the messages, no big thing either. I can't fix it myself because I have no mor test data.

I still have another backup at my workplace, where I have a 2% chance that I will find the latest version.

I am sorry for the inconvinience,

Greetings,

Alex

Re: Parsimony Converter

Posted: Mon Mar 24, 2008 11:59 pm
by Nico Haase
Hi,
a new version of a converter, inspired by Alex' script, is online at http://dev.nicohaase.de. Please have a look at it if you are searching for an importer for Parsimony's forums.
Nico