#
# Written by Feodor (feodor [at] mundo.ru)
#
# Modified for drupal 4.5.2 and phpbb 2.0.11-2.0.13
# by Alexander Mikhailian <mikhailian@altern.org>
#
# This script makes an assumption that phpbb and drupal tables are kept in
# one and the same database. Phpbb tables are expected to have the prefix
# phpbb_ and drupal tables are expected to have the prefix drupal_
#
# If phpBB forum use CP1251 (or another) encoding, the tables must be converted
# into UTF8. If version of MySQL less then 4.1 "iconv" command can be used for
# convertion of exported tables into UTF8.
#
# Example:
# iconv -fcp1251 -tutf8 < phpbb2.sql > phpbb2_utf-8.sql
#
# Here is a list of phpbb tables used by script for import into Drupal:
#
# <*> phpbb_categories
# <*> phpbb_forums
# <*> phpbb_posts
# <*> phpbb_posts_text
# <*> phpbb_users
# <*> phpbb_vote_desc
# <*> phpbb_vote_results
#
# You should probably edit the two variables below to match your result:

#
# The name of the forums taxanomy as it appears on the site
#
SELECT @forum_title:='Forums';

#
# Start importing users from this id. Do not forget that uid 1 is always 
# the Administrator in Drupal. Depending on whether you already created
# the Administrator user in Drupal or not, you may want to change this
# variable into 2.
#
SELECT @first_phpbb_user_id:=1; # uid 1 is always an administrator in Drupal

#
# Drupal variables for node counts
#
SELECT @phpbb_terms:=MAX(forum_id) FROM phpbb_forums;
SELECT @phpbb_cat:=MAX(cat_id) FROM phpbb_categories;
SELECT @drupal_term_data_tid:=id FROM drupal_sequences WHERE name = 'drupal_term_data_tid';
  SELECT IF( @drupal_term_data_tid>0,  @drupal_term_data_tid, @drupal_term_data_tid:=0);
SELECT @drupal_comments_cid:=id FROM drupal_sequences WHERE name = 'drupal_comments_cid';
  SELECT IF( @drupal_comments_cid>0,  @drupal_comments_cid, @drupal_comments_cid:=0);
SELECT @drupal_vocabulary_vid:=id FROM drupal_sequences WHERE name = 'drupal_vocabulary_vid';
  SELECT IF( @drupal_vocabulary_vid>0,  @drupal_vocabulary_vid, @drupal_vocabulary_vid:=0);
SELECT @drupal_node_nid:=id FROM drupal_sequences WHERE name = 'drupal_node_nid';
  SELECT IF( @drupal_node_nid>0,  @drupal_node_nid, @drupal_node_nid:=0);

#
# Import user forms phpbb_users
#
INSERT INTO drupal_users (uid,name,pass,mail,mode,sort,threshold,theme,signature,created,changed,status,timezone,language,picture,init,data)
  SELECT  user_id,username,user_password,user_email,0,0,0,'',user_sig,user_regdate,user_lastvisit,1,0,'',user_avatar,user_email,'a:1:{s:5:"roles";a:1:{i:0;s:1:"2";}}'
  FROM phpbb_users
  WHERE user_id>=@first_phpbb_user_id;

#
# Create a vocabulary (called "Forum")
#
DELETE FROM drupal_vocabulary WHERE nodes='forum';
INSERT INTO drupal_vocabulary (vid,name,description,help,relations,hierarchy,multiple,required,nodes,weight)
  SELECT @drupal_vocabulary_vid+1,@forum_title,'','','0','2','0','1','forum','0';

#
# Set up the vocabulary "Forum" as the forum vocabulary
#
DELETE FROM drupal_variable WHERE name = 'forum_nav_vocabulary';
INSERT INTO drupal_variable (name, value) VALUES ('forum_nav_vocabulary', CONCAT('s:1:\"',@drupal_vocabulary_vid+1,'\";'));

#
# The default comment order in phpbb2 is "oldest first"
#
DELETE FROM drupal_variable WHERE name = 'comment_default_order';
INSERT INTO drupal_variable (name, value) VALUES ('comment_default_order', 's:1:\"2\";');

#
# Import categories from phpbb_categories as terms
#
INSERT INTO drupal_term_data (tid,vid,name,weight)
  SELECT @drupal_term_data_tid+cat_id,@drupal_vocabulary_vid+1,cat_title,cat_order
  FROM phpbb_categories;

#
# Import terms from phpbb_forums
#  
INSERT INTO drupal_term_data (tid,vid,name,description,weight)
  SELECT @drupal_term_data_tid+@phpbb_cat+forum_id,@drupal_vocabulary_vid+1, forum_name,forum_desc,forum_order
  FROM phpbb_forums;

ALTER TABLE drupal_term_data ORDER BY tid;

