2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-23 12:47:22 -04:00
|
|
|
class Projects::WikisController < Projects::ApplicationController
|
2017-10-11 05:03:19 -04:00
|
|
|
include PreviewMarkdown
|
2018-07-31 02:08:19 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-10-11 05:03:19 -04:00
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :authorize_read_wiki!
|
2015-06-26 10:44:21 -04:00
|
|
|
before_action :authorize_create_wiki!, only: [:edit, :create, :history]
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :authorize_admin_wiki!, only: :destroy
|
|
|
|
before_action :load_project_wiki
|
2018-07-31 02:08:19 -04:00
|
|
|
before_action :load_page, only: [:show, :edit, :update, :history, :destroy]
|
|
|
|
before_action :valid_encoding?, only: [:show, :edit, :update], if: :load_page
|
|
|
|
before_action only: [:edit, :update], unless: :valid_encoding? do
|
|
|
|
redirect_to(project_wiki_path(@project, @page))
|
|
|
|
end
|
2013-01-11 14:04:14 -05:00
|
|
|
|
2012-08-09 00:34:29 -04:00
|
|
|
def pages
|
2016-03-19 17:37:54 -04:00
|
|
|
@wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page])
|
2016-12-26 20:54:36 -05:00
|
|
|
@wiki_entries = WikiPage.group_by_directory(@wiki_pages)
|
2012-08-09 00:34:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
def show
|
2018-05-29 03:28:49 -04:00
|
|
|
view_param = @project_wiki.empty? ? params[:view] : 'create'
|
|
|
|
|
2014-04-09 07:35:01 -04:00
|
|
|
if @page
|
2018-07-31 02:08:19 -04:00
|
|
|
set_encoding_error unless valid_encoding?
|
|
|
|
|
2012-07-26 07:45:17 -04:00
|
|
|
render 'show'
|
2014-03-28 16:15:25 -04:00
|
|
|
elsif file = @project_wiki.find_file(params[:id], params[:version_id])
|
2016-06-08 10:09:12 -04:00
|
|
|
response.headers['Content-Security-Policy'] = "default-src 'none'"
|
|
|
|
response.headers['X-Content-Security-Policy'] = "default-src 'none'"
|
|
|
|
|
2017-10-03 12:58:33 -04:00
|
|
|
send_data(
|
|
|
|
file.raw_data,
|
|
|
|
type: file.mime_type,
|
|
|
|
disposition: 'inline',
|
|
|
|
filename: file.name
|
|
|
|
)
|
2018-05-29 03:28:49 -04:00
|
|
|
elsif can?(current_user, :create_wiki, @project) && view_param == 'create'
|
2018-04-18 13:50:56 -04:00
|
|
|
@page = build_page(title: params[:id])
|
2013-03-03 22:43:52 -05:00
|
|
|
|
|
|
|
render 'edit'
|
2018-05-29 03:28:49 -04:00
|
|
|
else
|
|
|
|
render 'empty'
|
2012-02-19 09:35:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-06-26 09:55:56 -04:00
|
|
|
return render('empty') unless can?(current_user, :create_wiki, @project)
|
2013-03-03 22:43:52 -05:00
|
|
|
|
2017-03-20 09:53:23 -04:00
|
|
|
@page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
|
2016-04-26 06:54:49 -04:00
|
|
|
|
2017-03-20 09:53:23 -04:00
|
|
|
if @page.valid?
|
2015-04-07 11:58:12 -04:00
|
|
|
redirect_to(
|
2017-06-29 13:06:35 -04:00
|
|
|
project_wiki_path(@project, @page),
|
2015-04-07 11:58:12 -04:00
|
|
|
notice: 'Wiki was successfully updated.'
|
|
|
|
)
|
2013-03-03 22:43:52 -05:00
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
2018-04-18 13:50:56 -04:00
|
|
|
rescue WikiPage::PageChangedError, WikiPage::PageRenameError, Gitlab::Git::Wiki::OperationError => e
|
2018-02-05 12:17:21 -05:00
|
|
|
@error = e
|
2017-03-04 09:03:14 -05:00
|
|
|
render 'edit'
|
2012-02-19 09:35:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-02-28 02:26:52 -05:00
|
|
|
@page = WikiPages::CreateService.new(@project, current_user, wiki_params).execute
|
2013-03-03 22:43:52 -05:00
|
|
|
|
2016-04-04 23:36:43 -04:00
|
|
|
if @page.persisted?
|
2015-01-24 13:02:58 -05:00
|
|
|
redirect_to(
|
2017-06-29 13:06:35 -04:00
|
|
|
project_wiki_path(@project, @page),
|
2015-01-24 13:02:58 -05:00
|
|
|
notice: 'Wiki was successfully updated.'
|
|
|
|
)
|
2013-03-03 22:43:52 -05:00
|
|
|
else
|
|
|
|
render action: "edit"
|
2012-02-19 09:35:31 -05:00
|
|
|
end
|
2018-04-18 13:50:56 -04:00
|
|
|
rescue Gitlab::Git::Wiki::OperationError => e
|
|
|
|
@page = build_page(wiki_params)
|
|
|
|
@error = e
|
|
|
|
|
|
|
|
render 'edit'
|
2012-02-19 09:35:31 -05:00
|
|
|
end
|
2012-02-19 14:52:05 -05:00
|
|
|
|
|
|
|
def history
|
2017-11-17 06:48:32 -05:00
|
|
|
if @page
|
2018-01-02 03:23:39 -05:00
|
|
|
@page_versions = Kaminari.paginate_array(@page.versions(page: params[:page].to_i),
|
2017-11-17 06:48:32 -05:00
|
|
|
total_count: @page.count_versions)
|
2018-01-02 03:23:39 -05:00
|
|
|
.page(params[:page])
|
2017-11-17 06:48:32 -05:00
|
|
|
else
|
2015-01-24 13:02:58 -05:00
|
|
|
redirect_to(
|
2017-06-29 13:06:35 -04:00
|
|
|
project_wiki_path(@project, :home),
|
2015-01-24 13:02:58 -05:00
|
|
|
notice: "Page not found"
|
|
|
|
)
|
2014-04-09 07:35:01 -04:00
|
|
|
end
|
2012-02-19 14:52:05 -05:00
|
|
|
end
|
2013-01-11 14:04:14 -05:00
|
|
|
|
2012-02-19 09:35:31 -05:00
|
|
|
def destroy
|
2016-12-20 08:32:43 -05:00
|
|
|
WikiPages::DestroyService.new(@project, current_user).execute(@page)
|
2014-04-09 07:35:01 -04:00
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_wiki_path(@project, :home),
|
2017-06-06 18:45:16 -04:00
|
|
|
status: 302,
|
|
|
|
notice: "Page was successfully deleted"
|
2018-04-18 13:50:56 -04:00
|
|
|
rescue Gitlab::Git::Wiki::OperationError => e
|
|
|
|
@error = e
|
|
|
|
render 'edit'
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
2012-02-19 09:35:31 -05:00
|
|
|
|
2017-04-20 04:56:41 -04:00
|
|
|
def git_access
|
2016-03-30 14:04:58 -04:00
|
|
|
end
|
|
|
|
|
2013-03-03 22:43:52 -05:00
|
|
|
private
|
|
|
|
|
2014-04-09 07:35:01 -04:00
|
|
|
def load_project_wiki
|
2018-07-22 02:48:12 -04:00
|
|
|
@project_wiki = load_wiki
|
2013-03-03 22:43:52 -05:00
|
|
|
|
|
|
|
# Call #wiki to make sure the Wiki Repo is initialized
|
2014-04-09 07:35:01 -04:00
|
|
|
@project_wiki.wiki
|
2018-03-26 04:13:43 -04:00
|
|
|
|
2018-03-26 05:16:01 -04:00
|
|
|
@sidebar_page = @project_wiki.find_sidebar(params[:version_id])
|
2018-03-26 04:13:43 -04:00
|
|
|
|
|
|
|
unless @sidebar_page # Fallback to default sidebar
|
|
|
|
@sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages(limit: 15))
|
|
|
|
end
|
2015-10-03 01:56:37 -04:00
|
|
|
rescue ProjectWiki::CouldNotCreateWikiError
|
2013-03-03 22:43:52 -05:00
|
|
|
flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
|
2015-03-02 21:11:50 -05:00
|
|
|
redirect_to project_path(@project)
|
2018-07-02 06:43:06 -04:00
|
|
|
false
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
|
|
|
|
2018-07-22 02:48:12 -04:00
|
|
|
def load_wiki
|
|
|
|
ProjectWiki.new(@project, current_user)
|
|
|
|
end
|
|
|
|
|
2013-03-03 22:43:52 -05:00
|
|
|
def wiki_params
|
2017-07-23 08:26:02 -04:00
|
|
|
params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
|
2013-03-03 22:43:52 -05:00
|
|
|
end
|
2018-04-18 13:50:56 -04:00
|
|
|
|
|
|
|
def build_page(args)
|
|
|
|
WikiPage.new(@project_wiki).tap do |page|
|
2018-07-02 09:57:38 -04:00
|
|
|
page.update_attributes(args) # rubocop:disable Rails/ActiveRecordAliases
|
2018-04-18 13:50:56 -04:00
|
|
|
end
|
|
|
|
end
|
2018-07-31 02:08:19 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
flash.now[:notice] = "The content of this page is not encoded in UTF-8. Edits can only be made via the Git repository."
|
|
|
|
end
|
2012-02-19 09:35:31 -05:00
|
|
|
end
|