mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-05-02 00:44:47 -04:00
FIX: properly close topics in vBulletin importer
FEATURE: add backtrace when an exception happen (importers) FIX: post-processing should also happen on first posts (vBulletin importer) PERF: faster topic bypass when already imported
This commit is contained in:
parent
8ac955247c
commit
85cbb001ae
2 changed files with 57 additions and 11 deletions
script/import_scripts
|
@ -402,6 +402,7 @@ class ImportScripts::Base
|
||||||
skipped += 1
|
skipped += 1
|
||||||
puts "Exception while creating post #{import_id}. Skipping."
|
puts "Exception while creating post #{import_id}. Skipping."
|
||||||
puts e.message
|
puts e.message
|
||||||
|
puts e.backtrace.join("\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -52,6 +52,8 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
|
|
||||||
postprocess_posts
|
postprocess_posts
|
||||||
|
|
||||||
|
close_topics
|
||||||
|
|
||||||
puts "", "Done"
|
puts "", "Done"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -432,21 +434,29 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
def import_topics
|
def import_topics
|
||||||
puts "", "Importing topics..."
|
puts "", "Importing topics..."
|
||||||
|
|
||||||
|
# keep track of closed topics
|
||||||
|
@closed_topic_ids = []
|
||||||
|
|
||||||
# sort the topics
|
# sort the topics
|
||||||
@topics.sort_by! { |topic| topic[:threadid].to_i }
|
@topics.sort_by! { |topic| topic[:threadid].to_i }
|
||||||
|
|
||||||
create_posts(@topics) do |topic|
|
create_posts(@topics) do |topic|
|
||||||
|
id = "thread#" + topic[:threadid]
|
||||||
|
|
||||||
|
# store the list of closed topics
|
||||||
|
@closed_topic_ids << id if topic[:open] == "0"
|
||||||
|
|
||||||
|
next if post_id_from_imported_post_id(id)
|
||||||
next unless post = @posts.select { |p| p[:postid] == topic[:firstpostid] }.first
|
next unless post = @posts.select { |p| p[:postid] == topic[:firstpostid] }.first
|
||||||
|
|
||||||
t = {
|
t = {
|
||||||
id: "thread#" + topic[:threadid],
|
id: id,
|
||||||
user_id: user_id_from_imported_user_id(topic[:postuserid]) || Discourse::SYSTEM_USER_ID,
|
user_id: user_id_from_imported_user_id(topic[:postuserid]) || Discourse::SYSTEM_USER_ID,
|
||||||
title: CGI.unescapeHTML(topic[:title]).strip[0...255],
|
title: CGI.unescapeHTML(topic[:title]).strip[0...255],
|
||||||
category: category_from_imported_category_id(topic[:forumid]).try(:name),
|
category: category_from_imported_category_id(topic[:forumid]).try(:name),
|
||||||
raw: post[:raw],
|
raw: post[:raw],
|
||||||
created_at: Time.at(topic[:dateline].to_i),
|
created_at: Time.at(topic[:dateline].to_i),
|
||||||
visible: topic[:visible].to_i == 1,
|
visible: topic[:visible].to_i == 1,
|
||||||
closed: topic[:open].to_i == 0,
|
|
||||||
views: topic[:views].to_i,
|
views: topic[:views].to_i,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,12 +479,12 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
|
|
||||||
# reject all first posts
|
# reject all first posts
|
||||||
first_post_ids = Set.new(@topics.map { |t| t[:firstpostid] })
|
first_post_ids = Set.new(@topics.map { |t| t[:firstpostid] })
|
||||||
@posts.reject! { |post| first_post_ids.include?(post[:postid]) }
|
posts_to_import = @posts.reject { |post| first_post_ids.include?(post[:postid]) }
|
||||||
|
|
||||||
# sort the posts
|
# sort the posts
|
||||||
@posts.sort_by! { |post| post[:postid].to_i }
|
@posts.sort_by! { |post| post[:postid].to_i }
|
||||||
|
|
||||||
create_posts(@posts) do |post|
|
create_posts(posts_to_import) do |post|
|
||||||
next unless t = topic_lookup_from_imported_post_id("thread#" + post[:threadid])
|
next unless t = topic_lookup_from_imported_post_id("thread#" + post[:threadid])
|
||||||
|
|
||||||
p = {
|
p = {
|
||||||
|
@ -505,14 +515,29 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
max = @posts.size
|
max = @posts.size
|
||||||
|
|
||||||
@posts.each do |post|
|
@posts.each do |post|
|
||||||
new_raw = postprocess_post_raw(post[:raw])
|
begin
|
||||||
if new_raw != post[:raw]
|
new_raw = postprocess_post_raw(post[:raw])
|
||||||
p = Post.find(post_id_from_imported_post_id(post[:postid]))
|
|
||||||
p.raw = new_raw
|
if new_raw != post[:raw]
|
||||||
p.save
|
new_id = post_id_from_imported_post_id(post[:postid])
|
||||||
|
p = Post.find_by(id: new_id)
|
||||||
|
if p.nil?
|
||||||
|
puts "Could not save the post-processed raw of the post ##{new_id} (previous id: ##{post[:postid]})"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
p.raw = new_raw
|
||||||
|
p.save
|
||||||
|
end
|
||||||
|
rescue Exception => e
|
||||||
|
puts "", "-" * 100
|
||||||
|
puts e.message
|
||||||
|
puts e.backtrace.join("\n")
|
||||||
|
puts "-" * 100, ""
|
||||||
|
next
|
||||||
|
ensure
|
||||||
|
current += 1
|
||||||
|
print_status(current, max)
|
||||||
end
|
end
|
||||||
current += 1
|
|
||||||
print_status(current, max)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -583,6 +608,26 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
raw
|
raw
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def close_topics
|
||||||
|
puts "", "Closing topics..."
|
||||||
|
|
||||||
|
sql = <<-SQL
|
||||||
|
WITH closed_topic_ids AS (
|
||||||
|
SELECT t.id AS topic_id
|
||||||
|
FROM post_custom_fields pcf
|
||||||
|
JOIN posts p ON p.id = pcf.post_id
|
||||||
|
JOIN topics t ON t.id = p.topic_id
|
||||||
|
WHERE pcf.name = 'import_id'
|
||||||
|
AND pcf.value IN (?)
|
||||||
|
)
|
||||||
|
UPDATE topics
|
||||||
|
SET closed = true
|
||||||
|
WHERE id IN (SELECT topic_id FROM closed_topic_ids)
|
||||||
|
SQL
|
||||||
|
|
||||||
|
Topic.exec_sql(sql, @closed_topic_ids)
|
||||||
|
end
|
||||||
|
|
||||||
############################################################################
|
############################################################################
|
||||||
# OPTIONS #
|
# OPTIONS #
|
||||||
############################################################################
|
############################################################################
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue