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

166 lines
4.4 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
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!
before_action :authorize_create_wiki!, only: [:edit, :create, :history]
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]
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
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
2018-11-08 19:23:39 +05:30
view_param = @project_wiki.empty? ? params[:view] : 'create'
2014-09-02 18:07:02 +05:30
if @page
2018-11-18 11:00:15 +05:30
set_encoding_error unless valid_encoding?
2014-09-02 18:07:02 +05:30
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
)
2018-11-08 19:23:39 +05:30
elsif can?(current_user, :create_wiki, @project) && view_param == 'create'
2018-10-15 14:42:47 +05:30
@page = build_page(title: params[:id])
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),
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
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
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
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
@sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages(limit: 15))
end
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)
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
def build_page(args)
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
flash.now[:notice] = "The content of this page is not encoded in UTF-8. Edits can only be made via the Git repository."
end
2014-09-02 18:07:02 +05:30
end