1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
# frozen_string_literal: true
# rubocop:disable Rails/ActiveRecordAliases
class WikiPage
include Gitlab::Utils::StrongMemoize
PageChangedError = Class.new(StandardError)
PageRenameError = Class.new(StandardError)
FrontMatterTooLong = Class.new(StandardError)
include ActiveModel::Validations
include ActiveModel::Conversion
include StaticModel
extend ActiveModel::Naming
delegate :content, :front_matter, to: :parsed_content
def self.primary_key
'slug'
end
def self.model_name
ActiveModel::Name.new(self, nil, 'wiki')
end
def eql?(other)
return false unless other.present? && other.is_a?(self.class)
slug == other.slug && wiki.container == other.wiki.container
end
alias_method :==, :eql?
def self.unhyphenize(name)
name.gsub(/-+/, ' ')
end
def to_key
[:slug]
end
validates :title, presence: true
validate :validate_path_limits, if: :title_changed?
validate :validate_content_size_limit, if: :content_changed?
# The GitLab Wiki instance.
attr_reader :wiki
delegate :container, to: :wiki
# The raw Gitlab::Git::WikiPage instance.
attr_reader :page
# The attributes Hash used for storing and validating
# new Page values before writing to the raw repository.
attr_accessor :attributes
def hook_attrs
Gitlab::HookData::WikiPageBuilder.new(self).build
end
# Construct a new WikiPage
#
# @param [Wiki] wiki
# @param [Gitlab::Git::WikiPage] page
def initialize(wiki, page = nil)
@wiki = wiki
@page = page
@attributes = {}.with_indifferent_access
set_attributes if persisted?
end
# The escaped URL path of this page.
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
def human_title
return 'Home' if title == Wiki::HOMEPAGE
title
end
# The formatted title of this page.
def title
attributes[:title] || ''
end
# Sets the title of this page.
def title=(new_title)
attributes[:title] = new_title
end
def raw_content
attributes[:content] ||= page&.text_data
end
# The hierarchy of the directory this page is contained in.
def directory
wiki.page_title_and_dir(slug)&.last.to_s
end
# The markup format for the page.
def format
attributes[:format] || :markdown
end
# The commit message for this page version.
def message
version.try(:message)
end
# The GitLab Commit instance for this page.
def version
return unless persisted?
@version ||= @page.version
end
def path
return unless persisted?
@path ||= @page.path
end
# Returns a CommitCollection
#
# Queries the commits for current page's path, equivalent to
# `git log path/to/page`. Filters and options supported:
# https://gitlab.com/gitlab-org/gitaly/-/blob/master/proto/commit.proto#L322-344
def versions(options = {})
return [] unless persisted?
default_per_page = Kaminari.config.default_per_page
offset = [options[:page].to_i - 1, 0].max * options.fetch(:per_page, default_per_page)
wiki.repository.commits('HEAD',
path: page.path,
limit: options.fetch(:limit, default_per_page),
offset: offset)
end
def count_versions
return [] unless persisted?
wiki.wiki.count_page_versions(page.path)
end
def last_version
@last_version ||= versions(limit: 1).first
end
def last_commit_sha
last_version&.sha
end
# Returns boolean True or False if this instance
# is an old version of the page.
def historical?
return false unless last_commit_sha && version
page.historical? && last_commit_sha != version.sha
end
# Returns boolean True or False if this instance
# is the latest commit version of the page.
def latest?
!historical?
end
# Returns boolean True or False if this instance
# has been fully created on disk or not.
def persisted?
page.present?
end
# Creates a new Wiki Page.
#
# attr - Hash of attributes to set on the new page.
# :title - The title (optionally including dir) for the new page.
# :content - The raw markup content.
# :format - Optional symbol representing the
# content format. Can be any type
# listed in the Wiki::VALID_USER_MARKUPS
# Hash.
# :message - Optional commit message to set on
# the new page.
#
# Returns the String SHA1 of the newly created page
# or False if the save was unsuccessful.
def create(attrs = {})
update_attributes(attrs)
save do
wiki.create_page(title, content, format, attrs[:message])
end
end
# Updates an existing Wiki Page, creating a new version.
#
# attrs - Hash of attributes to be updated on the page.
# :content - The raw markup content to replace the existing.
# :format - Optional symbol representing the content format.
# See Wiki::VALID_USER_MARKUPS Hash for available formats.
# :message - Optional commit message to set on the new version.
# :last_commit_sha - Optional last commit sha to validate the page unchanged.
# :title - The Title (optionally including dir) to replace existing title
#
# Returns the String SHA1 of the newly created page
# or False if the save was unsuccessful.
def update(attrs = {})
last_commit_sha = attrs.delete(:last_commit_sha)
if last_commit_sha && last_commit_sha != self.last_commit_sha
raise PageChangedError, s_(
'WikiPageConflictMessage|Someone edited the page the same time you did. Please check out %{wikiLinkStart}the page%{wikiLinkEnd} and make sure your changes will not unintentionally remove theirs.')
end
update_attributes(attrs)
if title.present? && title_changed? && wiki.find_page(title, load_content: false).present?
attributes[:title] = page.title
raise PageRenameError, s_('WikiEdit|There is already a page with the same title in that path.')
end
save do
wiki.update_page(
page,
content: raw_content,
format: format,
message: attrs[:message],
title: title
)
end
end
# Destroys the Wiki Page.
#
# Returns boolean True or False.
def delete
if wiki.delete_page(page)
true
else
false
end
end
# Relative path to the partial to be used when rendering collections
# of this object.
def to_partial_path
'../shared/wikis/wiki_page'
end
def sha
page.version&.sha
end
def title_changed?
if persisted?
# A page's `title` will be returned from Gollum/Gitaly with any +<>
# characters changed to -, whereas the `path` preserves these characters.
path_without_extension = Pathname(page.path).sub_ext('').to_s
old_title, old_dir = wiki.page_title_and_dir(self.class.unhyphenize(path_without_extension))
new_title, new_dir = wiki.page_title_and_dir(self.class.unhyphenize(title))
new_title != old_title || (title.include?('/') && new_dir != old_dir)
else
title.present?
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?
update_front_matter(attrs)
attrs.slice!(:content, :format, :message, :title)
clear_memoization(:parsed_content) if attrs.has_key?(:content)
attributes.merge!(attrs)
end
def to_ability_name
'wiki_page'
end
def version_commit_timestamp
version&.commit&.committed_date
end
def diffs(diff_options = {})
Gitlab::Diff::FileCollection::WikiPage.new(self, diff_options: diff_options)
end
private
def serialize_front_matter(hash)
return '' unless hash.present?
YAML.dump(hash.transform_keys(&:to_s)) + "---\n"
end
def update_front_matter(attrs)
return unless Gitlab::WikiPages::FrontMatterParser.enabled?(container)
return unless attrs.has_key?(:front_matter)
fm_yaml = serialize_front_matter(attrs[:front_matter])
raise FrontMatterTooLong if fm_yaml.size > Gitlab::WikiPages::FrontMatterParser::MAX_FRONT_MATTER_LENGTH
attrs[:content] = fm_yaml + (attrs[:content].presence || content)
end
def parsed_content
strong_memoize(:parsed_content) do
Gitlab::WikiPages::FrontMatterParser.new(raw_content, container).parse
end
end
# Process and format the title based on the user input.
def process_title(title)
return if title.blank?
title = deep_title_squish(title)
current_dirname = File.dirname(title)
if persisted?
return title[1..] if current_dirname == '/'
return File.join([directory.presence, title].compact) if current_dirname == '.'
end
title
end
# This method squishes all the filename
# i.e: ' foo / bar / page_name' => 'foo/bar/page_name'
def deep_title_squish(title)
components = title.split(File::SEPARATOR).map(&:squish)
File.join(components)
end
def set_attributes
attributes[:slug] = @page.url_path
attributes[:title] = @page.title
attributes[:format] = @page.format
end
def save
return false unless valid?
unless yield
errors.add(:base, wiki.error_message)
return false
end
@page = wiki.find_page(title).page
set_attributes
true
end
def validate_path_limits
return unless title.present?
*dirnames, filename = title.split('/')
if filename && filename.bytesize > Gitlab::WikiPages::MAX_TITLE_BYTES
errors.add(:title, _("exceeds the limit of %{bytes} bytes") % {
bytes: Gitlab::WikiPages::MAX_TITLE_BYTES
})
end
invalid_dirnames = dirnames.select { |d| d.bytesize > Gitlab::WikiPages::MAX_DIRECTORY_BYTES }
invalid_dirnames.each do |dirname|
errors.add(:title, _('exceeds the limit of %{bytes} bytes for directory name "%{dirname}"') % {
bytes: Gitlab::WikiPages::MAX_DIRECTORY_BYTES,
dirname: dirname
})
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
WikiPage.prepend_mod
|