debian-mirror-gitlab/app/controllers/projects/wikis_controller.rb

133 lines
3.5 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Projects::WikisController < Projects::ApplicationController
2018-03-17 18:26:18 +05:30
include PreviewMarkdown
2015-09-11 14:41:01 +05:30
before_action :authorize_read_wiki!
before_action :authorize_create_wiki!, only: [:edit, :create, :history]
before_action :authorize_admin_wiki!, only: :destroy
before_action :load_project_wiki
2014-09-02 18:07:02 +05:30
def pages
2016-06-02 11:05:42 +05:30
@wiki_pages = Kaminari.paginate_array(@project_wiki.pages).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
def show
@page = @project_wiki.find_page(params[:id], params[:version_id])
if @page
render 'show'
elsif file = @project_wiki.find_file(params[:id], params[:version_id])
response.headers['Content-Security-Policy'] = "default-src 'none'"
response.headers['X-Content-Security-Policy'] = "default-src 'none'"
2018-03-17 18:26:18 +05:30
send_data(
file.raw_data,
type: file.mime_type,
disposition: 'inline',
filename: file.name
)
2014-09-02 18:07:02 +05:30
else
2015-09-11 14:41:01 +05:30
return render('empty') unless can?(current_user, :create_wiki, @project)
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
@page = build_page(title: params[:id])
2014-09-02 18:07:02 +05:30
render 'edit'
end
end
def edit
@page = @project_wiki.find_page(params[:id])
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
2016-06-02 11:05:42 +05:30
@page = @project_wiki.find_page(params[:id])
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),
2015-04-26 12:48:37 +05:30
notice: 'Wiki was successfully updated.'
)
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),
2015-04-26 12:48:37 +05:30
notice: 'Wiki was successfully updated.'
)
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
@page = @project_wiki.find_page(params[:id])
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),
2015-04-26 12:48:37 +05:30
notice: "Page not found"
)
2014-09-02 18:07:02 +05:30
end
end
def destroy
@page = @project_wiki.find_page(params[:id])
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),
status: 302,
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
def load_project_wiki
@project_wiki = ProjectWiki.new(@project, current_user)
# Call #wiki to make sure the Wiki Repo is initialized
@project_wiki.wiki
2018-03-17 18:26:18 +05:30
@sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages(limit: 15))
2015-10-24 18:46:33 +05:30
rescue ProjectWiki::CouldNotCreateWikiError
2014-09-02 18:07:02 +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)
2014-09-02 18:07:02 +05:30
return false
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
def build_page(args)
WikiPage.new(@project_wiki).tap do |page|
page.update_attributes(args)
end
end
2014-09-02 18:07:02 +05:30
end