|
|
|
@@ -21,7 +21,7 @@ module Wiki |
|
|
|
end |
|
|
|
|
|
|
|
def content! body:, message:, base_revision_id: |
|
|
|
normalised = normalise_body(body) |
|
|
|
normalised = normalise_body(body).gsub(/\n+$/, '') |
|
|
|
lines = split_lines(normalised) |
|
|
|
|
|
|
|
line_shas = lines.map { |line| Digest::SHA256.hexdigest(line) } |
|
|
|
@@ -37,10 +37,22 @@ module Wiki |
|
|
|
current_id = @page.wiki_revisions.maximum(:id) |
|
|
|
if current_id && current_id != base_revision_id.to_i |
|
|
|
raise Conflict, |
|
|
|
"競合が発生してゐます(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })." |
|
|
|
"競合が発生してゐます" + |
|
|
|
"(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })." |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
@page.update!(body: normalised) |
|
|
|
|
|
|
|
WikiVersionRecorder.record!( |
|
|
|
page: { title: @page.title, body: normalised }, |
|
|
|
event_type: @page.wiki_versions.exists? ? :update : :create, |
|
|
|
created_by_user: @created_user) |
|
|
|
tag = @page.tag_name.tag |
|
|
|
if tag |
|
|
|
TagVersionRecorder.record!(tag:, event_type: :update, created_by_user: @created_user) |
|
|
|
end |
|
|
|
|
|
|
|
rev = WikiRevision.create!( |
|
|
|
wiki_page: @page, |
|
|
|
base_revision_id:, |
|
|
|
@@ -54,47 +66,23 @@ module Wiki |
|
|
|
rows = line_ids.each_with_index.map do |line_id, pos| |
|
|
|
{ wiki_revision_id: rev.id, wiki_line_id: line_id, position: pos } |
|
|
|
end |
|
|
|
WikiRevisionLine.insert_all!(rows) |
|
|
|
WikiRevisionLine.insert_all!(rows) if rows.any? |
|
|
|
|
|
|
|
rev |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
def redirect! redirect_page:, message:, base_revision_id: |
|
|
|
ActiveRecord::Base.transaction do |
|
|
|
@page.lock! |
|
|
|
|
|
|
|
if base_revision_id.present? |
|
|
|
current_id = @page.wiki_revisions.maximum(:id) |
|
|
|
if current_id && current_id != base_revision_id.to_i |
|
|
|
raise Conflict, |
|
|
|
"競合が発生してゐます(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })." |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
WikiRevision.create!( |
|
|
|
wiki_page: @page, |
|
|
|
base_revision_id:, |
|
|
|
created_user: @created_user, |
|
|
|
kind: :redirect, |
|
|
|
redirect_page:, |
|
|
|
message:, |
|
|
|
lines_count: 0, |
|
|
|
tree_sha256: nil) |
|
|
|
end |
|
|
|
end |
|
|
|
def redirect!(redirect_page:, message:, base_revision_id:) = raise '廃止しました.' |
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
def normalise_body body |
|
|
|
s = body.to_s |
|
|
|
s.gsub!("\r\n", "\n") |
|
|
|
s.gsub!(/\r\n?/, "\n") |
|
|
|
s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '🖕') |
|
|
|
end |
|
|
|
|
|
|
|
def split_lines body |
|
|
|
body.split("\n") |
|
|
|
end |
|
|
|
def split_lines(body) = body.split("\n") |
|
|
|
|
|
|
|
def upsert_lines! lines, line_shas |
|
|
|
now = Time.current |
|
|
|
|