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

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

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