2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class Projects::WikisController < Projects::ApplicationController
|
2018-03-17 18:26:18 +05:30
|
|
|
include PreviewMarkdown
|
2018-12-13 13:39:08 +05:30
|
|
|
include SendsBlob
|
2018-11-18 11:00:15 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_read_wiki!
|
2019-10-12 21:52:04 +05:30
|
|
|
before_action :authorize_create_wiki!, only: [:edit, :create]
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_admin_wiki!, only: :destroy
|
|
|
|
before_action :load_project_wiki
|
2018-11-18 11:00:15 +05:30
|
|
|
before_action :load_page, only: [:show, :edit, :update, :history, :destroy]
|
2019-10-12 21:52:04 +05:30
|
|
|
before_action :valid_encoding?,
|
|
|
|
if: -> { %w[show edit update].include?(action_name) && load_page }
|
2018-11-18 11:00:15 +05:30
|
|
|
before_action only: [:edit, :update], unless: :valid_encoding? do
|
|
|
|
redirect_to(project_wiki_path(@project, @page))
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def new
|
|
|
|
redirect_to project_wiki_path(@project, SecureRandom.uuid, random_title: true)
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def pages
|
2019-07-07 11:18:12 +05:30
|
|
|
@wiki_pages = Kaminari.paginate_array(
|
2019-07-31 22:56:46 +05:30
|
|
|
@project_wiki.list_pages(sort: params[:sort], direction: params[:direction])
|
2019-07-07 11:18:12 +05:30
|
|
|
).page(params[:page])
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@wiki_entries = WikiPage.group_by_directory(@wiki_pages)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
# `#show` handles a number of scenarios:
|
|
|
|
#
|
|
|
|
# - If `id` matches a WikiPage, then show the wiki page.
|
|
|
|
# - If `id` is a file in the wiki repository, then send the file.
|
|
|
|
# - If we know the user wants to create a new page with the given `id`,
|
|
|
|
# then display a create form.
|
|
|
|
# - Otherwise show the empty wiki page and invite the user to create a page.
|
2014-09-02 18:07:02 +05:30
|
|
|
def show
|
|
|
|
if @page
|
2018-11-18 11:00:15 +05:30
|
|
|
set_encoding_error unless valid_encoding?
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
# Assign vars expected by MarkupHelper
|
|
|
|
@ref = params[:version_id]
|
|
|
|
@path = @page.path
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
render 'show'
|
2018-12-13 13:39:08 +05:30
|
|
|
elsif file_blob
|
2020-04-22 19:07:51 +05:30
|
|
|
send_blob(@project_wiki.repository, file_blob, allow_caching: @project.public?)
|
2019-12-04 20:38:33 +05:30
|
|
|
elsif show_create_form?
|
|
|
|
# Assign a title to the WikiPage unless `id` is a randomly generated slug from #new
|
|
|
|
title = params[:id] unless params[:random_title].present?
|
|
|
|
|
|
|
|
@page = build_page(title: title)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
render 'edit'
|
2018-11-08 19:23:39 +05:30
|
|
|
else
|
|
|
|
render 'empty'
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-09-11 14:41:01 +05:30
|
|
|
return render('empty') unless can?(current_user, :create_wiki, @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
@page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
if @page.valid?
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to(
|
2017-09-10 17:25:29 +05:30
|
|
|
project_wiki_path(@project, @page),
|
2019-07-07 11:18:12 +05:30
|
|
|
notice: _('Wiki was successfully updated.')
|
2015-04-26 12:48:37 +05:30
|
|
|
)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
rescue WikiPage::PageChangedError, WikiPage::PageRenameError, Gitlab::Git::Wiki::OperationError => e
|
2018-03-17 18:26:18 +05:30
|
|
|
@error = e
|
2017-09-10 17:25:29 +05:30
|
|
|
render 'edit'
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-06-02 11:05:42 +05:30
|
|
|
@page = WikiPages::CreateService.new(@project, current_user, wiki_params).execute
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
if @page.persisted?
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to(
|
2017-09-10 17:25:29 +05:30
|
|
|
project_wiki_path(@project, @page),
|
2019-07-07 11:18:12 +05:30
|
|
|
notice: _('Wiki was successfully updated.')
|
2015-04-26 12:48:37 +05:30
|
|
|
)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
|
|
|
render action: "edit"
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
rescue Gitlab::Git::Wiki::OperationError => e
|
|
|
|
@page = build_page(wiki_params)
|
|
|
|
@error = e
|
|
|
|
|
|
|
|
render 'edit'
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def history
|
2018-03-17 18:26:18 +05:30
|
|
|
if @page
|
|
|
|
@page_versions = Kaminari.paginate_array(@page.versions(page: params[:page].to_i),
|
|
|
|
total_count: @page.count_versions)
|
|
|
|
.page(params[:page])
|
|
|
|
else
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to(
|
2017-09-10 17:25:29 +05:30
|
|
|
project_wiki_path(@project, :home),
|
2019-07-07 11:18:12 +05:30
|
|
|
notice: _("Page not found")
|
2015-04-26 12:48:37 +05:30
|
|
|
)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2017-08-17 22:00:37 +05:30
|
|
|
WikiPages::DestroyService.new(@project, current_user).execute(@page)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_wiki_path(@project, :home),
|
2019-12-26 22:10:19 +05:30
|
|
|
status: :found,
|
2019-07-07 11:18:12 +05:30
|
|
|
notice: _("Page was successfully deleted")
|
2018-10-15 14:42:47 +05:30
|
|
|
rescue Gitlab::Git::Wiki::OperationError => e
|
|
|
|
@error = e
|
|
|
|
render 'edit'
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def git_access
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
private
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def show_create_form?
|
|
|
|
can?(current_user, :create_wiki, @project) &&
|
|
|
|
@page.nil? &&
|
|
|
|
# Always show the create form when the wiki has had at least one page created.
|
|
|
|
# Otherwise, we only show the form when the user has navigated from
|
|
|
|
# the 'empty wiki' page
|
|
|
|
(@project_wiki.exists? || params[:view] == 'create')
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def load_project_wiki
|
2018-11-18 11:00:15 +05:30
|
|
|
@project_wiki = load_wiki
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
# Call #wiki to make sure the Wiki Repo is initialized
|
|
|
|
@project_wiki.wiki
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
@sidebar_page = @project_wiki.find_sidebar(params[:version_id])
|
|
|
|
|
|
|
|
unless @sidebar_page # Fallback to default sidebar
|
2019-07-31 22:56:46 +05:30
|
|
|
@sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.list_pages(limit: 15))
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2015-10-24 18:46:33 +05:30
|
|
|
rescue ProjectWiki::CouldNotCreateWikiError
|
2019-07-07 11:18:12 +05:30
|
|
|
flash[:notice] = _("Could not create Wiki Repository at this time. Please try again later.")
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to project_path(@project)
|
2018-11-18 11:00:15 +05:30
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_wiki
|
|
|
|
ProjectWiki.new(@project, current_user)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_params
|
2017-09-10 17:25:29 +05:30
|
|
|
params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def build_page(args = {})
|
2018-10-15 14:42:47 +05:30
|
|
|
WikiPage.new(@project_wiki).tap do |page|
|
2018-11-18 11:00:15 +05:30
|
|
|
page.update_attributes(args) # rubocop:disable Rails/ActiveRecordAliases
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
2018-11-18 11:00:15 +05:30
|
|
|
|
|
|
|
def load_page
|
|
|
|
@page ||= @project_wiki.find_page(*page_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def page_params
|
|
|
|
keys = [:id]
|
|
|
|
keys << :version_id if params[:action] == 'show'
|
|
|
|
|
|
|
|
params.values_at(*keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_encoding?
|
|
|
|
strong_memoize(:valid_encoding) do
|
|
|
|
@page.content.encoding == Encoding::UTF_8
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_encoding_error
|
2019-07-07 11:18:12 +05:30
|
|
|
flash.now[:notice] = _("The content of this page is not encoded in UTF-8. Edits can only be made via the Git repository.")
|
2018-11-18 11:00:15 +05:30
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
def file_blob
|
|
|
|
strong_memoize(:file_blob) do
|
|
|
|
commit = @project_wiki.repository.commit(@project_wiki.default_branch)
|
|
|
|
|
|
|
|
next unless commit
|
|
|
|
|
|
|
|
@project_wiki.repository.blob_at(commit.id, params[:id])
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|