2017-10-03 12:58:33 -04:00
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class Wiki
|
|
|
|
DuplicatePageError = Class.new(StandardError)
|
|
|
|
|
|
|
|
CommitDetails = Struct.new(:name, :email, :message) do
|
|
|
|
def to_h
|
|
|
|
{ name: name, email: email, message: message }
|
|
|
|
end
|
|
|
|
end
|
2017-10-26 12:40:32 -04:00
|
|
|
PageBlob = Struct.new(:name)
|
2017-10-03 12:58:33 -04:00
|
|
|
|
2017-10-14 21:52:07 -04:00
|
|
|
attr_reader :repository
|
|
|
|
|
2017-10-03 12:58:33 -04:00
|
|
|
def self.default_ref
|
|
|
|
'master'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initialize with a Gitlab::Git::Repository instance
|
|
|
|
def initialize(repository)
|
|
|
|
@repository = repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_exists?
|
|
|
|
@repository.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_page(name, format, content, commit_details)
|
2017-10-13 09:11:53 -04:00
|
|
|
@repository.gitaly_migrate(:wiki_write_page) do |is_enabled|
|
|
|
|
if is_enabled
|
|
|
|
gitaly_write_page(name, format, content, commit_details)
|
|
|
|
gollum_wiki.clear_cache
|
|
|
|
else
|
|
|
|
gollum_write_page(name, format, content, commit_details)
|
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_page(page_path, commit_details)
|
2017-10-31 06:38:15 -04:00
|
|
|
@repository.gitaly_migrate(:wiki_delete_page) do |is_enabled|
|
|
|
|
if is_enabled
|
|
|
|
gitaly_delete_page(page_path, commit_details)
|
|
|
|
gollum_wiki.clear_cache
|
|
|
|
else
|
|
|
|
gollum_delete_page(page_path, commit_details)
|
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_page(page_path, title, format, content, commit_details)
|
2017-11-02 10:08:39 -04:00
|
|
|
@repository.gitaly_migrate(:wiki_update_page) do |is_enabled|
|
|
|
|
if is_enabled
|
|
|
|
gitaly_update_page(page_path, title, format, content, commit_details)
|
|
|
|
gollum_wiki.clear_cache
|
|
|
|
else
|
|
|
|
gollum_update_page(page_path, title, format, content, commit_details)
|
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
2017-11-17 06:48:32 -05:00
|
|
|
def pages(limit: nil)
|
|
|
|
@repository.gitaly_migrate(:wiki_get_all_pages, status: Gitlab::GitalyClient::MigrationStatus::DISABLED) do |is_enabled|
|
2017-11-09 18:27:00 -05:00
|
|
|
if is_enabled
|
|
|
|
gitaly_get_all_pages
|
|
|
|
else
|
2017-11-17 06:48:32 -05:00
|
|
|
gollum_get_all_pages(limit: limit)
|
2017-11-09 18:27:00 -05:00
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def page(title:, version: nil, dir: nil)
|
2017-10-19 10:18:26 -04:00
|
|
|
@repository.gitaly_migrate(:wiki_find_page) do |is_enabled|
|
|
|
|
if is_enabled
|
|
|
|
gitaly_find_page(title: title, version: version, dir: dir)
|
|
|
|
else
|
|
|
|
gollum_find_page(title: title, version: version, dir: dir)
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def file(name, version)
|
2017-10-23 16:12:11 -04:00
|
|
|
@repository.gitaly_migrate(:wiki_find_file) do |is_enabled|
|
|
|
|
if is_enabled
|
|
|
|
gitaly_find_file(name, version)
|
|
|
|
else
|
|
|
|
gollum_find_file(name, version)
|
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
2017-11-17 06:48:32 -05:00
|
|
|
# options:
|
|
|
|
# :page - The Integer page number.
|
|
|
|
# :per_page - The number of items per page.
|
|
|
|
# :limit - Total number of items to return.
|
|
|
|
def page_versions(page_path, options = {})
|
2017-10-03 12:58:33 -04:00
|
|
|
current_page = gollum_page_by_path(page_path)
|
2017-11-17 06:48:32 -05:00
|
|
|
|
|
|
|
commits_from_page(current_page, options).map do |gitlab_git_commit|
|
|
|
|
gollum_page = gollum_wiki.page(current_page.title, gitlab_git_commit.id)
|
|
|
|
Gitlab::Git::WikiPageVersion.new(gitlab_git_commit, gollum_page&.format)
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-17 06:48:32 -05:00
|
|
|
def count_page_versions(page_path)
|
|
|
|
@repository.count_commits(ref: 'HEAD', path: page_path)
|
|
|
|
end
|
|
|
|
|
2017-10-03 12:58:33 -04:00
|
|
|
def preview_slug(title, format)
|
2017-10-26 12:40:32 -04:00
|
|
|
# Adapted from gollum gem (Gollum::Wiki#preview_page) to avoid
|
|
|
|
# using Rugged through a Gollum::Wiki instance
|
|
|
|
page_class = Gollum::Page
|
|
|
|
page = page_class.new(nil)
|
|
|
|
ext = page_class.format_to_ext(format.to_sym)
|
|
|
|
name = page_class.cname(title) + '.' + ext
|
|
|
|
blob = PageBlob.new(name)
|
|
|
|
page.populate(blob)
|
|
|
|
page.url_path
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-17 06:48:32 -05:00
|
|
|
# options:
|
|
|
|
# :page - The Integer page number.
|
|
|
|
# :per_page - The number of items per page.
|
|
|
|
# :limit - Total number of items to return.
|
|
|
|
def commits_from_page(gollum_page, options = {})
|
|
|
|
unless options[:limit]
|
|
|
|
options[:offset] = ([1, options.delete(:page).to_i].max - 1) * Gollum::Page.per_page
|
|
|
|
options[:limit] = (options.delete(:per_page) || Gollum::Page.per_page).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
@repository.log(ref: gollum_page.last_version.id,
|
|
|
|
path: gollum_page.path,
|
|
|
|
limit: options[:limit],
|
|
|
|
offset: options[:offset])
|
|
|
|
end
|
|
|
|
|
2017-10-03 12:58:33 -04:00
|
|
|
def gollum_wiki
|
|
|
|
@gollum_wiki ||= Gollum::Wiki.new(@repository.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gollum_page_by_path(page_path)
|
|
|
|
page_name = Gollum::Page.canonicalize_filename(page_path)
|
|
|
|
page_dir = File.split(page_path).first
|
|
|
|
|
|
|
|
gollum_wiki.paged(page_name, page_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_page(gollum_page)
|
|
|
|
Gitlab::Git::WikiPage.new(gollum_page, new_version(gollum_page, gollum_page.version.id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_version(gollum_page, commit_id)
|
2017-11-17 06:48:32 -05:00
|
|
|
Gitlab::Git::WikiPageVersion.new(version(commit_id), gollum_page&.format)
|
|
|
|
end
|
|
|
|
|
|
|
|
def version(commit_id)
|
|
|
|
commit_find_proc = -> { Gitlab::Git::Commit.find(@repository, commit_id) }
|
|
|
|
|
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.fetch([:wiki_version_commit, commit_id]) { commit_find_proc.call }
|
|
|
|
else
|
|
|
|
commit_find_proc.call
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_type!(object, klass)
|
|
|
|
unless object.is_a?(klass)
|
|
|
|
raise ArgumentError, "expected a #{klass}, got #{object.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2017-10-13 09:11:53 -04:00
|
|
|
|
|
|
|
def gitaly_wiki_client
|
|
|
|
@gitaly_wiki_client ||= Gitlab::GitalyClient::WikiService.new(@repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gollum_write_page(name, format, content, commit_details)
|
|
|
|
assert_type!(format, Symbol)
|
|
|
|
assert_type!(commit_details, CommitDetails)
|
|
|
|
|
|
|
|
gollum_wiki.write_page(name, format, content, commit_details.to_h)
|
|
|
|
|
|
|
|
nil
|
|
|
|
rescue Gollum::DuplicatePageError => e
|
|
|
|
raise Gitlab::Git::Wiki::DuplicatePageError, e.message
|
|
|
|
end
|
|
|
|
|
2017-10-31 06:38:15 -04:00
|
|
|
def gollum_delete_page(page_path, commit_details)
|
|
|
|
assert_type!(commit_details, CommitDetails)
|
|
|
|
|
|
|
|
gollum_wiki.delete_page(gollum_page_by_path(page_path), commit_details.to_h)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-11-02 10:08:39 -04:00
|
|
|
def gollum_update_page(page_path, title, format, content, commit_details)
|
|
|
|
assert_type!(format, Symbol)
|
|
|
|
assert_type!(commit_details, CommitDetails)
|
|
|
|
|
|
|
|
gollum_wiki.update_page(gollum_page_by_path(page_path), title, format, content, commit_details.to_h)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-10-19 10:18:26 -04:00
|
|
|
def gollum_find_page(title:, version: nil, dir: nil)
|
|
|
|
if version
|
|
|
|
version = Gitlab::Git::Commit.find(@repository, version).id
|
|
|
|
end
|
|
|
|
|
|
|
|
gollum_page = gollum_wiki.page(title, version, dir)
|
|
|
|
return unless gollum_page
|
|
|
|
|
|
|
|
new_page(gollum_page)
|
|
|
|
end
|
|
|
|
|
2017-10-23 16:12:11 -04:00
|
|
|
def gollum_find_file(name, version)
|
|
|
|
version ||= self.class.default_ref
|
|
|
|
gollum_file = gollum_wiki.file(name, version)
|
|
|
|
return unless gollum_file
|
|
|
|
|
|
|
|
Gitlab::Git::WikiFile.new(gollum_file)
|
|
|
|
end
|
|
|
|
|
2017-11-17 06:48:32 -05:00
|
|
|
def gollum_get_all_pages(limit: nil)
|
|
|
|
gollum_wiki.pages(limit: limit).map { |gollum_page| new_page(gollum_page) }
|
2017-11-09 18:27:00 -05:00
|
|
|
end
|
|
|
|
|
2017-10-13 09:11:53 -04:00
|
|
|
def gitaly_write_page(name, format, content, commit_details)
|
|
|
|
gitaly_wiki_client.write_page(name, format, content, commit_details)
|
|
|
|
end
|
2017-10-31 06:38:15 -04:00
|
|
|
|
2017-11-02 10:08:39 -04:00
|
|
|
def gitaly_update_page(page_path, title, format, content, commit_details)
|
|
|
|
gitaly_wiki_client.update_page(page_path, title, format, content, commit_details)
|
|
|
|
end
|
|
|
|
|
2017-10-31 06:38:15 -04:00
|
|
|
def gitaly_delete_page(page_path, commit_details)
|
|
|
|
gitaly_wiki_client.delete_page(page_path, commit_details)
|
|
|
|
end
|
2017-10-19 10:18:26 -04:00
|
|
|
|
|
|
|
def gitaly_find_page(title:, version: nil, dir: nil)
|
|
|
|
wiki_page, version = gitaly_wiki_client.find_page(title: title, version: version, dir: dir)
|
|
|
|
return unless wiki_page
|
|
|
|
|
|
|
|
Gitlab::Git::WikiPage.new(wiki_page, version)
|
|
|
|
end
|
2017-10-23 16:12:11 -04:00
|
|
|
|
|
|
|
def gitaly_find_file(name, version)
|
|
|
|
wiki_file = gitaly_wiki_client.find_file(name, version)
|
|
|
|
return unless wiki_file
|
|
|
|
|
|
|
|
Gitlab::Git::WikiFile.new(wiki_file)
|
|
|
|
end
|
2017-11-09 18:27:00 -05:00
|
|
|
|
|
|
|
def gitaly_get_all_pages
|
|
|
|
gitaly_wiki_client.get_all_pages.map do |wiki_page, version|
|
|
|
|
Gitlab::Git::WikiPage.new(wiki_page, version)
|
|
|
|
end
|
|
|
|
end
|
2017-10-03 12:58:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|