2012-01-31 16:40:46 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-01-30 09:12:18 +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
|
|
|
|
2012-02-03 15:28:19 +00:00
|
|
|
before_filter :authenticate_user!
|
2012-01-26 20:51:11 +00:00
|
|
|
load_resource :project
|
2011-12-26 17:07:16 +00:00
|
|
|
|
2012-01-28 12:10:43 +00:00
|
|
|
before_filter :authorize_read_actions, :only => [:index, :show, :git, :compare, :compare_wiki, :history, :wiki_history, :search, :pages]
|
|
|
|
before_filter :authorize_write_actions, :only => [:edit, :update, :new, :create, :destroy, :revert, :revert_wiki, :preview]
|
2011-12-26 17:07:16 +00:00
|
|
|
before_filter :get_wiki
|
|
|
|
|
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
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params['id'])
|
|
|
|
redirect_to project_wiki_index_path(@project) and return if @name == 'Home'
|
|
|
|
|
|
|
|
ref = params['ref'] ? params['ref'] : @wiki.ref
|
|
|
|
@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
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = CGI.unescape(params[:id])
|
|
|
|
@page = @wiki.page(@name)
|
|
|
|
name = params[:rename] || @name
|
|
|
|
committer = Gollum::Committer.new(@wiki, commit)
|
|
|
|
commit_arg = {:committer => committer}
|
2011-12-29 18:00:43 +00:00
|
|
|
|
2012-01-29 18:02:18 +00:00
|
|
|
update_wiki_page(@wiki, @page, params[:content], commit_arg, name, params[:format])
|
|
|
|
update_wiki_page(@wiki, @page.footer, params[:footer], commit_arg) if params[:footer]
|
|
|
|
update_wiki_page(@wiki, @page.sidebar, params[:sidebar], commit_arg) if 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
|
|
|
|
2012-01-29 18:02:18 +00:00
|
|
|
flash[:notice] = t('flash.wiki.successfully_updated', :name => @name)
|
|
|
|
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 = ''
|
2011-12-23 17:01:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2012-01-31 23:20:42 +00:00
|
|
|
@name = CGI.unescape(params['page'])
|
2012-01-29 18:02:18 +00:00
|
|
|
format = params['format'].intern
|
|
|
|
|
|
|
|
begin
|
2012-01-31 16:40:46 +00:00
|
|
|
@wiki.write_page(@name, format, params['content'] || '', commit)
|
2012-01-29 18:02:18 +00:00
|
|
|
redirect_to project_wiki_path(@project, CGI.escape(@name))
|
|
|
|
rescue Gollum::DuplicatePageError => e
|
|
|
|
flash[:error] = t("flash.wiki.duplicate_page", :name => @name)
|
|
|
|
render :action => :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
|
|
|
|
@wiki.delete_page(page, commit.merge(:message => 'Page removed'))
|
|
|
|
flash[:notice] = t("flash.wiki.page_successfully_removed")
|
2012-01-18 14:33:35 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +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] || []
|
|
|
|
if @versions.size < 2
|
|
|
|
redirect_to history_project_wiki_index_path(@project)
|
2012-01-19 23:04:11 +00:00
|
|
|
else
|
2012-01-29 18:02:18 +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-23 12:45:38 +00:00
|
|
|
end
|
2012-01-29 18:02:18 +00:00
|
|
|
@diffs = @wiki.repo.diff(@versions.first, @versions.last)
|
|
|
|
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]
|
|
|
|
|
|
|
|
if @wiki.revert_page(@page, sha1, sha2, commit)
|
|
|
|
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.
|
|
|
|
# if second commit version is missed, then second version is
|
|
|
|
# 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]
|
|
|
|
if @wiki.revert_commit(sha1, sha2, commit)
|
|
|
|
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]
|
|
|
|
diffs = @wiki.repo.diff(@versions.first, @versions.last)
|
|
|
|
@diffs = [diffs.first]
|
|
|
|
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
|
2012-01-29 18:02:18 +00:00
|
|
|
@name = params['page']
|
|
|
|
@page = @wiki.preview_page(@name, params['content'], params['format'])
|
|
|
|
@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,
|
|
|
|
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)
|
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
|
2012-01-27 18:24:12 +00:00
|
|
|
# msg = "#{!!@wiki.page(@name) ? 'Updated page' : 'Created page'} #{@name}"
|
|
|
|
msg = case action_name.to_s
|
2012-01-28 12:10:43 +00:00
|
|
|
when 'create' then "Created page #{@name.to_s}"
|
|
|
|
when 'update' then "Updated page #{@name.to_s}"
|
|
|
|
when 'revert' then "Reverted page #{@name.to_s}"
|
|
|
|
when 'revert_wiki' then "Reverted wiki"
|
|
|
|
end
|
|
|
|
msg += " (#{params['format']})" if 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?
|
2012-01-20 22:11:46 +00:00
|
|
|
{ :message => msg }
|
2011-12-26 17:07:16 +00:00
|
|
|
end
|
2012-01-18 14:33:35 +00:00
|
|
|
|
|
|
|
def commit
|
2012-02-03 00:04:20 +00:00
|
|
|
commit_message.merge({:name => current_user.uname, :email => current_user.email})
|
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)
|
|
|
|
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
|
2012-02-13 18:28:43 +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
|
|
|
|
|
|
|
def authorize_read_actions
|
2012-02-13 18:28:43 +00:00
|
|
|
authorize! :read, @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
|
|
|
|