2011-12-29 18:00:43 +00:00
|
|
|
require 'lib/gollum'
|
2011-12-26 17:07:16 +00:00
|
|
|
require 'cgi'
|
|
|
|
|
2011-12-23 17:01:47 +00:00
|
|
|
class WikiController < ApplicationController
|
2012-01-17 22:28:59 +00:00
|
|
|
WIKI_OPTIONS = {}
|
2011-12-26 17:07:16 +00:00
|
|
|
|
|
|
|
load_and_authorize_resource :project
|
|
|
|
|
|
|
|
before_filter :get_wiki
|
|
|
|
|
2011-12-23 17:01:47 +00:00
|
|
|
def index
|
2011-12-26 17:07:16 +00:00
|
|
|
@name = 'Home'
|
|
|
|
if page = @wiki.page(@name)
|
|
|
|
@page = page
|
|
|
|
@content = page.formatted_data
|
|
|
|
@editable = true
|
|
|
|
render :show
|
2012-01-16 21:48:38 +00:00
|
|
|
elsif file = @wiki.file(@name)
|
|
|
|
render :text => file.raw_data, :content_type => file.mime_type
|
2011-12-26 17:07:16 +00:00
|
|
|
else
|
2012-01-16 21:48:38 +00:00
|
|
|
@new = true
|
|
|
|
@content = ''
|
2011-12-26 17:07:16 +00:00
|
|
|
render :new
|
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2012-01-16 21:48:38 +00:00
|
|
|
@name = params['id']
|
|
|
|
ref = params['ref'] ? params['ref'] : @wiki.ref
|
2012-01-17 23:46:44 +00:00
|
|
|
@page = @wiki.page(@name, ref)
|
|
|
|
if !@page && @wiki.page(@name)
|
|
|
|
flash[:error] = t('flash.wiki.ref_not_exist')
|
2012-01-18 00:44:42 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2012-01-17 23:46:44 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if @page
|
|
|
|
@content = @page.formatted_data
|
2011-12-29 18:00:43 +00:00
|
|
|
@editable = true
|
2012-01-16 21:48:38 +00:00
|
|
|
render
|
2011-12-29 18:00:43 +00:00
|
|
|
else
|
2012-01-16 21:48:38 +00:00
|
|
|
@new = true
|
|
|
|
render :new
|
2011-12-29 18:00:43 +00:00
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2011-12-29 18:00:43 +00:00
|
|
|
@name = params[:id]
|
|
|
|
if page = @wiki.page(@name)
|
|
|
|
@page = page
|
|
|
|
@content = page.raw_data
|
|
|
|
render :edit
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-01-17 23:30:04 +00:00
|
|
|
@name = params[:id]
|
2011-12-29 18:00:43 +00:00
|
|
|
page = @wiki.page(@name)
|
2012-01-17 23:30:04 +00:00
|
|
|
name = params[:rename] || @name
|
2012-01-18 14:33:35 +00:00
|
|
|
committer = Gollum::Committer.new(@wiki, commit)
|
2011-12-29 18:00:43 +00:00
|
|
|
commit = {:committer => committer}
|
|
|
|
|
|
|
|
update_wiki_page(@wiki, page, params[:content], commit, name, params[:format])
|
|
|
|
update_wiki_page(@wiki, page.footer, params[:footer], commit) if params[:footer]
|
|
|
|
update_wiki_page(@wiki, page.sidebar, params[:sidebar], commit) if params[:sidebar]
|
|
|
|
|
|
|
|
committer.commit
|
|
|
|
|
2012-01-19 14:37:36 +00:00
|
|
|
flash[:notice] = t('flash.wiki.successfully_updated', :name => @name)
|
2011-12-29 18:00:43 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2011-12-29 18:00:43 +00:00
|
|
|
@name = ''
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2012-01-16 21:48:38 +00:00
|
|
|
@name = params['page']
|
|
|
|
format = params['format'].intern
|
|
|
|
|
2011-12-26 17:07:16 +00:00
|
|
|
begin
|
2012-01-17 22:28:59 +00:00
|
|
|
@wiki.write_page(@name, format, params['content'], commit)
|
2012-01-18 00:44:42 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
2011-12-26 17:07:16 +00:00
|
|
|
rescue Gollum::DuplicatePageError => e
|
2012-01-19 14:37:36 +00:00
|
|
|
flash[:error] = t("flash.wiki.duplicate_page", :name => @name)
|
2011-12-26 17:07:16 +00:00
|
|
|
render :action => :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2012-01-18 14:33:35 +00:00
|
|
|
page = @wiki.page(params[:id])
|
|
|
|
if page
|
|
|
|
@wiki.delete_page(page, commit.merge(:message => 'Page removed'))
|
|
|
|
flash[:notice] = t("flash.wiki.page_successfully_removed")
|
|
|
|
else
|
2012-01-19 14:37:36 +00:00
|
|
|
flash[:notice] = t("flash.wiki.page_not_found", :name => params[:id])
|
2012-01-18 14:33:35 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
|
@name = params[:id]
|
|
|
|
if request.post?
|
|
|
|
@versions = params[:versions] || []
|
|
|
|
if @versions.size < 2
|
2012-01-23 12:45:38 +00:00
|
|
|
redirect_to history_project_wiki_path(@project, CGI.escape(@name))
|
2011-12-26 17:07:16 +00:00
|
|
|
else
|
2012-01-23 12:45:38 +00:00
|
|
|
redirect_to compare_versions_project_wiki_path(@project, CGI.escape(@name),
|
2011-12-26 17:07:16 +00:00
|
|
|
sprintf('%s...%s', @versions.last, @versions.first))
|
|
|
|
end
|
|
|
|
elsif request.get?
|
|
|
|
@versions = params[:versions].split(/\.{2,3}/)
|
2012-01-19 23:04:11 +00:00
|
|
|
if @versions.size < 2
|
2012-01-23 12:45:38 +00:00
|
|
|
redirect_to history_project_wiki_path(@project, CGI.escape(@name))
|
|
|
|
return
|
2012-01-19 23:04:11 +00:00
|
|
|
end
|
2012-01-23 12:45:38 +00:00
|
|
|
page = @wiki.page(@name)
|
|
|
|
@diffs = [@wiki.repo.diff(@versions.first, @versions.last, page.path).first]
|
|
|
|
render :compare
|
|
|
|
else
|
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_wiki
|
|
|
|
if request.post?
|
|
|
|
@versions = params[:versions] || []
|
|
|
|
if @versions.size < 2
|
|
|
|
redirect_to history_project_wiki_index_path(@project)
|
2012-01-19 23:04:11 +00:00
|
|
|
else
|
2012-01-23 12:45:38 +00:00
|
|
|
redirect_to compare_versions_project_wiki_index_path(@project,
|
|
|
|
sprintf('%s...%s', @versions.last, @versions.first))
|
|
|
|
end
|
|
|
|
elsif request.get?
|
|
|
|
@versions = params[:versions].split(/\.{2,3}/)
|
|
|
|
if @versions.size < 2
|
|
|
|
redirect_to history_project_wiki_index_path(@project)
|
|
|
|
return
|
2012-01-19 23:04:11 +00:00
|
|
|
end
|
2012-01-23 12:45:38 +00:00
|
|
|
@diffs = @wiki.repo.diff(@versions.first, @versions.last)
|
2011-12-26 17:07:16 +00:00
|
|
|
render :compare
|
|
|
|
else
|
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert
|
|
|
|
@name = params[:id]
|
|
|
|
@page = @wiki.page(@name)
|
|
|
|
sha1 = params[:sha1]
|
|
|
|
sha2 = params[:sha2]
|
|
|
|
|
|
|
|
if @wiki.revert_page(@page, sha1, sha2, commit_message)
|
2011-12-29 18:00:43 +00:00
|
|
|
flash[:notice] = t("flash.wiki.revert_success")
|
2011-12-26 17:07:16 +00:00
|
|
|
redirect_to project_wiki_path(@project, "#{CGI.escape(@name)}")
|
|
|
|
else
|
|
|
|
sha2, sha1 = sha1, "#{sha1}^" if !sha2
|
|
|
|
@versions = [sha1, sha2]
|
|
|
|
diffs = @wiki.repo.diff(@versions.first, @versions.last, @page.path)
|
2012-01-20 22:11:46 +00:00
|
|
|
@diffs = [diffs.first]
|
2011-12-29 18:00:43 +00:00
|
|
|
flash[:error] = t("flash.wiki.patch_does_not_apply")
|
2011-12-26 17:07:16 +00:00
|
|
|
render :compare
|
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
2011-12-26 17:07:16 +00:00
|
|
|
|
|
|
|
def preview
|
2012-01-19 12:07:08 +00:00
|
|
|
@name = params['page']#'Preview'
|
2012-01-16 21:48:38 +00:00
|
|
|
@page = @wiki.preview_page(@name, params['content'], params['format'])
|
2011-12-26 17:07:16 +00:00
|
|
|
@content = @page.formatted_data
|
|
|
|
@editable = false
|
|
|
|
render :show
|
|
|
|
end
|
|
|
|
|
|
|
|
def history
|
2012-01-19 23:04:11 +00:00
|
|
|
if @name = params['id']
|
|
|
|
if @page = @wiki.page(@name)
|
2012-01-23 12:45:38 +00:00
|
|
|
@versions = @page.versions
|
2012-01-19 23:04:11 +00:00
|
|
|
else
|
|
|
|
redirect_to :back
|
|
|
|
end
|
2012-01-17 22:28:59 +00:00
|
|
|
else
|
2012-01-23 12:45:38 +00:00
|
|
|
@versions = @wiki.log
|
2012-01-17 22:28:59 +00:00
|
|
|
end
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def search
|
|
|
|
@query = params[:q]
|
|
|
|
@results = @wiki.search @query
|
|
|
|
end
|
|
|
|
|
|
|
|
def pages
|
|
|
|
@results = @wiki.pages
|
|
|
|
@ref = @wiki.ref
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def get_wiki
|
2011-12-29 18:00:43 +00:00
|
|
|
@wiki = Gollum::Wiki.new(@project.wiki_path, WIKI_OPTIONS.merge(:base_path => project_wiki_index_path(@project)))
|
|
|
|
end
|
|
|
|
|
2012-01-20 22:11:46 +00:00
|
|
|
def update_wiki_page(wiki, page, content, commit_msg, name = nil, format = nil)
|
2011-12-29 18:00:43 +00:00
|
|
|
return if !page ||
|
|
|
|
((!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
|
2012-01-20 22:11:46 +00:00
|
|
|
if params['message'] and !params['message'].empty?
|
|
|
|
msg = params['message']
|
|
|
|
else
|
|
|
|
msg = "#{!!@wiki.page(@name) ? 'Updated page' : 'Created page'} #{@name}"
|
|
|
|
end
|
|
|
|
{ :message => msg }
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
2012-01-18 14:33:35 +00:00
|
|
|
|
|
|
|
def commit
|
|
|
|
commit_message.merge({:name => current_user.name, :email => current_user.email})
|
|
|
|
end
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
2011-12-26 17:07:16 +00:00
|
|
|
|