Guest User

Untitled

a guest
Nov 3rd, 2017
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. # Import forum data from Drupal 6 to Discourse
  2. # Modified from the Drupal 7 script distributed with Discourse by Noah Lackstein
  3.  
  4. require "mysql2"
  5. require File.expand_path(File.dirname(__FILE__) + "/base.rb")
  6.  
  7. class ImportScripts::Drupal < ImportScripts::Base
  8.  
  9. DRUPAL_DB = ENV['DRUPAL_DB'] || "mct"
  10. VID = ENV['DRUPAL_VID'] || 1
  11. DB_PASS = ENV['DBPASS']
  12.  
  13. def initialize
  14. super
  15.  
  16. @client = Mysql2::Client.new(
  17. host: "192.168.200.112",
  18. username: "root",
  19. password: DB_PASS,
  20. database: DRUPAL_DB
  21. )
  22. end
  23.  
  24. def categories_query
  25. @client.query("SELECT tid, name, description FROM term_data WHERE vid = #{VID}")
  26. end
  27.  
  28. def execute
  29. # begin
  30. # create_admin(email: 'noah@lackstein.com', username: UserNameSuggester.suggest('lackstein'))
  31. # rescue => e
  32. # puts '', "Failed to create admin user"
  33. # puts e.message
  34. # end
  35.  
  36. create_users(@client.query("
  37. SELECT u.uid id,
  38. u.name username,
  39. u.mail email,
  40. u.created,
  41. p.value name
  42. FROM users u,
  43. profile_values p
  44. WHERE u.uid = p.uid
  45. AND p.fid = 1;")) do |row|
  46. {id: row['id'], username: row['username'], name: row['name'], email: row['email'], created_at: Time.zone.at(row['created'])}
  47. #{id: row['id'], username: row['username'], name: row['name'], email: "#{SecureRandom.uuid}@mailinator.com", created_at: Time.zone.at(row['created'])}
  48. end
  49.  
  50. # You'll need to edit the following query for your Drupal install:
  51. #
  52. # * Drupal allows duplicate category names, so you may need to exclude some categories or rename them here.
  53. # * Table name may be term_data.
  54. # * May need to select a vid other than 1.
  55. create_categories(categories_query) do |c|
  56. {id: c['tid'], name: c['name'], description: c['description']}
  57. end
  58.  
  59. create_forum_topics
  60.  
  61. create_replies
  62. end
  63.  
  64. def create_forum_topics
  65. puts '', "creating forum topics"
  66.  
  67. total_count = @client.query("
  68. SELECT COUNT(*) count
  69. FROM forum fi, node n
  70. WHERE n.type = 'forum'
  71. AND fi.nid = n.nid
  72. AND n.status = 1;").first['count']
  73.  
  74. batch_size = 1000
  75.  
  76. batches(batch_size) do |offset|
  77. results = @client.query("
  78. SELECT fi.nid nid,
  79. fi.tid tid,
  80. n.uid uid,
  81. n.title title,
  82. n.created created,
  83. n.sticky sticky,
  84. f.body body
  85. FROM forum fi,
  86. node n,
  87. node_revisions f
  88. WHERE n.type = 'forum'
  89. AND fi.nid = n.nid
  90. AND n.vid = f.vid
  91. AND n.status = 1
  92. LIMIT #{batch_size}
  93. OFFSET #{offset};
  94. ", cache_rows: false)
  95.  
  96. break if results.size < 1
  97.  
  98. create_posts(results, total: total_count, offset: offset) do |row|
  99. {
  100. id: "nid:#{row['nid']}",
  101. user_id: user_id_from_imported_user_id(row['uid']) || -1,
  102. category: category_id_from_imported_category_id(row['tid']),
  103. raw: row['body'],
  104. created_at: Time.zone.at(row['created']),
  105. pinned_at: row['sticky'].to_i == 1 ? Time.zone.at(row['created']) : nil,
  106. title: row['title'].try(:strip)
  107. }
  108. end
  109. end
  110. end
  111.  
  112. def create_replies
  113. puts '', "creating replies in topics"
  114.  
  115. total_count = @client.query("
  116. SELECT COUNT(*) count
  117. FROM comments c,
  118. node n
  119. WHERE n.nid = c.nid
  120. AND c.status = 0
  121. AND n.type = 'forum'
  122. AND n.status = 1;").first['count']
  123.  
  124. batch_size = 1000
  125.  
  126. batches(batch_size) do |offset|
  127. results = @client.query("
  128. SELECT c.cid, c.pid, c.nid, c.uid, c.timestamp created,
  129. c.comment body
  130. FROM comments c,
  131. node n
  132. WHERE n.nid = c.nid
  133. AND c.status = 0
  134. AND n.type = 'forum'
  135. AND n.status = 1
  136. LIMIT #{batch_size}
  137. OFFSET #{offset};
  138. ", cache_rows: false)
  139.  
  140. break if results.size < 1
  141.  
  142. create_posts(results, total: total_count, offset: offset) do |row|
  143. topic_mapping = topic_lookup_from_imported_post_id("nid:#{row['nid']}")
  144. if topic_mapping && topic_id = topic_mapping[:topic_id]
  145. h = {
  146. id: "cid:#{row['cid']}",
  147. topic_id: topic_id,
  148. user_id: user_id_from_imported_user_id(row['uid']) || -1,
  149. raw: row['body'],
  150. created_at: Time.zone.at(row['created']),
  151. }
  152. if row['pid']
  153. parent = topic_lookup_from_imported_post_id("cid:#{row['pid']}")
  154. h[:reply_to_post_number] = parent[:post_number] if parent and parent[:post_number] > 1
  155. end
  156. h
  157. else
  158. puts "No topic found for comment #{row['cid']}"
  159. nil
  160. end
  161. end
  162. end
  163. end
  164.  
  165. end
  166.  
  167. if __FILE__==$0
  168. ImportScripts::Drupal.new.perform
  169. end
Add Comment
Please, Sign In to add comment