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