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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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. # The WikiController follows the Rails REST controller pattern but with
  19. # a few differences
  20. #
  21. # * index - shows a list of WikiPages grouped by page or date
  22. # * new - not used
  23. # * create - not used
  24. # * show - will also show the form for creating a new wiki page
  25. # * edit - used to edit an existing or new page
  26. # * update - used to save a wiki page update to the database, including new pages
  27. # * destroy - normal
  28. #
  29. # Other member and collection methods are also used
  30. #
  31. # TODO: still being worked on
  32. class WikiController < ApplicationController
  33. default_search_scope :wiki_pages
  34. before_filter :find_wiki, :authorize
  35. before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
  36. before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
  37. helper :attachments
  38. include AttachmentsHelper
  39. helper :watchers
  40. include Redmine::Export::PDF
  41. # List of pages, sorted alphabetically and by parent (hierarchy)
  42. def index
  43. load_pages_for_index
  44. @pages_by_parent_id = @pages.group_by(&:parent_id)
  45. end
  46. # List of page, by last update
  47. def date_index
  48. load_pages_for_index
  49. @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
  50. end
  51. # display a page (in editing mode if it doesn't exist)
  52. def show
  53. if @page.new_record?
  54. if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
  55. edit
  56. render :action => 'edit'
  57. else
  58. render_404
  59. end
  60. return
  61. end
  62. if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
  63. # Redirects user to the current version if he's not allowed to view previous versions
  64. redirect_to :version => nil
  65. return
  66. end
  67. @content = @page.content_for_version(params[:version])
  68. if User.current.allowed_to?(:export_wiki_pages, @project)
  69. if params[:format] == 'pdf'
  70. send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
  71. return
  72. elsif params[:format] == 'html'
  73. export = render_to_string :action => 'export', :layout => false
  74. send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
  75. return
  76. elsif params[:format] == 'txt'
  77. send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
  78. return
  79. end
  80. end
  81. @editable = editable?
  82. @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) &&
  83. @content.current_version? &&
  84. Redmine::WikiFormatting.supports_section_edit?
  85. render :action => 'show'
  86. end
  87. # edit an existing page or a new one
  88. def edit
  89. return render_403 unless editable?
  90. if @page.new_record?
  91. @page.content = WikiContent.new(:page => @page)
  92. if params[:parent].present?
  93. @page.parent = @page.wiki.find_page(params[:parent].to_s)
  94. end
  95. end
  96. @content = @page.content_for_version(params[:version])
  97. @content.text = initial_page_content(@page) if @content.text.blank?
  98. # don't keep previous comment
  99. @content.comments = nil
  100. # To prevent StaleObjectError exception when reverting to a previous version
  101. @content.version = @page.content.version
  102. @text = @content.text
  103. if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
  104. @section = params[:section].to_i
  105. @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
  106. render_404 if @text.blank?
  107. end
  108. end
  109. # Creates a new page or updates an existing one
  110. def update
  111. return render_403 unless editable?
  112. @page.content = WikiContent.new(:page => @page) if @page.new_record?
  113. @page.safe_attributes = params[:wiki_page]
  114. @content = @page.content_for_version(params[:version])
  115. @content.text = initial_page_content(@page) if @content.text.blank?
  116. # don't keep previous comment
  117. @content.comments = nil
  118. if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
  119. attachments = Attachment.attach_files(@page, params[:attachments])
  120. render_attachment_warning_if_needed(@page)
  121. # don't save content if text wasn't changed
  122. @page.save
  123. redirect_to :action => 'show', :project_id => @project, :id => @page.title
  124. return
  125. end
  126. @content.comments = params[:content][:comments]
  127. @text = params[:content][:text]
  128. if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
  129. @section = params[:section].to_i
  130. @section_hash = params[:section_hash]
  131. @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
  132. else
  133. @content.version = params[:content][:version]
  134. @content.text = @text
  135. end
  136. @content.author = User.current
  137. @page.content = @content
  138. if @page.save
  139. attachments = Attachment.attach_files(@page, params[:attachments])
  140. render_attachment_warning_if_needed(@page)
  141. call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
  142. redirect_to :action => 'show', :project_id => @project, :id => @page.title
  143. else
  144. render :action => 'edit'
  145. end
  146. rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
  147. # Optimistic locking exception
  148. flash.now[:error] = l(:notice_locking_conflict)
  149. render :action => 'edit'
  150. rescue ActiveRecord::RecordNotSaved
  151. render :action => 'edit'
  152. end
  153. # rename a page
  154. def rename
  155. return render_403 unless editable?
  156. @page.redirect_existing_links = true
  157. # used to display the *original* title if some AR validation errors occur
  158. @original_title = @page.pretty_title
  159. if request.post? && @page.update_attributes(params[:wiki_page])
  160. flash[:notice] = l(:notice_successful_update)
  161. redirect_to :action => 'show', :project_id => @project, :id => @page.title
  162. end
  163. end
  164. def protect
  165. @page.update_attribute :protected, params[:protected]
  166. redirect_to :action => 'show', :project_id => @project, :id => @page.title
  167. end
  168. # show page history
  169. def history
  170. @version_count = @page.content.versions.count
  171. @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
  172. # don't load text
  173. @versions = @page.content.versions.find :all,
  174. :select => "id, author_id, comments, updated_on, version",
  175. :order => 'version DESC',
  176. :limit => @version_pages.items_per_page + 1,
  177. :offset => @version_pages.current.offset
  178. render :layout => false if request.xhr?
  179. end
  180. def diff
  181. @diff = @page.diff(params[:version], params[:version_from])
  182. render_404 unless @diff
  183. end
  184. def annotate
  185. @annotate = @page.annotate(params[:version])
  186. render_404 unless @annotate
  187. end
  188. # Removes a wiki page and its history
  189. # Children can be either set as root pages, removed or reassigned to another parent page
  190. def destroy
  191. return render_403 unless editable?
  192. @descendants_count = @page.descendants.size
  193. if @descendants_count > 0
  194. case params[:todo]
  195. when 'nullify'
  196. # Nothing to do
  197. when 'destroy'
  198. # Removes all its descendants
  199. @page.descendants.each(&:destroy)
  200. when 'reassign'
  201. # Reassign children to another parent page
  202. reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
  203. return unless reassign_to
  204. @page.children.each do |child|
  205. child.update_attribute(:parent, reassign_to)
  206. end
  207. else
  208. @reassignable_to = @wiki.pages - @page.self_and_descendants
  209. return
  210. end
  211. end
  212. @page.destroy
  213. redirect_to :action => 'index', :project_id => @project
  214. end
  215. # Export wiki to a single pdf or html file
  216. def export
  217. @pages = @wiki.pages.all(:order => 'title', :include => [:content, :attachments], :limit => 75)
  218. respond_to do |format|
  219. format.html {
  220. export = render_to_string :action => 'export_multiple', :layout => false
  221. send_data(export, :type => 'text/html', :filename => "wiki.html")
  222. }
  223. format.pdf {
  224. send_data(wiki_pages_to_pdf(@pages, @project), :type => 'application/pdf', :filename => "#{@project.identifier}.pdf")
  225. }
  226. end
  227. end
  228. def preview
  229. page = @wiki.find_page(params[:id])
  230. # page is nil when previewing a new page
  231. return render_403 unless page.nil? || editable?(page)
  232. if page
  233. @attachements = page.attachments
  234. @previewed = page.content
  235. end
  236. @text = params[:content][:text]
  237. render :partial => 'common/preview'
  238. end
  239. def add_attachment
  240. return render_403 unless editable?
  241. attachments = Attachment.attach_files(@page, params[:attachments])
  242. render_attachment_warning_if_needed(@page)
  243. redirect_to :action => 'show', :id => @page.title, :project_id => @project
  244. end
  245. private
  246. def find_wiki
  247. @project = Project.find(params[:project_id])
  248. @wiki = @project.wiki
  249. render_404 unless @wiki
  250. rescue ActiveRecord::RecordNotFound
  251. render_404
  252. end
  253. # Finds the requested page or a new page if it doesn't exist
  254. def find_existing_or_new_page
  255. @page = @wiki.find_or_new_page(params[:id])
  256. if @wiki.page_found_with_redirect?
  257. redirect_to params.update(:id => @page.title)
  258. end
  259. end
  260. # Finds the requested page and returns a 404 error if it doesn't exist
  261. def find_existing_page
  262. @page = @wiki.find_page(params[:id])
  263. if @page.nil?
  264. render_404
  265. return
  266. end
  267. if @wiki.page_found_with_redirect?
  268. redirect_to params.update(:id => @page.title)
  269. end
  270. end
  271. # Returns true if the current user is allowed to edit the page, otherwise false
  272. def editable?(page = @page)
  273. page.editable_by?(User.current)
  274. end
  275. # Returns the default content of a new wiki page
  276. def initial_page_content(page)
  277. helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
  278. extend helper unless self.instance_of?(helper)
  279. helper.instance_method(:initial_page_content).bind(self).call(page)
  280. end
  281. def load_pages_for_index
  282. @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project})
  283. end
  284. end