#
# Import forum hierarchy 
# Drupal allows topics to be created at this level while phpbb2 does not. If you want to disallow topics below
# categories, mark them as containers in the forum configuration dialog
#
INSERT INTO drupal_term_hierarchy (tid,parent)
  SELECT @drupal_term_data_tid+cat_id,'0'
  FROM phpbb_categories;

INSERT INTO drupal_term_hierarchy (tid,parent)
  SELECT @drupal_term_data_tid+@phpbb_cat+forum_id, @drupal_term_data_tid+cat_id
  FROM phpbb_forums;

ALTER TABLE drupal_term_hierarchy ORDER BY tid;

#
# Create temporary tables for sorting topics and comments.
#

DROP TABLE IF EXISTS temp_posts;
CREATE TABLE temp_posts (
   post_id mediumint(8) UNSIGNED NOT NULL auto_increment,
   topic_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
   forum_id smallint(5) UNSIGNED DEFAULT '0' NOT NULL,
   poster_id mediumint(8) DEFAULT '0' NOT NULL,
   post_time int(11) DEFAULT '0' NOT NULL,
   post_edit_time int(11),
   post_subject char(512),
   post_text text,
   PRIMARY KEY (post_id),
   KEY forum_id (forum_id),
   KEY topic_id (topic_id),
   KEY poster_id (poster_id),
   KEY post_time (post_time)
);
DROP TABLE IF EXISTS temp_node;
CREATE TABLE temp_node (
   post_id mediumint(8) UNSIGNED NOT NULL auto_increment,
   topic_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
   PRIMARY KEY (post_id),
   KEY topic_id (topic_id)
);

#
# Copy into temporary table topics without comments
#
INSERT INTO temp_node (post_id,topic_id)
  SELECT MIN(post_id), topic_id
  FROM phpbb_posts
  GROUP BY topic_id;
INSERT INTO temp_posts (post_id, topic_id,forum_id,poster_id, post_time,post_edit_time,post_subject,post_text)
  SELECT c.post_id, c.topic_id, a.forum_id, IF(a.poster_id='-1','0',a.poster_id), a.post_time, a.post_edit_time,
  REPLACE(b.post_subject, CONCAT(':',b.bbcode_uid),''), REPLACE(b.post_text, CONCAT(':',b.bbcode_uid),'')
  FROM phpbb_posts AS a, phpbb_posts_text AS b, temp_node AS c
  WHERE c.post_id=a.post_id AND c.post_id=b.post_id;

#
# Insert nid and tid from temp_posts into drupal_term_node
#
INSERT INTO drupal_term_node (nid,tid)
  SELECT @drupal_node_nid+topic_id,@drupal_term_data_tid+@phpbb_cat+forum_id
  FROM temp_posts;

ALTER TABLE drupal_term_node ORDER BY nid;

#
# Insert forum topics from temp_posts into drupal_node
#
INSERT INTO drupal_node (nid,type,title,uid,created,comment,body,changed)
  SELECT @drupal_node_nid+topic_id,'forum',post_subject,poster_id,post_time,'2',post_text,
  IF(post_edit_time<>'NULL',post_edit_time,post_time)
  FROM temp_posts;

ALTER TABLE drupal_node ORDER BY nid;

#
# Insert nid into drupal_forum 
#
DELETE FROM drupal_forum;
INSERT INTO drupal_forum (nid,tid)
  SELECT @drupal_node_nid+topic_id,@drupal_term_data_tid+@phpbb_cat+forum_id
  FROM temp_posts;

#
# Insert comments into drupal_comments for topics from temp_posts
#
INSERT INTO drupal_comments (nid,uid,subject,comment,hostname,timestamp,users)
  SELECT @drupal_node_nid+a.topic_id,
  CASE WHEN a.poster_id='-1' THEN '0' ELSE a.poster_id END,
  REPLACE(c.post_subject, CONCAT(':',c.bbcode_uid),''),
  REPLACE(c.post_text, CONCAT(':',c.bbcode_uid),''),
  CONCAT_WS('.',CONV(SUBSTRING(a.poster_ip,1,2),16,10),
    CONV(SUBSTRING(a.poster_ip,3,2),16,10),
    CONV(SUBSTRING(a.poster_ip,5,2),16,10),
    CONV(SUBSTRING(a.poster_ip,7,2),16,10)),
  a.post_time,'a:1:{i:0;i:0;}'
  FROM phpbb_posts AS a LEFT JOIN temp_posts AS b ON a.post_id=b.post_id,phpbb_posts_text AS c
  WHERE b.post_id IS NULL AND a.post_id=c.post_id;
ALTER TABLE drupal_comments ORDER BY cid;

UPDATE drupal_comments,drupal_node
  SET drupal_comments.subject=IF(drupal_comments.subject='',
    CONCAT('Re:',drupal_node.title),drupal_comments.subject)
  WHERE drupal_comments.nid=drupal_node.nid;

