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

wiki_controller.rb 12 kB

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

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...