2012-01-30 09:12:18 +00:00
|
|
|
#require 'lib/gollum'
|
2011-12-26 17:07:16 +00:00
|
|
|
require 'cgi'
|
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
class Projects::WikiController < Projects::BaseController
|
2012-01-17 22:28:59 +00:00
|
|
|
WIKI_OPTIONS = {}
|
2011-12-26 17:07:16 +00:00
|
|
|
|
2015-03-04 23:19:19 +00:00
|
|
|
before_action :authenticate_user!
|
|
|
|
skip_before_action :authenticate_user!, only: [:show, :index, :git, :compare, :compare_wiki, :history, :wiki_history, :search, :pages] if APP_CONFIG['anonymous_access']
|
2012-01-26 20:51:11 +00:00
|
|
|
load_resource :project
|
2011-12-26 17:07:16 +00:00
|
|
|
|
2015-03-04 23:19:19 +00:00
|
|
|
before_action :authorize_read_actions, only: [:index, :show, :git, :compare, :compare_wiki, :history, :wiki_history, :search, :pages]
|
|
|
|
before_action :authorize_write_actions, only: [:edit, :update, :new, :create, :destroy, :revert, :revert_wiki, :preview]
|
|
|
|
before_action :get_wiki
|
2011-12-26 17:07:16 +00:00
|
|
|
|
2011-12-23 17:01:47 +00:00
|
|
|
def index
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = 'Home'
|
|
|
|
@page = @wiki.page(@name)
|
2012-01-26 14:10:25 +00:00
|
|
|
|
2012-01-29 18:02:18 +00:00
|
|
|
show_or_create_page
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2014-11-19 22:51:49 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_index_path(@project) and return if @name == 'Home'
|
|
|
|
|
2014-11-19 22:51:49 +00:00
|
|
|
ref = params[:ref].presence || @wiki.ref
|
2012-01-29 18:02:18 +00:00
|
|
|
@page = @wiki.page(@name, ref)
|
|
|
|
if !@page && @wiki.page(@name)
|
|
|
|
flash[:error] = t('flash.wiki.ref_not_exist')
|
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name)) and return
|
2012-01-17 23:46:44 +00:00
|
|
|
end
|
2012-01-29 18:02:18 +00:00
|
|
|
|
|
|
|
show_or_create_page
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
if page = @wiki.page(@name)
|
|
|
|
@page = page
|
2012-01-31 23:20:42 +00:00
|
|
|
@content = page.text_data
|
2012-01-29 18:02:18 +00:00
|
|
|
render :edit
|
2011-12-29 18:00:43 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
render :new
|
2011-12-29 18:00:43 +00:00
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2014-11-19 22:51:49 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
@page = @wiki.page(@name)
|
|
|
|
name = wiki_page_params[:rename] || @name
|
2011-12-29 18:00:43 +00:00
|
|
|
|
2014-11-19 22:51:49 +00:00
|
|
|
update_wiki_page(@wiki, @page, wiki_page_params[:content], {committer: committer}, name, wiki_page_params[:format])
|
|
|
|
update_wiki_page(@wiki, @page.footer, wiki_page_params[:footer], {committer: committer}) if wiki_page_params[:footer]
|
|
|
|
update_wiki_page(@wiki, @page.sidebar, wiki_page_params[:sidebar], {committer: committer}) if wiki_page_params[:sidebar]
|
2011-12-29 18:00:43 +00:00
|
|
|
|
2012-01-29 18:02:18 +00:00
|
|
|
committer.commit
|
2011-12-29 18:00:43 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
flash[:notice] = t('flash.wiki.successfully_updated', name: @name)
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = ''
|
2014-11-19 22:51:49 +00:00
|
|
|
@new = true
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-11-19 22:51:49 +00:00
|
|
|
@name = CGI.unescape(wiki_page_params[:page])
|
|
|
|
format = wiki_page_params[:format].intern
|
2012-01-29 18:02:18 +00:00
|
|
|
begin
|
2014-11-19 22:51:49 +00:00
|
|
|
@wiki.write_page(@name, format, wiki_page_params[:content] || '', {committer: committer}).commit
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2014-01-21 04:58:53 +00:00
|
|
|
rescue Gollum::DuplicatePageError => e
|
2014-01-21 04:51:49 +00:00
|
|
|
flash[:error] = t("flash.wiki.duplicate_page", name: @name)
|
2014-11-19 22:51:49 +00:00
|
|
|
render :new
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
page = @wiki.page(@name)
|
|
|
|
if page
|
2014-01-21 04:51:49 +00:00
|
|
|
@wiki.delete_page(page, {committer: committer}).commit
|
2012-01-29 18:02:18 +00:00
|
|
|
flash[:notice] = t("flash.wiki.page_successfully_removed")
|
2012-01-18 14:33:35 +00:00
|
|
|
else
|
2014-01-21 04:51:49 +00:00
|
|
|
flash[:notice] = t("flash.wiki.page_not_found", name: params[:id])
|
2012-01-18 14:33:35 +00:00
|
|
|
end
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_index_path(@project)
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
2012-01-20 15:01:59 +00:00
|
|
|
def git
|
|
|
|
end
|
|
|
|
|
2011-12-26 17:07:16 +00:00
|
|
|
def compare
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
if request.post?
|
|
|
|
@versions = params[:versions] || []
|
|
|
|
if @versions.size < 2
|
|
|
|
redirect_to history_project_wiki_path(@project, CGI.escape(@name))
|
2011-12-26 17:07:16 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to compare_versions_project_wiki_path(@project, CGI.escape(@name),
|
|
|
|
sprintf('%s...%s', @versions.last, @versions.first))
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
2012-01-29 18:02:18 +00:00
|
|
|
elsif request.get?
|
|
|
|
@versions = params[:versions].split(/\.{2,3}/)
|
|
|
|
if @versions.size < 2
|
|
|
|
redirect_to history_project_wiki_path(@project, CGI.escape(@name))
|
|
|
|
return
|
|
|
|
end
|
2012-01-31 23:20:42 +00:00
|
|
|
@page = @wiki.page(@name)
|
|
|
|
@diffs = [@wiki.repo.diff(@versions.first, @versions.last, @page.path).first]
|
2012-01-29 18:02:18 +00:00
|
|
|
render :compare
|
2012-01-23 12:45:38 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2012-01-23 12:45:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_wiki
|
2012-01-29 18:02:18 +00:00
|
|
|
if request.post?
|
|
|
|
@versions = params[:versions] || []
|
2012-02-14 14:54:44 +00:00
|
|
|
versions_string = case @versions.size
|
|
|
|
when 1 then @versions.first
|
|
|
|
when 2 then sprintf('%s...%s', @versions.last, @versions.first)
|
|
|
|
else begin
|
2012-02-14 22:13:52 +00:00
|
|
|
redirect_to history_project_wiki_index_path(@project)
|
|
|
|
return
|
2012-02-14 14:54:44 +00:00
|
|
|
end
|
2012-01-29 18:02:18 +00:00
|
|
|
end
|
2012-02-14 14:54:44 +00:00
|
|
|
redirect_to compare_versions_project_wiki_index_path(@project, versions_string)
|
2012-01-29 18:02:18 +00:00
|
|
|
elsif request.get?
|
2012-02-14 14:54:44 +00:00
|
|
|
@versions = params[:versions].split(/\.{2,3}/) || []
|
|
|
|
@diffs = case @versions.size
|
|
|
|
when 1 then @wiki.repo.commit_diff(@versions.first)
|
|
|
|
when 2 then @wiki.repo.diff(@versions.first, @versions.last)
|
|
|
|
else begin
|
|
|
|
redirect_to history_project_wiki_index_path(@project)
|
|
|
|
return
|
|
|
|
end
|
2012-01-23 12:45:38 +00:00
|
|
|
end
|
2012-01-29 18:02:18 +00:00
|
|
|
render :compare
|
2011-12-26 17:07:16 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
@page = @wiki.page(@name)
|
|
|
|
sha1 = params[:sha1]
|
|
|
|
sha2 = params[:sha2]
|
2012-02-14 14:54:44 +00:00
|
|
|
sha2 = nil if params[:sha2] == 'prev'
|
2012-01-29 18:02:18 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
if c = @wiki.revert_page(@page, sha1, sha2, {committer: committer}) and c.commit
|
2012-01-29 18:02:18 +00:00
|
|
|
flash[:notice] = t("flash.wiki.revert_success")
|
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2011-12-26 17:07:16 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
# if revert wasn't successful then redirect back to comparsion.
|
2014-01-21 04:51:49 +00:00
|
|
|
# if second commit version is missed, then second version is
|
2012-01-29 18:02:18 +00:00
|
|
|
# params[:sha1] and first version is parent of params[:sha1]
|
|
|
|
# (see Gollum::Wiki#revert_page)
|
|
|
|
sha2, sha1 = sha1, "#{sha1}^" if !sha2
|
|
|
|
@versions = [sha1, sha2]
|
|
|
|
diffs = @wiki.repo.diff(@versions.first, @versions.last, @page.path)
|
|
|
|
@diffs = [diffs.first]
|
|
|
|
flash[:error] = t("flash.wiki.patch_does_not_apply")
|
|
|
|
render :compare
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
2011-12-26 17:07:16 +00:00
|
|
|
|
2012-01-28 12:10:43 +00:00
|
|
|
def revert_wiki
|
2012-01-29 18:02:18 +00:00
|
|
|
sha1 = params[:sha1]
|
|
|
|
sha2 = params[:sha2]
|
2012-02-14 14:54:44 +00:00
|
|
|
sha2 = nil if sha2 == 'prev'
|
2014-01-21 04:51:49 +00:00
|
|
|
if c = @wiki.revert_commit(sha1, sha2, {committer: committer}) and c.commit
|
2012-01-29 18:02:18 +00:00
|
|
|
flash[:notice] = t("flash.wiki.revert_success")
|
|
|
|
redirect_to project_wiki_index_path(@project)
|
2012-01-28 12:10:43 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
sha2, sha1 = sha1, "#{sha1}^" if !sha2
|
|
|
|
@versions = [sha1, sha2]
|
2012-02-14 14:54:44 +00:00
|
|
|
@diffs = @wiki.repo.diff(@versions.first, @versions.last)
|
2012-01-29 18:02:18 +00:00
|
|
|
flash[:error] = t("flash.wiki.patch_does_not_apply")
|
|
|
|
render :compare
|
2012-01-28 12:10:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-26 17:07:16 +00:00
|
|
|
def preview
|
2014-11-19 22:51:49 +00:00
|
|
|
@name = wiki_page_params[:page]
|
|
|
|
@page = @wiki.preview_page(@name, wiki_page_params[:content], wiki_page_params[:format])
|
2012-01-29 18:02:18 +00:00
|
|
|
@content = @page.formatted_data
|
|
|
|
@editable = false
|
|
|
|
render :show
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def history
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
if @page = @wiki.page(@name)
|
|
|
|
@versions = @page.versions
|
2012-01-17 22:28:59 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to :back
|
2012-01-17 22:28:59 +00:00
|
|
|
end
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
2012-01-24 20:47:55 +00:00
|
|
|
def wiki_history
|
2012-01-29 18:02:18 +00:00
|
|
|
@versions = @wiki.log
|
|
|
|
render :history
|
2012-01-24 20:47:55 +00:00
|
|
|
end
|
|
|
|
|
2011-12-26 17:07:16 +00:00
|
|
|
def search
|
2012-01-29 18:02:18 +00:00
|
|
|
@query = params[:q]
|
|
|
|
@results = @wiki.search @query
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pages
|
2012-01-29 18:02:18 +00:00
|
|
|
@results = @wiki.pages
|
|
|
|
@ref = @wiki.ref
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def get_wiki
|
2012-01-23 13:38:10 +00:00
|
|
|
@wiki = Gollum::Wiki.new(@project.wiki_path,
|
2014-01-21 04:51:49 +00:00
|
|
|
WIKI_OPTIONS.merge(base_path: project_wiki_index_path(@project)))
|
2011-12-29 18:00:43 +00:00
|
|
|
end
|
|
|
|
|
2012-01-23 13:38:10 +00:00
|
|
|
# This method was grabbed from sinatra application, shipped with Gollum gem.
|
|
|
|
# See Gollum gem and Gollum License if you have any questions about license notes.
|
2012-01-30 15:27:54 +00:00
|
|
|
# https://github.com/github/gollum https://github.com/github/gollum/blob/master/LICENSE
|
2012-01-20 22:11:46 +00:00
|
|
|
def update_wiki_page(wiki, page, content, commit_msg, name = nil, format = nil)
|
2014-01-21 04:51:49 +00:00
|
|
|
return if !page ||
|
2011-12-29 18:00:43 +00:00
|
|
|
((!content || page.raw_data == content) && page.format == format)
|
|
|
|
name ||= page.name
|
|
|
|
format = (format || page.format).to_sym
|
|
|
|
content ||= page.raw_data
|
2012-01-20 22:11:46 +00:00
|
|
|
wiki.update_page(page, name, format, content.to_s, commit_msg)
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commit_message
|
2014-11-19 22:51:49 +00:00
|
|
|
msg = wiki_page_params[:message].presence
|
|
|
|
unless msg
|
2012-01-27 18:24:12 +00:00
|
|
|
msg = case action_name.to_s
|
2012-02-13 23:29:33 +00:00
|
|
|
when 'create' then "Created page #{@name.to_s}"
|
|
|
|
when 'update' then "Updated page #{@name.to_s}"
|
|
|
|
when 'destroy' then "Removed page #{@name.to_s}"
|
|
|
|
when 'revert' then "Reverted page #{@name.to_s}"
|
2012-01-28 12:10:43 +00:00
|
|
|
when 'revert_wiki' then "Reverted wiki"
|
|
|
|
end
|
2014-11-19 22:51:49 +00:00
|
|
|
msg << " (#{wiki_page_params[:format]})" if wiki_page_params[:format]
|
2012-01-20 22:11:46 +00:00
|
|
|
end
|
2012-01-28 12:10:43 +00:00
|
|
|
msg = 'Unhandled action' if !msg || msg.empty?
|
2014-01-21 04:51:49 +00:00
|
|
|
{ message: msg }
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
2012-01-18 14:33:35 +00:00
|
|
|
|
2012-02-13 23:29:33 +00:00
|
|
|
def committer
|
2012-03-11 18:28:14 +00:00
|
|
|
unless @committer
|
2014-01-21 04:51:49 +00:00
|
|
|
p = commit_message.merge({name: current_user.uname, email: current_user.email})
|
2012-03-11 18:28:14 +00:00
|
|
|
@committer = Gollum::Committer.new(@wiki, p)
|
2014-01-21 04:51:49 +00:00
|
|
|
GitHook.perform_later!(:notification, :process, {project_id: @project.id, actor_name: @committer.actor.name, commit_sha: @committer.commit})
|
2012-03-11 18:28:14 +00:00
|
|
|
end
|
2012-02-13 23:29:33 +00:00
|
|
|
@committer
|
2012-01-18 14:33:35 +00:00
|
|
|
end
|
2012-01-26 14:10:25 +00:00
|
|
|
|
|
|
|
def show_or_create_page
|
|
|
|
if @page
|
|
|
|
@content = @page.formatted_data
|
2012-01-28 02:05:13 +00:00
|
|
|
@editable = can?(:write, @project)
|
2012-01-26 14:10:25 +00:00
|
|
|
render :show
|
|
|
|
elsif file = @wiki.file(@name)
|
2014-01-21 04:51:49 +00:00
|
|
|
render text: file.raw_data, content_type: file.mime_type
|
2012-01-28 02:05:13 +00:00
|
|
|
elsif can? :write, @project
|
2012-01-26 14:10:25 +00:00
|
|
|
@new = true
|
|
|
|
render :new
|
2012-01-26 16:26:21 +00:00
|
|
|
else
|
2014-01-21 04:51:49 +00:00
|
|
|
redirect_to action: :index #forbidden_path
|
2012-01-26 14:10:25 +00:00
|
|
|
end
|
|
|
|
end
|
2012-01-28 12:10:43 +00:00
|
|
|
|
2014-11-19 22:51:49 +00:00
|
|
|
def wiki_page_params
|
|
|
|
@wiki_page_params ||= params[:wiki_page] || {}
|
|
|
|
end
|
|
|
|
|
2012-01-28 12:10:43 +00:00
|
|
|
def authorize_read_actions
|
2012-03-31 00:37:54 +01:00
|
|
|
authorize! :show, @project
|
2012-01-28 12:10:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_write_actions
|
2012-02-13 18:28:43 +00:00
|
|
|
authorize! :write, @project
|
2012-01-28 12:10:43 +00:00
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
2011-12-26 17:07:16 +00:00
|
|
|
|