Parent Directory
|
Revision Log
|
Revision Graph
Updated the phpbb2drupal_import.php -Removed some major bugs -Imports Polls Added redirection scripts -viewtopic.php -stories.php
| 1 | <?php |
| 2 | #!/usr/local/bin/php |
| 3 | |
| 4 | /* $Id$ */ |
| 5 | |
| 6 | /** |
| 7 | * Conversion script from phpbb 2.0.18 to Drupal 4.6.5. |
| 8 | * |
| 9 | * Written by: John Hwang |
| 10 | * Date: 2006-01-12 |
| 11 | * |
| 12 | * The following modules are required to be not only installed but also enabled |
| 13 | * |
| 14 | * forum |
| 15 | * node |
| 16 | * comment |
| 17 | * user |
| 18 | * profile |
| 19 | * taxonomy |
| 20 | * upload (optional, need for attachments) |
| 21 | * comment_upload (optional, need for attachments) |
| 22 | * |
| 23 | * Notes: |
| 24 | * 1. You have to specify where your PHPBB database and Drupal databases are. |
| 25 | * a. Open setting.php |
| 26 | * b. Change the default dsn_url to |
| 27 | * $db_url['default'] = 'mysql://username:password@localhost/drupal_database'; |
| 28 | * $db_url['phpbb'] = 'mysql://username:password@localhost/phpbb_database'; |
| 29 | * Because this script uses Drupal's db_set_active() to switch between databases |
| 30 | * The advantage of this method is that now you are not required to have the tables |
| 31 | * in the same database. It doesn't even require that either databases be on the |
| 32 | * same machine. |
| 33 | * 2. Adjust the $TIME_LIMIT for an appropriately large value. The larger the |
| 34 | * data you're trying to import, the higher the value should be. |
| 35 | * 3. To import attachments, change $PHPBB2DRUPAL_IMPORT_ATTACHMENTS's value to TRUE. |
| 36 | * 4. Once you enable the forum module, it's a good idea to go to adminster->forums |
| 37 | * so that the forum module creates a vocabulary for your forum. If not, the script |
| 38 | * will automatically create a vocabulary name "Forums." |
| 39 | * 5. The script does not support PHPBB2's attachment thumbnails |
| 40 | * 6. To import attachments, install and Enable both the new comment.module and the comment_upload.module |
| 41 | * 7. You must manually move/link the contents of the old attachment directory to drupal's files |
| 42 | * directory of your drupal installation. It import script does not check if the |
| 43 | * file actually exists. It won't move it for you either. It only imports whatever |
| 44 | * is in your PHPBB2 database. |
| 45 | */ |
| 46 | |
| 47 | // Disable access checking? |
| 48 | $access_check = TRUE; |
| 49 | |
| 50 | // error_reporting(E_ALL); |
| 51 | ini_set('display_errors', TRUE); |
| 52 | |
| 53 | $PHPBB2DRUPAL_IMPORT_ATTACHMENTS = TRUE; |
| 54 | $PHPBB2DRUPAL_TIME_LIMIT = 1200; // 20 Minutes still might not be enough... but it workes well for 300,000 |
| 55 | $PHPBB2DRUPAL_FORUM_NAME = 'Forums'; |
| 56 | |
| 57 | // Adjust how long you want the script to run... |
| 58 | if (!ini_get("safe_mode")) { |
| 59 | set_time_limit($PHPBB2DRUPAL_TIME_LIMIT); |
| 60 | } |
| 61 | |
| 62 | if (isset($_GET["op"])) { |
| 63 | include_once "includes/bootstrap.inc"; |
| 64 | include_once "includes/common.inc"; |
| 65 | |
| 66 | // Access check: |
| 67 | if (($access_check == 0) || ($user->uid == 1)) { |
| 68 | phpbb2drupal_page(); |
| 69 | } |
| 70 | else { |
| 71 | print phpbb2drupal_page_header("Access denied"); |
| 72 | print "<p>Access denied. You are not authorized to access this page. Please log in as the admin user (the first user you created). If you cannot log in, you will have to edit <code>phpbb2drupal.php</code> to bypass this access check. To do this:</p>"; |
| 73 | print "<ol>"; |
| 74 | print " <li>With a text editor find the phpbb2drupal.php file on your system. It should be in the main Drupal directory that you installed all the files into.</li>"; |
| 75 | print " <li>There is a line near top of phpbb2drupal.php that says <code>\$access_check = TRUE;</code>. Change it to <code>\$access_check = FALSE;</code>.</li>"; |
| 76 | print " <li>As soon as the script is done, you must change the phpbb2drupal.php script back to its original form to <code>\$access_check = TRUE;</code>.</li>"; |
| 77 | print " <li>To avoid having this problem in future, remember to log in to your website as the admin user (the user you first created) before you backup your database at the beginning of the update process.</li>"; |
| 78 | print "</ol>"; |
| 79 | |
| 80 | print phpbb2drupal_page_footer(); |
| 81 | } |
| 82 | } |
| 83 | else { |
| 84 | phpbb2drupal_info(); |
| 85 | } |
| 86 | |
| 87 | function phpbb2drupal_info() { |
| 88 | print phpbb2drupal_page_header("Drupal PHPBB2 Import"); |
| 89 | ?> |
| 90 | <h3>Before doing anything, <strong>backup your database!</strong> This process will change your database and its values, and some things might get lost!!</h3> |
| 91 | |
| 92 | <h3>Notes:</h3> |
| 93 | |
| 94 | The following modules are required to be not only installed but also enabled: |
| 95 | <ul> |
| 96 | <li>forum</li> |
| 97 | <li>node</li> |
| 98 | <li>comment</li> |
| 99 | <li>user</li> |
| 100 | <li>profile</li> |
| 101 | <li>taxonomy</li> |
| 102 | <li>upload (optional)</li> |
| 103 | <li>comment_upload (optional)</li> |
| 104 | </ul> |
| 105 | |
| 106 | <ol> |
| 107 | <li>You have to specify where your PHPBB database and Drupal databases are.</li> |
| 108 | <ol> |
| 109 | <li>Open setting.php</li> |
| 110 | <li>Change the default dsn_url to</li> |
| 111 | <ul> |
| 112 | <li>$db_url['default'] = 'mysql://username:password@localhost/drupal_database';</li> |
| 113 | <li>$db_url['phpbb'] = 'mysql://username:password@localhost/phpbb_database';</li> |
| 114 | </ul> |
| 115 | <li>Because this script uses Drupal's db_set_active() to switch between databases. The advantage of this method is that now you are not required to have the tables in the same database. It doesn't even require that either databases be on the same machine.</li> |
| 116 | </ol> |
| 117 | <li>Adjust the $PHPBB2DRUPAL_TIME_LIMIT for an appropriately large value. The larger the data you're trying to import, the higher the value should be.</li> |
| 118 | <li>Once you enable the forum module, it's a good idea to go to adminster->forums so that the forum module creates a vocabulary for your forum. If not, the script will automatically create a vocabulary name "Forums."</li> |
| 119 | <li>To import attachments, change $PHPBB2DRUPAL_IMPORT_ATTACHMENTS's value to TRUE. |
| 120 | <li>The script does not support PHPBB2's attachment thumbnails</li> |
| 121 | <li>Install and Enable both the new comment.module and the comment_upload.module</li> |
| 122 | <li>You must manually move/link the contents of the old attachment directory to drupal's files directory of your drupal installation. It import script does not check if the file actually exists. It won't move it for you either. It only imports whatever is in your PHPBB2 database.</li> |
| 123 | </ol> |
| 124 | |
| 125 | <h3><a href="phpbb2drupal_import.php?op=import">Begin the import process</a></h3> |
| 126 | <?php |
| 127 | print phpbb2drupal_page_footer(); |
| 128 | } |
| 129 | |
| 130 | function phpbb2drupal_page() { |
| 131 | if (isset($_POST['edit'])) { |
| 132 | $edit = $_POST['edit']; |
| 133 | } |
| 134 | |
| 135 | $PHPBB2DRUPAL_FUNCTIONS = array( |
| 136 | 'users' => 'Import Users', |
| 137 | 'categories' => 'Import Categories', |
| 138 | 'topics' => 'Import Topics', |
| 139 | 'polls' => 'Import Polls', |
| 140 | 'posts' => 'Import Posts', |
| 141 | 'cleanup' => 'Clean Up' |
| 142 | ); |
| 143 | |
| 144 | print phpbb2drupal_page_header("Drupal PHPBB2 import"); |
| 145 | $links[] = "<a href=\"index.php\">main page</a>"; |
| 146 | $links[] = "<a href=\"index.php?q=admin\">administration pages</a>"; |
| 147 | print theme("item_list", $links); |
| 148 | |
| 149 | $action = $edit['import']; |
| 150 | switch ($action) { |
| 151 | case "users": |
| 152 | |
| 153 | if(variable_get('phpbb2drupal_import_user_successful', 0) == '0') { |
| 154 | phpbb2drupal_import_users(); |
| 155 | $selected = 'users'; |
| 156 | } else { |
| 157 | $selected = 'categories'; |
| 158 | } |
| 159 | continue; |
| 160 | |
| 161 | case "categories": |
| 162 | # if(variable_get('phpbb2drupal_import_category_successful', 0) == 0) { |
| 163 | phpbb2drupal_import_categories(); |
| 164 | # $selected = 'categories'; |
| 165 | # } else { |
| 166 | $selected = 'topics'; |
| 167 | # } |
| 168 | continue; |
| 169 | |
| 170 | case "topics": |
| 171 | if(variable_get('phpbb2drupal_import_topic_successful', 0) == 0) { |
| 172 | phpbb2drupal_import_topics(); |
| 173 | $selected = 'topics'; |
| 174 | } else { |
| 175 | $selected = 'polls'; |
| 176 | } |
| 177 | |
| 178 | continue; |
| 179 | |
| 180 | case "polls": |
| 181 | if(variable_get('phpbb2drupal_import_poll_successful', 0) == 0) { |
| 182 | phpbb2drupal_import_polls(); |
| 183 | $selected = 'polls'; |
| 184 | } else { |
| 185 | $selected = 'posts'; |
| 186 | } |
| 187 | |
| 188 | continue; |
| 189 | |
| 190 | case "posts": |
| 191 | if(variable_get('phpbb2drupal_import_post_successful', 0) == 0) { |
| 192 | print '<h1>About to import posts</h1>'; |
| 193 | phpbb2drupal_import_posts(); |
| 194 | $selected = 'posts'; |
| 195 | } else { |
| 196 | $selected = 'cleanup'; |
| 197 | print "<h2>Congratulations. Import Finished</h2>"; |
| 198 | } |
| 199 | continue; |
| 200 | |
| 201 | case "cleanup": |
| 202 | phpbb2drupal_import_cleanup(); |
| 203 | continue; |
| 204 | |
| 205 | default: |
| 206 | // make update form and output it. |
| 207 | $selected = 'users'; |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | $form = form_select("Next import to perform", "import", $selected, $PHPBB2DRUPAL_FUNCTIONS); |
| 212 | $form .= form_submit("Import"); |
| 213 | print form($form); |
| 214 | |
| 215 | print phpbb2drupal_page_footer(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * User Import Functions |
| 220 | */ |
| 221 | function phpbb2drupal_import_users() { |
| 222 | |
| 223 | // check if the user database has been successfully imported |
| 224 | db_set_active('default'); |
| 225 | if(variable_get('phpbb2drupal_import_user_successful', 0) == 1) return; |
| 226 | |
| 227 | if(variable_get('phpbb2drupal_import_user_started', 0) == 0) { |
| 228 | // create temporary tables |
| 229 | db_set_active('default'); |
| 230 | db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_user}"); |
| 231 | db_query("CREATE TABLE {phpbb2drupal_temp_user} ( |
| 232 | user_id mediumint(8) DEFAULT '0' NOT NULL, |
| 233 | uid INTEGER UNSIGNED DEFAULT '0' NOT NULL, |
| 234 | KEY user_id (user_id))" |
| 235 | ); |
| 236 | |
| 237 | // create profile fields for icq, aim, msn...etc |
| 238 | db_query("INSERT INTO {profile_fields} (title, name, explanation, category, page, type, weight, required, register, visibility, options) VALUES ('YIM','user_yim','','Contact','','textfield',0,0,1,2,''),('AIM','user_aim','','Contact','','textfield',0,0,1,2,''),('MSN','user_msnm','','Contact','','textfield',0,0,1,2,''),('icq','user_icq','','Contact','','textfield',0,0,1,2,''),('Website','user_website','','Contact','','url',0,0,1,2,''),('Location','user_from','','Personal','','textfield',0,0,1,2,''),('Occupation','user_occ','','Personal','','textfield',0,0,1,2,''),('Interests','user_interests','','Personal','','textfield',0,0,1,2,'')"); |
| 239 | |
| 240 | variable_set('phpbb2drupal_import_user_started', 1); |
| 241 | } |
| 242 | |
| 243 | // adding the admin uid so that other functions can find the admin mapping |
| 244 | db_set_active('default'); |
| 245 | db_query("INSERT INTO {phpbb2drupal_temp_user} (user_id, uid) VALUES (2 , 1)"); |
| 246 | |
| 247 | $files_path = variable_get('file_directory_path', 'files'); |
| 248 | $pictures_path = variable_get('user_picture_path', 'pictures'); |
| 249 | |
| 250 | // Insert the users into drupal |
| 251 | db_set_active('phpbb'); |
| 252 | $user_ids = db_query("SELECT user_id FROM {phpbb_users} WHERE user_id > 2 ORDER BY user_id"); |
| 253 | |
| 254 | $user_count = db_num_rows($user_ids); |
| 255 | |
| 256 | if(!$user_count) { |
| 257 | exit("There were no users found: Aborting script"); |
| 258 | } |
| 259 | |
| 260 | print "<h3>Found $user_count users: Beginning Import</h3>"; |
| 261 | flush(); |
| 262 | |
| 263 | while($result = db_fetch_object($user_ids)) { |
| 264 | |
| 265 | db_set_active('phpbb'); |
| 266 | $user = db_fetch_object(db_query("SELECT * FROM {phpbb_users} WHERE user_id = %d", $result->user_id)); |
| 267 | |
| 268 | // Make sure the user is not on the banlist |
| 269 | /* db_set_active('phpbb'); |
| 270 | $banned = db_result(db_query("SELECT COUNT(*) FROM {phpbb_banlist} WHERE ban_userid = %d", $user->user_id)); |
| 271 | if($banned) { |
| 272 | db_set_active('phpbb'); |
| 273 | continue; |
| 274 | }*/ |
| 275 | |
| 276 | // Make sure the user is has not already been imported |
| 277 | db_set_active('default'); |
| 278 | $count = db_result(db_query("SELECT COUNT(*) FROM {phpbb2drupal_temp_user} WHERE user_id = %d", $user->user_id)); |
| 279 | if($count > 0) { |
| 280 | $user->user_active = 0; |
| 281 | } |
| 282 | |
| 283 | $user->user_aim = strtr($user->user_aim, array("+" => ' ')); # PHPBB stores spaces as +, replace with ' ' |
| 284 | $user->user_yim = strtr($user->user_yim, array("+" => ' ')); |
| 285 | $user->user_timezone = $user->user_timezone * 60 * 60; # Drupal stores timezones in seconds |
| 286 | |
| 287 | // remove the bbcode_uid from post_text |
| 288 | $user->user_sig = preg_replace("/:$user->user_sig_bbcode_uid/", '', $user->user_sig); |
| 289 | |
| 290 | // if the $user->user_avatar_type is not their own image, delete it |
| 291 | // drupal doesn't have pre-defined avatars. if we were to import it |
| 292 | // then multiple people would share the same avatar image and if one user |
| 293 | // were to changes their avatar then it would change it for everybody else. |
| 294 | if($user->user_avatar_type > 1) { |
| 295 | $user->user_avatar = ''; |
| 296 | } |
| 297 | |
| 298 | $user->user_avatar = ($user->user_avatar) ? "$files_path/$pictures_path/$user->user_avatar" : ''; |
| 299 | |
| 300 | $data = array( |
| 301 | 'name' => $user->username, |
| 302 | 'pass' => $user->user_password, |
| 303 | 'mail' => $user->user_email, |
| 304 | 'signature' => $user->user_sig, |
| 305 | 'created' => $user->user_regdate, |
| 306 | 'status' => $user->user_active, |
| 307 | 'timezone' => $user->user_timezone, |
| 308 | 'picture' => $user->user_avatar, |
| 309 | 'init' => $user->user_email, |
| 310 | 'roles' => array(0 => 2), # Authenticated User |
| 311 | 'user_website' => $user->user_website, |
| 312 | 'user_from' => $user->user_from, |
| 313 | 'user_icq' => $user->user_icq, |
| 314 | 'user_aim' => $user->user_aim, |
| 315 | 'user_yim' => $user->user_yim, |
| 316 | 'user_msnm' => $user->user_msnm, |
| 317 | 'user_occ' => $user->user_occ, |
| 318 | 'user_interests' => $user->user_interest |
| 319 | ); |
| 320 | |
| 321 | db_set_active('default'); |
| 322 | $drupal_user = phpbb2drupal_user_save($data, array('account', 'Personal', 'Contact')); |
| 323 | |
| 324 | // print "<pre>"; |
| 325 | // print_r($drupal_user); |
| 326 | // print "</pre>"; |
| 327 | |
| 328 | // populate the temporary user table |
| 329 | db_set_active('default'); |
| 330 | db_query("INSERT INTO {phpbb2drupal_temp_user} (user_id, uid) VALUES ($user->user_id , $drupal_user->uid)"); |
| 331 | |
| 332 | db_set_active('phpbb'); |
| 333 | } |
| 334 | |
| 335 | // set the user import successful flag in the variable table |
| 336 | db_set_active('default'); |
| 337 | variable_set('phpbb2drupal_import_user_successful', '1'); |
| 338 | |
| 339 | $count = db_result(db_query("SELECT COUNT(*) FROM {phpbb2drupal_temp_user}")); |
| 340 | print "<h3>Successfully Imported $count Users</h3>"; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * |
| 345 | * Create Forum Containers and Forums |
| 346 | * |
| 347 | */ |
| 348 | function phpbb2drupal_import_categories() { |
| 349 | |
| 350 | db_set_active('default'); |
| 351 | |
| 352 | // check if the forum database has been successfully imported |
| 353 | if(variable_get('phpbb2drupal_import_category_successful', 0) == 1) return; |
| 354 | |
| 355 | // forum mapping temporary tables |
| 356 | if(variable_get('phpbb2drupal_import_category_started', 0) == 0) { |
| 357 | db_set_active('default'); |
| 358 | db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_forum}"); |
| 359 | db_query("CREATE TABLE {phpbb2drupal_temp_forum} ( |
| 360 | forum_id smallint(5) UNSIGNED DEFAULT '0' NOT NULL, |
| 361 | tid integer UNSIGNED DEFAULT '0' NOT NULL, |
| 362 | KEY forum_id (forum_id))" |
| 363 | ); |
| 364 | variable_set('phpbb2drupal_import_category_started', 1); |
| 365 | } |
| 366 | |
| 367 | // Retrieve the vocabulary vid named "Forum" |
| 368 | $forum_vid = _forum_get_vid(); |
| 369 | |
| 370 | print "<h3>Forum vid: $forum_vid</h3>"; |
| 371 | flush(); |
| 372 | |
| 373 | // Get Categories/Forums from PHPBB |
| 374 | db_set_active('phpbb'); |
| 375 | $category_results = db_query("SELECT * FROM {phpbb_categories} ORDER BY cat_order"); |
| 376 | |
| 377 | $cat_count = db_num_rows($category_results); |
| 378 | |
| 379 | print "<h3>Found $cat_count categories: Beginning Import</h3>"; |
| 380 | flush(); |
| 381 | |
| 382 | while($category_result = db_fetch_array($category_results)) { |
| 383 | |
| 384 | $cat_id = $category_result['cat_id']; |
| 385 | $forums_results = db_query("SELECT * FROM {phpbb_forums} WHERE cat_id = $cat_id"); |
| 386 | |
| 387 | $phpbb2_forums = array(); # reinitialize the temp var not to include it multiple times |
| 388 | while($forum_result = db_fetch_object($forums_results)) { |
| 389 | //$phpbb2_categories[$category_result->cat_id]['forums'][] = $forums_result; |
| 390 | $phpbb2_forums[$forum_result->forum_id] = $forum_result; |
| 391 | } |
| 392 | |
| 393 | $phpbb2_categories[$cat_id] = array_merge($category_result, array('forums' => $phpbb2_forums)); |
| 394 | } |
| 395 | |
| 396 | // print "<pre>"; |
| 397 | // print_r($phpbb2_categories); |
| 398 | // print "</pre>"; |
| 399 | |
| 400 | // Insert the Containers / Forum into Drupal |
| 401 | db_set_active('default'); |
| 402 | |
| 403 | // Insert the Containers |
| 404 | $container_order = -10; |
| 405 | foreach($phpbb2_categories as $container) { |
| 406 | $edit = array('name' => $container['cat_title'], |
| 407 | 'vid' => $forum_vid, |
| 408 | 'description' => '', |
| 409 | 'weight' => $container_order); |
| 410 | |
| 411 | $edit = taxonomy_save_term($edit); |
| 412 | //print_r($edit); |
| 413 | |
| 414 | // serialize the forum containers |
| 415 | $containers = variable_get('forum_containers', array()); |
| 416 | $containers[] = $edit['tid']; |
| 417 | variable_set('forum_containers', $containers); |
| 418 | |
| 419 | // Insert the Forums |
| 420 | $forum_order = -10; |
| 421 | foreach($container['forums'] as $forum) { |
| 422 | // Make sure the forum/term is has not already been imported |
| 423 | if(!db_result(db_query("SELECT forum_id FROM {phpbb2drupal_temp_forum} WHERE forum_id = $forum->forum_id"))) { |
| 424 | $forum_edit = array('name' => $forum->forum_name, |
| 425 | 'vid' =>$forum_vid, |
| 426 | 'description' => $forum->forum_desc, |
| 427 | 'weight' => $forum_order, |
| 428 | 'parent' => array(0=>$edit['tid'])); |
| 429 | |
| 430 | $forum_edit = taxonomy_save_term($forum_edit); |
| 431 | |
| 432 | $forum_order++; |
| 433 | $tid = $forum_edit['tid']; |
| 434 | |
| 435 | db_set_active('default'); |
| 436 | db_query("INSERT INTO {phpbb2drupal_temp_forum} (forum_id, tid) VALUES ($forum->forum_id, $tid)"); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | $container_order++; |
| 441 | } |
| 442 | |
| 443 | db_set_active('default'); |
| 444 | // set the forums import successful flag in the variable table |
| 445 | variable_set('phpbb2drupal_import_category_successful', '1'); |
| 446 | |
| 447 | $count = db_result(db_query("SELECT COUNT(*) FROM {phpbb2drupal_temp_forum}")); |
| 448 | print "<h3>Successfully Imported $count forums</h3>"; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * |
| 453 | * Imports PHPBB topics to Drupal equivalent forum nodes |
| 454 | * |
| 455 | */ |
| 456 | function phpbb2drupal_import_topics() { |
| 457 | global $PHPBB2DRUPAL_IMPORT_ATTACHMENTS; |
| 458 | |
| 459 | db_set_active('default'); |
| 460 | |
| 461 | // check if the post database has been successfully imported |
| 462 | if(variable_get('phpbb2drupal_import_topic_successful', 0) == 1) return; |
| 463 | |
| 464 | if(variable_get('phpbb2drupal_import_topic_started', 0) == 0) { |
| 465 | db_set_active('default'); |
| 466 | db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_topic}"); |
| 467 | db_query("CREATE TABLE {phpbb2drupal_temp_topic} ( |
| 468 | topic_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, |
| 469 | post_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, |
| 470 | nid integer UNSIGNED DEFAULT '0' NOT NULL, |
| 471 | KEY topic_id (topic_id))" |
| 472 | ); |
| 473 | variable_set('phpbb2drupal_import_topic_started', 1); |
| 474 | } |
| 475 | |
| 476 | // Get All topics from PHPBB |
| 477 | db_set_active('phpbb'); |
| 478 | $topic_ids = db_query("SELECT topic_id |
| 479 | FROM {phpbb_topics} |
| 480 | WHERE topic_vote <> 1 |
| 481 | ORDER BY topic_id"); // topic_status == 2, Moved topics are duplicates don't import |
| 482 | |
| 483 | $topic_count = db_num_rows($topic_ids); |
| 484 | |
| 485 | print "<h3>About to import $topic_count topics</h3>"; |
| 486 | flush(); |
| 487 | |
| 488 | // Import the topics into drupal |
| 489 | $counter = 0; |
| 490 | db_set_active('phpbb'); |
| 491 | while($result = db_fetch_object($topic_ids)) { |
| 492 | |
| 493 | // check if this topic has been imported already just to be sure |
| 494 | db_set_active('default'); |
| 495 | $count = db_result(db_query("SELECT count(*) FROM {phpbb2drupal_temp_topic} WHERE topic_id = %d", $result->topic_id)); |
| 496 | if($count > 0) { |
| 497 | #print "<h3>Topic $result->topic_id has already been imported</h3>"; |
| 498 | #flush(); |
| 499 | db_set_active('phpbb'); |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | db_set_active('phpbb'); |
| 504 | /*$query = db_query("SELECT * |
| 505 | FROM {phpbb_topics} t |
| 506 | INNER JOIN {phpbb_posts} p ON t.topic_id = p.topic_id |
| 507 | INNER JOIN {phpbb_posts_text} pt ON p.post_id = pt.post_id |
| 508 | WHERE t.topic_id = %d |
| 509 | ORDER BY p.post_id |
| 510 | LIMIT 1", $result->topic_id); /**/ |
| 511 | |
| 512 | $query = db_query("SELECT * |
| 513 | FROM {phpbb_topics} t |
| 514 | INNER JOIN {phpbb_posts} p ON t.topic_id = p.topic_id |
| 515 | INNER JOIN {phpbb_posts_text} pt ON p.post_id = pt.post_id |
| 516 | WHERE p.post_id = t.topic_first_post_id |
| 517 | AND t.topic_id = %d", $result->topic_id); |
| 518 | |
| 519 | // check if the topic is a valid topic. if not, continue on |
| 520 | if(db_num_rows($query)) { |
| 521 | $topic = db_fetch_object($query); |
| 522 | } else { |
| 523 | print "<h3>Could not find post details of topic: $result->topic_id</h3>"; |
| 524 | flush(); |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | db_set_active('default'); |
| 529 | $uid = db_result(db_query("SELECT uid FROM {phpbb2drupal_temp_user} WHERE user_id = %d", $topic->topic_poster)); |
| 530 | $tid = db_result(db_query("SELECT tid FROM {phpbb2drupal_temp_forum} WHERE forum_id = %d", $topic->forum_id)); |
| 531 | |
| 532 | if($topic->topic_poster == 2) { // is the admin |
| 533 | $uid = 1; |
| 534 | } elseif($topic->topic_poster == -1) { |
| 535 | $uid = 0; |
| 536 | } |
| 537 | |
| 538 | if($topic->topic_type == 1) { |
| 539 | $sticky = 1; // sticky |
| 540 | $promote = 0; |
| 541 | } elseif ($topic->topic_type == 2) { |
| 542 | $sticky = 1; |
| 543 | $promote = 0; // display on the front page, i.e. promote |
| 544 | } else { |
| 545 | $sticky = 0; |
| 546 | $promote = 0; |
| 547 | } |
| 548 | |
| 549 | if ($topic->topic_status == 1) { // LOCKED |
| 550 | $comment = 1; // read-only |
| 551 | } else { // UNLOCKED & WATCH NOTIFIED |
| 552 | $comment = 2; // read-write |
| 553 | } |
| 554 | |
| 555 | // remove the bbcode_uid from post_text |
| 556 | $topic->post_text = preg_replace("/:$topic->bbcode_uid/", '', $topic->post_text); |
| 557 | |
| 558 | $teaser = node_teaser($topic->post_text); |
| 559 | |
| 560 | //construct the node |
| 561 | $node = array( |
| 562 | 'type' => 'forum', |
| 563 | 'title' => $topic->topic_title, |
| 564 | 'uid' => $uid, |
| 565 | 'status' => 1, // published or not - always publish |
| 566 | 'promote' => $promote, |
| 567 | 'created' => $topic->topic_time, |
| 568 | 'changed' => $topic->post_edit_time, |
| 569 | 'comment' => $comment, |
| 570 | 'moderate' => 0, |
| 571 | 'body' => $topic->post_text, |
| 572 | 'sticky' => $sticky, |
| 573 | 'teaser' => $teaser |
| 574 | ); |
| 575 | |
| 576 | if($topic->topic_status == 2) { |
| 577 | db_set_active('phpbb'); |
| 578 | $forum_id = db_result(db_query("SELECT forum_id FROM {phpbb_topics} WHERE topic_id = %d", $topic_moved_id)); |
| 579 | db_set_active('default'); |
| 580 | $moved_tid = db_result(db_query("SELECT tid FROM {phpbb2drupal_temp_forum} WHERE forum_id = %d", $forum_id)); |
| 581 | |
| 582 | $node['tid'] = $moved_tid; // which forum it used to be part of |
| 583 | } else { |
| 584 | $node['tid'] = $tid; |
| 585 | } |
| 586 | |
| 587 | $node = array2object($node); // node_save requires an object form |
| 588 | |
| 589 | // print "<pre>"; |
| 590 | // print_r($node); |
| 591 | // print "</pre>"; |
| 592 | |
| 593 | db_set_active('default'); |
| 594 | $nid = node_save($node); |
| 595 | taxonomy_node_save($nid, array(0 => $tid)); |
| 596 | |
| 597 | if(!$nid) { |
| 598 | print "<h3>Failed importing $topic->topic_id</h3>"; |
| 599 | flush(); |
| 600 | } |
| 601 | |
| 602 | // Handle attachments |
| 603 | if($PHPBB2DRUPAL_IMPORT_ATTACHMENTS) { |
| 604 | if($topic->topic_attachment == 1) { |
| 605 | |
| 606 | db_set_active('default'); |
| 607 | $file_path = variable_get('file_directory_path', 'files'); |
| 608 | |
| 609 | db_set_active('phpbb'); |
| 610 | $files = db_query("SELECT * |
| 611 | FROM {phpbb_attachments} a |
| 612 | INNER JOIN {phpbb_attachments_desc} ad ON a.attach_id = ad.attach_id |
| 613 | INNER JOIN {phpbb_posts} p ON a.post_id = p.post_id |
| 614 | WHERE p.topic_id = %d |
| 615 | ORDER BY a.attach_id", $topic->topic_id); |
| 616 | |
| 617 | while($file = db_fetch_object($files)) { |
| 618 | db_set_active('default'); |
| 619 | $fid = db_next_id('{files}_fid'); |
| 620 | db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize, list) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)", $fid, $nid, $file->real_filename, "$file_path/$file->physical_filename", $file->mimetype, $file->filesize, 1); |
| 621 | db_set_active('phpbb'); |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | db_set_active('default'); |
| 627 | db_query("INSERT INTO {phpbb2drupal_temp_topic} (topic_id, post_id, nid) VALUES (%d, %d, %d)", $topic->topic_id, $topic->post_id, $nid); |
| 628 | |
| 629 | db_set_active('phpbb'); |
| 630 | } |
| 631 | |
| 632 | db_set_active('default'); |
| 633 | // set the topic import successful flag in the variable table |
| 634 | variable_set('phpbb2drupal_import_topic_successful', '1'); |
| 635 | |
| 636 | $count = db_result(db_query("SELECT COUNT(*) FROM {phpbb2drupal_temp_topic}")); |
| 637 | print "<h3>Successfully Imported $count topics</h3>"; |
| 638 | } |
| 639 | |
| 640 | function phpbb2drupal_import_polls() { |
| 641 | db_set_active('default'); |
| 642 | |
| 643 | // check if the post database has been successfully imported |
| 644 | if(variable_get('phpbb2drupal_import_polls_successful', 0) == 1) return; |
| 645 | |
| 646 | if(variable_get('phpbb2drupal_import_polls_started', 0) == 0) { |
| 647 | variable_set('phpbb2drupal_import_poll_started', 1); |
| 648 | } |
| 649 | |
| 650 | // Get all polls from PHPBB |
| 651 | db_set_active('phpbb'); |
| 652 | $topics = db_query("SELECT * |
| 653 | FROM {phpbb_topics} t |
| 654 | WHERE topic_vote = 1 |
| 655 | ORDER BY topic_id"); |
| 656 | |
| 657 | $topic_count = db_num_rows($topics); |
| 658 | |
| 659 | print "<h3>About to import $topic_count polls</h3>"; |
| 660 | flush(); |
| 661 | |
| 662 | // insert into polls |
| 663 | while($topic = db_fetch_object($topics)) { |
| 664 | |
| 665 | // check if this topic has been imported already just to be sure |
| 666 | db_set_active('default'); |
| 667 | $count = db_result(db_query("SELECT count(*) FROM {phpbb2drupal_temp_topic} WHERE topic_id = %d", $topic->topic_id)); |
| 668 | if($count > 0) { |
| 669 | print "<h3>Poll $result->topic_id has already been imported</h3>"; |
| 670 | flush(); |
| 671 | db_set_active('phpbb'); |
| 672 | continue; |
| 673 | } |
| 674 | |
| 675 | // get the polls |
| 676 | db_set_active('phpbb'); |
| 677 | $query = db_query("SELECT * |
| 678 | FROM {phpbb_vote_desc} vd |
| 679 | WHERE topic_id = %d |
| 680 | ORDER BY vote_id", $topic->topic_id); |
| 681 | |
| 682 | if(db_num_rows($query)) { |
| 683 | $poll = db_fetch_object($query); |
| 684 | } else { |
| 685 | print "<h3>Could not find details of poll: $topic->topic_id</h3>"; |
| 686 | flush(); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | // get vote results |
| 691 | $query = db_query("SELECT * |
| 692 | FROM {phpbb_vote_results} |
| 693 | WHERE vote_id = %d |
| 694 | ORDER BY vote_option_id", $poll->vote_id); |
| 695 | |
| 696 | if(db_num_rows($query)) { |
| 697 | $choice = array(); |
| 698 | while($result = db_fetch_object($query)) { |
| 699 | $choice[] = array('chtext' => $result->vote_option_text, |
| 700 | 'chvotes' => $result->vote_result); |
| 701 | } |
| 702 | } else { |
| 703 | print "<h3>Could not find vote_results details of poll: $poll->vote_id</h3>"; |
| 704 | flush(); |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | // get voter information |
| 709 | $query = db_query("SELECT vote_user_id |
| 710 | FROM {phpbb_vote_voters} |
| 711 | WHERE vote_id = %d |
| 712 | ORDER BY vote_id", $poll->vote_id); |
| 713 | |
| 714 | if(db_num_rows($query)) { |
| 715 | $polled = ''; |
| 716 | db_set_active('phpbb'); |
| 717 | while($result = db_fetch_object($query)) { |
| 718 | db_set_active('default'); |
| 719 | $uid = db_result(db_query("SELECT uid FROM {phpbb2drupal_temp_user} WHERE user_id = %d", $result->vote_user_id)); |
| 720 | $polled = $polled . ' ' . "_" . $uid . "_"; |
| 721 | db_set_active('phpbb'); |
| 722 | } |
| 723 | //print "<pre>"; |
| 724 | //print_r($polled); |
| 725 | //print "</pre>"; |
| 726 | } |
| 727 | |
| 728 | db_set_active('default'); |
| 729 | $uid = db_result(db_query("SELECT uid FROM {phpbb2drupal_temp_user} WHERE user_id = %d", $topic->topic_poster)); |
| 730 | $tid = db_result(db_query("SELECT tid FROM {phpbb2drupal_temp_forum} WHERE forum_id = %d", $topic->forum_id)); |
| 731 | |
| 732 | if($topic->topic_poster == 2) { // is the admin |
| 733 | $uid = 1; |
| 734 | } elseif($topic->topic_poster == -1) { |
| 735 | $uid = 0; |
| 736 | } |
| 737 | |
| 738 | if($topic->topic_type == 1) { |
| 739 | $sticky = 1; // sticky |
| 740 | $promote = 0; |
| 741 | } elseif ($topic->topic_type == 2) { |
| 742 | $sticky = 0; |
| 743 | $promote = 1; // display on the front page, i.e. promote |
| 744 | } else { |
| 745 | $sticky = 0; |
| 746 | $promote = 0; |
| 747 | } |
| 748 | |
| 749 | if ($topic->topic_status == 1) { // LOCKED |
| 750 | $comment = 1; // read-only |
| 751 | } else { // UNLOCKED & WATCH NOTIFIED |
| 752 | $comment = 2; // read-write |
| 753 | } |
| 754 | |
| 755 | //construct the node |
| 756 | $node = array( |
| 757 | 'type' => 'poll', |
| 758 | 'title' => $poll->vote_text, |
| 759 | 'uid' => $uid, |
| 760 | 'status' => 1, // published or not - always publish |
| 761 | 'promote' => $promote, |
| 762 | 'created' => $topic->topic_time, |
| 763 | 'changed' => $topic->topic_time, |
| 764 | 'comment' => $comment, |
| 765 | 'moderate' => 0, |
| 766 | 'body' => '', |
| 767 | 'sticky' => $sticky, |
| 768 | 'teaser' => $teaser |
| 769 | ); |
| 770 | |
| 771 | |
| 772 | // handle moved nodes |
| 773 | if($topic->topic_status == 2) { |
| 774 | db_set_active('phpbb'); |
| 775 | $forum_id = db_result(db_query("SELECT forum_id FROM {phpbb_topics} WHERE topic_id = %d", $topic_moved_id)); |
| 776 | db_set_active('default'); |
| 777 | $moved_tid = db_result(db_query("SELECT tid FROM {phpbb2drupal_temp_forum} WHERE forum_id = %d", $forum_id)); |
| 778 | |
| 779 | $node['tid'] = $moved_tid; // which forum it used to be part of |
| 780 | } else { |
| 781 | $node['tid'] = $tid; |
| 782 | } |
| 783 | |
| 784 | // Add poll node information |
| 785 | $node['runtime'] = $poll->vote_length; |
| 786 | $node['active'] = (time() > ($poll->start+$poll->length)) ? 0 : 1; |
| 787 | $node['choice'] = $choice; |
| 788 | |
| 789 | $node = array2object($node); // node_save requires an object form |
| 790 | |
| 791 | db_set_active('default'); |
| 792 | $nid = node_save($node); |
| 793 | taxonomy_node_save($nid, array(0 => $tid)); |
| 794 | |
| 795 | if(!$nid) { |
| 796 | print "<h3>Failed importing $topic->topic_id</h3>"; |
| 797 | flush(); |
| 798 | } |
| 799 | |
| 800 | // manually update the poll table to store the uid of those who voted |
| 801 | db_query("UPDATE {poll} SET polled = '%s' WHERE nid = %d", $polled, $nid); |
| 802 | |
| 803 | db_set_active('default'); |
| 804 | db_query("INSERT INTO {phpbb2drupal_temp_topic} (topic_id, post_id, nid) VALUES (%d, %d, %d)", $topic->topic_id, $topic->post_id, $nid); |
| 805 | |
| 806 | db_set_active('phpbb'); |
| 807 | } |
| 808 | |
| 809 | db_set_active('default'); |
| 810 | // set the topic import successful flag in the variable table |
| 811 | variable_set('phpbb2drupal_import_poll_successful', '1'); |
| 812 | |
| 813 | print "<h3>Successfully imported polls</h3>"; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * |
| 818 | * PHPBB Posts --> Drupal Comments |
| 819 | * |
| 820 | */ |
| 821 | function phpbb2drupal_import_posts() { |
| 822 | global $PHPBB2DRUPAL_IMPORT_ATTACHMENTS; |
| 823 | |
| 824 | # db_set_active('phpbb'); |
| 825 | # $total_posts = db_result(db_query("SELECT COUNT(*) FROM {phpbb_posts} WHERE post_id <> topic_id")); |
| 826 | |
| 827 | db_set_active('default'); |
| 828 | // check if the post database has been successfully imported |
| 829 | if(variable_get('phpbb2drupal_import_post_successful', 0) == 1) return; |
| 830 | |
| 831 | if(variable_get('phpbb2drupal_import_post_started', 0) == 0) { |
| 832 | db_set_active('default'); |
| 833 | db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_post}"); |
| 834 | db_query("CREATE TABLE {phpbb2drupal_temp_post} ( |
| 835 | post_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, |
| 836 | cid int(10) DEFAULT '0' NOT NULL, |
| 837 | KEY post_id (post_id))" |
| 838 | ); |
| 839 | |
| 840 | db_set_active('default'); |
| 841 | variable_set('phpbb2drupal_import_post_started', 1); |
| 842 | } |
| 843 | |
| 844 | db_set_active('phpbb'); |
| 845 | $topic_ids = db_query("SELECT topic_id, topic_vote, topic_first_post_id, topic_last_post_id |
| 846 | FROM {phpbb_topics} |
| 847 | WHERE topic_replies > 0 |
| 848 | ORDER BY topic_id"); |
| 849 | |
| 850 | $topic_count = db_num_rows($topic_ids); |
| 851 | print "<h3>Importing comments of $topic_count topics</h3>"; |
| 852 | flush(); |
| 853 | |
| 854 | $errors = 0; |
| 855 | $loops = 0; |
| 856 | // Import the posts into drupal |
| 857 | while($obj = db_fetch_object($topic_ids)) { |
| 858 | $loops++; |
| 859 | |
| 860 | // skip first post if the post is not a poll |
| 861 | // stupid phpbb... make the way you store topics consistent for crying out loud |
| 862 | db_set_active('phpbb'); |
| 863 | if($obj->topic_vote == 0) { |
| 864 | $post_ids = db_query("SELECT post_id |
| 865 | FROM {phpbb_posts} |
| 866 | WHERE topic_id = %d |
| 867 | AND post_id <> $obj->topic_first_post_id |
| 868 | ORDER BY post_id", $obj->topic_id); |
| 869 | } else { |
| 870 | $post_ids = db_query("SELECT post_id |
| 871 | FROM {phpbb_posts} |
| 872 | WHERE topic_id = %d |
| 873 | ORDER BY post_id", $obj->topic_id); |
| 874 | } |
| 875 | |
| 876 | unset($obj); |
| 877 | |
| 878 | while($result = db_fetch_object($post_ids)) { |
| 879 | $loops++; |
| 880 | |
| 881 | db_set_active('phpbb'); |
| 882 | /*$query = db_query("SELECT * |
| 883 | FROM {phpbb_posts} p |
| 884 | INNER JOIN {phpbb_posts_text} pt ON p.post_id = pt.post_id |
| 885 | WHERE p.post_id = %d", $result->post_id); /**/ |
| 886 | |
| 887 | $query = db_query("SELECT * |
| 888 | FROM {phpbb_posts} p, {phpbb_posts_text} pt |
| 889 | WHERE p.post_id = pt.post_id |
| 890 | AND p.post_id = %d", $result->post_id); |
| 891 | |
| 892 | // make sure the post is valid |
| 893 | if(db_num_rows($query)) { |
| 894 | $post = db_fetch_object($query); |
| 895 | } else { |
| 896 | $errors++; |
| 897 | #print "<h3>Couldn't find post text for $result->post_id</h3>"; |
| 898 | #flush(); |
| 899 | continue; |
| 900 | } |
| 901 | |
| 902 | // skip if the post has already been imported |
| 903 | db_set_active('default'); |
| 904 | $count = db_result(db_query("SELECT COUNT(*) FROM {phpbb2drupal_temp_post} WHERE post_id = %d", $post->post_id)); |
| 905 | if($count > 0) { |
| 906 | $errors++; |
| 907 | //print "<h3>Error! $post->post_id was already inserted</h3>"; |
| 908 | //flush(); |
| 909 | db_set_active('phpbb'); |
| 910 | continue; |
| 911 | } |
| 912 | |
| 913 | db_set_active('default'); |
| 914 | $uid = db_result(db_query("SELECT uid FROM {phpbb2drupal_temp_user} WHERE user_id = %d", $post->poster_id)); |
| 915 | $nid = db_result(db_query("SELECT nid FROM {phpbb2drupal_temp_topic} WHERE topic_id = %d", $post->topic_id)); |
| 916 | $pid = db_result(db_query("SELECT MAX(pid) FROM {comments} WHERE nid = %d", $nid)); |
| 917 | |
| 918 | $pid = (is_null($pid)) ? 0 : $pid; |
| 919 | |
| 920 | if($post->poster_id == 2) { // is the admin |
| 921 | $uid = 1; |
| 922 | } elseif($post->poster_id == -1) { // anonymous |
| 923 | $uid = 0; |
| 924 | } |
| 925 | |
| 926 | $hostname = phpbb2drupal_decode_ip($post->poster_ip); |
| 927 | |
| 928 | // remove the :bbcode_uid from post_text |
| 929 | $post->post_text = preg_replace("/:$post->bbcode_uid/", '', $post->post_text); |
| 930 | |
| 931 | //construct the node |
| 932 | $comment = array( |
| 933 | 'pid' => $pid, |
| 934 | 'nid' => $nid, |
| 935 | 'uid' => $uid, |
| 936 | 'subject' => $post->post_subject, |
| 937 | 'comment' => $post->post_text, |
| 938 | 'hostname' => $hostname, |
| 939 | 'timestamp' => $post->post_time |
| 940 | ); |
| 941 | |
| 942 | // print "<pre>"; |
| 943 | // print_r($comment); |
| 944 | // print "</pre>"; |
| 945 | |
| 946 | db_set_active('default'); |
| 947 | $cid = phpbb2drupal_comment_save($comment); |
| 948 | |
| 949 | if(!$cid) { |
| 950 | $errors++; |
| 951 | #print "<h3>Failed importing $post->post_id</h3>"; |
| 952 | #flush(); |
| 953 | } |
| 954 | |
| 955 | // Handle attachments |
| 956 | if($PHPBB2DRUPAL_IMPORT_ATTACHMENTS) { |
| 957 | if($post->post_attachment == 1) { |
| 958 | db_set_active('default'); |
| 959 | $file_path = variable_get('file_directory_path', 'files'); |
| 960 | |
| 961 | db_set_active('phpbb'); |
| 962 | $files = db_query("SELECT * |
| 963 | FROM {phpbb_attachments} a |
| 964 | INNER JOIN {phpbb_attachments_desc} ad ON a.attach_id = ad.attach_id |
| 965 | WHERE a.post_id = %d", $post->post_id); |
| 966 | |
| 967 | while($file = db_fetch_object($files)) { |
| 968 | db_set_active('default'); |
| 969 | $fid = db_next_id('{files}_fid'); |
| 970 | db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize, list) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)", $fid, 0, $file->real_filename, "$file_path/$file->physical_filename", $file->mimetype, $file->filesize, 1); |
| 971 | db_query("INSERT INTO {comment_files} (cid, fid) VALUES (%d, %d)", $cid, $fid); |
| 972 | db_set_active('phpbb'); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | db_set_active('default'); |
| 978 | db_query("INSERT INTO {phpbb2drupal_temp_post} (post_id, cid) VALUES (%d, %d)", $post->post_id, $cid); |
| 979 | |
| 980 | db_set_active('phpbb'); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | // set the post import successful flag in the variable table |
| 985 | db_set_active('default'); |
| 986 | variable_set('phpbb2drupal_import_post_successful', '1'); |
| 987 | print "<h3>Successfully Imported $imported posts</h3>"; |
| 988 | print "<h3>The were $loops loops executed</h3>"; |
| 989 | print "<h3>There $errors errors while importing posts</h3>"; |
| 990 | //} |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * |
| 995 | * Clean UP |
| 996 | * |
| 997 | */ |
| 998 | function phpbb2drupal_import_cleanup() { |
| 999 | global $PHPBB2DRUPAL_IMPORT_ATTACHMENTS; |
| 1000 | |
| 1001 | # |
| 1002 | # Update Drupal sequence |
| 1003 | # |
| 1004 | db_set_active('default'); |
| 1005 | $term_data_tid = db_result(db_query("SELECT MAX(tid) FROM {term_data}")); |
| 1006 | $comments_cid = db_result(db_query("SELECT MAX(cid) FROM {comments}")); |
| 1007 | $node_nid = db_result(db_query("SELECT MAX(nid) FROM {node}")); |
| 1008 | $users_uid = db_result(db_query("SELECT MAX(uid) FROM {users}")); |
| 1009 | |
| 1010 | db_query("DELETE FROM {sequences} WHERE name='term_data_tid'"); |
| 1011 | db_query("DELETE FROM {sequences} WHERE name='comments_cid'"); |
| 1012 | db_query("DELETE FROM {sequences} WHERE name='node_nid'"); |
| 1013 | db_query("DELETE FROM {sequences} WHERE name='users_uid'"); |
| 1014 | |
| 1015 | db_query("INSERT INTO {sequences} (name,id) VALUE('term_data_tid', $term_data_tid)"); |
| 1016 | db_query("INSERT INTO {sequences} (name,id) VALUE('comments_cid', $comments_cid)"); |
| 1017 | db_query("INSERT INTO {sequences} (name,id) VALUE('node_nid', $node_nid)"); |
| 1018 | db_query("INSERT INTO {sequences} (name,id) VALUE('users_uid', $users_uid)"); |
| 1019 | |
| 1020 | if($PHPBB2DRUPAL_IMPORT_ATTACHMENTS) { |
| 1021 | $files_fid = db_result(db_query("SELECT MAX(fid) FROM {files}")); |
| 1022 | db_query("DELETE FROM {sequences} WHERE name='files_fid'"); |
| 1023 | db_query("INSERT INTO {sequences} (name,id) VALUE('files_fid', $files_fid)"); |
| 1024 | } |
| 1025 | |
| 1026 | #db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_user}"); |
| 1027 | #db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_forum}"); |
| 1028 | #db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_topic}"); |
| 1029 | #db_query("DROP TABLE IF EXISTS {phpbb2drupal_temp_post}"); |
| 1030 | |
| 1031 | variable_del('phpbb2drupal_import_user_successful'); |
| 1032 | variable_del('phpbb2drupal_import_user_started'); |
| 1033 | variable_del('phpbb2drupal_import_category_successful'); |
| 1034 | variable_del('phpbb2drupal_import_category_started'); |
| 1035 | variable_del('phpbb2drupal_import_topic_successful'); |
| 1036 | variable_del('phpbb2drupal_import_topic_started'); |
| 1037 | variable_del('phpbb2drupal_import_post_successful'); |
| 1038 | variable_del('phpbb2drupal_import_post_started'); |
| 1039 | |
| 1040 | db_query('DELETE FROM {cache}'); |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * |
| 1045 | * Helper Functions |
| 1046 | * |
| 1047 | */ |
| 1048 | function phpbb2drupal_user_save($array = array(), $category = array()) { |
| 1049 | // Dynamically compose a SQL query: |
| 1050 | $user_fields = user_fields(); |
| 1051 | |
| 1052 | //$array['created'] = time(); |
| 1053 | $array['changed'] = time(); |
| 1054 | $array['uid'] = db_next_id('{users}_uid'); |
| 1055 | |
| 1056 | // Note, we wait with saving the data column to prevent module-handled |
| 1057 | // fields from being saved there. We cannot invoke hook_user('insert') here |
| 1058 | // because we don't have a fully initialized user object yet. |
| 1059 | foreach ($array as $key => $value) { |
| 1060 | if ($key == 'pass') { |
| 1061 | $fields[] = db_escape_string($key); |
| 1062 | $values[] = $value; |
| 1063 | $s[] = "'%s'"; |
| 1064 | } |
| 1065 | else if (substr($key, 0, 4) !== 'auth') { |
| 1066 | if (in_array($key, $user_fields)) { |
| 1067 | $fields[] = db_escape_string($key); |
| 1068 | $values[] = $value; |
| 1069 | $s[] = "'%s'"; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values); |
| 1074 | |
| 1075 | // Reload user roles (delete just to be safe). |
| 1076 | db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']); |
| 1077 | foreach ($array['roles'] as $rid) { |
| 1078 | db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid); |
| 1079 | } |
| 1080 | |
| 1081 | // Build the initial user object. |
| 1082 | $user = user_load(array('uid' => $array['uid'])); |
| 1083 | |
| 1084 | db_set_active('default'); |
| 1085 | phpbb2drupal_profile_save_profile($array, $user, $category); // add user profile information |
| 1086 | |
| 1087 | // Build and save the serialized data field now |
| 1088 | $data = array(); |
| 1089 | foreach ($array as $key => $value) { |
| 1090 | if ((substr($key, 0, 4) !== 'auth') && (!in_array($key, $user_fields)) && ($value !== null)) { |
| 1091 | $data[$key] = $value; |
| 1092 | } |
| 1093 | } |
| 1094 | db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid); |
| 1095 | |
| 1096 | // Build the finished user object. |
| 1097 | $user = user_load(array('uid' => $array['uid'])); |
| 1098 | |
| 1099 | |
| 1100 | // Save distributed authentication mappings |
| 1101 | foreach ($array as $key => $value) { |
| 1102 | if (substr($key, 0, 4) == 'auth') { |
| 1103 | $authmaps[$key] = $value; |
| 1104 | } |
| 1105 | } |
| 1106 | if ($authmaps) { |
| 1107 | user_set_authmaps($user, $authmaps); |
| 1108 | } |
| 1109 | |
| 1110 | return $user; |
| 1111 | } |
| 1112 | |
| 1113 | function phpbb2drupal_profile_save_profile(&$edit, &$user, $category) { |
| 1114 | |
| 1115 | $result = db_query('SELECT fid, name, type, category, weight FROM {profile_fields} WHERE register = 1 ORDER BY category, weight'); |
| 1116 | |
| 1117 | while ($field = db_fetch_object($result)) { |
| 1118 | if (_profile_field_serialize($field->type)) { |
| 1119 | $edit[$field->name] = serialize($edit[$field->name]); |
| 1120 | } |
| 1121 | db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid); |
| 1122 | db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]); |
| 1123 | // Mark field as handled (prevents saving to user->data). |
| 1124 | $edit[$field->name] = null; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | function phpbb2drupal_comment_save($edit) { |
| 1129 | db_set_active('default'); |
| 1130 | // Here we are building the thread field. See the comment |
| 1131 | // in comment_render(). |
| 1132 | if ($edit['pid'] == 0) { |
| 1133 | // This is a comment with no parent comment (depth 0): we start |
| 1134 | // by retrieving the maximum thread level. |
| 1135 | $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid'])); |
| 1136 | |
| 1137 | // Strip the "/" from the end of the thread. |
| 1138 | $max = rtrim($max, '/'); |
| 1139 | |
| 1140 | // Next, we increase this value by one. Note that we can't |
| 1141 | // use 1, 2, 3, ... 9, 10, 11 because we order by string and |
| 1142 | // 10 would be right after 1. We use 1, 2, 3, ..., 9, 91, |
| 1143 | // 92, 93, ... instead. Ugly but fast. |
| 1144 | $decimals = (string) substr($max, 0, strlen($max) - 1); |
| 1145 | $units = substr($max, -1, 1); |
| 1146 | if ($units) { |
| 1147 | $units++; |
| 1148 | } |
| 1149 | else { |
| 1150 | $units = 1; |
| 1151 | } |
| 1152 | |
| 1153 | if ($units == 10) { |
| 1154 | $units = '90'; |
| 1155 | } |
| 1156 | |
| 1157 | // Finally, build the thread field for this new comment. |
| 1158 | $thread = $decimals . $units .'/'; |
| 1159 | } |
| 1160 | else { |
| 1161 | // This is comment with a parent comment: we increase |
| 1162 | // the part of the thread value at the proper depth. |
| 1163 | |
| 1164 | // Get the parent comment: |
| 1165 | $parent = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $edit['pid'])); |
| 1166 | |
| 1167 | // Strip the "/" from the end of the parent thread. |
| 1168 | $parent->thread = (string) rtrim((string) $parent->thread, '/'); |
| 1169 | |
| 1170 | // Get the max value in _this_ thread. |
| 1171 | $max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid'])); |
| 1172 | |
| 1173 | if ($max == '') { |
| 1174 | // First child of this parent. |
| 1175 | $thread = $parent->thread .'.1/'; |
| 1176 | } |
| 1177 | else { |
| 1178 | // Strip the "/" at the end of the thread. |
| 1179 | $max = rtrim($max, '/'); |
| 1180 | |
| 1181 | // We need to get the value at the correct depth. |
| 1182 | $parts = explode('.', $max); |
| 1183 | $parent_depth = count(explode('.', $parent->thread)); |
| 1184 | $last = $parts[$parent_depth]; |
| 1185 | |
| 1186 | // Next, we increase this value by one. Note that we can't |
| 1187 | // use 1, 2, 3, ... 9, 10, 11 because we order by string and |
| 1188 | // 10 would be right after 1. We use 1, 2, 3, ..., 9, 91, |
| 1189 | // 92, 93, ... instead. Ugly but fast. |
| 1190 | $decimals = (string)substr($last, 0, strlen($last) - 1); |
| 1191 | $units = substr($last, -1, 1); |
| 1192 | $units++; |
| 1193 | if ($units == 10) { |
| 1194 | $units = '90'; |
| 1195 | } |
| 1196 | |
| 1197 | // Finally, build the thread field for this new comment. |
| 1198 | $thread = $parent->thread .'.'. $decimals . $units .'/'; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | $edit['cid'] = db_next_id('{comments}_cid'); |
| 1203 | |
| 1204 | $status = 0; // 1 - not published, 0 - published |
| 1205 | $format = 1; // 1 - filtered, 2 - PHP, 3 FULL HTML |
| 1206 | $score = 0; // 0 default value, comments get higher score depending on the author's roles |
| 1207 | $users = serialize(array(0 => 1)); // default value for everybody!! |
| 1208 | |
| 1209 | db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit['cid'], $edit['nid'], $edit['pid'], $edit['uid'], $edit['subject'], $edit['comment'], $format, $edit['hostname'], $edit['timestamp'], $status, $score, $users, $thread, $edit['name'], $edit['mail'], $edit['homepage']); |
| 1210 | |
| 1211 | _comment_update_node_statistics($edit['nid']); |
| 1212 | |
| 1213 | return $edit['cid']; |
| 1214 | } |
| 1215 | |
| 1216 | // function for inserting polls into drupal |
| 1217 | function phpbb2_drupal_poll_insert($node) { |
| 1218 | if (!user_access('administer nodes')) { |
| 1219 | // Make sure all votes are 0 initially |
| 1220 | foreach ($node->choice as $i => $choice) { |
| 1221 | $node->choice[$i]['chvotes'] = 0; |
| 1222 | } |
| 1223 | $node->active = 1; |
| 1224 | } |
| 1225 | |
| 1226 | db_query("INSERT INTO {poll} (nid, runtime, polled, active) VALUES (%d, %d, '', %d)", $node->nid, $node->runtime, $node->active); |
| 1227 | |
| 1228 | foreach ($node->choice as $choice) { |
| 1229 | if ($choice['chtext'] != '') { |
| 1230 | db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | // PHPBB function for decoding the user ip |
| 1236 | function phpbb2drupal_decode_ip($int_ip) { |
| 1237 | $hexipbang = explode('.', chunk_split($int_ip, 2, '.')); |
| 1238 | return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]); |
| 1239 | } |
| 1240 | |
| 1241 | function phpbb2drupal_page_header($title) { |
| 1242 | $output = "<html><head><title>$title</title>"; |
| 1243 | $output .= <<<EOF |
| 1244 | <link rel="stylesheet" type="text/css" media="print" href="misc/print.css" /> |
| 1245 | <style type="text/css" title="layout" media="Screen"> |
| 1246 | @import url("misc/drupal.css"); |
| 1247 | </style> |
| 1248 | EOF; |
| 1249 | $output .= "</head><body>&q |