| 1 |
<?php
|
| 2 |
// ; $Id: phpbb2privatemsg.module,v 1.20 2009/05/23 23:25:39 nbz Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu()
|
| 6 |
*/
|
| 7 |
function phpbb2privatemsg_menu() {
|
| 8 |
$items = array();
|
| 9 |
|
| 10 |
$items['admin/phpbb2privatemsg'] = array(
|
| 11 |
'title' => 'phpBB to Privatemsg',
|
| 12 |
'access callback' => 'user_access',
|
| 13 |
'access arguments' => array('migrate phpBB'),
|
| 14 |
'page callback' => 'phpbb2privatemsg_main',
|
| 15 |
);
|
| 16 |
$items['admin/phpbb2privatemsg/cleanup'] = array(
|
| 17 |
'title' => 'Cleanup',
|
| 18 |
'access callback' => 'user_access',
|
| 19 |
'access arguments' => array('migrate phpBB'),
|
| 20 |
'page callback' => 'phpbb2privatemsg_cleanup',
|
| 21 |
'type' => MENU_CALLBACK,
|
| 22 |
);
|
| 23 |
return $items;
|
| 24 |
}
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Callback admin/phpbb2drupal
|
| 28 |
*/
|
| 29 |
function phpbb2privatemsg_main() {
|
| 30 |
$output .= '<p>';
|
| 31 |
$output .= t('This module imports private messages from phpBB to Drupal. It uses settings and data from the main phpbb2drupal module. Before importing Private Messages, you must:');
|
| 32 |
$output .= '</p>';
|
| 33 |
$output .= '<ol><li>';
|
| 34 |
$output .= l(t('Check import settings'), 'admin/settings/phpbb2drupal');
|
| 35 |
$output .= '</li>';
|
| 36 |
$output .= '<li>';
|
| 37 |
$output .= t('Import atleast user data from phpBB using the <a href="@phpbb2drupal">phpbb2drupal</a> module if you have not already done so', array('@phpbb2drupal' => url('admin/phpbb2drupal/execute')));
|
| 38 |
$output .= '</li>';
|
| 39 |
$output .= '<p>';
|
| 40 |
$output .= t('In cases where either recipients or senders of a message have NOT had their user data already imported into Drupal, that message will be lost.');
|
| 41 |
$output .= '</p>';
|
| 42 |
|
| 43 |
if (!variable_get('phpbb2drupal_ready', 0)) {
|
| 44 |
return '<p>'. t('the phpBB2Drupal settings. Please <a href="@settings">complete the setup first</a>', array('@settings' => url('admin/settings/phpbb2drupal'))) .'</p>';
|
| 45 |
}
|
| 46 |
if (variable_get('phpbb2privatemsg_import_successful', 0) == 1) {
|
| 47 |
return t('Private Messages have already been imported. You can now <a href="@cleanup">clean up</a> any variables set by this module and deactivate/uninstall it.', array('@cleanup' => url('admin/phpbb2privatemsg/cleanup')));
|
| 48 |
}
|
| 49 |
|
| 50 |
$pre = variable_get('phpbb2drupal_table_prefix', 'phpbb_');
|
| 51 |
$tables = array($pre .'privmsgs', $pre .'privmsgs_to');
|
| 52 |
$result = _phpbb2drupal_check_tables($tables, 'phpbb', 0);
|
| 53 |
$output .= $result['html'];
|
| 54 |
|
| 55 |
if ($result['result'] != 1) {
|
| 56 |
$output .= '<p class="marker">';
|
| 57 |
$output .= t('Please use the correct database settings!');
|
| 58 |
$output .= '</p>';
|
| 59 |
}
|
| 60 |
else{
|
| 61 |
$output .= drupal_get_form('phpbb2privatemsg_migrate_form');
|
| 62 |
}
|
| 63 |
return $output;
|
| 64 |
}
|
| 65 |
|
| 66 |
function phpbb2privatemsg_migrate_form() {
|
| 67 |
_phpbb2drupal_db_connect() ;
|
| 68 |
// Causes problems with form api redirect
|
| 69 |
//ini_set('display_errors', TRUE);
|
| 70 |
|
| 71 |
// Adjust how long you want the script to run...
|
| 72 |
if (!ini_get('safe_mode')) {
|
| 73 |
set_time_limit(variable_get('phpbb2drupal_time_limit', 0));
|
| 74 |
}
|
| 75 |
|
| 76 |
$form['import'] = array(
|
| 77 |
'#type' => 'hidden',
|
| 78 |
'#title' => t('Import Private Messages'),
|
| 79 |
'#options' => 'pm',
|
| 80 |
);
|
| 81 |
$form[] = array(
|
| 82 |
'#type' => 'submit',
|
| 83 |
'#value' => t('Import Private Messages'),
|
| 84 |
);
|
| 85 |
return $form;
|
| 86 |
}
|
| 87 |
|
| 88 |
function phpbb2privatemsg_migrate_form_submit($form, $form_state) {
|
| 89 |
if (isset($form_state['values']['import'])) {
|
| 90 |
phpbb2privatemsg_import();
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Private Message Import Functions
|
| 96 |
*/
|
| 97 |
function phpbb2privatemsg_import() {
|
| 98 |
$pre = variable_get('phpbb2drupal_table_prefix', 'phpbb_');
|
| 99 |
$format = variable_get('phpbb2drupal_input_format', 0);
|
| 100 |
if (variable_get('phpbb2privatemsg_import_successful', 0) == 1) {
|
| 101 |
return t('Messages have already been imported successfully.');
|
| 102 |
}
|
| 103 |
db_set_active('phpbb');
|
| 104 |
$query = "SELECT * FROM %sprivmsgs ORDER BY msg_id ASC";
|
| 105 |
$messages = db_query($query, $pre);
|
| 106 |
|
| 107 |
db_set_active('default');
|
| 108 |
while ($pm = db_fetch_object($messages)) {
|
| 109 |
$from = db_result(db_query('SELECT uid FROM {phpbb2drupal_temp_user} WHERE user_id = %d', $pm->author_id));
|
| 110 |
// don't import private messages from or to users who are not imported.
|
| 111 |
if ($from) {
|
| 112 |
// remove the bbcode_uid from post_text
|
| 113 |
if(!empty($pm->bbcode_uid)) {
|
| 114 |
$pm->message_text = preg_replace("/:$pm->bbcode_uid/", '', $pm->message_text);
|
| 115 |
}
|
| 116 |
$pm->message_text = _phpbb2drupal_strip_bbcode($pm->message_text);
|
| 117 |
$pm->message_text = _phpbb2drupal_text_sanitise($pm->message_text);
|
| 118 |
$pm->message_text = _phpbb2drupal_replace_links($pm->message_text);
|
| 119 |
$pm->message_subject = _phpbb2drupal_text_sanitise($pm->message_subject);
|
| 120 |
|
| 121 |
// Borrow and adapt code from patch to upgrade privatemsg module to Drupal 6 (http://drupal.org/node/202348#comment-700061).
|
| 122 |
$message = array(
|
| 123 |
'author' => $from,
|
| 124 |
'subject' => substr($pm->message_subject, 0, 64),
|
| 125 |
'body' => $pm->message_text,
|
| 126 |
'timestamp' => $pm->message_time,
|
| 127 |
);
|
| 128 |
|
| 129 |
//save message if no duplicates
|
| 130 |
$dupe = db_result(db_query('SELECT COUNT(*) FROM {pm_message} WHERE author = %d AND timestamp = %d', $message['author'], $message['timestamp']));
|
| 131 |
if (!($dupe)) {
|
| 132 |
|
| 133 |
$result = drupal_write_record('pm_message', $message);
|
| 134 |
$message['mid'] = db_last_insert_id('pm_message', 'mid');
|
| 135 |
|
| 136 |
// Fix the message threading.
|
| 137 |
if ($pm->root_level <> 0) {
|
| 138 |
// This message is part of an existing thread.
|
| 139 |
$message['thread_id'] = db_result(db_query('SELECT thread_id FROM {phpbb2privatemsg} WHERE msg_id = %d', $pm->root_level));
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$message['thread_id'] = $message['mid'];
|
| 143 |
}
|
| 144 |
|
| 145 |
$result = db_query('INSERT INTO {phpbb2privatemsg} (mid, thread_id, msg_id) VALUES (%d, %d, %d)', $message['mid'], $message['thread_id'], $pm->msg_id);
|
| 146 |
|
| 147 |
//insert index record for the author and all recipients of a message.
|
| 148 |
db_set_active('phpbb');
|
| 149 |
$query = "SELECT * FROM %sprivmsgs_to WHERE msg_id = %d ORDER BY msg_id ASC";
|
| 150 |
$result = db_query($query, $pre, $pm->msg_id);
|
| 151 |
db_set_active('default');
|
| 152 |
while ($index = db_fetch_object($result)) {
|
| 153 |
// Fill in appropriate fields.
|
| 154 |
$message['uid'] = db_result(db_query('SELECT uid FROM {phpbb2drupal_temp_user} WHERE user_id = %d', $index->user_id));
|
| 155 |
$message['is_new'] = $index->pm_unread;
|
| 156 |
$message['deleted'] = $index->pm_deleted;
|
| 157 |
|
| 158 |
//Save to the index.
|
| 159 |
$save = drupal_write_record('pm_index', $message);
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|
| 164 |
variable_set('phpbb2privatemsg_import_successful', 1);
|
| 165 |
drupal_set_message(t('Private Message Import successful'));
|
| 166 |
}
|
| 167 |
|
| 168 |
function phpbb2privatemsg_cleanup() {
|
| 169 |
|
| 170 |
variable_del('phpbb2privatemsg_import_successful');
|
| 171 |
return '<p>'. t('phpbb2privatemsg settings removed.') .'</p>';
|
| 172 |
|
| 173 |
}
|