Class: WikiPage
- Inherits:
-
Object
- Object
- WikiPage
- 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
-
#attributes ⇒ Object
The attributes Hash used for storing and validating new Page values before writing to the raw repository.
-
#page ⇒ Object
readonly
The raw Gitlab::Git::WikiPage instance.
-
#wiki ⇒ Object
readonly
The GitLab Wiki instance.
Class Method Summary collapse
-
.group_by_directory(pages) ⇒ Object
Sorts and groups pages by directory.
- .model_name ⇒ Object
- .primary_key ⇒ Object
- .unhyphenize(name) ⇒ Object
Instance Method Summary collapse
- #content_changed? ⇒ Boolean
- #count_versions ⇒ Object
-
#create(attrs = {}) ⇒ Object
Creates a new Wiki Page.
-
#delete ⇒ Object
Destroys the Wiki Page.
- #diffs(diff_options = {}) ⇒ Object
-
#directory ⇒ Object
The hierarchy of the directory this page is contained in.
- #eql?(other) ⇒ Boolean (also: #==)
-
#format ⇒ Object
The markup format for the page.
-
#historical? ⇒ Boolean
Returns boolean True or False if this instance is an old version of the page.
- #hook_attrs ⇒ Object
- #human_title ⇒ Object
-
#initialize(wiki, page = nil) ⇒ WikiPage
constructor
Construct a new WikiPage.
- #last_commit_sha ⇒ Object
- #last_version ⇒ Object
-
#latest? ⇒ Boolean
Returns boolean True or False if this instance is the latest commit version of the page.
-
#message ⇒ Object
The commit message for this page version.
- #path ⇒ Object
-
#persisted? ⇒ Boolean
Returns boolean True or False if this instance has been fully created on disk or not.
- #raw_content ⇒ Object
- #sha ⇒ Object
-
#slug ⇒ Object
(also: #id, #to_param)
The escaped URL path of this page.
-
#title ⇒ Object
The formatted title of this page.
-
#title=(new_title) ⇒ Object
Sets the title of this page.
- #title_changed? ⇒ Boolean
- #to_ability_name ⇒ Object
- #to_key ⇒ Object
-
#to_partial_path ⇒ Object
Relative path to the partial to be used when rendering collections of this object.
-
#update(attrs = {}) ⇒ Object
Updates an existing Wiki Page, creating a new version.
-
#update_attributes(attrs) ⇒ Object
Updates the current @attributes hash by merging a hash of params.
-
#version ⇒ Object
The GitLab Commit instance for this page.
- #version_commit_timestamp ⇒ Object
- #versions(options = {}) ⇒ Object
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
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
#attributes ⇒ Object
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 |
#page ⇒ Object (readonly)
The raw Gitlab::Git::WikiPage instance.
75 76 77 |
# File 'app/models/wiki_page.rb', line 75 def page @page end |
#wiki ⇒ Object (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_name ⇒ Object
22 23 24 |
# File 'app/models/wiki_page.rb', line 22 def self.model_name ActiveModel::Name.new(self, nil, 'wiki') end |
.primary_key ⇒ Object
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
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_versions ⇒ Object
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 |
#delete ⇒ Object
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( = {}) Gitlab::Diff::FileCollection::WikiPage.new(self, diff_options: ) end |
#directory ⇒ Object
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: ==
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 |
#format ⇒ Object
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.
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_attrs ⇒ Object
81 82 83 |
# File 'app/models/wiki_page.rb', line 81 def hook_attrs Gitlab::HookData::WikiPageBuilder.new(self).build end |
#human_title ⇒ Object
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_sha ⇒ Object
169 170 171 |
# File 'app/models/wiki_page.rb', line 169 def last_commit_sha last_version&.sha end |
#last_version ⇒ Object
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.
183 184 185 |
# File 'app/models/wiki_page.rb', line 183 def latest? !historical? end |
#message ⇒ Object
The commit message for this page version.
136 137 138 |
# File 'app/models/wiki_page.rb', line 136 def version.try(:message) end |
#path ⇒ Object
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.
189 190 191 |
# File 'app/models/wiki_page.rb', line 189 def persisted? page.present? end |
#raw_content ⇒ Object
121 122 123 |
# File 'app/models/wiki_page.rb', line 121 def raw_content attributes[:content] ||= page&.text_data end |
#sha ⇒ Object
269 270 271 |
# File 'app/models/wiki_page.rb', line 269 def sha page.version&.sha end |
#slug ⇒ Object 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 |
#title ⇒ Object
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
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_name ⇒ Object
309 310 311 |
# File 'app/models/wiki_page.rb', line 309 def to_ability_name 'wiki_page' end |
#to_key ⇒ Object
61 62 63 |
# File 'app/models/wiki_page.rb', line 61 def to_key [:slug] end |
#to_partial_path ⇒ Object
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 |
#version ⇒ Object
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_timestamp ⇒ Object
313 314 315 |
# File 'app/models/wiki_page.rb', line 313 def version&.commit&.committed_date end |
#versions(options = {}) ⇒ Object
153 154 155 156 157 |
# File 'app/models/wiki_page.rb', line 153 def versions( = {}) return [] unless persisted? wiki.wiki.page_versions(page.path, ) end |