From 28cbebe5ed23d004ebebc1d69e870e517402a5c7 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 2 Oct 2014 13:15:50 -0400 Subject: [PATCH] Better import for Drupal QA --- script/import_scripts/drupal.rb | 6 +++++- script/import_scripts/drupal_qa.rb | 27 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/script/import_scripts/drupal.rb b/script/import_scripts/drupal.rb index 9db4503dfe5..7194a9a0fb5 100644 --- a/script/import_scripts/drupal.rb +++ b/script/import_scripts/drupal.rb @@ -17,6 +17,10 @@ class ImportScripts::Drupal < ImportScripts::Base ) end + def categories_query + @client.query("SELECT tid, name, description FROM taxonomy_term_data WHERE vid = 1") + end + def execute create_users(@client.query("SELECT uid id, name, mail email, created FROM users;")) do |row| {id: row['id'], username: row['name'], email: row['email'], created_at: Time.zone.at(row['created'])} @@ -27,7 +31,7 @@ class ImportScripts::Drupal < ImportScripts::Base # * 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(@client.query("SELECT tid, name, description FROM taxonomy_term_data WHERE vid = 1;")) do |c| + create_categories(categories_query) do |c| {id: c['tid'], name: c['name'], description: c['description']} end diff --git a/script/import_scripts/drupal_qa.rb b/script/import_scripts/drupal_qa.rb index d86e912cab6..6542812c0af 100644 --- a/script/import_scripts/drupal_qa.rb +++ b/script/import_scripts/drupal_qa.rb @@ -5,7 +5,28 @@ require "mysql2" class ImportScripts::DrupalQA < ImportScripts::Drupal + def categories_query + result = @client.query("SELECT n.nid, GROUP_CONCAT(ti.tid) AS tids + FROM node AS n + INNER JOIN taxonomy_index AS ti ON ti.nid = n.nid + WHERE n.type = 'question' + AND n.status = 1 + GROUP BY n.nid") + + categories = {} + result.each do |r| + tids = r['tids'] + if tids.present? + tids = tids.split(',') + categories[tids[0].to_i] = true + end + end + + @client.query("SELECT tid, name, description FROM taxonomy_term_data WHERE tid IN (#{categories.keys.join(',')})") + end + def create_forum_topics + puts '', "creating forum topics" total_count = @client.query(" @@ -20,12 +41,12 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal results = @client.query(" SELECT n.nid, n.title, - MIN(t.field_question_tags_tid) AS tid, + GROUP_CONCAT(t.tid) AS tid, n.uid, n.created, f.body_value AS body FROM node AS n - LEFT OUTER JOIN field_data_field_question_tags AS t on t.entity_id = n.nid + LEFT OUTER JOIN taxonomy_index AS t on t.nid = n.nid INNER JOIN field_data_body AS f ON f.entity_id = n.nid WHERE n.type = 'question' AND n.status = 1 @@ -40,7 +61,7 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal { id: "nid:#{row['nid']}", user_id: user_id_from_imported_user_id(row['uid']) || -1, - category: category_from_imported_category_id(row['tid']).try(:name), + category: category_from_imported_category_id((row['tid'] || '').split(',')[0]).try(:name), raw: row['body'], created_at: Time.zone.at(row['created']), pinned_at: nil,