2013-03-04 03:43:52 +00:00
|
|
|
class WikiPage
|
2017-03-04 14:03:14 +00:00
|
|
|
PageChangedError = Class.new(StandardError)
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
include ActiveModel::Validations
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
include StaticModel
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
|
|
|
|
def self.primary_key
|
|
|
|
'slug'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.model_name
|
|
|
|
ActiveModel::Name.new(self, nil, 'wiki')
|
|
|
|
end
|
|
|
|
|
2016-12-19 04:34:35 +00:00
|
|
|
# Sorts and groups pages by directory.
|
|
|
|
#
|
|
|
|
# pages - an array of WikiPage objects.
|
|
|
|
#
|
2016-12-26 22:12:15 +00:00
|
|
|
# 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.
|
2016-12-16 04:12:21 +00:00
|
|
|
def self.group_by_directory(pages)
|
2016-12-26 22:12:15 +00:00
|
|
|
return [] if pages.blank?
|
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
pages.sort_by { |page| [page.directory, page.slug] }
|
|
|
|
.group_by(&:directory)
|
|
|
|
.map do |dir, pages|
|
2016-12-27 04:05:53 +00:00
|
|
|
if dir.present?
|
2016-12-26 22:12:15 +00:00
|
|
|
WikiDirectory.new(dir, pages)
|
2016-12-27 04:05:53 +00:00
|
|
|
else
|
|
|
|
pages
|
2016-12-26 22:12:15 +00:00
|
|
|
end
|
2017-06-21 13:48:12 +00:00
|
|
|
end
|
|
|
|
.flatten
|
2016-12-16 04:12:21 +00:00
|
|
|
end
|
|
|
|
|
2017-01-30 03:07:31 +00:00
|
|
|
def self.unhyphenize(name)
|
|
|
|
name.gsub(/-+/, ' ')
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
def to_key
|
|
|
|
[:slug]
|
|
|
|
end
|
|
|
|
|
|
|
|
validates :title, presence: true
|
|
|
|
validates :content, presence: true
|
|
|
|
|
2014-04-09 11:35:26 +00:00
|
|
|
# The Gitlab ProjectWiki instance.
|
2013-03-04 03:43:52 +00:00
|
|
|
attr_reader :wiki
|
|
|
|
|
|
|
|
# The raw Gollum::Page instance.
|
|
|
|
attr_reader :page
|
|
|
|
|
|
|
|
# The attributes Hash used for storing and validating
|
|
|
|
# new Page values before writing to the Gollum repository.
|
|
|
|
attr_accessor :attributes
|
|
|
|
|
2016-02-28 07:26:52 +00:00
|
|
|
def hook_attrs
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
def initialize(wiki, page = nil, persisted = false)
|
|
|
|
@wiki = wiki
|
|
|
|
@page = page
|
|
|
|
@persisted = persisted
|
|
|
|
@attributes = {}.with_indifferent_access
|
|
|
|
|
|
|
|
set_attributes if persisted?
|
|
|
|
end
|
|
|
|
|
|
|
|
# The escaped URL path of this page.
|
|
|
|
def slug
|
2016-07-25 03:07:46 +00:00
|
|
|
if @attributes[:slug].present?
|
|
|
|
@attributes[:slug]
|
|
|
|
else
|
|
|
|
wiki.wiki.preview_page(title, '', format).url_path
|
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
2015-02-03 04:57:10 +00:00
|
|
|
alias_method :to_param, :slug
|
2013-03-04 03:43:52 +00:00
|
|
|
|
|
|
|
# The formatted title of this page.
|
|
|
|
def title
|
2014-04-09 05:14:16 +00:00
|
|
|
if @attributes[:title]
|
2017-08-30 15:16:08 +00:00
|
|
|
CGI.unescape_html(self.class.unhyphenize(@attributes[:title]))
|
2014-04-09 05:14:16 +00:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the title of this page.
|
|
|
|
def title=(new_title)
|
|
|
|
@attributes[:title] = new_title
|
|
|
|
end
|
|
|
|
|
|
|
|
# The raw content of this page.
|
|
|
|
def content
|
2017-02-07 14:16:46 +00:00
|
|
|
@attributes[:content] ||= @page&.text_data
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
2016-12-18 23:22:20 +00:00
|
|
|
# The hierarchy of the directory this page is contained in.
|
|
|
|
def directory
|
2016-12-27 04:05:53 +00:00
|
|
|
wiki.page_title_and_dir(slug).last
|
2016-12-18 23:22:20 +00:00
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
# The processed/formatted content of this page.
|
|
|
|
def formatted_content
|
2017-02-07 14:16:46 +00:00
|
|
|
@attributes[:formatted_content] ||= @page&.formatted_data
|
2013-03-04 03:43:52 +00:00
|
|
|
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 nil unless persisted?
|
|
|
|
|
2014-09-25 10:56:23 +00:00
|
|
|
@version ||= @page.version
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array of Gitlab Commit instances.
|
|
|
|
def versions
|
|
|
|
return [] unless persisted?
|
|
|
|
|
2014-09-25 10:56:23 +00:00
|
|
|
@page.versions
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
2013-08-16 13:59:26 +00:00
|
|
|
def commit
|
|
|
|
versions.first
|
|
|
|
end
|
|
|
|
|
2017-07-27 13:00:06 +00:00
|
|
|
def last_commit_sha
|
|
|
|
commit&.sha
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
# Returns the Date that this latest version was
|
|
|
|
# created on.
|
|
|
|
def created_at
|
|
|
|
@page.version.date
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns boolean True or False if this instance
|
|
|
|
# is an old version of the page.
|
|
|
|
def historical?
|
2016-01-27 13:42:31 +00:00
|
|
|
@page.historical? && versions.first.sha != version.sha
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns boolean True or False if this instance
|
2017-03-15 05:44:05 +00:00
|
|
|
# is the latest commit version of the page.
|
|
|
|
def latest?
|
|
|
|
!historical?
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
# Returns boolean True or False if this instance
|
2017-03-20 13:53:23 +00:00
|
|
|
# has been fully created on disk or not.
|
2013-03-04 03:43:52 +00:00
|
|
|
def persisted?
|
|
|
|
@persisted == true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new Wiki Page.
|
|
|
|
#
|
|
|
|
# attr - Hash of attributes to set on the new page.
|
|
|
|
# :title - The title for the new page.
|
|
|
|
# :content - The raw markup content.
|
|
|
|
# :format - Optional symbol representing the
|
|
|
|
# content format. Can be any type
|
2014-04-09 11:35:26 +00:00
|
|
|
# listed in the ProjectWiki::MARKUPS
|
2013-03-04 03:43:52 +00:00
|
|
|
# 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.
|
2017-07-23 08:19:10 +00:00
|
|
|
def create(attrs = {})
|
|
|
|
@attributes.merge!(attrs)
|
2013-03-04 03:43:52 +00:00
|
|
|
|
2017-07-23 08:19:10 +00:00
|
|
|
save(page_details: title) do
|
|
|
|
wiki.create_page(title, content, format, message)
|
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Updates an existing Wiki Page, creating a new version.
|
|
|
|
#
|
2017-07-23 08:19:10 +00:00
|
|
|
# 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 ProjectWiki::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 to replace existing title
|
2013-03-04 03:43:52 +00:00
|
|
|
#
|
|
|
|
# Returns the String SHA1 of the newly created page
|
|
|
|
# or False if the save was unsuccessful.
|
2017-07-23 08:19:10 +00:00
|
|
|
def update(attrs = {})
|
|
|
|
last_commit_sha = attrs.delete(:last_commit_sha)
|
2017-07-27 13:00:06 +00:00
|
|
|
if last_commit_sha && last_commit_sha != self.last_commit_sha
|
2017-03-04 14:03:14 +00:00
|
|
|
raise PageChangedError.new("You are attempting to update a page that has changed since you started editing it.")
|
|
|
|
end
|
|
|
|
|
2017-07-23 08:19:10 +00:00
|
|
|
attrs.slice!(:content, :format, :message, :title)
|
|
|
|
@attributes.merge!(attrs)
|
|
|
|
page_details =
|
|
|
|
if title.present? && @page.title != title
|
|
|
|
title
|
|
|
|
else
|
|
|
|
@page.url_path
|
|
|
|
end
|
|
|
|
|
|
|
|
save(page_details: page_details) do
|
|
|
|
wiki.update_page(
|
|
|
|
@page,
|
|
|
|
content: content,
|
|
|
|
format: format,
|
|
|
|
message: attrs[:message],
|
|
|
|
title: title
|
|
|
|
)
|
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
2013-07-29 10:46:00 +00:00
|
|
|
# Destroys the Wiki Page.
|
2013-03-04 03:43:52 +00:00
|
|
|
#
|
|
|
|
# Returns boolean True or False.
|
|
|
|
def delete
|
|
|
|
if wiki.delete_page(@page)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 01:51:34 +00:00
|
|
|
# Relative path to the partial to be used when rendering collections
|
|
|
|
# of this object.
|
|
|
|
def to_partial_path
|
|
|
|
'projects/wikis/wiki_page'
|
|
|
|
end
|
|
|
|
|
2016-12-30 13:26:30 +00:00
|
|
|
def id
|
|
|
|
page.version.to_s
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_attributes
|
2016-01-08 16:23:45 +00:00
|
|
|
attributes[:slug] = @page.url_path
|
2013-03-04 03:43:52 +00:00
|
|
|
attributes[:title] = @page.title
|
|
|
|
attributes[:format] = @page.format
|
|
|
|
end
|
|
|
|
|
2017-07-23 08:19:10 +00:00
|
|
|
def save(page_details:)
|
|
|
|
return unless valid?
|
2017-03-20 13:53:23 +00:00
|
|
|
|
2017-07-23 08:19:10 +00:00
|
|
|
unless yield
|
|
|
|
errors.add(:base, wiki.error_message)
|
|
|
|
return false
|
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
|
2017-07-23 08:19:10 +00:00
|
|
|
page_title, page_dir = wiki.page_title_and_dir(page_details)
|
|
|
|
gollum_wiki = wiki.wiki
|
|
|
|
@page = gollum_wiki.paged(page_title, page_dir)
|
2013-03-04 03:43:52 +00:00
|
|
|
|
2017-07-23 08:19:10 +00:00
|
|
|
set_attributes
|
|
|
|
@persisted = errors.blank?
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
end
|