summaryrefslogtreecommitdiff
path: root/app/models/wiki_page.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/wiki_page.rb')
-rw-r--r--app/models/wiki_page.rb28
1 files changed, 26 insertions, 2 deletions
diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb
index 3dc90edb331..faf3d19d936 100644
--- a/app/models/wiki_page.rb
+++ b/app/models/wiki_page.rb
@@ -65,6 +65,7 @@ class WikiPage
validates :title, presence: true
validates :content, presence: true
validate :validate_path_limits, if: :title_changed?
+ validate :validate_content_size_limit, if: :content_changed?
# The GitLab Wiki instance.
attr_reader :wiki
@@ -97,6 +98,7 @@ class WikiPage
def slug
attributes[:slug].presence || wiki.wiki.preview_slug(title, format)
end
+ alias_method :id, :slug # required to use build_stubbed
alias_method :to_param, :slug
@@ -264,8 +266,8 @@ class WikiPage
'../shared/wikis/wiki_page'
end
- def id
- page.version.to_s
+ def sha
+ page.version&.sha
end
def title_changed?
@@ -282,6 +284,17 @@ class WikiPage
end
end
+ def content_changed?
+ if persisted?
+ # gollum-lib always converts CRLFs to LFs in Gollum::Wiki#normalize,
+ # so we need to do the same here.
+ # Also see https://gitlab.com/gitlab-org/gitlab/-/issues/21431
+ raw_content.delete("\r") != page&.text_data
+ else
+ raw_content.present?
+ end
+ end
+
# Updates the current @attributes hash by merging a hash of params
def update_attributes(attrs)
attrs[:title] = process_title(attrs[:title]) if attrs[:title].present?
@@ -391,4 +404,15 @@ class WikiPage
})
end
end
+
+ def validate_content_size_limit
+ current_value = raw_content.to_s.bytesize
+ max_size = Gitlab::CurrentSettings.wiki_page_max_content_bytes
+ return if current_value <= max_size
+
+ errors.add(:content, _('is too long (%{current_value}). The maximum size is %{max_size}.') % {
+ current_value: ActiveSupport::NumberHelper.number_to_human_size(current_value),
+ max_size: ActiveSupport::NumberHelper.number_to_human_size(max_size)
+ })
+ end
end