You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki_controller.rb 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'diff'
  18. class WikiController < ApplicationController
  19. layout 'base'
  20. before_filter :find_wiki, :authorize
  21. verify :method => :post, :only => [:destroy, :destroy_attachment], :redirect_to => { :action => :index }
  22. helper :attachments
  23. include AttachmentsHelper
  24. # display a page (in editing mode if it doesn't exist)
  25. def index
  26. page_title = params[:page]
  27. @page = @wiki.find_or_new_page(page_title)
  28. if @page.new_record?
  29. if User.current.allowed_to?(:edit_wiki_pages, @project)
  30. edit
  31. render :action => 'edit'
  32. else
  33. render_404
  34. end
  35. return
  36. end
  37. @content = @page.content_for_version(params[:version])
  38. if params[:export] == 'html'
  39. export = render_to_string :action => 'export', :layout => false
  40. send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
  41. return
  42. elsif params[:export] == 'txt'
  43. send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
  44. return
  45. end
  46. render :action => 'show'
  47. end
  48. # edit an existing page or a new one
  49. def edit
  50. @page = @wiki.find_or_new_page(params[:page])
  51. @page.content = WikiContent.new(:page => @page) if @page.new_record?
  52. @content = @page.content_for_version(params[:version])
  53. @content.text = "h1. #{@page.pretty_title}" if @content.text.blank?
  54. # don't keep previous comment
  55. @content.comments = nil
  56. if request.post?
  57. if !@page.new_record? && @content.text == params[:content][:text]
  58. # don't save if text wasn't changed
  59. redirect_to :action => 'index', :id => @project, :page => @page.title
  60. return
  61. end
  62. #@content.text = params[:content][:text]
  63. #@content.comments = params[:content][:comments]
  64. @content.attributes = params[:content]
  65. @content.author = User.current
  66. # if page is new @page.save will also save content, but not if page isn't a new record
  67. if (@page.new_record? ? @page.save : @content.save)
  68. redirect_to :action => 'index', :id => @project, :page => @page.title
  69. end
  70. end
  71. rescue ActiveRecord::StaleObjectError
  72. # Optimistic locking exception
  73. flash[:error] = l(:notice_locking_conflict)
  74. end
  75. # rename a page
  76. def rename
  77. @page = @wiki.find_page(params[:page])
  78. @page.redirect_existing_links = true
  79. # used to display the *original* title if some AR validation errors occur
  80. @original_title = @page.pretty_title
  81. if request.post? && @page.update_attributes(params[:wiki_page])
  82. flash[:notice] = l(:notice_successful_update)
  83. redirect_to :action => 'index', :id => @project, :page => @page.title
  84. end
  85. end
  86. # show page history
  87. def history
  88. @page = @wiki.find_page(params[:page])
  89. @version_count = @page.content.versions.count
  90. @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
  91. # don't load text
  92. @versions = @page.content.versions.find :all,
  93. :select => "id, author_id, comments, updated_on, version",
  94. :order => 'version DESC',
  95. :limit => @version_pages.items_per_page + 1,
  96. :offset => @version_pages.current.offset
  97. render :layout => false if request.xhr?
  98. end
  99. def diff
  100. @page = @wiki.find_page(params[:page])
  101. @diff = @page.diff(params[:version], params[:version_from])
  102. render_404 unless @diff
  103. end
  104. def annotate
  105. @page = @wiki.find_page(params[:page])
  106. @annotate = @page.annotate(params[:version])
  107. end
  108. # remove a wiki page and its history
  109. def destroy
  110. @page = @wiki.find_page(params[:page])
  111. @page.destroy if @page
  112. redirect_to :action => 'special', :id => @project, :page => 'Page_index'
  113. end
  114. # display special pages
  115. def special
  116. page_title = params[:page].downcase
  117. case page_title
  118. # show pages index, sorted by title
  119. when 'page_index', 'date_index'
  120. # eager load information about last updates, without loading text
  121. @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
  122. :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
  123. :order => 'title'
  124. @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
  125. # export wiki to a single html file
  126. when 'export'
  127. @pages = @wiki.pages.find :all, :order => 'title'
  128. export = render_to_string :action => 'export_multiple', :layout => false
  129. send_data(export, :type => 'text/html', :filename => "wiki.html")
  130. return
  131. else
  132. # requested special page doesn't exist, redirect to default page
  133. redirect_to :action => 'index', :id => @project, :page => nil and return
  134. end
  135. render :action => "special_#{page_title}"
  136. end
  137. def preview
  138. page = @wiki.find_page(params[:page])
  139. @attachements = page.attachments if page
  140. @text = params[:content][:text]
  141. render :partial => 'common/preview'
  142. end
  143. def add_attachment
  144. @page = @wiki.find_page(params[:page])
  145. attach_files(@page, params[:attachments])
  146. redirect_to :action => 'index', :page => @page.title
  147. end
  148. def destroy_attachment
  149. @page = @wiki.find_page(params[:page])
  150. @page.attachments.find(params[:attachment_id]).destroy
  151. redirect_to :action => 'index', :page => @page.title
  152. end
  153. private
  154. def find_wiki
  155. @project = Project.find(params[:id])
  156. @wiki = @project.wiki
  157. render_404 unless @wiki
  158. rescue ActiveRecord::RecordNotFound
  159. render_404
  160. end
  161. end