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

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