Error importing from vanilla: invalid byte sequence in UTF-8

I’m trying to import from a Vanilla forum using the instructions posted here. However, I get the following error when I run the vanilla.rb import script:

Loading existing groups...
Loading existing users...
Loading existing categories...
Loading existing posts...
Loading existing topics...
parsing file...
reading file...
Traceback (most recent call last):
	5: from script/import_scripts/vanilla.rb:254:in `<main>'
	4: from /var/www/discourse/script/import_scripts/base.rb:47:in `perform'
	3: from script/import_scripts/vanilla.rb:17:in `execute'
	2: from script/import_scripts/vanilla.rb:37:in `parse_file'
	1: from script/import_scripts/vanilla.rb:72:in `read_file'
script/import_scripts/vanilla.rb:72:in `gsub': invalid byte sequence in UTF-8 (ArgumentError)

I’ve tried changing the MySQL database character set to UTF8 following the instructions here and then re-exporting the porter file, but that didn’t resolve the issue. Any suggestions?

إعجاب واحد (1)

You either need to keep trying to get it to really be UTF-8 or modify the import script to do it. It is a frustrating problem.

إعجابَين (2)

@dpkoch Did you ever figure this out?

You can google stuff about UTF-8 encoding. You need to do something that will coerce the table into UTF-8. The time that I did it, there were further complications because some rows were in one format and others in another format. I think that I did some nonsense where I coerced things on a value-by-value basis.

إعجاب واحد (1)

Sounds awful… We’ll have to tinker with the table encoding and see what happens. Thanks!

Oh. It’s aweful. You best bet, based on a vague recollection of a single time I did this over a year ago, is to play around with as many different conversions as you can until you can finally hit on one that works for all or most data. I think that I did a bunch of one-by-one transformations that ended up being a waste of time when I stumbled on some conversion that worked for all (most?) data.

Here is what I did. Use at your own risk. (This was vbulletin, FWIW).

  def char_map(raw_original)
    raw = raw_original.dup
    debug = false # (raw.length > 50)

    # windows 1252
    all = ''
    win_encoded = ''

    ### WIN1252 encoding
    win_encoded = ''
    begin
      win_encoded = raw.force_encoding('utf-8').encode("Windows-1252",
                            invalid: :replace, undef: :replace, replace: ""
                           ).force_encoding('utf-8').scrub
    rescue => e
      puts "\n#{'-'*50}\nWin1252 failed for \n\n#{raw}\n\n"
      win_encoded = ''
    end

    ### ISO 8859 encoding
    iso_encoded = ''
    if all.length == 0 && win_encoded.length > 0 && win_encoded != raw
      all = (debug ? "Win1252--" : '') + win_encoded
    else
      all = raw
    end
    all = old_char_map(all)
    all
  end

That code is used within the import script or on the server/database side?

In the import script. I don’t like to mess with the database.

Somewhere you call this function on raw to fix raw (and maybe titles?).

Ok, thanks very much! This should give me a huge head-start in debugging this.

إعجاب واحد (1)

لقد نجحنا في ذلك بإضافة أمر بسيط لتشفير الملف بصيغة UTF-8 أثناء قراءته، باستخدام شيء مثل encode\"UTF-8\" في الأسطر 76-80 من نص الاستيراد vanilla.rb.

أنا فقط أنتظر تأكيدًا على الصيغة الدقيقة من الشخص الذي قام بذلك عبر سطر الأوامر. سأقوم بالتحديث عندما أحصل عليها.

إعجاب واحد (1)

إليك ما استخدمه لإصلاح هذا، بدءًا من السطر 76 من vanilla.rb

def read_file
  puts "reading file..."
  string = [File.read](http://file.read/)(@vanilla_file)
    .force_encoding('UTF-8').encode("UTF-8").gsub("\\N", "")
    .force_encoding('UTF-8').encode("UTF-8").gsub(/\\$\\n/m, "\\n")
    .force_encoding('UTF-8').encode("UTF-8").gsub("\\,", ",")
    .force_encoding('UTF-8').encode("UTF-8").gsub(/(?<!\\)\\"/, '""')
    .force_encoding('UTF-8').encode("UTF-8").gsub(/\\\\\\\\\\"/, '\\\"\"')
  [StringIO.new](http://stringio.new/)(string)
end
إعجاب واحد (1)

تم تقسيم منشور إلى موضوع جديد: كيفية تعديل برنامج استيراد على خادم الإنتاج؟

هذا لا يعمل. نفس الخطأ.

تحتاج إلى البحث في جوجل عن الترميز ومعرفة كيفية إصلاح الترميز المعطل لديك.

مرحباً @ddeveloper،

لقد مررت بهذه العملية قبل شهرين فقط (وأنا لست مطورًا :slightly_smiling_face:) وتمكنت من ترحيل منتدى Vanilla مستضاف ذاتيًا بنجاح إلى Discourse مستضاف ذاتيًا. كان أحد الأشياء الرئيسية بالنسبة لي هو التأكد عند تصدير البيانات باستخدام Vanilla Porter، من تحديد “Vanilla 2” كـ Source Forum Type في القائمة المنسدلة الأولى.

لقد استخدمت الإصدار Vanilla Porter 2.6 المتاح للتنزيل كملف مضغوط هنا: Vanilla Porter 2.6 RC1 — Vanilla Forums بدلاً من الإصدار 2.5 المرتبط في Migrate a Vanilla forum to Discourse.

إذا كنت أتذكر بشكل صحيح، لم أواجه خطأ UTF-8 مرة أخرى عند استخدام نص Vanilla Porter الأحدث ونوع المنتدى “Vanilla 2”.

إذا لم تحدث هذين الاقتراحين فرقًا في استيرادك، فيرجى تقديم بعض التفاصيل حول الخطوات التي اتخذتها حتى الآن وما تراه بالضبط. في بعض الأحيان تكون هناك اختلافات طفيفة في “نفس الخطأ” يمكن أن تحدث فرقًا كبيرًا عند استكشاف الأخطاء وإصلاحها.

5 إعجابات

لقد اتبعت نفس الدليل باستثناء استخدام إصدار porter 2.6. سأقوم بتصدير الملف من الإصدار 2.6 وأقوم بالتحديث هنا.

حسنًا، لقد جربت porter 2.6 وأدى ذلك إلى نفس خطأ UTF-8:

حتى الآن، اتبعت هذا الدليل: Migrate a Vanilla forum to Discourse

سار كل شيء على ما يرام حتى خطأ ترميز UTF-8 هذا. لقد حل بعض الأشخاص هذه المشكلة. لقد جربت تلك الحلول، ولم تنجح معي.

لقد جربت حل @Nick_Chomey أعلاه؛ محاولة فرض ترميز utf-8 أثناء قراءة ملف txt ولكن للأسف لم ينجح ذلك أيضًا.

فقط للتأكد، ما هو نوع المنتدى المصدر الذي حددته في القائمة المنسدلة لـ Vanilla Porter؟

هل يمكنك إخبارنا بنوع الكمبيوتر الذي تستخدمه؟ ستختلف التعليمات لتحويل ملفك إلى ترميز UTF-8.

إعجاب واحد (1)

شكراً لوقتك في مساعدة زميل في المنتدى.

لقد اخترت “Vanilla 2” في Source Forum Type.

يمكنني استخدام أجهزة تعمل بنظامي التشغيل Windows و Linux ولدي وصول إلى كليهما.