#
# Update thread in drupal_comments
#
DROP TABLE IF EXISTS drupal_comments_tmp;
CREATE TABLE drupal_comments_tmp (
   cid int(10) NOT NULL default '0',
    nid int(10) NOT NULL default '0',
    thread int(10) NOT NULL  auto_increment,
    PRIMARY KEY  (nid,thread)
    );

INSERT INTO drupal_comments_tmp (cid,nid)
  SELECT cid,nid
  FROM drupal_comments
  WHERE cid>@drupal_comments_cid;

UPDATE drupal_comments,drupal_comments_tmp
  SET drupal_comments.thread=CONCAT(CONCAT(REPEAT(9,
    LEFT(drupal_comments_tmp.thread,LENGTH(drupal_comments_tmp.thread)-1)),
    RIGHT(drupal_comments_tmp.thread,1)),'/')
  WHERE drupal_comments.cid=drupal_comments_tmp.cid;

#
# Update drupal_history
#
#INSERT INTO drupal_history (uid, nid, timestamp)
#  SELECT a.uid, b.nid, a.timestamp
#  FROM drupal_users AS a, drupal_node AS b
#  WHERE a.uid>0 AND b.nid>@drupal_node_nid;

#
# update topic statistics 
#

INSERT INTO drupal_node_comment_statistics
  (nid,last_comment_timestamp,last_comment_name,last_comment_uid,comment_count)
  SELECT @drupal_node_nid+pt.topic_id, pp.post_time, ppt.post_subject, pp.poster_id,
    pt.topic_replies
  FROM drupal_node dn, phpbb_topics pt, phpbb_posts pp, phpbb_posts_text ppt
  WHERE dn.nid = @drupal_node_nid+pt.topic_id
    AND pt.topic_last_post_id = pp.post_id
    AND pp.post_id = ppt.post_id;

#
# Delete all temp tables
#
DROP TABLE IF EXISTS temp_posts;
DROP TABLE IF EXISTS drupal_comments_tmp;
DROP TABLE IF EXISTS temp_node;

#
# Update Drupal variables 
#
SELECT @drupal_term_data_tid:=MAX(tid) FROM drupal_term_data;
SELECT @drupal_comments_cid:=MAX(cid) FROM drupal_comments;
SELECT @drupal_node_nid:=MAX(nid) FROM drupal_node WHERE type = 'forum';
SELECT @drupal_users_uid:=MAX(uid) FROM drupal_users;

DELETE FROM drupal_sequences WHERE name="drupal_term_data_tid";
DELETE FROM drupal_sequences WHERE name="drupal_comments_cid";
DELETE FROM drupal_sequences WHERE name="drupal_node_nid";
DELETE FROM drupal_sequences WHERE name="drupal_users_uid";

INSERT INTO drupal_sequences (name,id) SELECT "drupal_term_data_tid", @drupal_term_data_tid;
INSERT INTO drupal_sequences (name,id) SELECT "drupal_comments_cid",@drupal_comments_cid;
INSERT INTO drupal_sequences (name,id) SELECT "drupal_node_nid",@drupal_node_nid;
INSERT INTO drupal_sequences (name,id) SELECT "drupal_users_uid",@drupal_users_uid;

#
# this is for porting personal messages to privatemsg drupal module
#
# phpbb_privmsgs.privmsgs_type has the following codes:
# written=1, seen in inbox = 5, read = 0, saved = 3
#
# attention, drupal_privatemsg limits the title to 64 chars, this is too 
# little
#
INSERT INTO drupal_privatemsg ( id, author, recipient,subject,message,
  timestamp,new,hostname,folder,author_del,recipient_del )
  SELECT p.privmsgs_id, p.privmsgs_from_userid, p.privmsgs_to_userid,
    p.privmsgs_subject,pt.privmsgs_text,p.privmsgs_date,
    IF(p.privmsgs_type = 1 OR p.privmsgs_type = 5, 1, 0),
    p.privmsgs_ip,0,0,0
    FROM phpbb_privmsgs p
    LEFT JOIN (phpbb_privmsgs_text pt) ON p.privmsgs_id=pt.privmsgs_text_id;

#
# converting polls
#

INSERT INTO poll(nid,runtime,polled,active)
  SELECT pvd.topic_id+41,0,'',0
  FROM phpbb_vote_desc pvd;

INSERT INTO poll_choices(nid,chtext,chvotes,chorder)
  SELECT pvd.topic_id+41,
         pvr.vote_option_text,
         pvr.vote_result,
         vote_option_id # TODO chorder should be autoincremented from 1 every 
           # unique pvd.topic_id;
  FROM phpbb_vote_results pvr, phpbb_vote_desc pvd
  WHERE pvr.vote_id=pvd.vote_id;

UPDATE node n, phpbb_vote_desc pvd SET type='poll'
  WHERE n.nid=pvd.topic_id+41;