ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
2.2 KiB

  1. namespace :wiki do
  2. desc 'Wiki 移行'
  3. task migrate: :environment do
  4. require 'digest'
  5. require 'gollum-lib'
  6. wiki = Gollum::Wiki.new(Rails.root.join('wiki').to_s)
  7. WikiPage.where.missing(:wiki_revisions).find_each do |wiki_page|
  8. page = wiki.page("#{ wiki_page.id }.md")
  9. next unless page
  10. versions = page.versions
  11. next if versions.blank?
  12. base_revision_id = nil
  13. versions.reverse_each do |version|
  14. pg = wiki.page("#{ wiki_page.id }.md", version.id)
  15. raw = pg&.raw_data
  16. next unless raw
  17. lines = raw.force_encoding('UTF-8').split("\n")
  18. line_shas = lines.map { |l| Digest::SHA256.hexdigest(l) }
  19. tree_sha = Digest::SHA256.hexdigest(line_shas.join(','))
  20. at = version.authored_date
  21. line_id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
  22. missing_rows = []
  23. line_shas.each_with_index do |sha, i|
  24. next if line_id_by_sha.key?(sha)
  25. missing_rows << { sha256: sha,
  26. body: lines[i],
  27. created_at: at,
  28. updated_at: at }
  29. end
  30. if missing_rows.any?
  31. WikiLine.upsert_all(missing_rows)
  32. line_id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
  33. end
  34. line_ids = line_shas.map { |sha| line_id_by_sha.fetch(sha) }
  35. rev = nil
  36. ActiveRecord::Base.transaction do
  37. wiki_page.lock!
  38. rev = WikiRevision.create!(
  39. wiki_page:,
  40. base_revision_id:,
  41. created_user_id: (Integer(version.author.name) rescue 2),
  42. kind: :content,
  43. redirect_page_id: nil,
  44. message: nil,
  45. lines_count: lines.length,
  46. tree_sha256: tree_sha,
  47. created_at: at,
  48. updated_at: at)
  49. rows = line_ids.each_with_index.map do |line_id, pos|
  50. { wiki_revision_id: rev.id,
  51. wiki_line_id: line_id,
  52. position: pos }
  53. end
  54. WikiRevisionLine.insert_all!(rows)
  55. end
  56. base_revision_id = rev.id
  57. end
  58. end
  59. end
  60. end