Class: WikiPage

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, Gitlab::Utils::StrongMemoize, StaticModel
Defined in:
app/models/wiki_page.rb,
app/models/wiki_page/meta.rb,
app/models/wiki_page/slug.rb

Overview

rubocop:disable Rails/ActiveRecordAliases

Defined Under Namespace

Classes: Meta, MetaPolicy, Slug

Constant Summary collapse

PageChangedError =
Class.new(StandardError)
PageRenameError =
Class.new(StandardError)
FrontMatterTooLong =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StaticModel

#[], #destroyed?, #new_record?

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(wiki, page = nil) ⇒ WikiPage

Construct a new WikiPage

Parameters:


89
90
91
92
93
94
95
# File 'app/models/wiki_page.rb', line 89

def initialize(wiki, page = nil)
  @wiki       = wiki
  @page       = page
  @attributes = {}.with_indifferent_access

  set_attributes if persisted?
end

Instance Attribute Details

#attributesObject

The attributes Hash used for storing and validating new Page values before writing to the raw repository.


79
80
81
# File 'app/models/wiki_page.rb', line 79

def attributes
  @attributes
end

#pageObject (readonly)

The raw Gitlab::Git::WikiPage instance.


75
76
77
# File 'app/models/wiki_page.rb', line 75

def page
  @page
end

#wikiObject (readonly)

The GitLab Wiki instance.


71
72
73
# File 'app/models/wiki_page.rb', line 71

def wiki
  @wiki
end

Class Method Details

.group_by_directory(pages) ⇒ Object

Sorts and groups pages by directory.

pages - an array of WikiPage objects.

Returns an array of WikiPage and WikiDirectory objects. The entries are sorted by alphabetical order (directories and pages inside each directory). Pages at the root level come before everything.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/wiki_page.rb', line 41

def self.group_by_directory(pages)
  return [] if pages.blank?

  pages.each_with_object([]) do |page, grouped_pages|
    next grouped_pages << page unless page.directory.present?

    directory = grouped_pages.find do |obj|
      obj.is_a?(WikiDirectory) && obj.slug == page.directory
    end

    next directory.pages << page if directory

    grouped_pages << WikiDirectory.new(page.directory, [page])
  end
end

.model_nameObject


22
23
24
# File 'app/models/wiki_page.rb', line 22

def self.model_name
  ActiveModel::Name.new(self, nil, 'wiki')
end

.primary_keyObject


18
19
20
# File 'app/models/wiki_page.rb', line 18

def self.primary_key
  'slug'
end

.unhyphenize(name) ⇒ Object


57
58
59
# File 'app/models/wiki_page.rb', line 57

def self.unhyphenize(name)
  name.gsub(/-+/, ' ')
end

Instance Method Details

#content_changed?Boolean

Returns:

  • (Boolean)

287
288
289
290
291
292
293
294
295
296
# File 'app/models/wiki_page.rb', line 287

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

#count_versionsObject


159
160
161
162
163
# File 'app/models/wiki_page.rb', line 159

def count_versions
  return [] unless persisted?

  wiki.wiki.count_page_versions(page.path)
end

#create(attrs = {}) ⇒ Object

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::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.


207
208
209
210
211
212
213
# File 'app/models/wiki_page.rb', line 207

def create(attrs = {})
  update_attributes(attrs)

  save do
    wiki.create_page(title, content, format, attrs[:message])
  end
end

#deleteObject

Destroys the Wiki Page.

Returns boolean True or False.


255
256
257
258
259
260
261
# File 'app/models/wiki_page.rb', line 255

def delete
  if wiki.delete_page(page)
    true
  else
    false
  end
end

#diffs(diff_options = {}) ⇒ Object


317
318
319
# File 'app/models/wiki_page.rb', line 317

def diffs(diff_options = {})
  Gitlab::Diff::FileCollection::WikiPage.new(self, diff_options: diff_options)
end

#directoryObject

The hierarchy of the directory this page is contained in.


126
127
128
# File 'app/models/wiki_page.rb', line 126

def directory
  wiki.page_title_and_dir(slug)&.last.to_s
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)

26
27
28
29
30
# File 'app/models/wiki_page.rb', line 26

def eql?(other)
  return false unless other.present? && other.is_a?(self.class)

  slug == other.slug && wiki.container == other.wiki.container
end

#formatObject

The markup format for the page.


131
132
133
# File 'app/models/wiki_page.rb', line 131

def format
  attributes[:format] || :markdown
end

#historical?Boolean

