2013-03-03 22:43:52 -05:00
|
|
|
class WikiPage
|
|
|
|
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
|
|
|
|
|
|
|
|
def to_key
|
|
|
|
[:slug]
|
|
|
|
end
|
|
|
|
|
|
|
|
validates :title, presence: true
|
|
|
|
validates :content, presence: true
|
|
|
|
|
2014-04-09 07:35:26 -04:00
|
|
|
# The Gitlab ProjectWiki instance.
|
2013-03-03 22:43:52 -05: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
|
|
|
|
|
|
|
|
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
|
|
|
|
@attributes[:slug]
|
|
|
|
end
|
|
|
|
|
2015-02-02 23:57:10 -05:00
|
|
|
alias_method :to_param, :slug
|
2013-03-03 22:43:52 -05:00
|
|
|
|
|
|
|
# The formatted title of this page.
|
|
|
|
def title
|
2014-04-09 01:14:16 -04:00
|
|
|
if @attributes[:title]
|
|
|
|
@attributes[:title].gsub(/-+/, ' ')
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2013-03-03 22:43:52 -05: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
|
2014-04-11 04:02:52 -04:00
|
|
|
@attributes[:content] ||= if @page
|
2016-02-28 07:11:43 -05:00
|
|
|
@page.text_data
|
2014-04-11 04:02:52 -04:00
|
|
|
end
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The processed/formatted content of this page.
|
|
|
|
def formatted_content
|
2014-04-11 04:02:52 -04:00
|
|
|
@attributes[:formatted_content] ||= if @page
|
|
|
|
@page.formatted_data
|
|
|
|
end
|
2013-03-03 22:43:52 -05: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 06:56:23 -04:00
|
|
|
@version ||= @page.version
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array of Gitlab Commit instances.
|
|
|
|
def versions
|
|
|
|
return [] unless persisted?
|
|
|
|
|
2014-09-25 06:56:23 -04:00
|
|
|
@page.versions
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
|
|
|
|
2013-08-16 09:59:26 -04:00
|
|
|
def commit
|
|
|
|
versions.first
|
|
|
|
end
|
|
|
|
|
2013-03-03 22:43:52 -05: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 08:42:31 -05:00
|
|
|
@page.historical? && versions.first.sha != version.sha
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns boolean True or False if this instance
|
|
|
|
# has been fully saved to disk or not.
|
|
|
|
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 07:35:26 -04:00
|
|
|
# listed in the ProjectWiki::MARKUPS
|
2013-03-03 22:43:52 -05: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.
|
|
|
|
def create(attr = {})
|
|
|
|
@attributes.merge!(attr)
|
|
|
|
|
|
|
|
save :create_page, title, content, format, message
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updates an existing Wiki Page, creating a new version.
|
|
|
|
#
|
|
|
|
# new_content - The raw markup content to replace the existing.
|
|
|
|
# format - Optional symbol representing the content format.
|
2014-04-09 07:35:26 -04:00
|
|
|
# See ProjectWiki::MARKUPS Hash for available formats.
|
2013-03-03 22:43:52 -05:00
|
|
|
# message - Optional commit message to set on the new version.
|
|
|
|
#
|
|
|
|
# Returns the String SHA1 of the newly created page
|
|
|
|
# or False if the save was unsuccessful.
|
|
|
|
def update(new_content = "", format = :markdown, message = nil)
|
|
|
|
@attributes[:content] = new_content
|
|
|
|
@attributes[:format] = format
|
|
|
|
|
|
|
|
save :update_page, @page, content, format, message
|
|
|
|
end
|
|
|
|
|
2013-07-29 06:46:00 -04:00
|
|
|
# Destroys the Wiki Page.
|
2013-03-03 22:43:52 -05:00
|
|
|
#
|
|
|
|
# Returns boolean True or False.
|
|
|
|
def delete
|
|
|
|
if wiki.delete_page(@page)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_attributes
|
2016-01-08 11:23:45 -05:00
|
|
|
attributes[:slug] = @page.url_path
|
2013-03-03 22:43:52 -05:00
|
|
|
attributes[:title] = @page.title
|
|
|
|
attributes[:format] = @page.format
|
|
|
|
end
|
|
|
|
|
|
|
|
def save(method, *args)
|
2014-05-05 08:01:48 -04:00
|
|
|
project_wiki = wiki
|
|
|
|
if valid? && project_wiki.send(method, *args)
|
2014-04-28 10:22:31 -04:00
|
|
|
|
|
|
|
page_details = if method == :update_page
|
2015-03-21 21:04:29 -04:00
|
|
|
# Use url_path instead of path to omit format extension
|
|
|
|
@page.url_path
|
2014-04-28 10:22:31 -04:00
|
|
|
else
|
|
|
|
title
|
|
|
|
end
|
|
|
|
|
2014-05-05 08:01:48 -04:00
|
|
|
page_title, page_dir = project_wiki.page_title_and_dir(page_details)
|
|
|
|
gollum_wiki = project_wiki.wiki
|
|
|
|
@page = gollum_wiki.paged(page_title, page_dir)
|
2013-03-03 22:43:52 -05:00
|
|
|
|
|
|
|
set_attributes
|
|
|
|
|
|
|
|
@persisted = true
|
|
|
|
else
|
2014-05-05 08:01:48 -04:00
|
|
|
errors.add(:base, project_wiki.error_message) if project_wiki.error_message
|
2013-03-03 22:43:52 -05:00
|
|
|
@persisted = false
|
|
|
|
end
|
|
|
|
@persisted
|
|
|
|
end
|
|
|
|
end
|