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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  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_action :find_wiki, :authorize
  35. before_action :find_existing_or_new_page, :only => [:show, :edit]
  36. before_action :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version]
  37. before_action :find_attachments, :only => [:preview]
  38. accept_api_auth :index, :show, :update, :destroy
  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 do
  48. @pages_by_parent_id = @pages.group_by(&:parent_id)
  49. end
  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. def new
  59. @page = WikiPage.new(:wiki => @wiki, :title => params[:title])
  60. unless User.current.allowed_to?(:edit_wiki_pages, @project)
  61. render_403
  62. return
  63. end
  64. if request.post?
  65. @page.title = '' unless editable?
  66. @page.validate
  67. if @page.errors[:title].blank?
  68. path = project_wiki_page_path(@project, @page.title, :parent => params[:parent])
  69. respond_to do |format|
  70. format.html {redirect_to path}
  71. format.js {render :js => "window.location = #{path.to_json}"}
  72. end
  73. end
  74. end
  75. end
  76. # display a page (in editing mode if it doesn't exist)
  77. def show
  78. if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
  79. deny_access
  80. return
  81. end
  82. @content = @page.content_for_version(params[:version])
  83. if @content.nil?
  84. if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
  85. edit
  86. render :action => 'edit'
  87. else
  88. render_404
  89. end
  90. return
  91. end
  92. call_hook :controller_wiki_show_before_render, content: @content, format: params[:format]
  93. if User.current.allowed_to?(:export_wiki_pages, @project)
  94. if params[:format] == 'pdf'
  95. send_file_headers! :type => 'application/pdf', :filename => filename_for_content_disposition("#{@page.title}.pdf")
  96. return
  97. elsif params[:format] == 'html'
  98. export = render_to_string :action => 'export', :layout => false
  99. send_data(export, :type => 'text/html', :filename => filename_for_content_disposition("#{@page.title}.html"))
  100. return
  101. elsif params[:format] == 'txt'
  102. send_data(@content.text, :type => 'text/plain', :filename => filename_for_content_disposition("#{@page.title}.txt"))
  103. return
  104. end
  105. end
  106. @editable = editable?
  107. @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) &&
  108. @content.current_version? &&
  109. Redmine::WikiFormatting.supports_section_edit?
  110. respond_to do |format|
  111. format.html
  112. format.api
  113. end
  114. end
  115. # edit an existing page or a new one
  116. def edit
  117. return render_403 unless editable?
  118. if @page.new_record?
  119. if params[:parent].present?
  120. @page.parent = @page.wiki.find_page(params[:parent].to_s)
  121. end
  122. end
  123. @content = @page.content_for_version(params[:version])
  124. @content ||= WikiContent.new(:page => @page)
  125. @content.text = initial_page_content(@page) if @content.text.blank?
  126. # don't keep previous comment
  127. @content.comments = nil
  128. # To prevent StaleObjectError exception when reverting to a previous version
  129. @content.version = @page.content.version if @page.content
  130. @text = @content.text
  131. if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
  132. @section = params[:section].to_i
  133. @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
  134. render_404 if @text.blank?
  135. end
  136. end
  137. # Creates a new page or updates an existing one
  138. def update
  139. @page = @wiki.find_or_new_page(params[:id])
  140. return render_403 unless editable?
  141. was_new_page = @page.new_record?
  142. @page.safe_attributes = params[:wiki_page]
  143. @content = @page.content || WikiContent.new(:page => @page)
  144. content_params = params[:content]
  145. if content_params.nil? && params[:wiki_page].present?
  146. content_params = params[:wiki_page].slice(:text, :comments, :version)
  147. end
  148. content_params ||= {}
  149. @content.comments = content_params[:comments]
  150. @text = content_params[:text]
  151. if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
  152. @section = params[:section].to_i
  153. @section_hash = params[:section_hash]
  154. @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(@section, @text, @section_hash)
  155. else
  156. @content.version = content_params[:version] if content_params[:version]
  157. @content.text = @text
  158. end
  159. @content.author = User.current
  160. if @page.save_with_content(@content)
  161. attachments = Attachment.attach_files(@page, params[:attachments] || (params[:wiki_page] && params[:wiki_page][:uploads]))
  162. render_attachment_warning_if_needed(@page)
  163. call_hook(:controller_wiki_edit_after_save, {:params => params, :page => @page})
  164. respond_to do |format|
  165. format.html do
  166. anchor = @section ? "section-#{@section}" : nil
  167. redirect_to project_wiki_page_path(@project, @page.title, :anchor => anchor)
  168. end
  169. format.api do
  170. if was_new_page
  171. render :action => 'show', :status => :created, :location => project_wiki_page_path(@project, @page.title)
  172. else
  173. render_api_ok
  174. end
  175. end
  176. end
  177. else
  178. respond_to do |format|
  179. format.html {render :action => 'edit'}
  180. format.api {render_validation_errors(@content)}
  181. end
  182. end
  183. rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
  184. # Optimistic locking exception
  185. respond_to do |format|
  186. format.html do
  187. flash.now[:error] = l(:notice_locking_conflict)
  188. render :action => 'edit'
  189. end
  190. format.api {render_api_head :conflict}
  191. end
  192. end
  193. # rename a page
  194. def rename
  195. return render_403 unless editable?
  196. @page.redirect_existing_links = true
  197. # used to display the *original* title if some AR validation errors occur
  198. @original_title = @page.pretty_title
  199. @page.safe_attributes = params[:wiki_page]
  200. if request.post? && @page.save
  201. flash[:notice] = l(:notice_successful_update)
  202. redirect_to project_wiki_page_path(@page.project, @page.title)
  203. end
  204. end
  205. def protect
  206. @page.update_attribute :protected, params[:protected]
  207. redirect_to project_wiki_page_path(@project, @page.title)
  208. end
  209. # show page history
  210. def history
  211. @version_count = @page.content.versions.count
  212. @version_pages = Paginator.new @version_count, per_page_option, params['page']
  213. # don't load text
  214. @versions = @page.content.versions.
  215. select("id, author_id, comments, updated_on, version").
  216. reorder('version DESC').
  217. limit(@version_pages.per_page + 1).
  218. offset(@version_pages.offset).
  219. to_a
  220. render :layout => false if request.xhr?
  221. end
  222. def diff
  223. @diff = @page.diff(params[:version], params[:version_from])
  224. render_404 unless @diff
  225. end
  226. def annotate
  227. @annotate = @page.annotate(params[:version])
  228. render_404 unless @annotate
  229. end
  230. # Removes a wiki page and its history
  231. # Children can be either set as root pages, removed or reassigned to another parent page
  232. def destroy
  233. return render_403 unless editable?
  234. @descendants_count = @page.descendants.size
  235. if @descendants_count > 0
  236. case params[:todo]
  237. when 'nullify'
  238. # Nothing to do
  239. when 'destroy'
  240. # Removes all its descendants
  241. @page.descendants.each(&:destroy)
  242. when 'reassign'
  243. # Reassign children to another parent page
  244. reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
  245. return unless reassign_to
  246. @page.children.each do |child|
  247. child.update_attribute(:parent, reassign_to)
  248. end
  249. else
  250. @reassignable_to = @wiki.pages - @page.self_and_descendants
  251. # display the destroy form if it's a user request
  252. return unless api_request?
  253. end
  254. end
  255. @page.destroy
  256. respond_to do |format|
  257. format.html do
  258. flash[:notice] = l(:notice_successful_delete)
  259. redirect_to project_wiki_index_path(@project)
  260. end
  261. format.api {render_api_ok}
  262. end
  263. end
  264. def destroy_version
  265. return render_403 unless editable?
  266. if content = @page.content.versions.find_by_version(params[:version])
  267. content.destroy
  268. redirect_to_referer_or history_project_wiki_page_path(@project, @page.title)
  269. else
  270. render_404
  271. end
  272. end
  273. # Export wiki to a single pdf or html file
  274. def export
  275. @pages = @wiki.pages.
  276. includes([:content, {:attachments => :author}]).
  277. to_a
  278. respond_to do |format|
  279. format.html do
  280. export = render_to_string :action => 'export_multiple', :layout => false
  281. send_data(export, :type => 'text/html', :filename => "wiki.html")
  282. end
  283. format.pdf do
  284. send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}.pdf"
  285. end
  286. end
  287. end
  288. def preview
  289. page = @wiki.find_page(params[:id])
  290. # page is nil when previewing a new page
  291. return render_403 unless page.nil? || editable?(page)
  292. if page
  293. @attachments += page.attachments
  294. @previewed = page.content
  295. end
  296. @text = params[:content].present? ? params[:content][:text] : params[:text]
  297. render :partial => 'common/preview'
  298. end
  299. def add_attachment
  300. return render_403 unless editable?
  301. attachments = Attachment.attach_files(@page, params[:attachments])
  302. render_attachment_warning_if_needed(@page)
  303. redirect_to :action => 'show', :id => @page.title, :project_id => @project
  304. end
  305. private
  306. def find_wiki
  307. @project = Project.find(params[:project_id])
  308. @wiki = @project.wiki
  309. render_404 unless @wiki
  310. rescue ActiveRecord::RecordNotFound
  311. render_404
  312. end
  313. # Finds the requested page or a new page if it doesn't exist
  314. def find_existing_or_new_page
  315. @page = @wiki.find_or_new_page(params[:id])
  316. if @wiki.page_found_with_redirect?
  317. redirect_to_page @page
  318. end
  319. end
  320. # Finds the requested page and returns a 404 error if it doesn't exist
  321. def find_existing_page
  322. @page = @wiki.find_page(params[:id])
  323. if @page.nil?
  324. render_404
  325. return
  326. end
  327. if @wiki.page_found_with_redirect?
  328. redirect_to_page @page
  329. end
  330. end
  331. def redirect_to_page(page)
  332. if page.project && page.project.visible?
  333. redirect_to :action => action_name, :project_id => page.project, :id => page.title
  334. else
  335. render_404
  336. end
  337. end
  338. # Returns true if the current user is allowed to edit the page, otherwise false
  339. def editable?(page = @page)
  340. page.editable_by?(User.current)
  341. end
  342. # Returns the default content of a new wiki page
  343. def initial_page_content(page)
  344. helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
  345. extend helper unless self.instance_of?(helper)
  346. helper.instance_method(:initial_page_content).bind(self).call(page)
  347. end
  348. def load_pages_for_index
  349. @pages = @wiki.pages.with_updated_on.
  350. includes(:wiki => :project).
  351. includes(:parent).
  352. to_a
  353. end
  354. end