Returns boolean True or False if this instance is an old version of the page.

Returns:

  • (Boolean)

175
176
177
178
179
# File 'app/models/wiki_page.rb', line 175

def historical?
  return false unless last_commit_sha && version

  page.historical? && last_commit_sha != version.sha
end

#hook_attrsObject


81
82
83
# File 'app/models/wiki_page.rb', line 81

def hook_attrs
  Gitlab::HookData::WikiPageBuilder.new(self).build
end

#human_titleObject


105
106
107
108
109
# File 'app/models/wiki_page.rb', line 105

def human_title
  return 'Home' if title == Wiki::HOMEPAGE

  title
end

#last_commit_shaObject


169
170
171
# File 'app/models/wiki_page.rb', line 169

def last_commit_sha
  last_version&.sha
end

#last_versionObject


165
166
167
# File 'app/models/wiki_page.rb', line 165

def last_version
  @last_version ||= versions(limit: 1).first
end

#latest?Boolean

Returns boolean True or False if this instance is the latest commit version of the page.

Returns:

  • (Boolean)

183
184
185
# File 'app/models/wiki_page.rb', line 183

def latest?
  !historical?
end

#messageObject

The commit message for this page version.


136
137
138
# File 'app/models/wiki_page.rb', line 136

def message
  version.try(:message)
end

#pathObject


147
148
149
150
151
# File 'app/models/wiki_page.rb', line 147

def path
  return unless persisted?

  @path ||= @page.path
end

#persisted?Boolean

Returns boolean True or False if this instance has been fully created on disk or not.

Returns:

  • (Boolean)

189
190
191
# File 'app/models/wiki_page.rb', line 189

def persisted?
  page.present?
end

#raw_contentObject


121
122
123
# File 'app/models/wiki_page.rb', line 121

def raw_content
  attributes[:content] ||= page&.text_data
end

#shaObject


269
270
271
# File 'app/models/wiki_page.rb', line 269

def sha
  page.version&.sha
end

#slugObject Also known as: id, to_param

The escaped URL path of this page.


98
99
100
# File 'app/models/wiki_page.rb', line 98

def slug
  attributes[:slug].presence || wiki.wiki.preview_slug(title, format)
end

#titleObject

The formatted title of this page.


112
113
114
# File 'app/models/wiki_page.rb', line 112

def title
  attributes[:title] || ''
end

#title=(new_title) ⇒ Object

Sets the title of this page.


117
118
119
# File 'app/models/wiki_page.rb', line 117

def title=(new_title)
  attributes[:title] = new_title
end

#title_changed?Boolean

Returns:

  • (Boolean)

273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/models/wiki_page.rb', line 273

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

#to_ability_nameObject


309
310
311
# File 'app/models/wiki_page.rb', line 309

def to_ability_name
  'wiki_page'
end

#to_keyObject


61
62
63
# File 'app/models/wiki_page.rb', line 61

def to_key
  [:slug]
end

#to_partial_pathObject

Relative path to the partial to be used when rendering collections of this object.


265
266
267
# File 'app/models/wiki_page.rb', line 265

def to_partial_path
  '../shared/wikis/wiki_page'
end

#update(attrs = {}) ⇒ Object

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::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.


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/models/wiki_page.rb', line 227

def update(attrs = {})
  last_commit_sha = attrs.delete(:last_commit_sha)

  if last_commit_sha && last_commit_sha != self.last_commit_sha
    raise PageChangedError
  end

  update_attributes(attrs)

  if title.present? && title_changed? && wiki.find_page(title).present?
    attributes[:title] = page.title
    raise PageRenameError
  end

  save do
    wiki.update_page(
      page,
      content: raw_content,
      format: format,
      message: attrs[:message],
      title: title
    )
  end
end

#update_attributes(attrs) ⇒ Object

Updates the current @attributes hash by merging a hash of params


299
300
301
302
303
304
305
306
307
# File 'app/models/wiki_page.rb', line 299

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

#versionObject

The GitLab Commit instance for this page.


141
142
143
144
145
# File 'app/models/wiki_page.rb', line 141

def version
  return unless persisted?

  @version ||= @page.version
end

#version_commit_timestampObject


313
314
315
# File 'app/models/wiki_page.rb', line 313

def version_commit_timestamp
  version&.commit&.committed_date
end

#versions(options = {}) ⇒ Object


153
154
155
156
157
# File 'app/models/wiki_page.rb', line 153

def versions(options = {})
  return [] unless persisted?

  wiki.wiki.page_versions(page.path, options)
end