FIX: Importers should allow categories with existing name if the parent category is different
This changes the content of `@categories_lookup` from `Category` objects to IDs since the category names aren't needed anymore. The lookup method has been renamed too.
This commit is contained in:
parent
091af27a31
commit
b5426763e4
|
@ -47,7 +47,7 @@ class ImportScripts::Base
|
||||||
|
|
||||||
puts "loading existing categories..."
|
puts "loading existing categories..."
|
||||||
CategoryCustomField.where(name: 'import_id').pluck(:category_id, :value).each do |category_id, import_id|
|
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
|
end
|
||||||
|
|
||||||
puts "loading existing posts..."
|
puts "loading existing posts..."
|
||||||
|
@ -156,7 +156,7 @@ class ImportScripts::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get the Discourse Category id based on the id of the source category
|
# 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]
|
@categories_lookup[import_id] || @categories_lookup[import_id.to_s]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -330,7 +330,8 @@ class ImportScripts::Base
|
||||||
results.each do |c|
|
results.each do |c|
|
||||||
params = yield(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
|
# Basic massaging on the category name
|
||||||
params[:name] = "Blank" if params[:name].blank?
|
params[:name] = "Blank" if params[:name].blank?
|
||||||
|
@ -347,13 +348,13 @@ class ImportScripts::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
new_category = create_category(params, params[:id])
|
new_category = create_category(params, params[:id])
|
||||||
@categories_lookup[params[:id]] = new_category
|
@categories_lookup[params[:id]] = new_category.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_category(opts, import_id)
|
def create_category(opts, import_id)
|
||||||
existing = category_from_imported_category_id(import_id) || Category.where("LOWER(name) = ?", opts[:name].downcase).first
|
existing = Category.where("LOWER(name) = ?", opts[:name].downcase).first
|
||||||
return existing if existing
|
return existing if existing && existing.parent_category.try(:id) == opts[:parent_category_id]
|
||||||
|
|
||||||
post_create_action = opts.delete(:post_create_action)
|
post_create_action = opts.delete(:post_create_action)
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ class ImportScripts::Bbpress < ImportScripts::Base
|
||||||
mapped[:custom_fields] = {import_id: post["id"]}
|
mapped[:custom_fields] = {import_id: post["id"]}
|
||||||
|
|
||||||
if post["post_type"] == "topic"
|
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"]
|
mapped[:title] = CGI.unescapeHTML post["post_title"]
|
||||||
else
|
else
|
||||||
parent = topic_lookup_from_imported_post_id(post["post_parent"])
|
parent = topic_lookup_from_imported_post_id(post["post_parent"])
|
||||||
|
|
|
@ -199,7 +199,7 @@ class ImportScripts::Bespoke < ImportScripts::Base
|
||||||
topic = topics[post[:topic_id]]
|
topic = topics[post[:topic_id]]
|
||||||
|
|
||||||
unless topic[:post_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]
|
mapped[:title] = post[:title]
|
||||||
topic[:post_id] = post[:id]
|
topic[:post_id] = post[:id]
|
||||||
else
|
else
|
||||||
|
|
|
@ -171,8 +171,7 @@ class ImportScripts::DiscuzX < ImportScripts::Base
|
||||||
position: row['position'].to_i + max_position
|
position: row['position'].to_i + max_position
|
||||||
}
|
}
|
||||||
if row['parent_id'].to_i > 0
|
if row['parent_id'].to_i > 0
|
||||||
parent = category_from_imported_category_id(row['parent_id'])
|
h[:parent_category_id] = category_id_from_imported_category_id(row['parent_id'])
|
||||||
h[:parent_category_id] = parent.id if parent
|
|
||||||
end
|
end
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
@ -217,7 +216,7 @@ class ImportScripts::DiscuzX < ImportScripts::Base
|
||||||
mapped[:created_at] = Time.zone.at(m['post_time'])
|
mapped[:created_at] = Time.zone.at(m['post_time'])
|
||||||
|
|
||||||
if m['is_first_post'] == 1
|
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'])
|
mapped[:title] = CGI.unescapeHTML(m['title'])
|
||||||
@first_post_id_by_topic_id[m['topic_id']] = m['id']
|
@first_post_id_by_topic_id[m['topic_id']] = m['id']
|
||||||
else
|
else
|
||||||
|
|
|
@ -122,7 +122,7 @@ class ImportScripts::Drupal < ImportScripts::Base
|
||||||
{
|
{
|
||||||
id: "nid:#{row['nid']}",
|
id: "nid:#{row['nid']}",
|
||||||
user_id: user_id_from_imported_user_id(row['uid']) || -1,
|
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'],
|
raw: row['body'],
|
||||||
created_at: Time.zone.at(row['created']),
|
created_at: Time.zone.at(row['created']),
|
||||||
pinned_at: row['sticky'].to_i == 1 ? Time.zone.at(row['created']) : nil,
|
pinned_at: row['sticky'].to_i == 1 ? Time.zone.at(row['created']) : nil,
|
||||||
|
|
|
@ -61,7 +61,7 @@ class ImportScripts::DrupalQA < ImportScripts::Drupal
|
||||||
{
|
{
|
||||||
id: "nid:#{row['nid']}",
|
id: "nid:#{row['nid']}",
|
||||||
user_id: user_id_from_imported_user_id(row['uid']) || -1,
|
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'],
|
raw: row['body'],
|
||||||
created_at: Time.zone.at(row['created']),
|
created_at: Time.zone.at(row['created']),
|
||||||
pinned_at: nil,
|
pinned_at: nil,
|
||||||
|
|
|
@ -41,8 +41,7 @@ class ImportScripts::Kunena < ImportScripts::Base
|
||||||
create_categories(@client.query("SELECT id, parent, name, description, ordering FROM jos_kunena_categories ORDER BY parent, id;")) do |c|
|
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}
|
h = {id: c['id'], name: c['name'], description: c['description'], position: c['ordering'].to_i}
|
||||||
if c['parent'].to_i > 0
|
if c['parent'].to_i > 0
|
||||||
parent = category_from_imported_category_id(c['parent'])
|
h[:parent_category_id] = category_id_from_imported_category_id(c['parent'])
|
||||||
h[:parent_category_id] = parent.id if parent
|
|
||||||
end
|
end
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
@ -121,7 +120,7 @@ class ImportScripts::Kunena < ImportScripts::Base
|
||||||
mapped[:created_at] = Time.zone.at(m['time'])
|
mapped[:created_at] = Time.zone.at(m['time'])
|
||||||
|
|
||||||
if m['id'] == m['thread']
|
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']
|
mapped[:title] = m['subject']
|
||||||
else
|
else
|
||||||
parent = topic_lookup_from_imported_post_id(m['parent'])
|
parent = topic_lookup_from_imported_post_id(m['parent'])
|
||||||
|
|
|
@ -69,8 +69,7 @@ class ImportScripts::MyBB < ImportScripts::Base
|
||||||
create_categories(results) do |row|
|
create_categories(results) do |row|
|
||||||
h = {id: row['id'], name: CGI.unescapeHTML(row['name']), description: CGI.unescapeHTML(row['description'])}
|
h = {id: row['id'], name: CGI.unescapeHTML(row['name']), description: CGI.unescapeHTML(row['description'])}
|
||||||
if row['parent_id'].to_i > 0
|
if row['parent_id'].to_i > 0
|
||||||
parent = category_from_imported_category_id(row['parent_id'])
|
h[:parent_category_id] = category_id_from_imported_category_id(row['parent_id'])
|
||||||
h[:parent_category_id] = parent.id if parent
|
|
||||||
end
|
end
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
@ -125,7 +124,7 @@ class ImportScripts::MyBB < ImportScripts::Base
|
||||||
mapped[:created_at] = Time.zone.at(m['post_time'])
|
mapped[:created_at] = Time.zone.at(m['post_time'])
|
||||||
|
|
||||||
if m['id'] == m['first_post_id']
|
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'])
|
mapped[:title] = CGI.unescapeHTML(m['title'])
|
||||||
else
|
else
|
||||||
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
|
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
|
||||||
|
|
|
@ -200,7 +200,7 @@ class ImportScripts::Ning < ImportScripts::Base
|
||||||
mapped[:user_id] = user_id_from_imported_user_id(topic["contributorName"]) || -1
|
mapped[:user_id] = user_id_from_imported_user_id(topic["contributorName"]) || -1
|
||||||
mapped[:created_at] = Time.zone.parse(topic["createdDate"])
|
mapped[:created_at] = Time.zone.parse(topic["createdDate"])
|
||||||
unless topic["category"].nil? || topic["category"].downcase == "uncategorized"
|
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
|
end
|
||||||
if topic["category"].nil? && default_category
|
if topic["category"].nil? && default_category
|
||||||
mapped[:category] = default_category
|
mapped[:category] = default_category
|
||||||
|
|
|
@ -114,8 +114,7 @@ class ImportScripts::PhpBB3 < ImportScripts::Base
|
||||||
create_categories(results) do |row|
|
create_categories(results) do |row|
|
||||||
h = {id: row['id'], name: CGI.unescapeHTML(row['name']), description: CGI.unescapeHTML(row['description'])}
|
h = {id: row['id'], name: CGI.unescapeHTML(row['name']), description: CGI.unescapeHTML(row['description'])}
|
||||||
if row['parent_id'].to_i > 0
|
if row['parent_id'].to_i > 0
|
||||||
parent = category_from_imported_category_id(row['parent_id'])
|
h[:parent_category_id] = category_id_from_imported_category_id(row['parent_id'])
|
||||||
h[:parent_category_id] = parent.id if parent
|
|
||||||
end
|
end
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
@ -156,7 +155,7 @@ class ImportScripts::PhpBB3 < ImportScripts::Base
|
||||||
mapped[:created_at] = Time.zone.at(m['post_time'])
|
mapped[:created_at] = Time.zone.at(m['post_time'])
|
||||||
|
|
||||||
if m['id'] == m['first_post_id']
|
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'])
|
mapped[:title] = CGI.unescapeHTML(m['title'])
|
||||||
else
|
else
|
||||||
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
|
parent = topic_lookup_from_imported_post_id(m['first_post_id'])
|
||||||
|
|
|
@ -145,7 +145,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
FROM {prefix}boards
|
FROM {prefix}boards
|
||||||
ORDER BY id_parent ASC, id_board ASC
|
ORDER BY id_parent ASC, id_board ASC
|
||||||
SQL
|
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)
|
groups = (board[:member_groups] || "").split(/,/).map(&:to_i)
|
||||||
restricted = !groups.include?(GUEST_GROUP) && !groups.include?(MEMBER_GROUP)
|
restricted = !groups.include?(GUEST_GROUP) && !groups.include?(MEMBER_GROUP)
|
||||||
{
|
{
|
||||||
|
@ -257,7 +257,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
if message[:id_msg] == message[:id_first_msg]
|
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])
|
post[:title] = decode_entities(message[:subject])
|
||||||
else
|
else
|
||||||
parent = topic_lookup_from_imported_post_id(message[:id_first_msg])
|
parent = topic_lookup_from_imported_post_id(message[:id_first_msg])
|
||||||
|
|
|
@ -144,8 +144,7 @@ class ImportScripts::Vanilla < ImportScripts::Base
|
||||||
description: clean_up(category[:description]),
|
description: clean_up(category[:description]),
|
||||||
}
|
}
|
||||||
if category[:parent_category_id] != "-1"
|
if category[:parent_category_id] != "-1"
|
||||||
parent_category = category_from_imported_category_id(category[:parent_category_id])
|
c[:parent_category_id] = category_id_from_imported_category_id(category[:parent_category_id])
|
||||||
c[:parent_category_id] = parent_category.id if parent_category
|
|
||||||
end
|
end
|
||||||
c
|
c
|
||||||
end
|
end
|
||||||
|
@ -162,7 +161,7 @@ class ImportScripts::Vanilla < ImportScripts::Base
|
||||||
id: "discussion#" + discussion[:discussion_id],
|
id: "discussion#" + discussion[:discussion_id],
|
||||||
user_id: user_id_from_imported_user_id(discussion[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
user_id: user_id_from_imported_user_id(discussion[:insert_user_id]) || Discourse::SYSTEM_USER_ID,
|
||||||
title: discussion[:name],
|
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]),
|
raw: clean_up(discussion[:body]),
|
||||||
created_at: parse_date(discussion[:date_inserted]),
|
created_at: parse_date(discussion[:date_inserted]),
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,7 +181,7 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
name: @htmlentities.decode(category["title"]).strip,
|
name: @htmlentities.decode(category["title"]).strip,
|
||||||
position: category["displayorder"],
|
position: category["displayorder"],
|
||||||
description: @htmlentities.decode(category["description"]).strip,
|
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
|
||||||
end
|
end
|
||||||
|
@ -216,7 +216,7 @@ class ImportScripts::VBulletin < ImportScripts::Base
|
||||||
id: topic_id,
|
id: topic_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: @htmlentities.decode(topic["title"]).strip[0...255],
|
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,
|
raw: raw,
|
||||||
created_at: parse_timestamp(topic["dateline"]),
|
created_at: parse_timestamp(topic["dateline"]),
|
||||||
visible: topic["visible"].to_i == 1,
|
visible: topic["visible"].to_i == 1,
|
||||||
|
|
|
@ -460,7 +460,7 @@ class ImportScripts::VBulletinOld < ImportScripts::Base
|
||||||
id: id,
|
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_id_from_imported_category_id(topic[:forumid]),
|
||||||
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,
|
||||||
|
|
Loading…
Reference in New Issue