Reviewed-on: #432 Co-authored-by: miteruzo <miteruzo@naver.com>
このコミットはプルリクエスト #432 でマージされました。
このコミットが含まれているのは:
@@ -1,14 +1,18 @@
|
||||
class TagDescriptionsController < ApplicationController
|
||||
def show
|
||||
description = TagDescription.find_by(tag_id: params[:id], language_code: 'ja')
|
||||
locale = resolve_locale!
|
||||
description = TagDescription.find_by(tag_id: params[:id], language_code: locale.language_code)
|
||||
return head :not_found unless description
|
||||
|
||||
render json: representation(description)
|
||||
end
|
||||
|
||||
def show_by_title
|
||||
tag_name = TagName.find_by(language_code: 'ja', name: params[:name].to_s.strip)
|
||||
description = tag_name&.tag && TagDescription.find_by(tag: tag_name.tag, language_code: 'ja')
|
||||
locale = resolve_locale!
|
||||
tag_name = TagName.find_by(
|
||||
language_code: locale.language_code, name: params[:name].to_s.strip)
|
||||
description = tag_name&.tag && TagDescription.find_by(
|
||||
tag: tag_name.tag, language_code: locale.language_code)
|
||||
return head :not_found unless description
|
||||
|
||||
render json: representation(description)
|
||||
@@ -21,18 +25,19 @@ class TagDescriptionsController < ApplicationController
|
||||
body = params[:body].to_s
|
||||
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||
|
||||
locale = resolve_locale!
|
||||
tag = Tag.find_by(id: params[:id])
|
||||
return head :not_found unless tag
|
||||
|
||||
description = TagDescription.find_by(tag:, language_code: 'ja')
|
||||
description = TagDescription.find_by(tag:, language_code: locale.language_code)
|
||||
|
||||
if description
|
||||
TagDescriptionOperation.update!(tag:, body:, updated_by_user: current_user)
|
||||
TagDescriptionOperation.update!(locale, tag:, body:, updated_by_user: current_user)
|
||||
description.reload
|
||||
render json: representation(description)
|
||||
else
|
||||
TagDescriptionOperation.create!(tag:, body:, created_by_user: current_user)
|
||||
description = TagDescription.find_by!(tag:, language_code: 'ja')
|
||||
TagDescriptionOperation.create!(locale, tag:, body:, created_by_user: current_user)
|
||||
description = TagDescription.find_by!(tag:, language_code: locale.language_code)
|
||||
render json: representation(description), status: :created
|
||||
end
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
|
||||
@@ -1,33 +1,37 @@
|
||||
class TagGroupsController < ApplicationController
|
||||
def index
|
||||
locale = resolve_locale!
|
||||
groups = TagGroup.includes(:created_by_user, :updated_by_user).order(:id)
|
||||
render json: TagGroupRepr.many(groups.to_a)
|
||||
render json: TagGroupRepr.many(locale, groups.to_a)
|
||||
end
|
||||
|
||||
def show
|
||||
locale = resolve_locale!
|
||||
group = TagGroup.includes(:created_by_user, :updated_by_user).find_by(id: params[:id])
|
||||
return head :not_found unless group
|
||||
|
||||
render json: TagGroupRepr.one(group)
|
||||
render json: TagGroupRepr.one(locale, group)
|
||||
end
|
||||
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
group = TagGroupOperation.create!(**write_attributes, actor: current_user)
|
||||
render json: TagGroupRepr.one(group), status: :created
|
||||
locale = resolve_locale!
|
||||
group = TagGroupOperation.create!(locale, **write_attributes, actor: current_user)
|
||||
render json: TagGroupRepr.one(locale, group), status: :created
|
||||
end
|
||||
|
||||
def update
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless current_user.gte_member?
|
||||
|
||||
locale = resolve_locale!
|
||||
group = TagGroup.find_by(id: params[:id])
|
||||
return head :not_found unless group
|
||||
|
||||
TagGroupOperation.update!(group:, **write_attributes, actor: current_user)
|
||||
render json: TagGroupRepr.one(group.reload)
|
||||
TagGroupOperation.update!(locale, group:, **write_attributes, actor: current_user)
|
||||
render json: TagGroupRepr.one(locale, group.reload)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -770,31 +770,15 @@ class TagsController < ApplicationController
|
||||
TagVersioning.ensure_snapshot!(old_owner_tag, created_by_user: current_user)
|
||||
end
|
||||
|
||||
current_tag_name.wiki_page&.update!(tag_name_id: promoted_tag_name.id)
|
||||
current_tag_name.association(:wiki_page).reset
|
||||
|
||||
current_tag_name.update!(canonical_id: promoted_tag_name.id,
|
||||
primary_flg: false, auto_generated: false)
|
||||
promoted_tag_name.update!(tag:, canonical_id: nil, primary_flg: true, auto_generated: false)
|
||||
|
||||
TagName.where(tag:,
|
||||
language_code: current_tag_name.language_code,
|
||||
primary_flg: false)
|
||||
.where.not(id: promoted_tag_name.id)
|
||||
.find_each do |alias_tag_name|
|
||||
alias_tag_name.update!(canonical_id: promoted_tag_name.id, primary_flg: false)
|
||||
end
|
||||
|
||||
tag.update!(tag_name_id: promoted_tag_name.id)
|
||||
current_tag_name.association(:wiki_page).reset
|
||||
current_tag_name.association(:tag).reset
|
||||
current_tag_name.reload.update!(canonical_id: promoted_tag_name.id, primary_flg: false)
|
||||
current_tag_name&.update!(primary_flg: false, auto_generated: false)
|
||||
promoted_tag_name.update!(tag:, primary_flg: true, auto_generated: false)
|
||||
tag.association(:tag_names).reset
|
||||
|
||||
return unless old_owner_tag && old_owner_tag != tag
|
||||
|
||||
record_tag_version!(old_owner_tag.reload, event_type: :update,
|
||||
created_by_user: current_user,
|
||||
language_code: current_tag_name.language_code)
|
||||
language_code: promoted_tag_name.language_code)
|
||||
end
|
||||
|
||||
def update_aliases! locale, tag, alias_names
|
||||
@@ -802,20 +786,15 @@ class TagsController < ApplicationController
|
||||
|
||||
affected_tags = [tag]
|
||||
|
||||
primary_tag_name = TagName.find_by!(tag:,
|
||||
language_code: locale.language_code,
|
||||
primary_flg: true)
|
||||
current_aliases = tag.tag_names.where(language_code: locale.language_code,
|
||||
primary_flg: false).to_a
|
||||
|
||||
current_aliases.each do |alias_tag_name|
|
||||
next if alias_names.include?(alias_tag_name.name)
|
||||
|
||||
affected_tags << alias_tag_name.tag
|
||||
end
|
||||
requested_aliases = TagName.where(
|
||||
language_code: locale.language_code, name: alias_names)
|
||||
.includes(:tag).index_by(&:name)
|
||||
|
||||
alias_names.each do |alias_name|
|
||||
alias_tag_name = TagName.find_by(language_code: locale.language_code, name: alias_name)
|
||||
alias_tag_name = requested_aliases[alias_name]
|
||||
if alias_tag_name&.primary? && alias_tag_name.tag_id && alias_tag_name.tag_id != tag.id
|
||||
alias_tag_name.errors.add(:name, 'その名前は既に使はれてゐます.')
|
||||
raise ActiveRecord::RecordInvalid, alias_tag_name
|
||||
@@ -823,7 +802,8 @@ class TagsController < ApplicationController
|
||||
affected_tags << alias_tag_name&.tag
|
||||
end
|
||||
|
||||
affected_tags.compact.uniq.each do |affected_tag|
|
||||
affected_tags = affected_tags.compact.uniq(&:id)
|
||||
affected_tags.each do |affected_tag|
|
||||
TagVersioning.ensure_snapshot!(affected_tag, created_by_user: current_user,
|
||||
language_code: locale.language_code)
|
||||
end
|
||||
@@ -831,19 +811,21 @@ class TagsController < ApplicationController
|
||||
current_aliases.each do |alias_tag_name|
|
||||
next if alias_names.include?(alias_tag_name.name)
|
||||
|
||||
alias_tag_name.update!(canonical_id: nil, tag_id: nil)
|
||||
alias_tag_name.destroy!
|
||||
end
|
||||
|
||||
alias_names.each do |alias_name|
|
||||
alias_tag_name = TagName.find_or_initialize_by(
|
||||
alias_tag_name = requested_aliases[alias_name] || TagName.new(
|
||||
language_code: locale.language_code,
|
||||
name: alias_name)
|
||||
alias_tag_name.script_code = locale.script_code
|
||||
alias_tag_name.update!(tag:, primary_flg: false, auto_generated: false,
|
||||
canonical_id: primary_tag_name.id)
|
||||
alias_tag_name.update!(tag:, primary_flg: false, auto_generated: false)
|
||||
end
|
||||
|
||||
affected_tags.compact.uniq.each do |affected_tag|
|
||||
affected_tags.each do |affected_tag|
|
||||
# update_all records this Tag after its parent relationships are updated.
|
||||
next if affected_tag.id == tag.id
|
||||
|
||||
record_tag_version!(affected_tag, event_type: :update,
|
||||
created_by_user: current_user,
|
||||
language_code: locale.language_code)
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
class WikiPagesController < ApplicationController
|
||||
rescue_from Wiki::Commit::Conflict, with: :render_wiki_conflict
|
||||
# 互換性のための Facade API
|
||||
|
||||
rescue_from TagDescriptionOperation::Conflict, with: :render_wiki_conflict
|
||||
|
||||
def index
|
||||
# 互換性のための化石リソース
|
||||
locale = resolve_locale!
|
||||
|
||||
title = params[:title].to_s.strip
|
||||
descriptions =
|
||||
TagDescription.where(language_code: 'ja')
|
||||
TagDescription
|
||||
.where(language_code: locale.language_code)
|
||||
.includes(tag: :tag_names)
|
||||
|
||||
descriptions = descriptions.order(updated_at: :desc, id: :asc)
|
||||
@@ -15,13 +18,14 @@ class WikiPagesController < ApplicationController
|
||||
pattern = "%#{ ApplicationRecord.sanitize_sql_like(title) }%"
|
||||
descriptions =
|
||||
descriptions.joins(tag: :tag_names)
|
||||
.where(tag_names: { language_code: 'ja', primary_flg: true })
|
||||
.where(tag_names: { language_code: locale.language_code, primary_flg: true })
|
||||
.where('tag_names.name LIKE ?', pattern).limit(20)
|
||||
tag_groups = tag_groups.where(
|
||||
'CONCAT(?, name) LIKE ?', TagGroupRepr::PREFIX, pattern).limit(20)
|
||||
'CONCAT(?, name) LIKE ? OR CONCAT(?, name) LIKE ?',
|
||||
TagGroupRepr::PREFIX, pattern, 'tag_group:', pattern).limit(20)
|
||||
end
|
||||
|
||||
json = descriptions.map { |description| WikiPageRepr.tag_description(description) }
|
||||
json = descriptions.map { |description| WikiPageRepr.tag_description(locale, description) }
|
||||
json.concat(tag_groups.map { WikiPageRepr.tag_group(_1) })
|
||||
json.sort_by! do |row|
|
||||
[-row['updated_at'].to_f, row.fetch('resource_type'), row.fetch('id')]
|
||||
@@ -31,47 +35,53 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
|
||||
def show
|
||||
tag_description = TagDescription.find_by(id: params[:id])
|
||||
locale = resolve_locale!
|
||||
tag_description = TagDescription.find_by(id: params[:id], language_code: locale.language_code)
|
||||
|
||||
return render_wiki_page_by_tag_description tag_description if tag_description
|
||||
return render_wiki_page_by_tag_description locale, tag_description if tag_description
|
||||
|
||||
tag_group = TagGroup.find_by(id: params[:id])
|
||||
return render_tag_group tag_group if tag_group
|
||||
return render_tag_group locale, tag_group if tag_group
|
||||
|
||||
render_wiki_page_or_404 nil
|
||||
head :not_found
|
||||
end
|
||||
|
||||
def show_by_title
|
||||
locale = resolve_locale!
|
||||
title = params[:title].to_s.strip
|
||||
|
||||
if title.start_with?(TagGroupRepr::PREFIX)
|
||||
name = title.delete_prefix(TagGroupRepr::PREFIX)
|
||||
if (name = TagGroupRepr.name_from_title(title))
|
||||
tag_group = TagGroup.find_by(name:)
|
||||
return render_tag_group tag_group if tag_group
|
||||
return render_tag_group locale, tag_group if tag_group
|
||||
return head :not_found
|
||||
end
|
||||
|
||||
tag_description = tag_description_by_title(title)
|
||||
tag_description = tag_description_by_title(locale, title)
|
||||
|
||||
return render_wiki_page_by_tag_description tag_description if tag_description
|
||||
return render_wiki_page_by_tag_description locale, tag_description if tag_description
|
||||
|
||||
render_wiki_page_or_404 nil
|
||||
head :not_found
|
||||
end
|
||||
|
||||
def exists
|
||||
return head :no_content if TagDescription.exists?(id: params[:id])
|
||||
locale = resolve_locale!
|
||||
if TagDescription.exists?(id: params[:id], language_code: locale.language_code)
|
||||
return head :no_content
|
||||
end
|
||||
return head :no_content if TagGroup.exists?(id: params[:id])
|
||||
|
||||
head :not_found
|
||||
end
|
||||
|
||||
def exists_by_title
|
||||
locale = resolve_locale!
|
||||
title = params[:title].to_s.strip
|
||||
if title.start_with?(TagGroupRepr::PREFIX)
|
||||
exists = TagGroup.exists?(name: title.delete_prefix(TagGroupRepr::PREFIX))
|
||||
if (name = TagGroupRepr.name_from_title(title))
|
||||
exists = TagGroup.exists?(name:)
|
||||
return head exists ? :no_content : :not_found
|
||||
end
|
||||
return head :no_content if tag_description_by_title(title)
|
||||
|
||||
return head :no_content if tag_description_by_title(locale, title)
|
||||
|
||||
head :not_found
|
||||
end
|
||||
@@ -80,15 +90,17 @@ class WikiPagesController < ApplicationController
|
||||
id = params[:id]
|
||||
return render_bad_request('id は必須です.') if id.blank?
|
||||
|
||||
locale = resolve_locale!
|
||||
|
||||
from = params[:from].presence
|
||||
to = params[:to].presence
|
||||
|
||||
description = TagDescription.find_by(id:)
|
||||
description = TagDescription.find_by(id:, language_code: locale.language_code)
|
||||
if description
|
||||
versions = description_versions(description)
|
||||
from_rev = from && versions.find(from)
|
||||
to_rev = to ? versions.find(to) : versions.find_by!(version_no: description.version_no)
|
||||
resource = WikiPageRepr.tag_description(description)
|
||||
resource = WikiPageRepr.tag_description(locale, description)
|
||||
else
|
||||
tag_group = TagGroup.find_by(id:)
|
||||
if tag_group
|
||||
@@ -96,8 +108,8 @@ class WikiPagesController < ApplicationController
|
||||
to_rev = to ? group_version(tag_group, to) : current_group_version(tag_group)
|
||||
return head :not_found if (from && !from_rev) || !to_rev
|
||||
|
||||
from_body = from_rev && render_group_version(from_rev)
|
||||
to_body = render_group_version(to_rev)
|
||||
from_body = from_rev && render_group_version(locale, from_rev)
|
||||
to_body = render_group_version(locale, to_rev)
|
||||
resource = { 'id' => tag_group.id,
|
||||
'title' => "#{ TagGroupRepr::PREFIX }#{ tag_group.name }",
|
||||
'deprecated_at' => nil }
|
||||
@@ -131,30 +143,31 @@ class WikiPagesController < ApplicationController
|
||||
return render_unprocessable_entity('タイトルは必須です.', field: :title) if title.blank?
|
||||
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||
|
||||
if title.start_with?(TagGroupRepr::PREFIX)
|
||||
name = title.delete_prefix(TagGroupRepr::PREFIX)
|
||||
parsed = TagGroupMarkdownParser.parse(body:, group: nil)
|
||||
group = TagGroupOperation.create!(name:, **parsed, actor: current_user,
|
||||
require_compatibility: true)
|
||||
locale = resolve_locale!
|
||||
|
||||
if (name = TagGroupRepr.name_from_title(title))
|
||||
parsed = TagGroupMarkdownParser.parse(locale, body:, group: nil)
|
||||
group = TagGroupOperation.create!(
|
||||
locale, name:, **parsed.except(:compatibility_body), actor: current_user,
|
||||
require_compatibility: true)
|
||||
version = current_group_version(group)
|
||||
return render_group_representation(TagGroupRepr.one(group), version,
|
||||
body: parsed.fetch(:compatibility_body),
|
||||
return render_group_representation(TagGroupRepr.one(locale, group), version,
|
||||
status: :created)
|
||||
end
|
||||
|
||||
page =
|
||||
description =
|
||||
if title.include?(':')
|
||||
return render_unprocessable_entity field: :title
|
||||
else
|
||||
tag_name = TagName.find_by(language_code: 'ja', name: title)
|
||||
tag_name = TagName.find_by(language_code: locale.language_code, name: title)
|
||||
return render_unprocessable_entity field: :title unless tag_name&.tag
|
||||
|
||||
TagDescriptionOperation.create!(
|
||||
locale,
|
||||
tag: tag_name.tag, body:, created_by_user: current_user, message:)
|
||||
end
|
||||
|
||||
description = TagDescription.find_by!(tag_id: page.tag_name.tag_id, language_code: 'ja')
|
||||
render json: WikiPageRepr.tag_description(description), status: :created
|
||||
render json: WikiPageRepr.tag_description(locale, description), status: :created
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
render_validation_error e.record
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
@@ -173,28 +186,30 @@ class WikiPagesController < ApplicationController
|
||||
return render_unprocessable_entity('タイトルは必須です.', field: :title) if title.blank?
|
||||
return render_unprocessable_entity('本文は必須です.', field: :body) if body.blank?
|
||||
|
||||
locale = resolve_locale!
|
||||
|
||||
group = TagGroup.find_by(id: params[:id])
|
||||
if title.start_with?(TagGroupRepr::PREFIX)
|
||||
if (name = TagGroupRepr.name_from_title(title))
|
||||
return head :not_found unless group
|
||||
|
||||
name = title.delete_prefix(TagGroupRepr::PREFIX)
|
||||
parsed = TagGroupMarkdownParser.parse(body:, group:)
|
||||
TagGroupOperation.update!(group:, name:, **parsed, actor: current_user,
|
||||
require_compatibility: true)
|
||||
parsed = TagGroupMarkdownParser.parse(locale, body:, group:)
|
||||
TagGroupOperation.update!(
|
||||
locale, group:, name:, **parsed.except(:compatibility_body), actor: current_user,
|
||||
require_compatibility: true)
|
||||
return head :ok
|
||||
end
|
||||
|
||||
return render_unprocessable_entity field: :title if title.include?(':')
|
||||
description = TagDescription.find_by(id: params[:id])
|
||||
description = TagDescription.find_by(id: params[:id], language_code: locale.language_code)
|
||||
unless description
|
||||
return render_unprocessable_entity field: :title if group || WikiPage.exists?(id: params[:id])
|
||||
return render_unprocessable_entity field: :title if group
|
||||
return head :not_found
|
||||
end
|
||||
|
||||
tag = description.tag
|
||||
tag_name = TagDescriptionOperation.compatibility_tag_name(tag)
|
||||
if tag_name.name.include?(':') || (group && title != tag_name.name)
|
||||
return render_unprocessable_entity field: :title
|
||||
end
|
||||
tag_name = tag.tag_name(locale.language_code)
|
||||
return render_unprocessable_entity field: :title unless tag_name
|
||||
return render_unprocessable_entity field: :title if tag_name.name.include?(':')
|
||||
|
||||
base_revision_id = params[:base_revision_id].presence
|
||||
|
||||
@@ -208,7 +223,7 @@ class WikiPagesController < ApplicationController
|
||||
tag_name.update!(name: title) if title != old_title
|
||||
|
||||
message = params[:message].presence
|
||||
TagDescriptionOperation.update!(tag:, body:, updated_by_user: current_user,
|
||||
TagDescriptionOperation.update!(locale, tag:, body:, updated_by_user: current_user,
|
||||
message:, base_revision_id:)
|
||||
|
||||
if title != old_title
|
||||
@@ -227,10 +242,12 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
|
||||
def changes
|
||||
locale = resolve_locale!
|
||||
|
||||
id = params[:id].presence
|
||||
description = id && TagDescription.find_by(id:)
|
||||
description = id && TagDescription.find_by(id:, language_code: locale.language_code)
|
||||
if description
|
||||
return render json: description_changes(TagDescription.where(id: description.id))
|
||||
return render json: description_changes(locale, TagDescription.where(id: description.id))
|
||||
end
|
||||
|
||||
tag_group = id && TagGroup.find_by(id:)
|
||||
@@ -241,7 +258,8 @@ class WikiPagesController < ApplicationController
|
||||
json = []
|
||||
unless id
|
||||
json.concat(tag_group_changes(TagGroupVersion.all))
|
||||
json.concat(description_changes(TagDescription.where(language_code: 'ja')))
|
||||
json.concat(description_changes(locale,
|
||||
TagDescription.where(language_code: locale.language_code)))
|
||||
json = json.each_with_index.sort_by do |row, index|
|
||||
[-row.fetch(:timestamp).to_f, index]
|
||||
end.map(&:first)
|
||||
@@ -251,27 +269,7 @@ class WikiPagesController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def legacy_group_page? page
|
||||
return false unless page
|
||||
return true if page.title.start_with?(TagGroupRepr::PREFIX)
|
||||
|
||||
prefix = ApplicationRecord.sanitize_sql_like(TagGroupRepr::PREFIX)
|
||||
page.wiki_versions.where('title LIKE ?', "#{ prefix }%").exists?
|
||||
end
|
||||
|
||||
def legacy_update_identified? page, title, group
|
||||
return false unless page
|
||||
return true if page.title == title
|
||||
return false if title == group.name
|
||||
|
||||
namespace = page.title.split(':', 2)
|
||||
return true if namespace.size == 2 && title.start_with?("#{ namespace.first }:")
|
||||
|
||||
base = params[:base_revision_id].presence
|
||||
base && page.wiki_revisions.exists?(id: base)
|
||||
end
|
||||
|
||||
def render_tag_group group
|
||||
def render_tag_group locale, group
|
||||
version = if params[:version].present?
|
||||
group_version(group, params[:version])
|
||||
else
|
||||
@@ -279,16 +277,17 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
return head :not_found unless version
|
||||
|
||||
representation = if params[:version].present?
|
||||
TagGroupRepr.historical(version)
|
||||
else
|
||||
TagGroupRepr.one(group)
|
||||
end
|
||||
representation =
|
||||
if params[:version].present?
|
||||
TagGroupRepr.historical(locale, version)
|
||||
else
|
||||
TagGroupRepr.one(locale, group)
|
||||
end
|
||||
render_group_representation(representation, version)
|
||||
end
|
||||
|
||||
def render_group_version version
|
||||
representation = TagGroupRepr.historical(version)
|
||||
def render_group_version locale, version
|
||||
representation = TagGroupRepr.historical(locale, version)
|
||||
TagGroupMarkdownRenderer.render(representation)
|
||||
end
|
||||
|
||||
@@ -357,28 +356,7 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def render_wiki_page_or_404 page
|
||||
return head :not_found unless page
|
||||
|
||||
rev = find_revision(page)
|
||||
return head :not_found unless rev
|
||||
|
||||
if rev.redirect?
|
||||
return (
|
||||
redirect_to wiki_page_by_title_path(title: rev.redirect_page.title),
|
||||
status: :moved_permanently)
|
||||
end
|
||||
|
||||
body = rev.body
|
||||
revision_id = rev.id
|
||||
pred = page.pred_revision_id(revision_id)
|
||||
succ = page.succ_revision_id(revision_id)
|
||||
updated_at = rev.created_at
|
||||
|
||||
render json: WikiPageRepr.base(page).merge(body:, revision_id:, pred:, succ:, updated_at:)
|
||||
end
|
||||
|
||||
def render_wiki_page_by_tag_description description
|
||||
def render_wiki_page_by_tag_description locale, description
|
||||
versions = description_versions(description)
|
||||
historical = params[:version].present?
|
||||
rev = historical ? versions.find_by(id: params[:version]) :
|
||||
@@ -389,7 +367,7 @@ class WikiPagesController < ApplicationController
|
||||
succ = versions.find_by(version_no: rev.version_no + 1)&.id
|
||||
body = historical ? rev.body : description.body
|
||||
updated_at = historical ? rev.created_at : description.updated_at
|
||||
render json: WikiPageRepr.tag_description(description).merge(
|
||||
render json: WikiPageRepr.tag_description(locale, description).merge(
|
||||
body:, revision_id: rev.id, pred:, succ:, updated_at:)
|
||||
end
|
||||
|
||||
@@ -398,13 +376,14 @@ class WikiPagesController < ApplicationController
|
||||
language_code: description.language_code)
|
||||
end
|
||||
|
||||
def tag_description_by_title title
|
||||
def tag_description_by_title locale, title
|
||||
TagDescription.joins(tag: :tag_names)
|
||||
.find_by(language_code: 'ja', tag_names: { language_code: 'ja', name: title })
|
||||
.find_by(language_code: locale.language_code,
|
||||
tag_names: { language_code: locale.language_code, name: title })
|
||||
end
|
||||
|
||||
def description_changes descriptions
|
||||
versions = TagDescriptionVersion.where(language_code: 'ja',
|
||||
def description_changes locale, descriptions
|
||||
versions = TagDescriptionVersion.where(language_code: locale.language_code,
|
||||
tag_id: descriptions.select(:tag_id))
|
||||
.order(id: :desc).limit(200).to_a
|
||||
return [] if versions.empty?
|
||||
@@ -413,7 +392,7 @@ class WikiPagesController < ApplicationController
|
||||
.includes(tag: :tag_names).index_by(&:tag_id)
|
||||
users = User.where(id: versions.map(&:created_by_user_id)).index_by(&:id)
|
||||
predecessors = TagDescriptionVersion.where(
|
||||
language_code: 'ja', tag_id: by_tag.keys,
|
||||
language_code: locale.language_code, tag_id: by_tag.keys,
|
||||
version_no: versions.map { |version| version.version_no - 1 })
|
||||
.pluck(:tag_id, :version_no, :id)
|
||||
.to_h { |tag_id, version_no, id| [[tag_id, version_no], id] }
|
||||
@@ -423,7 +402,7 @@ class WikiPagesController < ApplicationController
|
||||
{ revision_id: version.id,
|
||||
pred: predecessors[[version.tag_id, version.version_no - 1]],
|
||||
succ: nil,
|
||||
wiki_page: { id: description.id, title: description.tag.name('ja'),
|
||||
wiki_page: { id: description.id, title: description.tag.name(locale.language_code),
|
||||
deprecated_at: description.tag.deprecated_at },
|
||||
user: user && { id: user.id, name: user.name },
|
||||
kind: 'content',
|
||||
@@ -432,14 +411,6 @@ class WikiPagesController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def find_revision page
|
||||
if params[:version].present?
|
||||
page.wiki_revisions.find_by(id: params[:version])
|
||||
else
|
||||
page.current_revision
|
||||
end
|
||||
end
|
||||
|
||||
def render_wiki_conflict err
|
||||
render json: { error: 'conflict', message: err.message }, status: :conflict
|
||||
end
|
||||
|
||||
@@ -13,14 +13,13 @@ class Locale < ApplicationRecord
|
||||
def generate_tag_names!
|
||||
ApplicationRecord.transaction do
|
||||
tag_ids = TagName.where(language_code:, primary_flg: true).pluck(:tag_id)
|
||||
Tag.where.not(id: tag_ids).find_each do
|
||||
Tag.where.not(id: tag_ids).includes(:tag_names).find_each do
|
||||
TagName.create!(tag_id: _1.id,
|
||||
language_code:,
|
||||
name: TagName.generate_name(self, _1, _1.tag_name('ja')),
|
||||
script_code:,
|
||||
primary_flg: true,
|
||||
auto_generated: true,
|
||||
canonical_id: nil)
|
||||
auto_generated: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+14
-21
@@ -91,7 +91,6 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
end
|
||||
def name(language_code) = tag_name(language_code)&.name
|
||||
def wiki_page = tag_name('ja')&.wiki_page
|
||||
|
||||
def system?
|
||||
tag_name('ja')&.name.in?(SYSTEM_TAG_NAMES)
|
||||
@@ -260,16 +259,13 @@ class Tag < ApplicationRecord
|
||||
name = name.to_s.strip
|
||||
|
||||
ApplicationRecord.transaction do
|
||||
tn = TagName.find_or_create_by!(language_code:, name:) do
|
||||
_1.script_code = locale.script_code
|
||||
_1.primary_flg = true
|
||||
end
|
||||
tn = TagName.find_by(language_code:, name:)
|
||||
next tn.tag if tn
|
||||
|
||||
tag = tn.tag
|
||||
next tag if tag
|
||||
|
||||
tag = Tag.create!(tag_name_id: tn.id, category:)
|
||||
tn.update!(tag:, primary_flg: true, canonical_id: nil, auto_generated: false)
|
||||
tag = Tag.create!(category:)
|
||||
tn = TagName.create!(tag:, language_code:, name:,
|
||||
script_code: locale.script_code, primary_flg: true,
|
||||
auto_generated: false)
|
||||
generate_tag_names!(locale, tag, tn)
|
||||
tag.association(:tag_names).reset
|
||||
|
||||
@@ -303,20 +299,18 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
source_tag_name = source_tag.tag_name('ja')
|
||||
|
||||
if source_tag_name.wiki_page.present?
|
||||
raise ActiveRecord::RecordInvalid.new(source_tag_name)
|
||||
if source_tag.tag_descriptions.exists?
|
||||
source_tag.errors.add(:base, :invalid)
|
||||
raise ActiveRecord::RecordInvalid, source_tag
|
||||
end
|
||||
|
||||
TagVersioning.record!(source_tag, event_type: :discard, created_by_user:)
|
||||
source_tag.tag_names.update_all(tag_id: target_tag.id, primary_flg: false,
|
||||
updated_at: Time.current)
|
||||
target_languages = target_tag.tag_names.where(primary_flg: true).pluck(:language_code)
|
||||
source_tag.tag_names.where(language_code: target_languages)
|
||||
.update_all(primary_flg: false, auto_generated: false, updated_at: Time.current)
|
||||
source_tag.tag_names.update_all(tag_id: target_tag.id, updated_at: Time.current)
|
||||
source_tag.destroy!
|
||||
|
||||
source_tag_name.update_columns(canonical_id: target_tag.tag_name_id,
|
||||
updated_at: Time.current)
|
||||
|
||||
TagVersioning.record!(target_tag, event_type: :update, created_by_user:)
|
||||
end
|
||||
|
||||
@@ -376,8 +370,7 @@ class Tag < ApplicationRecord
|
||||
name: TagName.generate_name(locale, tag, base_tag_name),
|
||||
script_code:,
|
||||
primary_flg: true,
|
||||
auto_generated: true,
|
||||
canonical_id: nil)
|
||||
auto_generated: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
class TagDescriptionVersion < ApplicationRecord
|
||||
;
|
||||
before_create { SharedResourceIdAllocator.assign!(self) }
|
||||
end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class TagGroup < ApplicationRecord
|
||||
before_create { SharedResourceIdAllocator.assign!(self) }
|
||||
before_destroy :delete_current_structure!
|
||||
|
||||
belongs_to :created_by_user, class_name: 'User', optional: true
|
||||
belongs_to :updated_by_user, class_name: 'User', optional: true
|
||||
@@ -11,8 +12,16 @@ class TagGroup < ApplicationRecord
|
||||
has_many :placements, -> { order(:position) },
|
||||
class_name: 'TagGroupPlacement', dependent: :delete_all
|
||||
has_many :versions, -> { order(:version_no) },
|
||||
class_name: 'TagGroupVersion', dependent: :delete_all
|
||||
class_name: 'TagGroupVersion'
|
||||
|
||||
validates :name, presence: true, uniqueness: true, length: { maximum: 255 }
|
||||
validates :version_no, numericality: { only_integer: true, greater_than: 0 }
|
||||
|
||||
private
|
||||
|
||||
def delete_current_structure!
|
||||
placements.reorder(position: :desc).each(&:delete)
|
||||
tag_group_tags.delete_all
|
||||
tag_group_groups.delete_all
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
class TagGroupVersion < ApplicationRecord
|
||||
include VersionRecord
|
||||
|
||||
before_create { SharedResourceIdAllocator.assign!(self) }
|
||||
|
||||
belongs_to :tag_group
|
||||
|
||||
scope :ordered, -> { order(:version_no) }
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
class TagName < ApplicationRecord
|
||||
belongs_to :language, foreign_key: :language_code, primary_key: :code
|
||||
belongs_to :tag, optional: true
|
||||
has_one :wiki_page
|
||||
belongs_to :tag
|
||||
|
||||
validates :name, presence: true,
|
||||
length: { maximum: 255 },
|
||||
uniqueness: { scope: :language_code }
|
||||
|
||||
validate :alias_name_must_not_have_prefix
|
||||
validate :alias_must_not_have_wiki_page
|
||||
validate :name_must_be_sanitised
|
||||
validate :name_mustnt_start_with_nico
|
||||
validate :generated_name_must_be_reserved
|
||||
@@ -21,14 +19,16 @@ class TagName < ApplicationRecord
|
||||
|
||||
tns = TagName.where(language_code: locale.language_code, name: names).index_by(&:name)
|
||||
|
||||
primary_names = TagName.where(
|
||||
language_code: locale.language_code, tag_id: tns.values.map(&:tag_id),
|
||||
primary_flg: true).pluck(:tag_id, :name).to_h
|
||||
|
||||
names.map { |name|
|
||||
tag_name = tns[name]
|
||||
if !tag_name || tag_name.primary? || tag_name.tag_id.nil?
|
||||
if !tag_name || tag_name.primary?
|
||||
name
|
||||
else
|
||||
TagName.find_by(language_code: locale.language_code,
|
||||
tag_id: tag_name.tag_id,
|
||||
primary_flg: true)&.name || name
|
||||
primary_names[tag_name.tag_id] || name
|
||||
end
|
||||
}.uniq
|
||||
end
|
||||
@@ -53,12 +53,6 @@ class TagName < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def alias_must_not_have_wiki_page
|
||||
if !(primary?) && wiki_page
|
||||
errors.add :primary_flg, 'Wiki 参照がある名前はエーリアスになれません.'
|
||||
end
|
||||
end
|
||||
|
||||
def name_must_be_sanitised
|
||||
if name? && name != TagNameSanitisationRule.sanitise(name)
|
||||
errors.add :name, '名前に使用できない文字が含まれてゐます.'
|
||||
|
||||
@@ -16,37 +16,36 @@ class TagNameSanitisationRule < ApplicationRecord
|
||||
|
||||
def apply!
|
||||
Language.find_each do |language|
|
||||
TagName.where(language:).find_each do |tn|
|
||||
name = sanitise(tn.name)
|
||||
next if name == tn.name
|
||||
|
||||
TagName.where(language:).find_each do |candidate|
|
||||
TagName.transaction do
|
||||
tn = TagName.find_by(id: candidate.id)
|
||||
next unless tn
|
||||
|
||||
name = sanitise(tn.name)
|
||||
next if name == tn.name
|
||||
|
||||
existing_tn = TagName.find_by(language:, name:)
|
||||
if existing_tn
|
||||
unless existing_tn.primary_flg
|
||||
existing_tn = TagName.find_by!(language:,
|
||||
tag_id: existing_tn.tag_id,
|
||||
primary_flg: true)
|
||||
source_tag = tn.tag
|
||||
target_tag = existing_tn ? existing_tn.tag : source_tag
|
||||
[source_tag, target_tag].uniq(&:id).sort_by(&:id).each(&:lock!)
|
||||
TagVersioning.ensure_snapshot!(source_tag, created_by_user: nil)
|
||||
|
||||
if existing_tn && target_tag != source_tag
|
||||
Tag.merge_tags!(target_tag, source_tag)
|
||||
tn.reload.destroy!
|
||||
elsif existing_tn
|
||||
if tn.primary?
|
||||
existing_tn.destroy!
|
||||
tn.update_columns(name:, updated_at: Time.current)
|
||||
else
|
||||
tn.destroy!
|
||||
end
|
||||
next if existing_tn.id == tn.id
|
||||
|
||||
existing_tag = existing_tn.tag
|
||||
source_tag = tn.tag
|
||||
|
||||
if existing_tag
|
||||
Tag.merge_tags!(existing_tag, source_tag) if tn.tag
|
||||
elsif source_tag
|
||||
tn.update_columns(tag_id: nil, updated_at: Time.current)
|
||||
source_tag.update_columns(tag_name_id: existing_tn.id, updated_at: Time.current)
|
||||
existing_tn.update_columns(tag_id: source_tag.id, updated_at: Time.current)
|
||||
end
|
||||
tn.destroy!
|
||||
|
||||
next
|
||||
else
|
||||
# TagName 側の自動サニタイズを回避
|
||||
tn.update_columns(name:, updated_at: Time.current)
|
||||
end
|
||||
|
||||
# TagName 側の自動サニタイズを回避
|
||||
tn.update_columns(name:, updated_at: Time.current)
|
||||
TagVersioning.record!(target_tag, event_type: :update, created_by_user: nil)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,10 +13,10 @@ class User < ApplicationRecord
|
||||
has_many :ip_addresses, through: :user_ips
|
||||
has_many :user_post_views, dependent: :destroy
|
||||
has_many :viewed_posts, through: :user_post_views, source: :post
|
||||
has_many :created_wiki_pages,
|
||||
class_name: 'WikiPage', foreign_key: :created_user_id, dependent: :nullify
|
||||
has_many :updated_wiki_pages,
|
||||
class_name: 'WikiPage', foreign_key: :updated_user_id, dependent: :nullify
|
||||
has_many :created_tag_descriptions, class_name: 'TagDescription',
|
||||
foreign_key: :created_by_user_id, dependent: :nullify
|
||||
has_many :updated_tag_descriptions, class_name: 'TagDescription',
|
||||
foreign_key: :updated_by_user_id, dependent: :nullify
|
||||
|
||||
def viewed?(post) = user_post_views.exists?(post_id: post.id)
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
class WikiLine < ApplicationRecord
|
||||
has_many :wiki_revision_lines, dependent: :restrict_with_exception
|
||||
|
||||
validates :sha256, presence: true, uniqueness: true, length: { is: 64 }
|
||||
validates :body, presence: true
|
||||
|
||||
def self.upsert_by_body! body
|
||||
sha = Digest::SHA256.hexdigest(body)
|
||||
now = Time.current
|
||||
|
||||
upsert(sha256: sha, body:, created_at: now, updated_at: now)
|
||||
|
||||
find_by!(sha256: sha)
|
||||
end
|
||||
end
|
||||
@@ -1,58 +0,0 @@
|
||||
require 'set'
|
||||
|
||||
|
||||
class WikiPage < ApplicationRecord
|
||||
include MyDiscard
|
||||
|
||||
has_many :wiki_revisions, dependent: :destroy
|
||||
belongs_to :created_user, class_name: 'User'
|
||||
belongs_to :updated_user, class_name: 'User'
|
||||
|
||||
has_many :redirected_from_revisions,
|
||||
class_name: 'WikiRevision',
|
||||
foreign_key: :redirect_page_id,
|
||||
dependent: :nullify
|
||||
|
||||
has_many :wiki_versions
|
||||
|
||||
attribute :version_no, :integer, default: 1
|
||||
|
||||
belongs_to :tag_name
|
||||
validates :tag_name, presence: true
|
||||
validates :body, presence: true
|
||||
|
||||
def title = tag_name.name
|
||||
def deprecated_at = tag_name.tag&.deprecated_at
|
||||
|
||||
def title= val
|
||||
(self.tag_name ||= build_tag_name).name = val
|
||||
end
|
||||
|
||||
def current_revision = wiki_revisions.order(id: :desc).first
|
||||
|
||||
def resolve_redirect limit: 10
|
||||
page = self
|
||||
visited = Set.new
|
||||
|
||||
limit.times do
|
||||
return page if visited.include?(page.id)
|
||||
|
||||
visited.add(page.id)
|
||||
|
||||
rev = page.current_revision
|
||||
return page if !(rev&.redirect?) || !(rev.redirect_page)
|
||||
|
||||
page = rev.redirect_page
|
||||
end
|
||||
|
||||
page
|
||||
end
|
||||
|
||||
def pred_revision_id revision_id
|
||||
wiki_revisions.where('id < ?', revision_id).order(id: :desc).limit(1).pick(:id)
|
||||
end
|
||||
|
||||
def succ_revision_id revision_id
|
||||
wiki_revisions.where('id > ?', revision_id).order(id: :asc).limit(1).pick(:id)
|
||||
end
|
||||
end
|
||||
@@ -1,55 +0,0 @@
|
||||
class WikiRevision < ApplicationRecord
|
||||
belongs_to :wiki_page
|
||||
belongs_to :base_revision, class_name: 'WikiRevision', optional: true
|
||||
belongs_to :created_user, class_name: 'User'
|
||||
belongs_to :redirect_page, class_name: 'WikiPage', optional: true
|
||||
|
||||
has_many :wiki_revision_lines, dependent: :delete_all
|
||||
has_many :wiki_lines, through: :wiki_revision_lines
|
||||
|
||||
enum :kind, content: 0, redirect: 1
|
||||
|
||||
validates :kind, presence: true
|
||||
validates :lines_count, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :tree_sha256, length: { is: 64 }, allow_nil: true
|
||||
|
||||
validate :kind_consistency
|
||||
|
||||
def body
|
||||
return unless content?
|
||||
|
||||
wiki_revision_lines
|
||||
.includes(:wiki_line)
|
||||
.order(:position)
|
||||
.map { |rev| rev.wiki_line.body }
|
||||
.join("\n")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def kind_consistency
|
||||
if content?
|
||||
if tree_sha256.blank?
|
||||
errors.add(:tree_sha256, '種類がページの場合は必須です.')
|
||||
end
|
||||
|
||||
if redirect_page_id.present?
|
||||
errors.add(:redirect_page_id, '種類がページの場合は空である必要があります.')
|
||||
end
|
||||
end
|
||||
|
||||
if redirect?
|
||||
if redirect_page_id.blank?
|
||||
errors.add(:redirect_page_id, '種類がリダイレクトの場合は必須です.')
|
||||
end
|
||||
|
||||
if tree_sha256.present?
|
||||
errors.add(:tree_sha256, '種類がリダイレクトの場合は空である必要があります.')
|
||||
end
|
||||
|
||||
if lines_count.to_i > 0
|
||||
errors.add(:lines_count, '種類がリダイレクトの場合は 0 である必要があります.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,8 +0,0 @@
|
||||
class WikiRevisionLine < ApplicationRecord
|
||||
belongs_to :wiki_revision
|
||||
belongs_to :wiki_line
|
||||
|
||||
validates :position, presence: true,
|
||||
numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :position, uniqueness: { scope: :wiki_revision_id }
|
||||
end
|
||||
@@ -1,8 +0,0 @@
|
||||
class WikiVersion < ApplicationRecord
|
||||
include VersionRecord
|
||||
|
||||
belongs_to :wiki_page
|
||||
|
||||
validates :title, presence: true
|
||||
validates :body, presence: true
|
||||
end
|
||||
@@ -3,7 +3,12 @@ module TagGroupRepr
|
||||
|
||||
module_function
|
||||
|
||||
def many groups
|
||||
def name_from_title title
|
||||
prefix = [PREFIX, 'tag_group:'].find { title.downcase.start_with?(_1) }
|
||||
title[prefix.length..] if prefix
|
||||
end
|
||||
|
||||
def many locale, groups
|
||||
ids = groups.map(&:id)
|
||||
return [] if ids.empty?
|
||||
|
||||
@@ -14,14 +19,20 @@ module TagGroupRepr
|
||||
placements = TagGroupPlacement.where(tag_group_id: ids).order(:position)
|
||||
.group_by(&:tag_group_id)
|
||||
|
||||
target_ids = tag_members.values.flatten.map(&:tag_id) +
|
||||
group_members.values.flatten.map(&:child_tag_group_id) +
|
||||
target_ids =
|
||||
tag_members.values.flatten.map(&:tag_id) +
|
||||
placements.values.flatten.filter_map(&:tag_id)
|
||||
child_ids = group_members.values.flatten.map(&:child_tag_group_id) +
|
||||
child_ids =
|
||||
group_members.values.flatten.map(&:child_tag_group_id) +
|
||||
placements.values.flatten.filter_map(&:child_tag_group_id)
|
||||
tags = Tag.select(:id, :category).where(id: target_ids.uniq).index_by(&:id)
|
||||
names = TagName.where(tag_id: target_ids.uniq, language_code: 'ja', primary_flg: true)
|
||||
.pluck(:tag_id, :name).to_h
|
||||
names =
|
||||
TagName
|
||||
.where(tag_id: target_ids.uniq,
|
||||
language_code: locale.language_code,
|
||||
primary_flg: true)
|
||||
.pluck(:tag_id, :name)
|
||||
.to_h
|
||||
child_groups = TagGroup.where(id: child_ids.uniq).index_by(&:id)
|
||||
|
||||
groups.map do |group|
|
||||
@@ -32,8 +43,8 @@ module TagGroupRepr
|
||||
end
|
||||
end
|
||||
|
||||
def one group
|
||||
many([group]).first
|
||||
def one locale, group
|
||||
many(locale, [group]).first
|
||||
end
|
||||
|
||||
def tag_group wiki_page
|
||||
@@ -48,7 +59,7 @@ module TagGroupRepr
|
||||
TagGroupVersion.find_by(tag_group_id: group.id, version_no: group.version_no)
|
||||
end
|
||||
|
||||
def historical version
|
||||
def historical locale, version
|
||||
members = Array(version.members)
|
||||
placements = Array(version.placements)
|
||||
tag_references = members.filter_map { |member| member['tag_id'] } +
|
||||
@@ -68,10 +79,10 @@ module TagGroupRepr
|
||||
[member.fetch('tag_group_id'), member.fetch('version_no')]
|
||||
end.to_h
|
||||
historical_members = members.map do |member|
|
||||
historical_member(member, tag_versions, group_versions)
|
||||
historical_member(locale, member, tag_versions, group_versions)
|
||||
end
|
||||
historical_placements = placements.map do |node|
|
||||
historical_placement(node, tag_versions, group_versions,
|
||||
historical_placement(locale, node, tag_versions, group_versions,
|
||||
tag_version_numbers, group_version_numbers)
|
||||
end
|
||||
{ 'id' => version.tag_group_id,
|
||||
@@ -89,7 +100,7 @@ module TagGroupRepr
|
||||
def group_json group, tag_members, group_members, rows, tags, names, child_groups
|
||||
tree = placement_tree(rows, tags, names, child_groups)
|
||||
members = tag_members.sort_by(&:tag_id).map do |member|
|
||||
{ 'tag' => tag_json(tags.fetch(member.tag_id), names.fetch(member.tag_id)) }
|
||||
{ 'tag' => tag_json(tags.fetch(member.tag_id), names[member.tag_id]) }
|
||||
end
|
||||
members.concat(group_members.sort_by(&:child_tag_group_id).map do |member|
|
||||
{ 'tag_group' => group_json_reference(child_groups.fetch(member.child_tag_group_id)) }
|
||||
@@ -115,7 +126,7 @@ module TagGroupRepr
|
||||
build = lambda do |parent|
|
||||
by_parent.fetch(parent, []).sort_by(&:position).map do |row|
|
||||
node = if row.tag_id
|
||||
{ 'tag' => tag_json(tags.fetch(row.tag_id), names.fetch(row.tag_id)) }
|
||||
{ 'tag' => tag_json(tags.fetch(row.tag_id), names[row.tag_id]) }
|
||||
elsif row.child_tag_group_id
|
||||
{ 'tag_group' => group_json_reference(child_groups.fetch(row.child_tag_group_id)) }
|
||||
else
|
||||
@@ -153,11 +164,11 @@ module TagGroupRepr
|
||||
end
|
||||
end
|
||||
|
||||
def historical_member member, tag_versions, group_versions
|
||||
def historical_member locale, member, tag_versions, group_versions
|
||||
if member['tag_id']
|
||||
id = member.fetch('tag_id')
|
||||
version = tag_versions.fetch([id, member.fetch('version_no')])
|
||||
{ 'tag' => { 'id' => id, 'name' => japanese_version_name(version),
|
||||
{ 'tag' => { 'id' => id, 'name' => version_name(locale, version),
|
||||
'category' => version.category } }
|
||||
else
|
||||
id = member.fetch('tag_group_id')
|
||||
@@ -166,13 +177,13 @@ module TagGroupRepr
|
||||
end
|
||||
end
|
||||
|
||||
def historical_placement(node, tag_versions, group_versions,
|
||||
tag_version_numbers, group_version_numbers)
|
||||
def historical_placement locale, node, tag_versions, group_versions,
|
||||
tag_version_numbers, group_version_numbers
|
||||
result = if node['tag_id']
|
||||
id = node.fetch('tag_id')
|
||||
version_no = tag_version_numbers.fetch(id)
|
||||
version = tag_versions.fetch([id, version_no])
|
||||
{ 'tag' => { 'id' => id, 'name' => japanese_version_name(version),
|
||||
{ 'tag' => { 'id' => id, 'name' => version_name(locale, version),
|
||||
'category' => version.category } }
|
||||
elsif node['tag_group_id']
|
||||
id = node.fetch('tag_group_id')
|
||||
@@ -183,12 +194,12 @@ module TagGroupRepr
|
||||
{ 'section_name' => node.fetch('section_name'), 'description' => node['description'] }
|
||||
end
|
||||
result.merge('children' => Array(node['children']).map do |child|
|
||||
historical_placement(child, tag_versions, group_versions,
|
||||
historical_placement(locale, child, tag_versions, group_versions,
|
||||
tag_version_numbers, group_version_numbers)
|
||||
end)
|
||||
end
|
||||
|
||||
def japanese_version_name version
|
||||
version.names.fetch('ja').fetch('name')
|
||||
def version_name locale, version
|
||||
version.names[locale.language_code]&.fetch('name')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,19 +2,14 @@
|
||||
|
||||
|
||||
module WikiPageRepr
|
||||
BASE = { methods: [:title, :deprecated_at] }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base wiki_page
|
||||
wiki_page.as_json(BASE)
|
||||
end
|
||||
|
||||
def tag_description description
|
||||
def tag_description locale, description
|
||||
tag = description.tag
|
||||
|
||||
{ 'id' => description.id,
|
||||
'resource_type' => 'tag_description',
|
||||
'tag_name_id' => tag.tag_name('ja')&.id,
|
||||
'tag_name_id' => tag.tag_name(locale.language_code)&.id,
|
||||
'body' => description.body,
|
||||
'created_user_id' => description.created_by_user_id,
|
||||
'updated_user_id' => description.updated_by_user_id,
|
||||
@@ -22,15 +17,11 @@ module WikiPageRepr
|
||||
'updated_at' => description.updated_at,
|
||||
'discarded_at' => nil,
|
||||
'version_no' => description.version_no,
|
||||
'title' => tag.name('ja'),
|
||||
'title' => tag.name(locale.language_code),
|
||||
'deprecated_at' => tag.deprecated_at }
|
||||
end
|
||||
|
||||
def tag_group tag_group
|
||||
TagGroupRepr.tag_group(tag_group).merge('resource_type' => 'tag_group')
|
||||
end
|
||||
|
||||
def many wiki_pages
|
||||
wiki_pages.map { |p| base(p) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -95,14 +95,10 @@ class PostCreatePlan
|
||||
def build_default_tag_specs locale, direct_tag_specs
|
||||
default_tag_specs = []
|
||||
if direct_tag_specs.length < 10 && direct_tag_specs.none? { _1[:name] == TAGME_TAG_NAME }
|
||||
default_tag_specs << {
|
||||
name: TAGME_TAG_NAME,
|
||||
category: :meta }
|
||||
default_tag_specs << { name: TAGME_TAG_NAME, category: :meta }
|
||||
end
|
||||
if direct_tag_specs.none? { deerjikist_tag_spec?(locale, _1) }
|
||||
default_tag_specs << {
|
||||
name: NO_DEERJIKIST_TAG_NAME,
|
||||
category: :meta }
|
||||
default_tag_specs << { name: NO_DEERJIKIST_TAG_NAME, category: :meta }
|
||||
end
|
||||
|
||||
default_tag_specs
|
||||
@@ -117,9 +113,9 @@ class PostCreatePlan
|
||||
end
|
||||
|
||||
def validate_new_tag_spec! locale, spec
|
||||
tag_name = TagName.new(name: spec[:name], language_code: locale.language_code,
|
||||
script_code: locale.script_code, primary_flg: true)
|
||||
tag = Tag.new(category: spec[:category])
|
||||
tag_name = TagName.new(tag:, name: spec[:name], language_code: locale.language_code,
|
||||
script_code: locale.script_code, primary_flg: true)
|
||||
return if tag_name.valid? && tag.valid?
|
||||
|
||||
post = Post.new
|
||||
@@ -140,10 +136,10 @@ class PostCreatePlan
|
||||
end
|
||||
expanded_parent_specs =
|
||||
Tag.expand_parent_tags(existing_snapshot_tags)
|
||||
.reject(&:deprecated?)
|
||||
.map { |tag|
|
||||
{ name: tag.name(locale.language_code),
|
||||
category: tag.category.to_sym }
|
||||
.reject(&:deprecated?)
|
||||
.map { |tag|
|
||||
{ name: tag.name(locale.language_code),
|
||||
category: tag.category.to_sym }
|
||||
}
|
||||
merge_tag_specs(snapshot_tag_specs + expanded_parent_specs)
|
||||
end
|
||||
@@ -164,9 +160,10 @@ class PostCreatePlan
|
||||
def existing_tags_by_name locale
|
||||
@existing_tags_by_name ||= begin
|
||||
names = tag_names.map { canonical_tag_name_without_sections(locale, _1) }.uniq
|
||||
TagName.where(language_code: locale.language_code, primary_flg: true, name: names)
|
||||
.includes(:tag)
|
||||
.each_with_object({ }) { |tag_name, tags| tags[tag_name.name] = tag_name.tag }
|
||||
TagName
|
||||
.where(language_code: locale.language_code, primary_flg: true, name: names)
|
||||
.includes(:tag)
|
||||
.each_with_object({ }) { |tag_name, tags| tags[tag_name.name] = tag_name.tag }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -4,7 +4,14 @@ require 'digest'
|
||||
class SharedResourceIdAllocator
|
||||
SPACES = {
|
||||
'tags' => ['tags', 'external_tags'],
|
||||
'wiki' => ['tag_descriptions', 'tag_groups'] }.freeze
|
||||
'wiki' => ['tag_descriptions', 'tag_groups'],
|
||||
'wiki_history' => ['tag_description_versions', 'tag_group_versions'] }.freeze
|
||||
HISTORY_IDS = {
|
||||
'tags' => [
|
||||
['tag_versions', 'tag_id'],
|
||||
['nico_tag_versions', 'tag_id'],
|
||||
['tag_description_versions', 'tag_id']],
|
||||
'wiki' => [['tag_group_versions', 'tag_group_id']] }.freeze
|
||||
LOCK_TIMEOUT = 10
|
||||
|
||||
def self.assign! record
|
||||
@@ -13,7 +20,9 @@ class SharedResourceIdAllocator
|
||||
|
||||
connection = record.class.connection
|
||||
transaction = record.class.current_transaction
|
||||
raise ActiveRecord::ActiveRecordError, 'Shared ID requires a transaction' unless transaction.open?
|
||||
unless transaction.open?
|
||||
raise ActiveRecord::ActiveRecordError, 'Shared ID requires a transaction'
|
||||
end
|
||||
|
||||
database = connection.pool.db_config.database
|
||||
name = "btrc:ids:#{ Digest::SHA256.hexdigest(database)[0, 16] }:#{ space }"
|
||||
@@ -41,16 +50,42 @@ class SharedResourceIdAllocator
|
||||
raise
|
||||
end
|
||||
|
||||
# 明示-tic な Id. 指定は許可する.
|
||||
return if record.id.present?
|
||||
|
||||
# Locking reads see committed maxima even after earlier repeatable reads.
|
||||
maxima = record.class.uncached do
|
||||
tables.map do |table|
|
||||
next_id = record.class.uncached do
|
||||
historical_max = HISTORY_IDS.fetch(space, []).map { |table, column|
|
||||
quoted_table = connection.quote_table_name(table)
|
||||
connection.select_value(
|
||||
"SELECT id FROM #{ quoted_table } ORDER BY id DESC LIMIT 1 FOR UPDATE").to_i
|
||||
end
|
||||
quoted_column = connection.quote_column_name(column)
|
||||
connection.select_value("SELECT MAX(#{ quoted_column }) FROM #{ quoted_table }").to_i
|
||||
}.max.to_i
|
||||
|
||||
[next_ids(connection, tables).max, historical_max + 1].max
|
||||
end
|
||||
record.id = maxima.max + 1
|
||||
|
||||
record.id = next_id
|
||||
end
|
||||
|
||||
def self.next_ids connection, tables
|
||||
expiry = connection.select_value('SELECT @@SESSION.information_schema_stats_expiry').to_i
|
||||
begin
|
||||
connection.execute('SET SESSION information_schema_stats_expiry = 0')
|
||||
quoted_tables = tables.map { connection.quote(_1) }.join(', ')
|
||||
rows = connection.select_all(<<~SQL).to_a
|
||||
SELECT TABLE_NAME, ENGINE, AUTO_INCREMENT
|
||||
FROM information_schema.tables
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME IN (#{ quoted_tables })
|
||||
SQL
|
||||
persistent = rows.all? do |row|
|
||||
row['ENGINE'] == 'InnoDB' && row['AUTO_INCREMENT'].to_i.positive?
|
||||
end
|
||||
unless rows.size == tables.size && persistent
|
||||
raise ActiveRecord::ActiveRecordError, 'Persistent InnoDB counters are required'
|
||||
end
|
||||
rows.map { _1.fetch('AUTO_INCREMENT').to_i }
|
||||
ensure
|
||||
connection.execute("SET SESSION information_schema_stats_expiry = #{ expiry }")
|
||||
end
|
||||
end
|
||||
private_class_method :next_ids
|
||||
end
|
||||
|
||||
@@ -1,72 +1,67 @@
|
||||
class TagDescriptionOperation
|
||||
LANGUAGE_CODE = 'ja'
|
||||
class Conflict < StandardError
|
||||
;
|
||||
end
|
||||
|
||||
def self.create!(tag:, body:, created_by_user:, message: nil)
|
||||
def self.create! locale, tag:, body:, created_by_user:, message: nil
|
||||
ApplicationRecord.transaction do
|
||||
tag.lock!
|
||||
raise ActiveRecord::RecordNotUnique if TagDescription.exists?(
|
||||
tag_id: tag.id, language_code: LANGUAGE_CODE)
|
||||
tag_id: tag.id, language_code: locale.language_code)
|
||||
|
||||
page = Wiki::CompatibilityWriter.create!(
|
||||
tag_name: compatibility_tag_name(tag), body:, created_by_user:, message:)
|
||||
revision = page.wiki_revisions.order(id: :desc).first
|
||||
record_version!(tag:, revision:, body: revision.body, user: created_by_user,
|
||||
description: nil)
|
||||
page
|
||||
description = TagDescription.new(tag:, language_code: locale.language_code)
|
||||
body = normalise_body!(description, body)
|
||||
record_version!(description:, body:, user: created_by_user)
|
||||
|
||||
description
|
||||
end
|
||||
end
|
||||
|
||||
def self.update!(tag:, body:, updated_by_user:, message: nil,
|
||||
base_revision_id: nil)
|
||||
def self.update! locale, tag:, body:, updated_by_user:, message: nil, base_revision_id: nil
|
||||
ApplicationRecord.transaction do
|
||||
tag.lock!
|
||||
description = TagDescription.lock.find_by!(
|
||||
tag_id: tag.id, language_code: LANGUAGE_CODE)
|
||||
tag_name = compatibility_tag_name(tag)
|
||||
page = WikiPage.lock.find_by(tag_name_id: tag_name.id)
|
||||
unless page
|
||||
versions = TagDescriptionVersion.where(tag_id: tag.id, language_code: LANGUAGE_CODE)
|
||||
if base_revision_id.present? && versions.maximum(:id) != base_revision_id.to_i
|
||||
raise Wiki::Commit::Conflict, '競合が発生してゐます.'
|
||||
end
|
||||
page = Wiki::CompatibilityWriter.create!(
|
||||
tag_name:, body:, created_by_user: updated_by_user, message:)
|
||||
revision = page.current_revision
|
||||
record_version!(tag:, revision:, body: revision.body,
|
||||
user: updated_by_user, description:)
|
||||
next revision
|
||||
tag_id: tag.id, language_code: locale.language_code)
|
||||
body = normalise_body!(description, body)
|
||||
latest = TagDescriptionVersion.find_by(
|
||||
tag_id: tag.id, language_code: locale.language_code,
|
||||
version_no: description.version_no)
|
||||
if base_revision_id.present? && latest&.id != base_revision_id.to_i
|
||||
raise Conflict, '競合が発生してゐます.'
|
||||
end
|
||||
revision = Wiki::CompatibilityWriter.update!(
|
||||
page:, body:, updated_by_user:, message:,
|
||||
base_revision_id:)
|
||||
next unless revision
|
||||
next if latest && normalise_body(description.body) == body
|
||||
|
||||
record_version!(tag:, revision:, body: revision.body,
|
||||
user: updated_by_user, description:)
|
||||
revision
|
||||
record_version!(description:, body:, user: updated_by_user)
|
||||
end
|
||||
end
|
||||
|
||||
def self.compatibility_tag_name tag
|
||||
tag.tag_name(LANGUAGE_CODE) || TagName.find_by!(id: tag.tag_name_id,
|
||||
language_code: LANGUAGE_CODE)
|
||||
end
|
||||
|
||||
def self.record_version! tag:, revision:, body:, user:, description:
|
||||
version_no = description ? description.version_no + 1 : 1
|
||||
def self.record_version! description:, body:, user:
|
||||
creating = description.new_record?
|
||||
version_no = creating ? 1 : description.version_no + 1
|
||||
description.created_by_user_id = user.id if creating
|
||||
description.update!(body:, version_no:, updated_by_user_id: user.id)
|
||||
TagDescriptionVersion.create!(
|
||||
id: revision.id, tag_id: tag.id, language_code: LANGUAGE_CODE,
|
||||
version_no:, event_type: description ? 'update' : 'create', body:,
|
||||
created_by_user_id: user.id, created_at: revision.created_at)
|
||||
if description
|
||||
description.update!(body:, version_no:, updated_by_user_id: user.id)
|
||||
else
|
||||
TagDescription.create!(
|
||||
tag_id: tag.id,
|
||||
language_code: LANGUAGE_CODE, version_no:, body:,
|
||||
created_by_user_id: user.id, updated_by_user_id: user.id)
|
||||
end
|
||||
tag_id: description.tag_id, language_code: description.language_code,
|
||||
version_no:, event_type: creating ? 'create' : 'update', body:,
|
||||
source_language_code: description.source_language_code,
|
||||
source_version_no: description.source_version_no,
|
||||
created_by_user_id: user.id, created_at: Time.current)
|
||||
end
|
||||
|
||||
private_class_method :record_version!
|
||||
|
||||
def self.normalise_body body
|
||||
body.to_s.gsub(/\r\n?/, "\n")
|
||||
.encode('UTF-8', invalid: :replace, undef: :replace, replace: '🖕')
|
||||
.sub(/\n+\z/, '')
|
||||
end
|
||||
private_class_method :normalise_body
|
||||
|
||||
def self.normalise_body! description, body
|
||||
normalised = normalise_body(body)
|
||||
return normalised if normalised.present?
|
||||
|
||||
description.errors.add(:body, :blank)
|
||||
raise ActiveRecord::RecordInvalid, description
|
||||
end
|
||||
private_class_method :normalise_body!
|
||||
end
|
||||
|
||||
@@ -7,8 +7,8 @@ class TagGroupMarkdownParser
|
||||
;
|
||||
end
|
||||
|
||||
def self.parse(body:, group: nil)
|
||||
new(body:, group:).parse
|
||||
def self.parse(locale, body:, group: nil)
|
||||
new(body:, group:).parse(locale)
|
||||
end
|
||||
|
||||
def initialize(body:, group:)
|
||||
@@ -16,7 +16,7 @@ class TagGroupMarkdownParser
|
||||
@group = group
|
||||
end
|
||||
|
||||
def parse
|
||||
def parse locale
|
||||
parse_body = @body.gsub(/([^\n])\n([\x23]{1,6}[ \t]+\S)/, "\\1\n\n\\2")
|
||||
root = Kramdown::Document.new(parse_body, input: 'GFM', auto_ids: false).root
|
||||
placements = []
|
||||
@@ -51,7 +51,7 @@ class TagGroupMarkdownParser
|
||||
when :ul
|
||||
invalid!('未対応のリスト項目があります.') if node.attr.any?
|
||||
target = headings.any? ? headings.last.fetch(:node).fetch('children') : placements
|
||||
target.concat(node.children.map { parse_list_item(_1) })
|
||||
target.concat(node.children.map { parse_list_item(locale, _1) })
|
||||
else
|
||||
invalid!("未対応の Markdown 構文 #{ node.type.inspect } です.")
|
||||
end
|
||||
@@ -66,7 +66,7 @@ class TagGroupMarkdownParser
|
||||
|
||||
private
|
||||
|
||||
def parse_list_item(item)
|
||||
def parse_list_item locale, item
|
||||
invalid!('リスト項目ではありません.') unless item.type == :li
|
||||
invalid!('未対応のリスト項目があります.') if item.attr.any?
|
||||
|
||||
@@ -81,29 +81,30 @@ class TagGroupMarkdownParser
|
||||
label = own.map { inline_text(_1) }.join(' ').strip
|
||||
invalid!('空のリスト項目は使用できません.') if label.blank?
|
||||
|
||||
node = if label.start_with?(PREFIX)
|
||||
group_name = label.delete_prefix(PREFIX).strip
|
||||
child_group = TagGroup.find_by(name: group_name)
|
||||
invalid!("Tag Group #{ group_name.inspect } を解決できません.") unless child_group
|
||||
invalid!('自分自身を含めることはできません.') if child_group.id == @group&.id
|
||||
{ 'tag_group_id' => child_group.id }
|
||||
elsif label.end_with?(':', ':')
|
||||
section_name = label.sub(/[::]\z/, '').strip
|
||||
invalid!('Section 名は必須です.') if section_name.blank?
|
||||
{ 'section_name' => section_name, 'description' => nil }
|
||||
else
|
||||
{ 'tag_id' => resolve_tag(label) }
|
||||
end
|
||||
node =
|
||||
if (group_name = TagGroupRepr.name_from_title(label))
|
||||
group_name = group_name.strip
|
||||
child_group = TagGroup.find_by(name: group_name)
|
||||
invalid!("Tag Group #{ group_name.inspect } を解決できません.") unless child_group
|
||||
invalid!('自分自身を含めることはできません.') if child_group.id == @group&.id
|
||||
{ 'tag_group_id' => child_group.id }
|
||||
elsif label.end_with?(':', ':')
|
||||
section_name = label.sub(/[::]\z/, '').strip
|
||||
invalid!('Section 名は必須です.') if section_name.blank?
|
||||
{ 'section_name' => section_name, 'description' => nil }
|
||||
else
|
||||
{ 'tag_id' => resolve_tag(locale, label) }
|
||||
end
|
||||
|
||||
node['children'] = nested.flat_map do |list|
|
||||
invalid!('未対応の入れ子リストです.') unless list.type == :ul
|
||||
list.children.map { parse_list_item(_1) }
|
||||
list.children.map { parse_list_item(locale, _1) }
|
||||
end
|
||||
node
|
||||
end
|
||||
|
||||
def resolve_tag(name)
|
||||
names = TagName.where(language_code: 'ja', name:).to_a
|
||||
def resolve_tag locale, name
|
||||
names = TagName.where(language_code: locale.language_code, name:).to_a
|
||||
tag_ids = names.filter_map(&:tag_id).uniq
|
||||
invalid!("Tag #{ name.inspect } を解決できません.") if tag_ids.empty?
|
||||
invalid!("Tag 名 #{ name.inspect } が曖昧です.") if tag_ids.many?
|
||||
|
||||
@@ -19,15 +19,20 @@ class TagGroupMarkdownRenderer
|
||||
blocks << clean_text(@group['description'])
|
||||
end
|
||||
|
||||
sections = placements.select { _1.key?('section_name') }
|
||||
if sections.any? && sections.size != placements.size
|
||||
raise UnsupportedStructure,
|
||||
'最上位の見出し Section と Placement の混在は Markdown にできません.'
|
||||
end
|
||||
first_section = placements.index { _1.key?('section_name') }
|
||||
|
||||
if placements.all? { _1.key?('section_name') }
|
||||
blocks.concat(placements.map { render_heading(_1, 2) })
|
||||
else
|
||||
if first_section
|
||||
if placements.drop(first_section).any? { !_1.key?('section_name') }
|
||||
raise UnsupportedStructure,
|
||||
'見出し Section の後に最上位 Placement は配置できません.'
|
||||
end
|
||||
|
||||
if first_section.positive?
|
||||
blocks << render_list(placements.take(first_section), 0)
|
||||
end
|
||||
|
||||
blocks.concat(placements.drop(first_section).map { render_heading(_1, 2) })
|
||||
elsif placements.any?
|
||||
blocks << render_list(placements, 0)
|
||||
end
|
||||
|
||||
@@ -74,7 +79,7 @@ class TagGroupMarkdownRenderer
|
||||
first_section = section_indexes.first
|
||||
if children[first_section..].any? { !_1.key?('section_name') }
|
||||
raise UnsupportedStructure,
|
||||
'見出し Section と兄弟 Placement の交互配置は Markdown にできません.'
|
||||
'見出し Section と兄弟 Placement の交互配置は Markdown にできません.'
|
||||
end
|
||||
list_nodes = children[0...first_section]
|
||||
blocks << render_list(list_nodes, 0) if list_nodes.any?
|
||||
|
||||
@@ -1,49 +1,51 @@
|
||||
class TagGroupOperation
|
||||
MAX_PLACEMENT_DEPTH = 5
|
||||
PREFIX = TagGroupRepr::PREFIX
|
||||
|
||||
def self.create!(name:, description:, members:, placements:, actor:,
|
||||
require_compatibility: false, compatibility_body: nil)
|
||||
group, sync_issue = ApplicationRecord.transaction do
|
||||
def self.create!(locale, name:, description:, members:, placements:, actor:,
|
||||
require_compatibility: false)
|
||||
ApplicationRecord.transaction do
|
||||
group = TagGroup.new(version_no: 1, created_by_user: actor,
|
||||
updated_by_user: actor)
|
||||
attributes = prepare!(group:, name:, description:, members:, placements:)
|
||||
attributes = prepare!(locale:, group:, name:, description:, members:, placements:)
|
||||
group.assign_attributes(name: attributes.fetch(:name),
|
||||
description: attributes.fetch(:description))
|
||||
group.save!
|
||||
persist_structure!(group:, attributes:, actor:)
|
||||
validate_compatibility!(locale, group) if require_compatibility
|
||||
persist_version!(group:, attributes:, actor:, event_type: 'create')
|
||||
issue = sync_compatibility!(group:, actor:, old_name: nil,
|
||||
require_compatibility:, compatibility_body:)
|
||||
[group, issue]
|
||||
group
|
||||
end
|
||||
report_sync_issue(group, actor, sync_issue)
|
||||
group
|
||||
end
|
||||
|
||||
def self.update!(group:, name:, description:, members:, placements:, actor:,
|
||||
require_compatibility: false, compatibility_body: nil)
|
||||
group, sync_issue = ApplicationRecord.transaction do
|
||||
def self.update!(locale, group:, name:, description:, members:, placements:, actor:,
|
||||
require_compatibility: false)
|
||||
ApplicationRecord.transaction do
|
||||
group.lock!
|
||||
old_name = group.name
|
||||
attributes = prepare!(group:, name:, description:, members:, placements:)
|
||||
next [group, nil] if unchanged?(group, attributes)
|
||||
attributes = prepare!(locale:, group:, name:, description:, members:, placements:)
|
||||
if unchanged?(group, attributes)
|
||||
validate_compatibility!(locale, group) if require_compatibility
|
||||
next group
|
||||
end
|
||||
|
||||
group.update!(name: attributes.fetch(:name),
|
||||
description: attributes.fetch(:description),
|
||||
version_no: group.version_no + 1,
|
||||
updated_by_user: actor)
|
||||
persist_structure!(group:, attributes:, actor:)
|
||||
validate_compatibility!(locale, group) if require_compatibility
|
||||
persist_version!(group:, attributes:, actor:, event_type: 'update')
|
||||
issue = sync_compatibility!(group:, actor:, old_name:,
|
||||
require_compatibility:, compatibility_body:)
|
||||
[group, issue]
|
||||
group
|
||||
end
|
||||
report_sync_issue(group, actor, sync_issue)
|
||||
group
|
||||
end
|
||||
|
||||
def self.prepare!(group:, name:, description:, members:, placements:)
|
||||
def self.validate_compatibility! locale, group
|
||||
TagGroupMarkdownRenderer.render(TagGroupRepr.one(locale, group))
|
||||
rescue TagGroupMarkdownRenderer::UnsupportedStructure => error
|
||||
invalid!(group, :base, error.message)
|
||||
end
|
||||
private_class_method :validate_compatibility!
|
||||
|
||||
def self.prepare!(locale:, group:, name:, description:, members:, placements:)
|
||||
record = group
|
||||
normalized_name = name.to_s.strip
|
||||
invalid!(record, :name, 'グループ名は必須です.') if normalized_name.blank?
|
||||
@@ -61,7 +63,7 @@ class TagGroupOperation
|
||||
|
||||
member_rows = normalize_members(record, members)
|
||||
placement_tree = normalize_placements(record, placements)
|
||||
validate_references!(record, member_rows, placement_tree)
|
||||
validate_references!(locale, record, member_rows, placement_tree)
|
||||
validate_containment!(record, member_rows)
|
||||
{ name: normalized_name, description:, members: member_rows,
|
||||
placements: placement_tree }
|
||||
@@ -185,7 +187,7 @@ class TagGroupOperation
|
||||
end
|
||||
private_class_method :normalize_placement_nodes
|
||||
|
||||
def self.validate_references!(record, members, placements)
|
||||
def self.validate_references!(locale, record, members, placements)
|
||||
membership = members.to_set
|
||||
placed = placement_references(placements).uniq
|
||||
missing = placed - membership.to_a
|
||||
@@ -204,13 +206,14 @@ class TagGroupOperation
|
||||
tag_versions = TagVersion.where(tag_id: tag_ids).pluck(:tag_id, :version_no).to_set
|
||||
group_versions = TagGroupVersion.where(tag_group_id: group_ids)
|
||||
.pluck(:tag_group_id, :version_no).to_set
|
||||
tag_names = TagName.where(tag_id: tag_ids, language_code: 'ja', primary_flg: true)
|
||||
tag_names = TagName.where(
|
||||
tag_id: tag_ids, language_code: locale.language_code, primary_flg: true)
|
||||
.pluck(:tag_id).uniq
|
||||
|
||||
members.each do |member|
|
||||
if member.key?('tag_id')
|
||||
id = member.fetch('tag_id')
|
||||
invalid!(record, :members, "Tag ##{ id } に日本語の正規名がありません.") unless
|
||||
invalid!(record, :members, :invalid) unless
|
||||
tag_names.include?(id)
|
||||
version_no = tags.fetch(id).version_no
|
||||
unless tag_versions.include?([id, version_no])
|
||||
@@ -313,57 +316,6 @@ class TagGroupOperation
|
||||
end
|
||||
private_class_method :persist_version!
|
||||
|
||||
def self.sync_compatibility!(group:, actor:, old_name:,
|
||||
require_compatibility:, compatibility_body:)
|
||||
representation = TagGroupRepr.one(group)
|
||||
body = begin
|
||||
TagGroupMarkdownRenderer.render(representation)
|
||||
rescue TagGroupMarkdownRenderer::UnsupportedStructure => error
|
||||
if compatibility_body.present?
|
||||
compatibility_body
|
||||
elsif require_compatibility
|
||||
invalid!(group, :base, error.message)
|
||||
else
|
||||
return error.message
|
||||
end
|
||||
end
|
||||
invalid!(group, :base, '本文は必須です.') if body.blank?
|
||||
|
||||
title = "#{ PREFIX }#{ group.name }"
|
||||
old_title = old_name && "#{ PREFIX }#{ old_name }"
|
||||
page = if old_title
|
||||
WikiPage.joins(:tag_name).find_by(tag_names: { name: old_title })
|
||||
else
|
||||
nil
|
||||
end
|
||||
page ||= WikiPage.joins(:tag_name).find_by(tag_names: { name: title })
|
||||
|
||||
if page
|
||||
tag_name = page.tag_name
|
||||
tag_name.update!(name: title) if tag_name.name != title
|
||||
Wiki::Commit.legacy_content!(page:, body:, created_user: actor, message: nil,
|
||||
base_revision_id: nil)
|
||||
else
|
||||
tag_name = TagName.find_or_initialize_by(language_code: 'ja', name: title)
|
||||
tag_name.script_code = 'Jpan'
|
||||
tag_name.primary_flg = true
|
||||
tag_name.auto_generated = false
|
||||
tag_name.save!
|
||||
Wiki::CompatibilityWriter.create!(tag_name:, body:, created_by_user: actor)
|
||||
end
|
||||
nil
|
||||
end
|
||||
private_class_method :sync_compatibility!
|
||||
|
||||
def self.report_sync_issue(group, actor, reason)
|
||||
return unless reason
|
||||
|
||||
payload = { status: 'unsupported_structure', tag_group_id: group.id,
|
||||
version_no: group.version_no, user_id: actor&.id, reason: }
|
||||
Rails.logger.warn("tag_group_wiki_sync_skipped #{ payload.to_json }")
|
||||
end
|
||||
private_class_method :report_sync_issue
|
||||
|
||||
def self.placement_references(nodes)
|
||||
nodes.flat_map do |node|
|
||||
own = if node.key?('tag_id')
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
require 'digest'
|
||||
|
||||
|
||||
module Wiki
|
||||
class Commit
|
||||
class Conflict < StandardError
|
||||
;
|
||||
end
|
||||
|
||||
def self.create_content! tag_name:, body:, created_by_user:, message: nil
|
||||
if tag_name.tag && tag_name.language_code == 'ja' && !tag_name.name.include?(':')
|
||||
return TagDescriptionOperation.create!(
|
||||
tag: tag_name.tag, body:, created_by_user:, message:)
|
||||
end
|
||||
|
||||
tag_name.errors.add(:base, :invalid)
|
||||
raise ActiveRecord::RecordInvalid, tag_name
|
||||
end
|
||||
|
||||
def self.create_legacy_content! tag_name:, body:, created_by_user:, message: nil
|
||||
normalised = normalise_body(body)
|
||||
|
||||
page = WikiPage.new(tag_name:,
|
||||
body: normalised,
|
||||
created_user: created_by_user,
|
||||
updated_user: created_by_user)
|
||||
|
||||
if normalised.blank?
|
||||
page.errors.add(:body, :blank)
|
||||
raise ActiveRecord::RecordInvalid, page
|
||||
end
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
page.save!
|
||||
|
||||
new(page:, created_user: created_by_user).content!(
|
||||
body: normalised,
|
||||
message:,
|
||||
base_revision_id: nil)
|
||||
|
||||
page
|
||||
end
|
||||
end
|
||||
|
||||
def self.content! page:, body:, created_user:, message: nil, base_revision_id: nil
|
||||
description = TagDescription.find_by(tag_id: page.tag_name.tag_id, language_code: 'ja')
|
||||
description ||= TagDescription.joins(:tag).find_by(
|
||||
tags: { tag_name_id: page.tag_name_id }, language_code: 'ja')
|
||||
if description && !page.title.include?(':')
|
||||
return TagDescriptionOperation.update!(
|
||||
tag: description.tag, body:, updated_by_user: created_user,
|
||||
message:, base_revision_id:)
|
||||
end
|
||||
|
||||
page.errors.add(:base, :invalid)
|
||||
raise ActiveRecord::RecordInvalid, page
|
||||
end
|
||||
|
||||
def self.legacy_content! page:, body:, created_user:, message: nil,
|
||||
base_revision_id: nil
|
||||
new(page:, created_user:).content!(body:, message:, base_revision_id:)
|
||||
end
|
||||
|
||||
def self.redirect! page:, redirect_page:, created_user:, message: nil, base_revision_id: nil
|
||||
new(page:, created_user:).redirect!(redirect_page:, message:, base_revision_id:)
|
||||
end
|
||||
|
||||
def initialize page:, created_user:
|
||||
@page = page
|
||||
@created_user = created_user
|
||||
end
|
||||
|
||||
def content! body:, message:, base_revision_id:
|
||||
normalised = self.class.normalise_body(body)
|
||||
if normalised.blank?
|
||||
@page.errors.add(:body, :blank)
|
||||
raise ActiveRecord::RecordInvalid, @page
|
||||
end
|
||||
|
||||
lines = split_lines(normalised)
|
||||
|
||||
line_shas = lines.map { |line| Digest::SHA256.hexdigest(line) }
|
||||
tree_sha = Digest::SHA256.hexdigest(line_shas.join(','))
|
||||
|
||||
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
|
||||
|
||||
line_id_by_sha = upsert_lines!(lines, line_shas)
|
||||
line_ids = line_shas.map { |sha| line_id_by_sha.fetch(sha) }
|
||||
@page.update!(body: normalised)
|
||||
|
||||
WikiVersionRecorder.record!(
|
||||
page: @page,
|
||||
event_type: @page.wiki_versions.exists? ? :update : :create,
|
||||
reason: message,
|
||||
created_by_user: @created_user)
|
||||
|
||||
rev = WikiRevision.create!(
|
||||
wiki_page: @page,
|
||||
base_revision_id:,
|
||||
created_user: @created_user,
|
||||
kind: :content,
|
||||
redirect_page_id: nil,
|
||||
message:,
|
||||
lines_count: lines.length,
|
||||
tree_sha256: tree_sha)
|
||||
|
||||
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) if rows.any?
|
||||
rev
|
||||
end
|
||||
end
|
||||
|
||||
def redirect!(redirect_page:, message:, base_revision_id:) = raise '廃止しました.'
|
||||
|
||||
def self.normalise_body body
|
||||
s = body.to_s
|
||||
s.gsub!(/\r\n?/, "\n")
|
||||
s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '🖕')
|
||||
s.gsub(/\n+$/, '')
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def split_lines(body) = body.split("\n")
|
||||
|
||||
def upsert_lines! lines, line_shas
|
||||
now = Time.current
|
||||
|
||||
id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
|
||||
|
||||
missing_by_sha = { }
|
||||
|
||||
line_shas.each_with_index do |sha, i|
|
||||
next if id_by_sha.key?(sha)
|
||||
next if missing_by_sha.key?(sha)
|
||||
|
||||
missing_by_sha[sha] = {
|
||||
sha256: sha,
|
||||
body: lines[i],
|
||||
created_at: now,
|
||||
updated_at: now }
|
||||
end
|
||||
|
||||
if missing_by_sha.any?
|
||||
WikiLine.upsert_all(missing_by_sha.values)
|
||||
id_by_sha = WikiLine.where(sha256: line_shas).pluck(:sha256, :id).to_h
|
||||
end
|
||||
|
||||
id_by_sha
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,43 +0,0 @@
|
||||
module Wiki
|
||||
class CompatibilityWriter
|
||||
def self.create! tag_name:, body:, created_by_user:, message: nil
|
||||
ApplicationRecord.transaction do
|
||||
tag_name.lock!
|
||||
page = WikiPage.lock.find_by(tag_name_id: tag_name.id)
|
||||
if page
|
||||
update!(page:, body:, updated_by_user: created_by_user, message:)
|
||||
page
|
||||
else
|
||||
Commit.create_legacy_content!(tag_name:, body:, created_by_user:, message:)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.update! page:, body:, updated_by_user:, message: nil,
|
||||
base_revision_id: nil
|
||||
normalised = Commit.normalise_body(body)
|
||||
if normalised.blank?
|
||||
return Commit.legacy_content!(
|
||||
page:, body: normalised, created_user: updated_by_user, message:,
|
||||
base_revision_id:)
|
||||
end
|
||||
|
||||
if base_revision_id.present?
|
||||
current_id = page.wiki_revisions.maximum(:id)
|
||||
if current_id && current_id != base_revision_id.to_i
|
||||
raise Commit::Conflict,
|
||||
"競合が発生してゐます" +
|
||||
"(現在の Id.:#{ current_id },ベース Id.:#{ base_revision_id })."
|
||||
end
|
||||
end
|
||||
|
||||
if Commit.normalise_body(page.body) == normalised && page.wiki_revisions.exists?
|
||||
return
|
||||
end
|
||||
|
||||
Commit.legacy_content!(
|
||||
page:, body: normalised, created_user: updated_by_user, message:,
|
||||
base_revision_id:)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,21 +0,0 @@
|
||||
class WikiVersionRecorder < VersionRecorder
|
||||
def self.record! page:, event_type:, reason: nil, created_by_user:
|
||||
new(page:, event_type:, reason:, created_by_user:).record!
|
||||
end
|
||||
|
||||
def initialize page:, event_type:, reason: nil, created_by_user:
|
||||
@reason = reason
|
||||
super(record: page, event_type:, created_by_user:)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def version_class = WikiVersion
|
||||
def version_association = :wiki_versions
|
||||
def record_key = :wiki_page
|
||||
|
||||
def snapshot_attributes = {
|
||||
title: @record.title,
|
||||
body: @record.body,
|
||||
reason: @reason }
|
||||
end
|
||||
新しいイシューから参照
ユーザーをブロックする