comparison app/controllers/wiki_controller.rb @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents 433d4f72a19b
children e248c7af89ec
comparison
equal deleted inserted replaced
1296:038ba2d95de8 1464:261b3d9a4903
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
12 # GNU General Public License for more details. 12 # GNU General Public License for more details.
13 # 13 #
14 # You should have received a copy of the GNU General Public License 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 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. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require 'diff'
19 17
20 # The WikiController follows the Rails REST controller pattern but with 18 # The WikiController follows the Rails REST controller pattern but with
21 # a few differences 19 # a few differences
22 # 20 #
23 # * index - shows a list of WikiPages grouped by page or date 21 # * index - shows a list of WikiPages grouped by page or date
35 default_search_scope :wiki_pages 33 default_search_scope :wiki_pages
36 before_filter :find_wiki, :authorize 34 before_filter :find_wiki, :authorize
37 before_filter :find_existing_or_new_page, :only => [:show, :edit, :update] 35 before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
38 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version] 36 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version]
39 accept_api_auth :index, :show, :update, :destroy 37 accept_api_auth :index, :show, :update, :destroy
38 before_filter :find_attachments, :only => [:preview]
40 39
41 helper :attachments 40 helper :attachments
42 include AttachmentsHelper 41 include AttachmentsHelper
43 helper :watchers 42 helper :watchers
44 include Redmine::Export::PDF 43 include Redmine::Export::PDF
61 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} 60 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
62 end 61 end
63 62
64 # display a page (in editing mode if it doesn't exist) 63 # display a page (in editing mode if it doesn't exist)
65 def show 64 def show
66 if @page.new_record? 65 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
66 deny_access
67 return
68 end
69 @content = @page.content_for_version(params[:version])
70 if @content.nil?
67 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request? 71 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
68 edit 72 edit
69 render :action => 'edit' 73 render :action => 'edit'
70 else 74 else
71 render_404 75 render_404
72 end 76 end
73 return 77 return
74 end 78 end
75 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
76 deny_access
77 return
78 end
79 @content = @page.content_for_version(params[:version])
80 if User.current.allowed_to?(:export_wiki_pages, @project) 79 if User.current.allowed_to?(:export_wiki_pages, @project)
81 if params[:format] == 'pdf' 80 if params[:format] == 'pdf'
82 send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf") 81 send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
83 return 82 return
84 elsif params[:format] == 'html' 83 elsif params[:format] == 'html'
103 102
104 # edit an existing page or a new one 103 # edit an existing page or a new one
105 def edit 104 def edit
106 return render_403 unless editable? 105 return render_403 unless editable?
107 if @page.new_record? 106 if @page.new_record?
108 @page.content = WikiContent.new(:page => @page)
109 if params[:parent].present? 107 if params[:parent].present?
110 @page.parent = @page.wiki.find_page(params[:parent].to_s) 108 @page.parent = @page.wiki.find_page(params[:parent].to_s)
111 end 109 end
112 end 110 end
113 111
114 @content = @page.content_for_version(params[:version]) 112 @content = @page.content_for_version(params[:version])
113 @content ||= WikiContent.new(:page => @page)
115 @content.text = initial_page_content(@page) if @content.text.blank? 114 @content.text = initial_page_content(@page) if @content.text.blank?
116 # don't keep previous comment 115 # don't keep previous comment
117 @content.comments = nil 116 @content.comments = nil
118 117
119 # To prevent StaleObjectError exception when reverting to a previous version 118 # To prevent StaleObjectError exception when reverting to a previous version
120 @content.version = @page.content.version 119 @content.version = @page.content.version if @page.content
121 120
122 @text = @content.text 121 @text = @content.text
123 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? 122 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
124 @section = params[:section].to_i 123 @section = params[:section].to_i
125 @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section) 124 @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
129 128
130 # Creates a new page or updates an existing one 129 # Creates a new page or updates an existing one
131 def update 130 def update
132 return render_403 unless editable? 131 return render_403 unless editable?
133 was_new_page = @page.new_record? 132 was_new_page = @page.new_record?
134 @page.content = WikiContent.new(:page => @page) if @page.new_record?
135 @page.safe_attributes = params[:wiki_page] 133 @page.safe_attributes = params[:wiki_page]
136 134
137 @content = @page.content 135 @content = @page.content || WikiContent.new(:page => @page)
138 content_params = params[:content] 136 content_params = params[:content]
139 if content_params.nil? && params[:wiki_page].is_a?(Hash) 137 if content_params.nil? && params[:wiki_page].is_a?(Hash)
140 content_params = params[:wiki_page].slice(:text, :comments, :version) 138 content_params = params[:wiki_page].slice(:text, :comments, :version)
141 end 139 end
142 content_params ||= {} 140 content_params ||= {}
144 @content.comments = content_params[:comments] 142 @content.comments = content_params[:comments]
145 @text = content_params[:text] 143 @text = content_params[:text]
146 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? 144 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
147 @section = params[:section].to_i 145 @section = params[:section].to_i
148 @section_hash = params[:section_hash] 146 @section_hash = params[:section_hash]
149 @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash) 147 @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(@section, @text, @section_hash)
150 else 148 else
151 @content.version = content_params[:version] if content_params[:version] 149 @content.version = content_params[:version] if content_params[:version]
152 @content.text = @text 150 @content.text = @text
153 end 151 end
154 @content.author = User.current 152 @content.author = User.current
155 153
156 if @page.save_with_content 154 if @page.save_with_content(@content)
157 attachments = Attachment.attach_files(@page, params[:attachments]) 155 attachments = Attachment.attach_files(@page, params[:attachments])
158 render_attachment_warning_if_needed(@page) 156 render_attachment_warning_if_needed(@page)
159 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) 157 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
160 158
161 respond_to do |format| 159 respond_to do |format|
162 format.html { redirect_to :action => 'show', :project_id => @project, :id => @page.title } 160 format.html {
161 anchor = @section ? "section-#{@section}" : nil
162 redirect_to project_wiki_page_path(@project, @page.title, :anchor => anchor)
163 }
163 format.api { 164 format.api {
164 if was_new_page 165 if was_new_page
165 render :action => 'show', :status => :created, :location => url_for(:controller => 'wiki', :action => 'show', :project_id => @project, :id => @page.title) 166 render :action => 'show', :status => :created, :location => project_wiki_page_path(@project, @page.title)
166 else 167 else
167 render_api_ok 168 render_api_ok
168 end 169 end
169 } 170 }
170 end 171 end
197 @page.redirect_existing_links = true 198 @page.redirect_existing_links = true
198 # used to display the *original* title if some AR validation errors occur 199 # used to display the *original* title if some AR validation errors occur
199 @original_title = @page.pretty_title 200 @original_title = @page.pretty_title
200 if request.post? && @page.update_attributes(params[:wiki_page]) 201 if request.post? && @page.update_attributes(params[:wiki_page])
201 flash[:notice] = l(:notice_successful_update) 202 flash[:notice] = l(:notice_successful_update)
202 redirect_to :action => 'show', :project_id => @project, :id => @page.title 203 redirect_to project_wiki_page_path(@project, @page.title)
203 end 204 end
204 end 205 end
205 206
206 def protect 207 def protect
207 @page.update_attribute :protected, params[:protected] 208 @page.update_attribute :protected, params[:protected]
208 redirect_to :action => 'show', :project_id => @project, :id => @page.title 209 redirect_to project_wiki_page_path(@project, @page.title)
209 end 210 end
210 211
211 # show page history 212 # show page history
212 def history 213 def history
213 @version_count = @page.content.versions.count 214 @version_count = @page.content.versions.count
214 @version_pages = Paginator.new self, @version_count, per_page_option, params['page'] 215 @version_pages = Paginator.new @version_count, per_page_option, params['page']
215 # don't load text 216 # don't load text
216 @versions = @page.content.versions.find :all, 217 @versions = @page.content.versions.
217 :select => "id, author_id, comments, updated_on, version", 218 select("id, author_id, comments, updated_on, version").
218 :order => 'version DESC', 219 reorder('version DESC').
219 :limit => @version_pages.items_per_page + 1, 220 limit(@version_pages.per_page + 1).
220 :offset => @version_pages.current.offset 221 offset(@version_pages.offset).
222 all
221 223
222 render :layout => false if request.xhr? 224 render :layout => false if request.xhr?
223 end 225 end
224 226
225 def diff 227 def diff
258 return unless api_request? 260 return unless api_request?
259 end 261 end
260 end 262 end
261 @page.destroy 263 @page.destroy
262 respond_to do |format| 264 respond_to do |format|
263 format.html { redirect_to :action => 'index', :project_id => @project } 265 format.html { redirect_to project_wiki_index_path(@project) }
264 format.api { render_api_ok } 266 format.api { render_api_ok }
265 end 267 end
266 end 268 end
267 269
268 def destroy_version 270 def destroy_version
269 return render_403 unless editable? 271 return render_403 unless editable?
270 272
271 @content = @page.content_for_version(params[:version]) 273 @content = @page.content_for_version(params[:version])
272 @content.destroy 274 @content.destroy
273 redirect_to_referer_or :action => 'history', :id => @page.title, :project_id => @project 275 redirect_to_referer_or history_project_wiki_page_path(@project, @page.title)
274 end 276 end
275 277
276 # Export wiki to a single pdf or html file 278 # Export wiki to a single pdf or html file
277 def export 279 def export
278 @pages = @wiki.pages.all(:order => 'title', :include => [:content, {:attachments => :author}]) 280 @pages = @wiki.pages.all(:order => 'title', :include => [:content, {:attachments => :author}])
290 def preview 292 def preview
291 page = @wiki.find_page(params[:id]) 293 page = @wiki.find_page(params[:id])
292 # page is nil when previewing a new page 294 # page is nil when previewing a new page
293 return render_403 unless page.nil? || editable?(page) 295 return render_403 unless page.nil? || editable?(page)
294 if page 296 if page
295 @attachements = page.attachments 297 @attachments += page.attachments
296 @previewed = page.content 298 @previewed = page.content
297 end 299 end
298 @text = params[:content][:text] 300 @text = params[:content][:text]
299 render :partial => 'common/preview' 301 render :partial => 'common/preview'
300 end 302 end
347 extend helper unless self.instance_of?(helper) 349 extend helper unless self.instance_of?(helper)
348 helper.instance_method(:initial_page_content).bind(self).call(page) 350 helper.instance_method(:initial_page_content).bind(self).call(page)
349 end 351 end
350 352
351 def load_pages_for_index 353 def load_pages_for_index
352 @pages = @wiki.pages.with_updated_on.order("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all 354 @pages = @wiki.pages.with_updated_on.reorder("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all
353 end 355 end
354 end 356 end