mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
DEV: Apply syntax_tree formatting to script/*
This commit is contained in:
@ -5,41 +5,51 @@ require File.expand_path(File.dirname(__FILE__) + "/base.rb")
|
||||
require File.expand_path(File.dirname(__FILE__) + "/drupal.rb")
|
||||
|
||||
class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
|
||||
def categories_query
|
||||
result = @client.query("SELECT n.nid, GROUP_CONCAT(ti.tid) AS tids
|
||||
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")
|
||||
GROUP BY n.nid",
|
||||
)
|
||||
|
||||
categories = {}
|
||||
result.each do |r|
|
||||
tids = r['tids']
|
||||
tids = r["tids"]
|
||||
if tids.present?
|
||||
tids = tids.split(',')
|
||||
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(',')})")
|
||||
@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"
|
||||
|
||||
puts '', "creating forum topics"
|
||||
|
||||
total_count = @client.query("
|
||||
total_count =
|
||||
@client.query(
|
||||
"
|
||||
SELECT COUNT(*) count
|
||||
FROM node n
|
||||
WHERE n.type = 'question'
|
||||
AND n.status = 1;").first['count']
|
||||
AND n.status = 1;",
|
||||
).first[
|
||||
"count"
|
||||
]
|
||||
|
||||
batch_size = 1000
|
||||
|
||||
batches(batch_size) do |offset|
|
||||
results = @client.query("
|
||||
results =
|
||||
@client.query(
|
||||
"
|
||||
SELECT n.nid,
|
||||
n.title,
|
||||
GROUP_CONCAT(t.tid) AS tid,
|
||||
@ -54,40 +64,48 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
GROUP BY n.nid, n.title, n.uid, n.created, f.body_value
|
||||
LIMIT #{batch_size}
|
||||
OFFSET #{offset}
|
||||
", cache_rows: false)
|
||||
",
|
||||
cache_rows: false,
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :posts, results.map { |p| "nid:#{p['nid']}" }
|
||||
next if all_records_exist? :posts, results.map { |p| "nid:#{p["nid"]}" }
|
||||
|
||||
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'] || '').split(',')[0]),
|
||||
raw: row['body'],
|
||||
created_at: Time.zone.at(row['created']),
|
||||
id: "nid:#{row["nid"]}",
|
||||
user_id: user_id_from_imported_user_id(row["uid"]) || -1,
|
||||
category: category_id_from_imported_category_id((row["tid"] || "").split(",")[0]),
|
||||
raw: row["body"],
|
||||
created_at: Time.zone.at(row["created"]),
|
||||
pinned_at: nil,
|
||||
title: row['title'].try(:strip)
|
||||
title: row["title"].try(:strip),
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_direct_replies
|
||||
puts '', "creating replies in topics"
|
||||
puts "", "creating replies in topics"
|
||||
|
||||
total_count = @client.query("
|
||||
total_count =
|
||||
@client.query(
|
||||
"
|
||||
SELECT COUNT(*) count
|
||||
FROM node n
|
||||
WHERE n.type = 'answer'
|
||||
AND n.status = 1;").first['count']
|
||||
AND n.status = 1;",
|
||||
).first[
|
||||
"count"
|
||||
]
|
||||
|
||||
batch_size = 1000
|
||||
|
||||
batches(batch_size) do |offset|
|
||||
|
||||
results = @client.query("
|
||||
results =
|
||||
@client.query(
|
||||
"
|
||||
SELECT n.nid AS cid,
|
||||
q.field_answer_question_nid AS nid,
|
||||
n.uid,
|
||||
@ -100,25 +118,27 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
AND n.type = 'answer'
|
||||
LIMIT #{batch_size}
|
||||
OFFSET #{offset}
|
||||
", cache_rows: false)
|
||||
",
|
||||
cache_rows: false,
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :posts, results.map { |p| "cid:#{p['cid']}" }
|
||||
next if all_records_exist? :posts, results.map { |p| "cid:#{p["cid"]}" }
|
||||
|
||||
create_posts(results, total: total_count, offset: offset) do |row|
|
||||
topic_mapping = topic_lookup_from_imported_post_id("nid:#{row['nid']}")
|
||||
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']}",
|
||||
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']),
|
||||
user_id: user_id_from_imported_user_id(row["uid"]) || -1,
|
||||
raw: row["body"],
|
||||
created_at: Time.zone.at(row["created"]),
|
||||
}
|
||||
h
|
||||
else
|
||||
puts "No topic found for answer #{row['cid']}"
|
||||
puts "No topic found for answer #{row["cid"]}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
@ -126,21 +146,27 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
end
|
||||
|
||||
def create_nested_replies
|
||||
puts '', "creating nested replies to posts in topics"
|
||||
puts "", "creating nested replies to posts in topics"
|
||||
|
||||
total_count = @client.query("
|
||||
total_count =
|
||||
@client.query(
|
||||
"
|
||||
SELECT COUNT(c.cid) count
|
||||
FROM node n
|
||||
INNER JOIN comment AS c ON n.nid = c.nid
|
||||
WHERE n.type = 'question'
|
||||
AND n.status = 1;").first['count']
|
||||
AND n.status = 1;",
|
||||
).first[
|
||||
"count"
|
||||
]
|
||||
|
||||
batch_size = 1000
|
||||
|
||||
batches(batch_size) do |offset|
|
||||
|
||||
# WARNING: If there are more than 1000000 this might have to be revisited
|
||||
results = @client.query("
|
||||
results =
|
||||
@client.query(
|
||||
"
|
||||
SELECT (c.cid + 1000000) as cid,
|
||||
c.nid,
|
||||
c.uid,
|
||||
@ -153,45 +179,53 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
AND n.type = 'question'
|
||||
LIMIT #{batch_size}
|
||||
OFFSET #{offset}
|
||||
", cache_rows: false)
|
||||
",
|
||||
cache_rows: false,
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :posts, results.map { |p| "cid:#{p['cid']}" }
|
||||
next if all_records_exist? :posts, results.map { |p| "cid:#{p["cid"]}" }
|
||||
|
||||
create_posts(results, total: total_count, offset: offset) do |row|
|
||||
topic_mapping = topic_lookup_from_imported_post_id("nid:#{row['nid']}")
|
||||
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']}",
|
||||
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']),
|
||||
user_id: user_id_from_imported_user_id(row["uid"]) || -1,
|
||||
raw: row["body"],
|
||||
created_at: Time.zone.at(row["created"]),
|
||||
}
|
||||
h
|
||||
else
|
||||
puts "No topic found for comment #{row['cid']}"
|
||||
puts "No topic found for comment #{row["cid"]}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts '', "creating nested replies to answers in topics"
|
||||
puts "", "creating nested replies to answers in topics"
|
||||
|
||||
total_count = @client.query("
|
||||
total_count =
|
||||
@client.query(
|
||||
"
|
||||
SELECT COUNT(c.cid) count
|
||||
FROM node n
|
||||
INNER JOIN comment AS c ON n.nid = c.nid
|
||||
WHERE n.type = 'answer'
|
||||
AND n.status = 1;").first['count']
|
||||
AND n.status = 1;",
|
||||
).first[
|
||||
"count"
|
||||
]
|
||||
|
||||
batch_size = 1000
|
||||
|
||||
batches(batch_size) do |offset|
|
||||
|
||||
# WARNING: If there are more than 1000000 this might have to be revisited
|
||||
results = @client.query("
|
||||
results =
|
||||
@client.query(
|
||||
"
|
||||
SELECT (c.cid + 1000000) as cid,
|
||||
q.field_answer_question_nid AS nid,
|
||||
c.uid,
|
||||
@ -205,25 +239,27 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
AND n.type = 'answer'
|
||||
LIMIT #{batch_size}
|
||||
OFFSET #{offset}
|
||||
", cache_rows: false)
|
||||
",
|
||||
cache_rows: false,
|
||||
)
|
||||
|
||||
break if results.size < 1
|
||||
|
||||
next if all_records_exist? :posts, results.map { |p| "cid:#{p['cid']}" }
|
||||
next if all_records_exist? :posts, results.map { |p| "cid:#{p["cid"]}" }
|
||||
|
||||
create_posts(results, total: total_count, offset: offset) do |row|
|
||||
topic_mapping = topic_lookup_from_imported_post_id("nid:#{row['nid']}")
|
||||
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']}",
|
||||
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']),
|
||||
user_id: user_id_from_imported_user_id(row["uid"]) || -1,
|
||||
raw: row["body"],
|
||||
created_at: Time.zone.at(row["created"]),
|
||||
}
|
||||
h
|
||||
else
|
||||
puts "No topic found for comment #{row['cid']}"
|
||||
puts "No topic found for comment #{row["cid"]}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
@ -234,9 +270,6 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||
create_direct_replies
|
||||
create_nested_replies
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
ImportScripts::DrupalQA.new.perform
|
||||
end
|
||||
ImportScripts::DrupalQA.new.perform if __FILE__ == $0
|
||||
|
Reference in New Issue
Block a user