FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Porting X to phpBB3





Fire Boar
Hey folks,

As several of you are probably aware, phpBB3 is one of the most difficult systems to modify that is in wide use today. The reason is simple: it has no API whatsoever.

After many hours of fiddling, I have finally managed to come up with a system of porting forums from one system to phpBB3 that works. It doesn't work well (yet), but it's adequate and a good starting point.

The code below demonstrates how to port a Drupal forum to phpBB3 (not a great idea at the best of times, but the boss insisted). You have to first log in as a forum administrator, create the required forums, then add entries to the $forums array in the form $oldid => $newid. The reason for this step is that creating forums is if possible even more headache-inducing than submitting lots of posts by different users. While you're at it, give the "registered users" and "guests" groups the "Standard Access" permission role on each forum. You can set up access properly later, the important thing is getting all the posts in. Then run the script with ?sid=(whatever sid= is in the phpBB admin control panel url) in the address bar.

Code:
<?php
// POPULATE BEFORE STARTING.
$forums = array();
$db_username = 'old_database_username';
$db_password = 'old_database_password';
$db_database = 'old_database_name';


define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
include($phpbb_root_path . 'includes/message_parser.' . $phpEx);

$old = new PDO('mysql:host=localhost;dbname=' . $db_database, $db_username, $db_password);

$user->session_begin();

$users = array(0 => array(
  'username' => 'Anonymous',
  'user_id' => 1,
  'user_ip' => '0.0.0.0',
));
foreach ($old->query('SELECT * FROM users WHERE uid > 0') as $u)
{
  $q2 = $old->query('SELECT hostname FROM accesslog WHERE uid = ' . $u['uid'] .
   ' ORDER BY timestamp DESC LIMIT 1');
  $q2->execute();
  $r2 = $q2->fetch();
  $a = (empty($r2) ? array('hostname' => '0.0.0.0') : $r2[0]);
  $users[$u['uid']] = array(
    'username'      => $u['name'],
    'user_password' => $u['pass'], // phpBB will support md5 passwords! Yes!
    'user_email'    => $u['mail'],
    'group_id'      => ($u['uid'] == 1 ? 5 : 2),
    'user_timezone' => ((float) $u['timezone']) / 3600,
    'user_type'     => 0,
    'user_ip'       => $a['hostname'],
    'user_regdate'  => $u['created'],
  );
  $users[$u['uid']]['user_id'] = user_add($users[$u['uid']]);
}

foreach ($old->query("SELECT n.*, t.*, v.* FROM node n INNER JOIN term_node t ON t.nid=n.nid INNER JOIN node_revisions v ON v.nid=n.nid WHERE v.vid=n.vid AND n.type='forum' AND t.tid IN (SELECT term_data.tid FROM term_data INNER JOIN vocabulary ON term_data.vid=vocabulary.vid WHERE vocabulary.name='Forums')") as $topic)
{
  set_time_limit(600);

  $message = utf8_normalize_nfc($topic['body']);
  $topic_title = utf8_normalize_nfc($topic[21]);
  $uid;
  $bitfield;
  $flags;

  $user->ip = $users[$topic['uid']]['user_ip'];
  $user->data['user_id'] = $users[$topic['uid']]['user_id'];
  $user->data['user_colour'] = ($topic['uid'] == 1 ? 'AA0000' : '');
  $user->data['is_registered'] = ($topic['uid'] != 0);
  $user->data['username'] = $users[$topic['uid']]['username'];
  $auth->acl($user->data);

  generate_text_for_storage($message, $uid, $bitfield, $flags, true, true, true);

  $data = array(
    'forum_id' => $forums[$topic['tid']],
    'topic_id' => 0,
    'icon_id' => false,
    'enable_bbcode' => true,
    'enable_smilies' => true,
    'enable_urls' => true,
    'enable_sig' => true,
    'message' => $message,
    'message_md5' => md5($message),
    'post_edit_locked' => 0,
    'topic_title' => $topic_title,
    'notify_set' => false,
    'notify' => false,
    'post_time' => $topic['timestamp'],
    'forum_name' => '',
    'enable_indexing' => true,
    'force_approved_state' => true,
    'bbcode_uid' => $uid,
    'bbcode_bitfield' => $bitfield,
    'poster_ip' => $user->ip,
    'topic_time_limit' => 0,
  );

  $poll = array();
  submit_post('post', $topic_title, 'Anonymous', ($topic['sticky'] ? POST_STICKY : POST_NORMAL), $poll, $data);

  foreach ($old->query("SELECT * FROM comments WHERE nid='" . $topic['nid'] . "'") as $post)
  {
    $message = utf8_normalize_nfc($post['comment']);
    $uid;
    $bitfield;
    $flags;

    $user->ip = $post['hostname'];
    $user->data['user_id'] = $users[$post['uid']]['user_id'];
    $user->data['user_colour'] = ($post['uid'] == 1 ? 'AA0000' : '');
    $user->data['is_registered'] = ($post['uid'] != 0);
    $user->data['username'] = $users[$post['uid']]['username'];
    $auth->acl($user->data);

    generate_text_for_storage($message, $uid, $bitfield, $flags, true, true, true);

    $data1 = array(
      'forum_id' => $forums[$topic['tid']],
      'topic_id' => $data['topic_id'],
      'icon_id' => false,
      'enable_bbcode' => true,
      'enable_smilies' => true,
      'enable_urls' => true,
      'enable_sig' => true,
      'message' => $message,
      'message_md5' => md5($message),
      'post_edit_locked' => 0,
      'topic_title' => $post['subject'],
      'notify_set' => false,
      'notify' => false,
      'post_time' => $post['timestamp'],
      'forum_name' => '',
      'enable_indexing' => true,
      'force_approved_state' => true,
      'bbcode_uid' => $uid,
      'bbcode_bitfield' => $bitfield,
      'poster_ip' => $user->ip,
      'topic_time_limit' => 0,
    );

    $poll = array();
    submit_post('reply', (isset($post['title']) ? utf8_normalize_nfc($post['title']) : 'Re: ' . $topic_title), 'Anonymous', ($topic['sticky'] ? POST_STICKY : POST_NORMAL), $poll, $data1);
  }
}


While this script is tied to the Drupal database, it's a good starting point for other types of migration. Note that with some systems, migration of user passwords will be impossible. However, phpBB3 supports MD5 password hashes, since phpBB2 used MD5. Drupal also uses MD5, so passwords Just Work.
Related topics
Do you read comics? What title?
Fantastico could be updated
Coldplay
Symphony X
350 x 20 User Bar + Tutorial
Galaxywars
Overview/Help on cPanel X
X Arcade Version 1.0
X-Mailer - The Ultimate File Attachment Software Website
Play Raiden-X
Direct X Version Driver
OS X
FlyAkiteOS/X -- Anyone heard of it?
Macintosh Tiger OS X
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.