Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Importers should create duplicate categories if the parent category is different #3270

Merged
merged 1 commit into from Mar 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions script/import_scripts/base.rb
Expand Up @@ -47,7 +47,7 @@ def initialize

puts "loading existing categories..."
CategoryCustomField.where(name: 'import_id').pluck(:category_id, :value).each do |category_id, import_id|
@categories_lookup[import_id] = Category.find(category_id.to_i)
@categories_lookup[import_id] = category_id
end

puts "loading existing posts..."
Expand Down Expand Up @@ -156,7 +156,7 @@ def find_user_by_import_id(import_id)
end

# Get the Discourse Category id based on the id of the source category
def category_from_imported_category_id(import_id)
def category_id_from_imported_category_id(import_id)
@categories_lookup[import_id] || @categories_lookup[import_id.to_s]
end

Expand Down Expand Up @@ -330,7 +330,8 @@ def create_categories(results)
results.each do |c|
params = yield(c)

next if params.nil? # block returns nil to skip
# block returns nil to skip
next if params.nil? || category_id_from_imported_category_id(params[:id])

# Basic massaging on the category name
params[:name] = "Blank" if params[:name].blank?
Expand All @@ -347,13 +348,13 @@ def create_categories(results)
end

new_category = create_category(params, params[:id])
@categories_lookup[params[:id]] = new_category
@categories_lookup[params[:id]] = new_category.id
end
end

def create_category(opts, import_id)
existing = category_from_imported_category_id(import_id) || Category.where("LOWER(name) = ?", opts[:name].downcase).first
return existing if existing
existing = Category.where("LOWER(name) = ?", opts[:name].downcase).first
return existing if existing && existing.parent_category.try(:id) == opts[:parent_category_id]

post_create_action = opts.delete(:post_create_action)

Expand Down
2 changes: 1 addition & 1 deletion script/import_scripts/bbpress.rb
Expand Up @@ -87,7 +87,7 @@ def import_posts
mapped[:custom_fields] = {import_id: post["id"]}

if post["post_type"] == "topic"
mapped[:category] = category_from_imported_category_id(post["post_parent"]).try(:name)
mapped[:category] = category_id_from_imported_category_id(post["post_parent"])
mapped[:title] = CGI.unescapeHTML post["post_title"]
else
parent = topic_lookup_from_imported_post_id(post["post_parent"])
Expand Down
2 changes: 1 addition & 1 deletion script/import_scripts/bespoke_1.rb
Expand Up @@ -199,7 +199,7 @@ def import_post_batch!(posts, topics, offset, total)
topic = topics[post[:topic_id]]

unless topic[:post_id]
mapped[:category] = category_from_imported_category_id(topic[:category_id]).try(:name)
mapped[:category] = category_id_from_imported_category_id(topic[:category_id])
mapped[:title] = post[:title]
topic[:post_id] = post[:id]
else
Expand Down
5 changes: 2 additions & 3 deletions script/import_scripts/discuz_x.rb
Expand Up @@ -171,8 +171,7 @@ def import_categories
position: row['position'].to_i + max_position
}
if row['parent_id'].to_i > 0
parent = category_from_imported_category_id(row['parent_id'])
h[:parent_category_id] = parent.id if parent
h[:parent_category_id] = category_id_from_imported_category_id(row['parent_id'])
end
h
end
Expand Down Expand Up @@ -217,7 +216,7 @@ def import_posts
mapped[:created_at] = Time.zone.at(m['post_time'])

