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