2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
class Wiki
|
2018-12-13 13:39:08 +05:30
|
|
|
include Gitlab::Git::WrapsGitalyErrors
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
DuplicatePageError = Class.new(StandardError)
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
DEFAULT_PAGINATION = Kaminari.config.default_per_page
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
CommitDetails = Struct.new(:user_id, :username, :name, :email, :message) do
|
2018-03-17 18:26:18 +05:30
|
|
|
def to_h
|
2018-10-15 14:42:47 +05:30
|
|
|
{ user_id: user_id, username: username, name: name, email: email, message: message }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
# GollumSlug inlines just enough knowledge from Gollum::Page to generate a
|
|
|
|
# slug, which is used when previewing pages that haven't been persisted
|
|
|
|
class GollumSlug
|
|
|
|
class << self
|
|
|
|
def cname(name, char_white_sub = '-', char_other_sub = '-')
|
|
|
|
if name.respond_to?(:gsub)
|
|
|
|
name.gsub(/\s/, char_white_sub).gsub(/[<>+]/, char_other_sub)
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_to_ext(format)
|
|
|
|
format == :markdown ? "md" : format.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def canonicalize_filename(filename)
|
|
|
|
::File.basename(filename, ::File.extname(filename)).tr('-', ' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate(title, format)
|
|
|
|
ext = format_to_ext(format.to_sym)
|
|
|
|
name = cname(title) + '.' + ext
|
|
|
|
canonical_name = canonicalize_filename(name)
|
|
|
|
|
|
|
|
path =
|
|
|
|
if name.include?('/')
|
|
|
|
name.sub(%r{/[^/]+$}, '/')
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
path + cname(canonical_name, '-', '-')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
attr_reader :repository
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
# TODO remove argument when issue
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/329190
|
|
|
|
# is closed.
|
|
|
|
def self.default_ref(container = nil)
|
|
|
|
Gitlab::DefaultBranch.value(object: container)
|
2018-03-17 18:26:18 +05:30
|
|
|
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)
|
2018-12-13 13:39:08 +05:30
|
|
|
wrapped_gitaly_errors do
|
2018-11-08 19:23:39 +05:30
|
|
|
gitaly_write_page(name, format, content, commit_details)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_page(page_path, title, format, content, commit_details)
|
2018-12-13 13:39:08 +05:30
|
|
|
wrapped_gitaly_errors do
|
2018-11-08 19:23:39 +05:30
|
|
|
gitaly_update_page(page_path, title, format, content, commit_details)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def list_pages(limit: 0, sort: nil, direction_desc: false, load_content: false)
|
2018-12-13 13:39:08 +05:30
|
|
|
wrapped_gitaly_errors do
|
2019-07-31 22:56:46 +05:30
|
|
|
gitaly_list_pages(
|
|
|
|
limit: limit,
|
|
|
|
sort: sort,
|
|
|
|
direction_desc: direction_desc,
|
|
|
|
load_content: load_content
|
|
|
|
)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def page(title:, version: nil, dir: nil)
|
2018-12-13 13:39:08 +05:30
|
|
|
wrapped_gitaly_errors do
|
2018-11-08 19:23:39 +05:30
|
|
|
gitaly_find_page(title: title, version: version, dir: dir)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def count_page_versions(page_path)
|
|
|
|
@repository.count_commits(ref: 'HEAD', path: page_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview_slug(title, format)
|
2018-12-13 13:39:08 +05:30
|
|
|
GollumSlug.generate(title, format)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def gitaly_wiki_client
|
|
|
|
@gitaly_wiki_client ||= Gitlab::GitalyClient::WikiService.new(@repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_write_page(name, format, content, commit_details)
|
|
|
|
gitaly_wiki_client.write_page(name, format, content, commit_details)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_update_page(page_path, title, format, content, commit_details)
|
|
|
|
gitaly_wiki_client.update_page(page_path, title, format, content, commit_details)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitaly_find_page(title:, version: nil, dir: nil)
|
2021-03-11 19:13:27 +05:30
|
|
|
return unless title.present?
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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)
|
2021-01-03 14:25:43 +05:30
|
|
|
rescue GRPC::InvalidArgument
|
|
|
|
nil
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def gitaly_list_pages(limit: 0, sort: nil, direction_desc: false, load_content: false)
|
|
|
|
params = { limit: limit, sort: sort, direction_desc: direction_desc }
|
|
|
|
|
|
|
|
gitaly_pages =
|
|
|
|
if load_content
|
2021-01-03 14:25:43 +05:30
|
|
|
gitaly_wiki_client.load_all_pages(**params)
|
2019-07-31 22:56:46 +05:30
|
|
|
else
|
2021-01-03 14:25:43 +05:30
|
|
|
gitaly_wiki_client.list_all_pages(**params)
|
2019-07-31 22:56:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
gitaly_pages.map do |wiki_page, version|
|
2018-03-17 18:26:18 +05:30
|
|
|
Gitlab::Git::WikiPage.new(wiki_page, version)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|