if m['is_first_post'] == 1
mapped[:category] = category_from_imported_category_id(m['category_id']).try(:name)
mapped[:category] = category_id_from_imported_category_id(m['category_id'])
mapped[:title] = CGI.unescapeHTML(m['title'])
@first_post_id_by_topic_id[m['topic_id']] = m['id']
else
Expand Down
2 changes: 1 addition & 1 deletion script/import_scripts/drupal.rb
Expand Up @@ -122,7 +122,7 @@ def create_forum_topics
{
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_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,
Expand Down
2 changes: 1 addition & 1 deletion script/import_scripts/drupal_qa.rb
Expand Up @@ -61,7 +61,7 @@ def create_forum_topics
{
id: "nid:#{row['nid']}",
user_id: user_id_from_imported_user_id(row['uid']) || -1,
category: category_from_imported_category_id((row['tid'] || '').split(',')[0]).try(:name),
category: category_id_from_imported_category_id((row['tid'] || '').split(',')[0]),
raw: row['body'],
created_at: Time.zone.at(row['created']),
pinned_at: nil,
Expand Down
5 changes: 2 additions & 3 deletions script/import_scripts/kunena.rb
Expand Up @@ -41,8 +41,7 @@ def execute
create_categories(@client.query("SELECT id, parent, name, description, ordering FROM jos_kunena_categories ORDER BY parent, id;")) do |c|
h = {id: c['id'], name: c['name'], description: c['description'], position: c['ordering'].to_i}
if c['parent'].to_i > 0
parent = category_from_imported_category_id(c['parent'])
h[:parent_category_id] = parent.id if parent
h[:parent_category_id] = category_id_from_imported_category_id(c['parent'])
end
h
end
Expand Down Expand Up @@ -121,7 +120,7 @@ def import_posts
mapped[:created_at] = Time.zone.at(m['time'])

if m['id'] == m['thread']
mapped[:category] = category_from_imported_category_id(m['catid']).try(:name)
mapped[:category] = category_id_from_imported_category_id(m['catid'])
mapped[:title] = m['subject']
else
parent = topic_lookup_from_imported_post_id(m['parent'])
Expand Down
5 changes: 2 additions & 3 deletions script/import_scripts/mybb.rb
Expand Up @@ -69,8 +69,7 @@ def import_categories
create_categories(results) do |row|
h = {id: row['id'], name: CGI.unescapeHTML(row['name']), description: CGI.unescapeHTML(row['description'])}
if row['parent_id'].to_i > 0
parent = category_from_imported_category_id(row['parent_id'])
h[:parent_category_id] = parent.id if parent
h[:parent_category_id] = category_id_from_imported_category_id(row['parent_id'])
end
h
end
Expand Down Expand Up @@ -125,7 +124,7 @@ def import_posts
mapped[:created_at] = Time.zone.at(m['post_time'])

if m['id'] == m['first_post_id']
mapped[:category] = category_from_imported_category_id(m['category_id']).try(:name)
mapped[:category] = category_id_from_imported_category_id(m['category_id'])
mapped[:title] = CGI.unescapeHTML(m['title'])
else
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
Expand Down
2 changes: 1 addition & 1 deletion script/import_scripts/ning.rb
Expand Up @@ -200,7 +200,7 @@ def import_topics(topics_json, default_category=nil)
mapped[:user_id] = user_id_from_imported_user_id(topic["contributorName"]) || -1
mapped[:created_at] = Time.zone.parse(topic["createdDate"])
unless topic["category"].nil? || topic["category"].downcase == "uncategorized"
mapped[:category] = category_from_imported_category_id(topic["category"]).try(:name)
mapped[:category] = category_id_from_imported_category_id(topic["category"])
end
if topic["category"].nil? && default_category
mapped[:category] = default_category
Expand Down
5 changes: 2 additions & 3 deletions script/import_scripts/phpbb3.rb
Expand Up @@ -114,8 +114,7 @@ def import_categories
create_categories(results) do |row|
h = {id: row['id'], name: CGI.unescapeHTML(row['name']), description: CGI.unescapeHTML(row['description'])}
if row['parent_id'].to_i > 0
parent = category_from_imported_category_id(row['parent_id'])
h[:parent_category_id] = parent.id if parent
h[:parent_category_id] = category_id_from_imported_category_id(row['parent_id'])
end
h
end
Expand Down Expand Up @@ -156,7 +155,7 @@ def import_posts
mapped[:created_at] = Time.zone.at(m['post_time'])

if m['id'] == m['first_post_id']
mapped[:category] = category_from_imported_category_id(m['category_id']).try(:name)
mapped[:category] = category_id_from_imported_category_id(m['category_id'])
mapped[:title] = CGI.unescapeHTML(m['title'])
else
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
Expand Down
4 changes: 2 additions & 2 deletions script/import_scripts/smf2.rb
Expand Up @@ -145,7 +145,7 @@ def import_categories
FROM {prefix}boards
ORDER BY id_parent ASC, id_board ASC
SQL
parent_id = category_from_imported_category_id(board[:id_parent]).id if board[:id_parent] > 0
parent_id = category_id_from_imported_category_id(board[:id_parent]) if board[:id_parent] > 0
groups = (board[:member_groups] || "").split(/,/).map(&:to_i)
restricted = !groups.include?(GUEST_GROUP) && !groups.include?(MEMBER_GROUP)
{
Expand Down Expand Up @@ -257,7 +257,7 @@ def guardian
end
}
if message[:id_msg] == message[:id_first_msg]
post[:category] = category_from_imported_category_id(message[:id_board]).try(:name)
post[:category] = category_id_from_imported_category_id(message[:id_board])
post[:title] = decode_entities(message[:subject])
else
parent = topic_lookup_from_imported_post_id(message[:id_first_msg])
Expand Down
5 changes: 2 additions & 3 deletions script/import_scripts/vanilla.rb
Expand Up @@ -144,8 +144,7 @@ def import_category(category)
description: clean_up(category[:description]),
}
if category[:parent_category_id] != "-1"
parent_category = category_from_imported_category_id(category[:parent_category_id])
c[:parent_category_id] = parent_category.id if parent_category
c[:parent_category_id] = category_id_from_imported_category_id(category[:parent_category_id])
end
c
end
Expand All @@ -162,7 +161,7 @@ def import_topics
id: "discussion#" + discussion[:discussion_id],
user_id: user_id_from_imported_user_id(discussion[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
title: discussion[:name],
category: category_from_imported_category_id(discussion[:category_id]).try(:name),
category: category_id_from_imported_category_id(discussion[:category_id]),
raw: clean_up(discussion[:body]),
created_at: parse_date(discussion[:date_inserted]),
}
Expand Down
4 changes: 2 additions & 2 deletions script/import_scripts/vbulletin.rb
Expand Up @@ -181,7 +181,7 @@ def import_categories
name: @htmlentities.decode(category["title"]).strip,
position: category["displayorder"],
description: @htmlentities.decode(category["description"]).strip,
parent_category_id: category_from_imported_category_id(category["parentid"]).try(:[], "id")
parent_category_id: category_id_from_imported_category_id(category["parentid"])
}
end
end
Expand Down Expand Up @@ -216,7 +216,7 @@ def import_topics
id: topic_id,
user_id: user_id_from_imported_user_id(topic["postuserid"]) || Discourse::SYSTEM_USER_ID,
title: @htmlentities.decode(topic["title"]).strip[0...255],
category: category_from_imported_category_id(topic["forumid"]).try(:name),
category: category_id_from_imported_category_id(topic["forumid"]),
raw: raw,
created_at: parse_timestamp(topic["dateline"]),
visible: topic["visible"].to_i == 1,
Expand Down
2 changes: 1 addition & 1 deletion script/import_scripts/vbulletin_old.rb
Expand Up @@ -460,7 +460,7 @@ def import_topics
id: id,
user_id: user_id_from_imported_user_id(topic[:postuserid]) || Discourse::SYSTEM_USER_ID,
title: CGI.unescapeHTML(topic[:title]).strip[0...255],
category: category_from_imported_category_id(topic[:forumid]).try(:name),
category: category_id_from_imported_category_id(topic[:forumid]),
raw: post[:raw],
created_at: Time.at(topic[:dateline].to_i),
visible: topic[:visible].to_i == 1,
Expand Down