こんにちは、
muut のインポートスクリプトを実行した際に、以下のエラーが発生しています。
カテゴリをインポート中
2 / 2 (100.0%) [5170854 項目/分]
トピックをインポート中
Traceback (most recent call last):
8: from script/import_scripts/muut.rb:184:in `\u003cmain\u003e'
7: from /var/www/discourse/script/import_scripts/base.rb:47:in `perform'
6: from script/import_scripts/muut.rb:25:in `execute'
5: from script/import_scripts/muut.rb:82:in `import_discussions'
4: from script/import_scripts/muut.rb:82:in `each'
3: from script/import_scripts/muut.rb:84:in `block in import_discussions'
2: from script/import_scripts/muut.rb:84:in `each'
1: from script/import_scripts/muut.rb:110:in `block (2 levels) in import_discussions'
script/import_scripts/muut.rb:162:in `process_muut_post_body': undefined method `gsub!' for nil:NilClass (NoMethodError)
このエラーを回避する方法について、どなたかご助言いただけないでしょうか。
pfaffman
(Jay Pfaffman)
2
一部のフィールド(おそらく投稿データ)が欠落しています。これは、スクリプトが誤ったフィールド名にアクセスしているためかもしれません。
以下は、私で機能した muut.rb の更新ファイルです:
# frozen_string_literal: true
require "csv"
require File.expand_path(File.dirname(__FILE__) + "/base.rb")
# インポートデータの定数と初期化メソッドを編集してください。
class ImportScripts::Muut < ImportScripts::Base
JSON_FILE_PATH = "/path/to/json/file"
CSV_FILE_PATH = "/path/to/csv/file"
def initialize
super
@imported_users = load_csv
@imported_json = load_json
end
def execute
puts "", "Muut からインポート中..."
import_users
import_categories
import_discussions
puts "", "完了"
end
def load_json
JSON.parse(repair_json(File.read(JSON_FILE_PATH)))
end
def load_csv
CSV.parse(File.read(CSV_FILE_PATH))
end
def repair_json(arg)
arg.gsub!(/^\(/, "") # ファイルの内容が ( ) で囲まれている
arg.gsub!(/\)$/, "")
arg.gsub!(/\]\]$/, "]") # 末尾に余分な ] がある場合
arg.gsub!(/\}\{/, "},{") # 時としてカンマが欠落している!
arg.gsub!("}]{", "},{") # 予期しない角括弧
arg.gsub!("}[{", "},{") # :troll:
arg
end
def import_users
puts '', "ユーザーをインポート中"
create_users(@imported_users) do |u|
{
id: u[0],
email: u[1],
created_at: Time.now
}
end
end
def import_categories
puts "", "カテゴリをインポート中"
create_categories(@imported_json['categories']) do |category|
{
id: category['path'], # muut にはカテゴリの ID がないため、パスを使用
name: category['title'],
slug: category['path']
}
end
end
def import_discussions
puts "", "トピックをインポート中"
topics = 0
posts = 0
@imported_json['categories'].each do |category|
@imported_json['threads'][category['path']].each do |thread|
next if thread["seed"]["key"] == "skip-this-topic"
mapped = {}
mapped[:id] = "#{thread["seed"]["key"]}-#{thread["seed"]["date"]}"
if thread["seed"]["author"] && user_id_from_imported_user_id(thread["seed"]["author"]["path"]) != ""
mapped[:user_id] = user_id_from_imported_user_id(thread["seed"]["author"]["path"]) || -1
else
mapped[:user_id] = -1
end
# ユーザーの表示名を更新
if thread["seed"]["author"] && thread["seed"]["author"]["displayname"] != "" && mapped[:user_id] != -1
user = User.find_by(id: mapped[:user_id])
if user
user.name = thread["seed"]["author"]["displayname"]
user.save!
end
end
mapped[:created_at] = Time.zone.at(thread["seed"]["date"])
mapped[:category] = category_id_from_imported_category_id(thread["seed"]["path"])
mapped[:title] = CGI.unescapeHTML(thread["seed"]["title"])
if thread["seed"]["body"] == ""
thread["seed"]["body"] = " ";
end
mapped[:raw] = process_muut_post_body(thread["seed"]["body"])
mapped[:raw] = CGI.unescapeHTML(thread["seed"]["title"]) if mapped[:raw] == ""
parent_post = create_post(mapped, mapped[:id])
unless parent_post.is_a?(Post)
puts "トピック #{mapped[:id]} の作成エラー。スキップします。"
puts parent_post.inspect
end
# パーマリンクを作成するには、以下の行のコメントを外してください
# Permalink.create(url: "#{thread["seed"]["path"]}:#{thread["seed"]["key"]}", topic_id: parent_post.topic_id)
# 返信を作成
if thread["replies"].present? && thread["replies"].count > 0
thread["replies"].reverse_each do |post|
if post_id_from_imported_post_id(post["id"])
next # この投稿はすでにインポート済み
end
if post["body"] == ""
post["body"] = " "
end
new_post = create_post({
id: "#{post["key"]}-#{post["date"]}",
topic_id: parent_post.topic_id,
# 以下の行を変更して、author/path 値から一意の user_id を取得してください。
user_id: user_id_from_imported_user_id(post["author"]["path"]) || -1,
raw: process_muut_post_body(post["body"]),
created_at: Time.zone.at(post["date"])
}, post["id"])
if new_post.is_a?(Post)
posts += 1
else
puts "投稿 #{post["id"]} の作成エラー。スキップします。"
puts new_post.inspect
end
end
end
topics += 1
end
end
puts "", "#{topics} トピックと #{topics + posts} 投稿をインポートしました。"
end
def process_muut_post_body(arg)
raw = arg.dup
raw = raw.to_s
raw = raw[0..-1]
# 改行
if raw != nil
raw.gsub!(/\\n/, "\n")
# コードブロック
raw.gsub!("---", "```\n")
# タブ
raw.gsub!(/\\t/, ' ')
# 二重引用符
raw.gsub!(/\\\"/, '"')
raw = CGI.unescapeHTML(raw)
end
raw
end
def file_full_path(relpath)
File.join JSON_FILES_DIR, relpath.split("?").first
end
end
if __FILE__ == $0
ImportScripts::Muut.new.perform
end