Migrate a XenForo forum to Discourse

Thanks Justin, I’ve had a quick look at it - does this seem ok to you? (Feel free to add it to the official importer if you like)

  XENFORO_DB = "xenforo_db_3"
  TABLE_PREFIX = "xf_"
  BATCH_SIZE = 1000
  ATTACHMENT_DIR = '/FULL/PATH/TO/attachments'
  AVATAR_DIR = '/FULL/PATH/TO/avatars'

(Added last line^^ - means you’ll need to copy avatars there)

  def execute
    import_users
    import_categories
    import_posts
    import_avatars
  end

(Added last line^^)

  def import_avatars
    if AVATAR_DIR
      users = User.all
      users.each do |u|
        unless u.custom_fields["import_id"].nil?
          import_id = u.custom_fields["import_id"]
          if import_id.to_i < 1000
            dir_num = "0"
          elsif import_id.to_i > 1000
            dir_num = import_id.first
          end
        
          avatar_filename = "#{import_id}.jpg"
          file_path = "#{AVATAR_DIR}/l/#{dir_num}"
          file_path_and_name = "#{file_path}/#{avatar_filename}"

          if File.exists?(file_path_and_name)
            upload = create_upload(u.id, file_path_and_name, avatar_filename)
            if upload.persisted?
              u.import_mode = false
              u.create_user_avatar
              u.import_mode = true
              u.user_avatar.update(custom_upload_id: upload.id)
              u.update(uploaded_avatar_id: upload.id)
            else
              puts "Error: Upload did not persist for #{u.username} #{avatar_filename}!"
            end
          end
        end
      end
    end
  end

Its very late here so I may have made some mistakes or missed a load of stuff out but this is assuming all avatars are .jpg (which seems like all of mine are). I wasn’t too sure about the u.import_mode switches tho so just commented them out.

Totally untested (it.s nearly 5am here :zzz:)

I did a test on my dev machine on a forum with 100K posts and it took 90minutes. The one I want to do the import on has a couple million posts so 10 times as long maybe?

Nice! Will you be sharing it with us? Any idea when?

3 Likes