2014-04-09 11:35:01 +00:00
|
|
|
require 'project_wiki'
|
|
|
|
|
2013-06-23 16:47:22 +00:00
|
|
|
class Projects::WikisController < Projects::ApplicationController
|
2015-04-16 12:03:37 +00:00
|
|
|
before_action :authorize_read_wiki!
|
2015-06-26 14:44:21 +00:00
|
|
|
before_action :authorize_create_wiki!, only: [:edit, :create, :history]
|
2015-04-16 12:03:37 +00:00
|
|
|
before_action :authorize_admin_wiki!, only: :destroy
|
|
|
|
before_action :load_project_wiki
|
2013-01-11 19:04:14 +00:00
|
|
|
|
2012-08-09 04:34:29 +00:00
|
|
|
def pages
|
2016-03-19 21:37:54 +00:00
|
|
|
@wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page])
|
2012-08-09 04:34:29 +00:00
|
|
|
end
|
|
|
|
|
2012-02-19 14:35:31 +00:00
|
|
|
def show
|
2014-04-09 11:35:01 +00:00
|
|
|
@page = @project_wiki.find_page(params[:id], params[:version_id])
|
2012-02-21 22:31:18 +00:00
|
|
|
|
2014-04-09 11:35:01 +00:00
|
|
|
if @page
|
2012-07-26 11:45:17 +00:00
|
|
|
render 'show'
|
2014-03-28 20:15:25 +00:00
|
|
|
elsif file = @project_wiki.find_file(params[:id], params[:version_id])
|
2016-06-08 14:09:12 +00:00
|
|
|
response.headers['Content-Security-Policy'] = "default-src 'none'"
|
|
|
|
response.headers['X-Content-Security-Policy'] = "default-src 'none'"
|
|
|
|
|
2015-02-03 05:59:28 +00:00
|
|
|
if file.on_disk?
|
|
|
|
send_file file.on_disk_path, disposition: 'inline'
|
|
|
|
else
|
|
|
|
send_data(
|
|
|
|
file.raw_data,
|
|
|
|
type: file.mime_type,
|
|
|
|
disposition: 'inline',
|
|
|
|
filename: file.name
|
|
|
|
)
|
|
|
|
end
|
2012-07-26 11:45:17 +00:00
|
|
|
else
|
2015-06-26 13:55:56 +00:00
|
|
|
return render('empty') unless can?(current_user, :create_wiki, @project)
|
2014-04-09 11:35:01 +00:00
|
|
|
@page = WikiPage.new(@project_wiki)
|
|
|
|
@page.title = params[:id]
|
2013-03-04 03:43:52 +00:00
|
|
|
|
|
|
|
render 'edit'
|
2012-02-19 14:35:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2014-04-09 11:35:01 +00:00
|
|
|
@page = @project_wiki.find_page(params[:id])
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-06-26 13:55:56 +00:00
|
|
|
return render('empty') unless can?(current_user, :create_wiki, @project)
|
2013-03-04 03:43:52 +00:00
|
|
|
|
2016-04-26 10:54:49 +00:00
|
|
|
@page = @project_wiki.find_page(params[:id])
|
|
|
|
|
2016-02-28 07:26:52 +00:00
|
|
|
if @page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
|
2015-04-07 15:58:12 +00:00
|
|
|
redirect_to(
|
|
|
|
namespace_project_wiki_path(@project.namespace, @project, @page),
|
|
|
|
notice: 'Wiki was successfully updated.'
|
|
|
|
)
|
2013-03-04 03:43:52 +00:00
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
2012-02-19 14:35:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-02-28 07:26:52 +00:00
|
|
|
@page = WikiPages::CreateService.new(@project, current_user, wiki_params).execute
|
2013-03-04 03:43:52 +00:00
|
|
|
|
2016-04-05 03:36:43 +00:00
|
|
|
if @page.persisted?
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to(
|
|
|
|
namespace_project_wiki_path(@project.namespace, @project, @page),
|
|
|
|
notice: 'Wiki was successfully updated.'
|
|
|
|
)
|
2013-03-04 03:43:52 +00:00
|
|
|
else
|
|
|
|
render action: "edit"
|
2012-02-19 14:35:31 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-19 19:52:05 +00:00
|
|
|
|
|
|
|
def history
|
2014-04-09 11:35:01 +00:00
|
|
|
@page = @project_wiki.find_page(params[:id])
|
2013-04-03 18:05:10 +00:00
|
|
|
|
2014-04-09 11:35:01 +00:00
|
|
|
unless @page
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to(
|
|
|
|
namespace_project_wiki_path(@project.namespace, @project, :home),
|
|
|
|
notice: "Page not found"
|
|
|
|
)
|
2014-04-09 11:35:01 +00:00
|
|
|
end
|
2012-02-19 19:52:05 +00:00
|
|
|
end
|
2013-01-11 19:04:14 +00:00
|
|
|
|
2012-02-19 14:35:31 +00:00
|
|
|
def destroy
|
2014-04-09 11:35:01 +00:00
|
|
|
@page = @project_wiki.find_page(params[:id])
|
|
|
|
@page.delete if @page
|
|
|
|
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to(
|
|
|
|
namespace_project_wiki_path(@project.namespace, @project, :home),
|
|
|
|
notice: "Page was successfully deleted"
|
|
|
|
)
|
2013-03-04 03:43:52 +00:00
|
|
|
end
|
2012-02-19 14:35:31 +00:00
|
|
|
|
2016-04-13 12:17:42 +00:00
|
|
|
def preview_markdown
|
2016-03-30 18:04:58 +00:00
|
|
|
text = params[:text]
|
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
ext = Gitlab::ReferenceExtractor.new(@project, current_user)
|
|
|
|
ext.analyze(text, author: current_user)
|
2016-03-30 18:04:58 +00:00
|
|
|
|
|
|
|
render json: {
|
2016-06-08 05:02:50 +00:00
|
|
|
body: view_context.markdown(text, pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id]),
|
2016-03-30 18:04:58 +00:00
|
|
|
references: {
|
|
|
|
users: ext.users.map(&:username)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-03-04 03:43:52 +00:00
|
|
|
def git_access
|
2012-02-19 14:35:31 +00:00
|
|
|
end
|
2013-03-04 03:43:52 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-04-09 11:35:01 +00:00
|
|
|
def load_project_wiki
|
|
|
|
@project_wiki = ProjectWiki.new(@project, current_user)
|
2013-03-04 03:43:52 +00:00
|
|
|
|
|
|
|
# Call #wiki to make sure the Wiki Repo is initialized
|
2014-04-09 11:35:01 +00:00
|
|
|
@project_wiki.wiki
|
2016-11-16 18:10:16 +00:00
|
|
|
|
|
|
|
@sidebar_wiki_pages = @project_wiki.pages.first(15)
|
2015-10-03 05:56:37 +00:00
|
|
|
rescue ProjectWiki::CouldNotCreateWikiError
|
2013-03-04 03:43:52 +00:00
|
|
|
flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
|
2015-03-03 02:11:50 +00:00
|
|
|
redirect_to project_path(@project)
|
2013-03-04 03:43:52 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_params
|
|
|
|
params[:wiki].slice(:title, :content, :format, :message)
|
|
|
|
end
|
2012-02-19 14:35:31 +00:00
|
|
|
end
|