PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/forum-merge/forum-merge.py

https://gitlab.com/wushin/evol-tools
Python | 240 lines | 98 code | 73 blank | 69 comment | 0 complexity | 34734c806b4ba2f5c623b0fbf9e86c7e MD5 | raw file
  1. #!/usr/bin/python
  2. import MySQLdb
  3. #written by rein of tmw/evol
  4. #credit to bruder torgen of phpbb.com. for his black magic sql statements.
  5. #https://www.phpbb.com/community/viewtopic.php?f=65&t=1917165
  6. #hope it runs smooth as it does for me
  7. # this script has little to no error checking
  8. # it is uggly!
  9. # presented as is , working in test environment,
  10. # ready for inspction and testing in production environment.
  11. #note.. did not fully implement privmsgs
  12. #but left code for it in included file
  13. #-----------preperation-------------------------------------------------------
  14. # backup both forum dbs (in the event of failure start fresh . do not attempt salvage or a second execution on damaged db)
  15. #
  16. # db1 source
  17. # db2 target
  18. # will yeild a forum with the settings of db2 with combined content of both
  19. #
  20. # from db1 backup tables:
  21. # phpbb_users,phpbb_topics,phpbb_forums,phpbb_posts,phpbb_privmsgs,phpbb_privmsgs_to,phpbb_attachments,phpbb_poll_votes,phpbb_poll_options
  22. #
  23. # edit the backup tables
  24. # find and "replace all" add a suffix of _2 (eg. phpbb_users_2,phpbb_topics_2 etc ) to the table names
  25. # import modified tables into db2
  26. #
  27. # db2 is now the working database for the merge
  28. #---------operation------------------------------------
  29. #fill in the nessicary db connection info then execute with python.
  30. #this will take some luck
  31. #after sucesfull execution .
  32. #caches will need to be clered
  33. #resynch and reset everything (post count users online cache active sessions etc)
  34. #and permissions will need to be setup or the forums will be invisable to admin and user alike
  35. #ignore the example values they are from bruder torgens forum entries .used as refference
  36. global maxuser #example 133
  37. global maxforum #example 34
  38. global maxright #example 68
  39. global maxattach
  40. global maxtopic #example 424
  41. global maxpost #example 9959
  42. global maxmsg #example 406
  43. print"rein: hello tmw/evol"
  44. # Open database connection - user pass database
  45. print "connecting"
  46. db = MySQLdb.connect("localhost","phpBB","pass500","phpBB" )
  47. cursor = db.cursor()
  48. #----------------users--------------------------------------------------------------------------------------
  49. #add duplicate user colum to users2
  50. cursor.execute("alter table phpbb_users_2 add user_id_old mediumint(8) ")
  51. #get usercount`
  52. cursor.execute("SELECT max( user_id ) FROM phpbb_users;")
  53. data = cursor.fetchone()
  54. maxuser=data
  55. print "maxuser : %s " % maxuser
  56. # remove the auto increment attribute and primary key index
  57. cursor.execute("ALTER TABLE phpbb_users_2 CHANGE `user_id` `user_id` MEDIUMINT( 8 ) UNSIGNED NOT NULL;")
  58. cursor.execute("alter table phpbb_users_2 drop primary key;")
  59. # update users in user2 with new incremented ids
  60. print "updating ids"
  61. cursor.execute("update phpbb_users_2 set user_id = user_id + %s;" % maxuser)
  62. cursor.execute("update phpbb_posts_2 set poster_id = poster_id + %s;" % maxuser)
  63. ##update related tables
  64. cursor.execute("update phpbb_topics_2 set topic_poster = topic_poster + %s;" % maxuser)
  65. cursor.execute("update phpbb_topics_2 set topic_last_poster_id = topic_last_poster_id +%s;" % maxuser)
  66. cursor.execute("update phpbb_forums_2 set forum_last_poster_id = forum_last_poster_id + %s;" % maxuser)
  67. cursor.execute("update phpbb_privmsgs_2 set author_id = author_id + %s;" % maxuser)
  68. cursor.execute("update phpbb_privmsgs_to_2 set user_id = user_id + %s;" % maxuser)
  69. cursor.execute("update phpbb_privmsgs_to_2 set author_id = author_id+ %s ;" % maxuser)
  70. cursor.execute("update phpbb_attachments_2 set poster_id = poster_id + %s;" % maxuser)
  71. cursor.execute("update phpbb_poll_votes_2 set vote_user_id = vote_user_id + %s;" % maxuser)
  72. #identify duplicates
  73. cursor.execute("UPDATE phpbb_users_2, phpbb_users SET phpbb_users_2.user_id_old = phpbb_users_2.user_id, phpbb_users_2.user_id = phpbb_users.user_id WHERE phpbb_users_2.username_clean LIKE phpbb_users.username_clean;")
  74. cursor.execute("update phpbb_posts_2, phpbb_users_2 set phpbb_posts_2.poster_id = phpbb_users_2.user_id where phpbb_posts_2.poster_id = phpbb_users_2.user_id_old;")
  75. cursor.execute("update phpbb_forums_2, phpbb_users_2 set phpbb_forums_2.forum_last_poster_id = phpbb_users_2.user_id where phpbb_forums_2.forum_last_poster_id = phpbb_users_2.user_id_old;")
  76. cursor.execute("update phpbb_topics_2, phpbb_users_2 set phpbb_topics_2.topic_poster = phpbb_users_2.user_id where phpbb_topics_2.topic_poster = phpbb_users_2.user_id_old;")
  77. cursor.execute("update phpbb_topics_2, phpbb_users_2 set phpbb_topics_2.topic_last_poster_id = phpbb_users_2.user_id where phpbb_topics_2.topic_last_poster_id = phpbb_users_2.user_id_old;")
  78. cursor.execute("update phpbb_privmsgs_2, phpbb_users_2 set phpbb_privmsgs_2.author_id = phpbb_users_2.user_id where phpbb_privmsgs_2.author_id = phpbb_users_2.user_id_old;")
  79. cursor.execute("update phpbb_privmsgs_to_2, phpbb_users_2 set phpbb_privmsgs_to_2.author_id = phpbb_users_2.user_id where phpbb_privmsgs_to_2.author_id = phpbb_users_2.user_id_old;")
  80. cursor.execute("update phpbb_privmsgs_to_2, phpbb_users_2 set phpbb_privmsgs_to_2.user_id = phpbb_users_2.user_id where phpbb_privmsgs_to_2.user_id = phpbb_users_2.user_id_old;")
  81. #deactivate added users . optionallllll and annoying dont enable lol
  82. #cursor.execute("update phpbb_users_2 set user_type = 1 where user_id_old is null;")
  83. #merge users
  84. print "merging users!"
  85. cursor.execute("INSERT INTO phpbb_users (user_id, user_type, group_id, user_permissions, user_perm_from, user_ip, user_regdate, username, username_clean, user_password, user_passchg, user_pass_convert, user_email, user_email_hash, user_birthday, user_lastvisit, user_lastmark, user_lastpost_time, user_lastpage, user_last_confirm_key, user_last_search, user_warnings, user_last_warning, user_login_attempts, user_inactive_reason, user_inactive_time, user_posts, user_lang, user_timezone, user_dst, user_dateformat, user_style, user_rank, user_colour, user_new_privmsg, user_unread_privmsg, user_last_privmsg, user_message_rules, user_full_folder, user_emailtime, user_topic_show_days, user_topic_sortby_type, user_topic_sortby_dir, user_post_show_days, user_post_sortby_type, user_post_sortby_dir, user_notify, user_notify_pm, user_notify_type, user_allow_pm, user_allow_viewonline, user_allow_viewemail, user_allow_massemail, user_options, user_avatar, user_avatar_type, user_avatar_width, user_avatar_height, user_sig, user_sig_bbcode_uid, user_sig_bbcode_bitfield, user_from, user_icq, user_aim, user_yim, user_msnm, user_jabber, user_website, user_occ, user_interests, user_actkey, user_newpasswd, user_form_salt) SELECT user_id, user_type, group_id, user_permissions, user_perm_from, user_ip, user_regdate, username, username_clean, user_password, user_passchg, user_pass_convert, user_email, user_email_hash, user_birthday, user_lastvisit, user_lastmark, user_lastpost_time, user_lastpage, user_last_confirm_key, user_last_search, user_warnings, user_last_warning, user_login_attempts, user_inactive_reason, user_inactive_time, user_posts, user_lang, user_timezone, user_dst, user_dateformat, user_style, user_rank, user_colour, user_new_privmsg, user_unread_privmsg, user_last_privmsg, user_message_rules, user_full_folder, user_emailtime, user_topic_show_days, user_topic_sortby_type, user_topic_sortby_dir, user_post_show_days, user_post_sortby_type, user_post_sortby_dir, user_notify, user_notify_pm, user_notify_type, user_allow_pm, user_allow_viewonline, user_allow_viewemail, user_allow_massemail, user_options, user_avatar, user_avatar_type, user_avatar_width, user_avatar_height, user_sig, user_sig_bbcode_uid, user_sig_bbcode_bitfield, user_from, user_icq, user_aim, user_yim, user_msnm, user_jabber, user_website, user_occ, user_interests, user_actkey, user_newpasswd, user_form_salt from phpbb_users_2 where user_id_old is null;")
  86. #---------------------------------privmsgs-----------------------------------------------------------------
  87. #see stored routine file for instruction
  88. #----------------forums-------------------------------------------------------------------------------------
  89. #get usercount`
  90. cursor.execute("SELECT max( forum_id ) FROM phpbb_forums;")
  91. data = cursor.fetchone()
  92. maxforum=data
  93. print "maxforum : %s " % maxforum
  94. cursor.execute("SELECT max( right_id ) FROM phpbb_forums;")
  95. data = cursor.fetchone()
  96. maxright=data
  97. print "maxright : %s " % maxright
  98. # remove the auto increment attribute and primary key index againnnn
  99. cursor.execute("ALTER TABLE phpbb_forums_2 CHANGE `forum_id` `forum_id` MEDIUMINT( 8 ) UNSIGNED NOT NULL;")
  100. cursor.execute("alter table phpbb_forums_2 drop primary key;")
  101. # update users in forums2 with new incremented ids
  102. print "updating ids, again"
  103. cursor.execute("update phpbb_forums_2 set forum_id = forum_id + %s;" % maxforum)
  104. cursor.execute("update phpbb_forums_2 set right_id = right_id + %s;" % maxright)
  105. cursor.execute("update phpbb_forums_2 set left_id = left_id + %s;" % maxright) #right left whats the diff . olol
  106. #^^^^^ but really dont mess with this little section or the forums will break hard .
  107. #things turning into subtopics of other things or outright dissapearing lolol
  108. #increase non parent ids
  109. cursor.execute("update phpbb_forums_2 set parent_id = parent_id + %s where parent_id <> 0;" % maxforum)
  110. #update related tables
  111. cursor.execute("update phpbb_topics_2 set forum_id = forum_id + %s;" % maxforum)
  112. cursor.execute("update phpbb_posts_2 set forum_id = forum_id + %s;" % maxforum)
  113. #----------------topics-------------------------------------------------------------------------------------
  114. cursor.execute("SELECT max( topic_id ) FROM phpbb_topics;")
  115. data = cursor.fetchone()
  116. maxtopic=data
  117. print "maxtopic : %s " % maxtopic
  118. # remove the auto increment attribute and primary key index againnnn
  119. cursor.execute("ALTER TABLE phpbb_topics_2 CHANGE `topic_id` `topic_id` MEDIUMINT( 8 ) UNSIGNED NOT NULL;")
  120. cursor.execute("alter table phpbb_topics_2 drop primary key;")
  121. #update related tables again
  122. cursor.execute("update phpbb_topics_2 set topic_id = topic_id + %s;" % maxtopic)
  123. cursor.execute("update phpbb_posts_2 set topic_id = topic_id + %s;" % maxtopic)
  124. cursor.execute("update phpbb_attachments_2 set topic_id = topic_id + %s;" % maxtopic)
  125. cursor.execute("update phpbb_poll_options_2 set topic_id = topic_id + %s;" % maxtopic)
  126. cursor.execute("update phpbb_poll_votes_2 set topic_id = topic_id + %s;" % maxtopic)
  127. #----------------posts_-------------------------------------------------------------------------------------
  128. cursor.execute("SELECT max( post_id ) FROM phpbb_posts;")
  129. data = cursor.fetchone()
  130. maxpost=data
  131. print "maxpost : %s " % maxpost
  132. # remove the auto increment attribute and primary key index yet again
  133. cursor.execute("ALTER TABLE phpbb_posts_2 CHANGE `post_id` `post_id` MEDIUMINT( 8 ) UNSIGNED NOT NULL;")
  134. cursor.execute("alter table phpbb_posts_2 drop primary key;")
  135. #update related tables yet again
  136. cursor.execute("update phpbb_posts_2 set post_id = post_id + %s;" % maxpost)
  137. cursor.execute("update phpbb_forums_2 set forum_last_post_id = forum_last_post_id + %s;" % maxpost)
  138. cursor.execute("update phpbb_topics_2 set topic_first_post_id = topic_first_post_id +%s;" % maxpost)
  139. cursor.execute("update phpbb_topics_2 set topic_last_post_id = topic_last_post_id + %s;" % maxpost)
  140. cursor.execute("update phpbb_attachments_2 set post_msg_id= post_msg_id + %s;" % maxpost)
  141. #----------------attachments_---------------------------------------------------------------------------------
  142. cursor.execute("SELECT max( attach_id ) FROM phpbb_attachments;")
  143. data = cursor.fetchone()
  144. maxattach=data
  145. print "maxattach : %s " % maxattach
  146. cursor.execute("update phpbb_attachments_2 set attach_id = attach_id + %s;" % maxattach)
  147. #cursor.execute("update phpbb_attachments_2 set post_msg_id= post_msg_id + %s;" % maxpost)
  148. #cursor.execute("update phpbb_attachments_2 set topic_id = topic_id + %s;" % maxtopic)
  149. #cursor.execute("update phpbb_attachments_2 set poster_id = poster_id + %s;" % maxuser)
  150. #------------------merge--------------------------------------------------------------------------------------
  151. print "time for the real deal!"
  152. print "merging forums!"
  153. cursor.execute("INSERT INTO phpbb_forums (forum_id, parent_id, left_id, right_id, forum_parents, forum_name, forum_desc, forum_desc_bitfield, forum_desc_options, forum_desc_uid, forum_link, forum_password, forum_style, forum_image, forum_rules, forum_rules_link, forum_rules_bitfield, forum_rules_options,forum_rules_uid, forum_topics_per_page, forum_type, forum_status, forum_posts, forum_topics, forum_topics_real, forum_last_post_id, forum_last_poster_id, forum_last_post_subject, forum_last_post_time, forum_last_poster_name, forum_last_poster_colour, forum_flags, forum_options, display_subforum_list, display_on_index, enable_indexing, enable_icons, enable_prune, prune_next, prune_days, prune_viewed, prune_freq) SELECT forum_id, parent_id, left_id, right_id, forum_parents, forum_name, forum_desc, forum_desc_bitfield, forum_desc_options, forum_desc_uid, forum_link, forum_password, forum_style, forum_image, forum_rules, forum_rules_link, forum_rules_bitfield, forum_rules_options, forum_rules_uid, forum_topics_per_page, forum_type, forum_status, forum_posts, forum_topics, forum_topics_real, forum_last_post_id, forum_last_poster_id, forum_last_post_subject, forum_last_post_time, forum_last_poster_name, forum_last_poster_colour, forum_flags, forum_options, display_subforum_list, display_on_index, enable_indexing, enable_icons, enable_prune, prune_next, prune_days, prune_viewed, prune_freq from phpbb_forums_2;")
  154. #bruder torgen said there wa ssome unspecified issue with a colum here he coudlnt rememebr
  155. print "merging topics!"
  156. cursor.execute("INSERT INTO phpbb_topics (topic_id, forum_id, icon_id, topic_attachment, topic_approved, topic_reported, topic_title, topic_poster, topic_time, topic_time_limit, topic_views, topic_replies, topic_replies_real, topic_status, topic_type, topic_first_post_id, topic_first_poster_name, topic_first_poster_colour, topic_last_post_id, topic_last_poster_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_subject, topic_last_post_time, topic_last_view_time, topic_moved_id, topic_bumped, topic_bumper, poll_title, poll_start, poll_length, poll_max_options, poll_last_vote, poll_vote_change) SELECT topic_id, forum_id, icon_id, topic_attachment, topic_approved, topic_reported, topic_title, topic_poster, topic_time, topic_time_limit, topic_views, topic_replies, topic_replies_real, topic_status, topic_type, topic_first_post_id, topic_first_poster_name, topic_first_poster_colour, topic_last_post_id, topic_last_poster_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_subject, topic_last_post_time, topic_last_view_time, topic_moved_id, topic_bumped, topic_bumper, poll_title, poll_start, poll_length, poll_max_options, poll_last_vote, poll_vote_change from phpbb_topics_2;")
  157. print "merging posts!"
  158. cursor.execute("INSERT INTO phpbb_posts (post_id, topic_id, forum_id, poster_id, icon_id, poster_ip, post_time, post_approved, post_reported, enable_bbcode, enable_smilies, enable_magic_url, enable_sig, post_username, post_subject, post_text, post_checksum, post_attachment, bbcode_bitfield, bbcode_uid, post_postcount, post_edit_time, post_edit_reason, post_edit_user, post_edit_count, post_edit_locked)SELECT post_id, topic_id, forum_id, poster_id, icon_id, poster_ip, post_time, post_approved, post_reported, enable_bbcode, enable_smilies, enable_magic_url, enable_sig, post_username, post_subject, post_text, post_checksum, post_attachment, bbcode_bitfield, bbcode_uid, post_postcount, post_edit_time,post_edit_reason, post_edit_user, post_edit_count, post_edit_locked from phpbb_posts_2;")
  159. print "merging attachments!"
  160. cursor.execute("INSERT INTO phpbb_attachments (attach_id,post_msg_id,topic_id,in_message,poster_id,is_orphan,physical_filename,real_filename,download_count,attach_comment,extension,mimetype,filesize,filetime,thumbnail) SELECT attach_id,post_msg_id,topic_id,in_message,poster_id,is_orphan,physical_filename,real_filename,download_count,attach_comment,extension,mimetype,filesize,filetime,thumbnail from phpbb_attachments_2;")
  161. print "merging polls!"
  162. cursor.execute("INSERT INTO phpbb_poll_options (poll_option_id,topic_id,poll_option_text,poll_option_total) SELECT poll_option_id,topic_id,poll_option_text,poll_option_total from phpbb_poll_options_2;")
  163. cursor.execute("INSERT INTO phpbb_poll_votes (topic_id,poll_option_id,vote_user_id,vote_user_ip) SELECT topic_id,poll_option_id,vote_user_id,vote_user_ip from phpbb_poll_votes_2;")
  164. #dont forget to commit
  165. db.commit()
  166. # disconnect
  167. db.close()