gitlab-org--gitlab-foss/app/controllers/projects/wikis_controller.rb

166 lines
4.4 KiB
Ruby
Raw Normal View History

class Projects::WikisController < Projects::ApplicationController
include PreviewMarkdown
2018-07-31 02:08:19 -04:00
include Gitlab::Utils::StrongMemoize
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-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
@wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page])
@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
view_param = @project_wiki.empty? ? params[:view] : 'create'
if @page
2018-07-31 02:08:19 -04:00
set_encoding_error unless valid_encoding?
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'"
2017-10-03 12:58:33 -04:00
send_data(
file.raw_data,
type: file.mime_type,
disposition: 'inline',
filename: file.name
)
elsif can?(current_user, :create_wiki, @project) && view_param == 'create'
@page = build_page(title: params[:id])
render 'edit'
else
render 'empty'
2012-02-19 09:35:31 -05:00
end
end
def edit
end
def update
return render('empty') unless can?(current_user, :create_wiki, @project)
2017-03-20 09:53:23 -04:00
@page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
2017-03-20 09:53:23 -04:00
if @page.valid?
redirect_to(
project_wiki_path(@project, @page),
notice: 'Wiki was successfully updated.'
)
else
render 'edit'
end
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
@page = WikiPages::CreateService.new(@project, current_user, wiki_params).execute
if @page.persisted?
redirect_to(
project_wiki_path(@project, @page),
notice: 'Wiki was successfully updated.'
)
else
render action: "edit"
2012-02-19 09:35:31 -05:00
end
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
if @page
@page_versions = Kaminari.paginate_array(@page.versions(page: params[:page].to_i),
total_count: @page.count_versions)
.page(params[:page])
else
redirect_to(
project_wiki_path(@project, :home),
notice: "Page not found"
)
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
WikiPages::DestroyService.new(@project, current_user).execute(@page)
redirect_to project_wiki_path(@project, :home),
status: 302,
notice: "Page was successfully deleted"
rescue Gitlab::Git::Wiki::OperationError => e
@error = e
render 'edit'
end
2012-02-19 09:35:31 -05:00
def git_access
end
private
def load_project_wiki
@project_wiki = load_wiki
# Call #wiki to make sure the Wiki Repo is initialized
@project_wiki.wiki
@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-03 01:56:37 -04:00
rescue ProjectWiki::CouldNotCreateWikiError
flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
redirect_to project_path(@project)
2018-07-02 06:43:06 -04:00
false
end
def load_wiki
ProjectWiki.new(@project, current_user)
end
def wiki_params
params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
end
def build_page(args)
WikiPage.new(@project_wiki).tap do |page|
page.update_attributes(args) # rubocop:disable Rails/ActiveRecordAliases
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