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

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