Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import forum data from Drupal 6 to Discourse
- # Modified from the Drupal 7 script distributed with Discourse by Noah Lackstein
- require "mysql2"
- require File.expand_path(File.dirname(__FILE__) + "/base.rb")
- class ImportScripts::Drupal < ImportScripts::Base
- DRUPAL_DB = ENV['DRUPAL_DB'] || "mct"
- VID = ENV['DRUPAL_VID'] || 1
- DB_PASS = ENV['DBPASS']
- def initialize
- super
- @client = Mysql2::Client.new(
- host: "192.168.200.112",
- username: "root",
- password: DB_PASS,
- database: DRUPAL_DB
- )
- end
- def categories_query
- @client.query("SELECT tid, name, description FROM term_data WHERE vid = #{VID}")
- end
- def execute
- # begin
- # create_admin(email: 'noah@lackstein.com', username: UserNameSuggester.suggest('lackstein'))
- # rescue => e
- # puts '', "Failed to create admin user"
- # puts e.message
- # end
- create_users(@client.query("
- SELECT u.uid id,
- u.name username,
- u.mail email,
- u.created,
- p.value name
- FROM users u,
- profile_values p
- WHERE u.uid = p.uid
- AND p.fid = 1;")) do |row|
- {id: row['id'], username: row['username'], name: row['name'], email: row['email'], created_at: Time.zone.at(row['created'])}
- #{id: row['id'], username: row['username'], name: row['name'], email: "#{SecureRandom.uuid}@mailinator.com", created_at: Time.zone.at(row['created'])}
- end
- # You'll need to edit the following query for your Drupal install:
- #
- # * Drupal allows duplicate category names, so you may need to exclude some categories or rename them here.
- # * Table name may be term_data.
- # * May need to select a vid other than 1.
- create_categories(categories_query) do |c|
- {id: c['tid'], name: c['name'], description: c['description']}
- end
- create_forum_topics
- create_replies
- end
- def create_forum_topics
- puts '', "creating forum topics"
- total_count = @client.query("
- SELECT COUNT(*) count
- FROM forum fi, node n
- WHERE n.type = 'forum'
- AND fi.nid = n.nid
- AND n.status = 1;").first['count']
- batch_size = 1000
- batches(batch_size) do |offset|
- results = @client.query("
- SELECT fi.nid nid,
- fi.tid tid,
- n.uid uid,
- n.title title,
- n.created created,
- n.sticky sticky,
- f.body body
- FROM forum fi,
- node n,
- node_revisions f
- WHERE n.type = 'forum'
- AND fi.nid = n.nid
- AND n.vid = f.vid
- AND n.status = 1
- LIMIT #{batch_size}
- OFFSET #{offset};
- ", cache_rows: false)
- break if results.size < 1
- create_posts(results, total: total_count, offset: offset) do |row|
- {
- id: "nid:#{row['nid']}",
- user_id: user_id_from_imported_user_id(row['uid']) || -1,
- category: category_id_from_imported_category_id(row['tid']),
- raw: row['body'],
- created_at: Time.zone.at(row['created']),
- pinned_at: row['sticky'].to_i == 1 ? Time.zone.at(row['created']) : nil,
- title: row['title'].try(:strip)
- }
- end
- end
- end
- def create_replies
- puts '', "creating replies in topics"
- total_count = @client.query("
- SELECT COUNT(*) count
- FROM comments c,
- node n
- WHERE n.nid = c.nid
- AND c.status = 0
- AND n.type = 'forum'
- AND n.status = 1;").first['count']
- batch_size = 1000
- batches(batch_size) do |offset|
- results = @client.query("
- SELECT c.cid, c.pid, c.nid, c.uid, c.timestamp created,
- c.comment body
- FROM comments c,
- node n
- WHERE n.nid = c.nid
- AND c.status = 0
- AND n.type = 'forum'
- AND n.status = 1
- LIMIT #{batch_size}
- OFFSET #{offset};
- ", cache_rows: false)
- break if results.size < 1
- create_posts(results, total: total_count, offset: offset) do |row|
- topic_mapping = topic_lookup_from_imported_post_id("nid:#{row['nid']}")
- if topic_mapping && topic_id = topic_mapping[:topic_id]
- h = {
- id: "cid:#{row['cid']}",
- topic_id: topic_id,
- user_id: user_id_from_imported_user_id(row['uid']) || -1,
- raw: row['body'],
- created_at: Time.zone.at(row['created']),
- }
- if row['pid']
- parent = topic_lookup_from_imported_post_id("cid:#{row['pid']}")
- h[:reply_to_post_number] = parent[:post_number] if parent and parent[:post_number] > 1
- end
- h
- else
- puts "No topic found for comment #{row['cid']}"
- nil
- end
- end
- end
- end
- end
- if __FILE__==$0
- ImportScripts::Drupal.new.perform
- end
Add Comment
Please, Sign In to add comment