##// END OF EJS Templates
Refactor: extract finder to a utility method...
Eric Davis -
r4138:bbbfd4ee4cf3
parent child
Show More
@@ -1,252 +1,257
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 require 'diff'
18 require 'diff'
19
19
20 class WikiController < ApplicationController
20 class WikiController < ApplicationController
21 default_search_scope :wiki_pages
21 default_search_scope :wiki_pages
22 before_filter :find_wiki, :authorize
22 before_filter :find_wiki, :authorize
23 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
23 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
24
24
25 verify :method => :post, :only => [:destroy, :protect], :redirect_to => { :action => :index }
25 verify :method => :post, :only => [:destroy, :protect], :redirect_to => { :action => :index }
26
26
27 helper :attachments
27 helper :attachments
28 include AttachmentsHelper
28 include AttachmentsHelper
29 helper :watchers
29 helper :watchers
30
30
31 # display a page (in editing mode if it doesn't exist)
31 # display a page (in editing mode if it doesn't exist)
32 def index
32 def index
33 page_title = params[:page]
33 page_title = params[:page]
34 @page = @wiki.find_or_new_page(page_title)
34 @page = @wiki.find_or_new_page(page_title)
35 if @page.new_record?
35 if @page.new_record?
36 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
36 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
37 edit
37 edit
38 render :action => 'edit'
38 render :action => 'edit'
39 else
39 else
40 render_404
40 render_404
41 end
41 end
42 return
42 return
43 end
43 end
44 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
44 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
45 # Redirects user to the current version if he's not allowed to view previous versions
45 # Redirects user to the current version if he's not allowed to view previous versions
46 redirect_to :version => nil
46 redirect_to :version => nil
47 return
47 return
48 end
48 end
49 @content = @page.content_for_version(params[:version])
49 @content = @page.content_for_version(params[:version])
50 if User.current.allowed_to?(:export_wiki_pages, @project)
50 if User.current.allowed_to?(:export_wiki_pages, @project)
51 if params[:format] == 'html'
51 if params[:format] == 'html'
52 export = render_to_string :action => 'export', :layout => false
52 export = render_to_string :action => 'export', :layout => false
53 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
53 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
54 return
54 return
55 elsif params[:format] == 'txt'
55 elsif params[:format] == 'txt'
56 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
56 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
57 return
57 return
58 end
58 end
59 end
59 end
60 @editable = editable?
60 @editable = editable?
61 render :action => 'show'
61 render :action => 'show'
62 end
62 end
63
63
64 # edit an existing page or a new one
64 # edit an existing page or a new one
65 def edit
65 def edit
66 @page = @wiki.find_or_new_page(params[:page])
66 @page = @wiki.find_or_new_page(params[:page])
67 return render_403 unless editable?
67 return render_403 unless editable?
68 @page.content = WikiContent.new(:page => @page) if @page.new_record?
68 @page.content = WikiContent.new(:page => @page) if @page.new_record?
69
69
70 @content = @page.content_for_version(params[:version])
70 @content = @page.content_for_version(params[:version])
71 @content.text = initial_page_content(@page) if @content.text.blank?
71 @content.text = initial_page_content(@page) if @content.text.blank?
72 # don't keep previous comment
72 # don't keep previous comment
73 @content.comments = nil
73 @content.comments = nil
74 if request.get?
74 if request.get?
75 # To prevent StaleObjectError exception when reverting to a previous version
75 # To prevent StaleObjectError exception when reverting to a previous version
76 @content.version = @page.content.version
76 @content.version = @page.content.version
77 else
77 else
78 if !@page.new_record? && @content.text == params[:content][:text]
78 if !@page.new_record? && @content.text == params[:content][:text]
79 attachments = Attachment.attach_files(@page, params[:attachments])
79 attachments = Attachment.attach_files(@page, params[:attachments])
80 render_attachment_warning_if_needed(@page)
80 render_attachment_warning_if_needed(@page)
81 # don't save if text wasn't changed
81 # don't save if text wasn't changed
82 redirect_to :action => 'index', :id => @project, :page => @page.title
82 redirect_to :action => 'index', :id => @project, :page => @page.title
83 return
83 return
84 end
84 end
85 #@content.text = params[:content][:text]
85 #@content.text = params[:content][:text]
86 #@content.comments = params[:content][:comments]
86 #@content.comments = params[:content][:comments]
87 @content.attributes = params[:content]
87 @content.attributes = params[:content]
88 @content.author = User.current
88 @content.author = User.current
89 # if page is new @page.save will also save content, but not if page isn't a new record
89 # if page is new @page.save will also save content, but not if page isn't a new record
90 if (@page.new_record? ? @page.save : @content.save)
90 if (@page.new_record? ? @page.save : @content.save)
91 attachments = Attachment.attach_files(@page, params[:attachments])
91 attachments = Attachment.attach_files(@page, params[:attachments])
92 render_attachment_warning_if_needed(@page)
92 render_attachment_warning_if_needed(@page)
93 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
93 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
94 redirect_to :action => 'index', :id => @project, :page => @page.title
94 redirect_to :action => 'index', :id => @project, :page => @page.title
95 end
95 end
96 end
96 end
97 rescue ActiveRecord::StaleObjectError
97 rescue ActiveRecord::StaleObjectError
98 # Optimistic locking exception
98 # Optimistic locking exception
99 flash[:error] = l(:notice_locking_conflict)
99 flash[:error] = l(:notice_locking_conflict)
100 end
100 end
101
101
102 # rename a page
102 # rename a page
103 def rename
103 def rename
104 return render_403 unless editable?
104 return render_403 unless editable?
105 @page.redirect_existing_links = true
105 @page.redirect_existing_links = true
106 # used to display the *original* title if some AR validation errors occur
106 # used to display the *original* title if some AR validation errors occur
107 @original_title = @page.pretty_title
107 @original_title = @page.pretty_title
108 if request.post? && @page.update_attributes(params[:wiki_page])
108 if request.post? && @page.update_attributes(params[:wiki_page])
109 flash[:notice] = l(:notice_successful_update)
109 flash[:notice] = l(:notice_successful_update)
110 redirect_to :action => 'index', :id => @project, :page => @page.title
110 redirect_to :action => 'index', :id => @project, :page => @page.title
111 end
111 end
112 end
112 end
113
113
114 def protect
114 def protect
115 @page.update_attribute :protected, params[:protected]
115 @page.update_attribute :protected, params[:protected]
116 redirect_to :action => 'index', :id => @project, :page => @page.title
116 redirect_to :action => 'index', :id => @project, :page => @page.title
117 end
117 end
118
118
119 # show page history
119 # show page history
120 def history
120 def history
121 @version_count = @page.content.versions.count
121 @version_count = @page.content.versions.count
122 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
122 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
123 # don't load text
123 # don't load text
124 @versions = @page.content.versions.find :all,
124 @versions = @page.content.versions.find :all,
125 :select => "id, author_id, comments, updated_on, version",
125 :select => "id, author_id, comments, updated_on, version",
126 :order => 'version DESC',
126 :order => 'version DESC',
127 :limit => @version_pages.items_per_page + 1,
127 :limit => @version_pages.items_per_page + 1,
128 :offset => @version_pages.current.offset
128 :offset => @version_pages.current.offset
129
129
130 render :layout => false if request.xhr?
130 render :layout => false if request.xhr?
131 end
131 end
132
132
133 def diff
133 def diff
134 @diff = @page.diff(params[:version], params[:version_from])
134 @diff = @page.diff(params[:version], params[:version_from])
135 render_404 unless @diff
135 render_404 unless @diff
136 end
136 end
137
137
138 def annotate
138 def annotate
139 @annotate = @page.annotate(params[:version])
139 @annotate = @page.annotate(params[:version])
140 render_404 unless @annotate
140 render_404 unless @annotate
141 end
141 end
142
142
143 # Removes a wiki page and its history
143 # Removes a wiki page and its history
144 # Children can be either set as root pages, removed or reassigned to another parent page
144 # Children can be either set as root pages, removed or reassigned to another parent page
145 def destroy
145 def destroy
146 return render_403 unless editable?
146 return render_403 unless editable?
147
147
148 @descendants_count = @page.descendants.size
148 @descendants_count = @page.descendants.size
149 if @descendants_count > 0
149 if @descendants_count > 0
150 case params[:todo]
150 case params[:todo]
151 when 'nullify'
151 when 'nullify'
152 # Nothing to do
152 # Nothing to do
153 when 'destroy'
153 when 'destroy'
154 # Removes all its descendants
154 # Removes all its descendants
155 @page.descendants.each(&:destroy)
155 @page.descendants.each(&:destroy)
156 when 'reassign'
156 when 'reassign'
157 # Reassign children to another parent page
157 # Reassign children to another parent page
158 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
158 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
159 return unless reassign_to
159 return unless reassign_to
160 @page.children.each do |child|
160 @page.children.each do |child|
161 child.update_attribute(:parent, reassign_to)
161 child.update_attribute(:parent, reassign_to)
162 end
162 end
163 else
163 else
164 @reassignable_to = @wiki.pages - @page.self_and_descendants
164 @reassignable_to = @wiki.pages - @page.self_and_descendants
165 return
165 return
166 end
166 end
167 end
167 end
168 @page.destroy
168 @page.destroy
169 redirect_to :action => 'special', :id => @project, :page => 'Page_index'
169 redirect_to :action => 'special', :id => @project, :page => 'Page_index'
170 end
170 end
171
171
172 # display special pages
172 # display special pages
173 def special
173 def special
174 page_title = params[:page].downcase
174 page_title = params[:page].downcase
175 case page_title
175 case page_title
176 # show pages index, sorted by title
176 # show pages index, sorted by title
177 when 'page_index', 'date_index'
177 when 'page_index', 'date_index'
178 # eager load information about last updates, without loading text
178 load_pages_grouped_by_date_without_content
179 @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
180 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
181 :order => 'title'
182 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
183 @pages_by_parent_id = @pages.group_by(&:parent_id)
184 when 'export'
179 when 'export'
185 redirect_to :action => 'export', :id => @project # Compatibility stub while refactoring
180 redirect_to :action => 'export', :id => @project # Compatibility stub while refactoring
186 return
181 return
187 else
182 else
188 # requested special page doesn't exist, redirect to default page
183 # requested special page doesn't exist, redirect to default page
189 redirect_to :action => 'index', :id => @project, :page => nil
184 redirect_to :action => 'index', :id => @project, :page => nil
190 return
185 return
191 end
186 end
192 render :action => "special_#{page_title}"
187 render :action => "special_#{page_title}"
193 end
188 end
194
189
195 # Export wiki to a single html file
190 # Export wiki to a single html file
196 def export
191 def export
197 if User.current.allowed_to?(:export_wiki_pages, @project)
192 if User.current.allowed_to?(:export_wiki_pages, @project)
198 @pages = @wiki.pages.find :all, :order => 'title'
193 @pages = @wiki.pages.find :all, :order => 'title'
199 export = render_to_string :action => 'export_multiple', :layout => false
194 export = render_to_string :action => 'export_multiple', :layout => false
200 send_data(export, :type => 'text/html', :filename => "wiki.html")
195 send_data(export, :type => 'text/html', :filename => "wiki.html")
201 else
196 else
202 redirect_to :action => 'index', :id => @project, :page => nil
197 redirect_to :action => 'index', :id => @project, :page => nil
203 end
198 end
204 end
199 end
205
200
206 def preview
201 def preview
207 page = @wiki.find_page(params[:page])
202 page = @wiki.find_page(params[:page])
208 # page is nil when previewing a new page
203 # page is nil when previewing a new page
209 return render_403 unless page.nil? || editable?(page)
204 return render_403 unless page.nil? || editable?(page)
210 if page
205 if page
211 @attachements = page.attachments
206 @attachements = page.attachments
212 @previewed = page.content
207 @previewed = page.content
213 end
208 end
214 @text = params[:content][:text]
209 @text = params[:content][:text]
215 render :partial => 'common/preview'
210 render :partial => 'common/preview'
216 end
211 end
217
212
218 def add_attachment
213 def add_attachment
219 return render_403 unless editable?
214 return render_403 unless editable?
220 attachments = Attachment.attach_files(@page, params[:attachments])
215 attachments = Attachment.attach_files(@page, params[:attachments])
221 render_attachment_warning_if_needed(@page)
216 render_attachment_warning_if_needed(@page)
222 redirect_to :action => 'index', :page => @page.title
217 redirect_to :action => 'index', :page => @page.title
223 end
218 end
224
219
225 private
220 private
226
221
227 def find_wiki
222 def find_wiki
228 @project = Project.find(params[:id])
223 @project = Project.find(params[:id])
229 @wiki = @project.wiki
224 @wiki = @project.wiki
230 render_404 unless @wiki
225 render_404 unless @wiki
231 rescue ActiveRecord::RecordNotFound
226 rescue ActiveRecord::RecordNotFound
232 render_404
227 render_404
233 end
228 end
234
229
235 # Finds the requested page and returns a 404 error if it doesn't exist
230 # Finds the requested page and returns a 404 error if it doesn't exist
236 def find_existing_page
231 def find_existing_page
237 @page = @wiki.find_page(params[:page])
232 @page = @wiki.find_page(params[:page])
238 render_404 if @page.nil?
233 render_404 if @page.nil?
239 end
234 end
240
235
241 # Returns true if the current user is allowed to edit the page, otherwise false
236 # Returns true if the current user is allowed to edit the page, otherwise false
242 def editable?(page = @page)
237 def editable?(page = @page)
243 page.editable_by?(User.current)
238 page.editable_by?(User.current)
244 end
239 end
245
240
246 # Returns the default content of a new wiki page
241 # Returns the default content of a new wiki page
247 def initial_page_content(page)
242 def initial_page_content(page)
248 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
243 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
249 extend helper unless self.instance_of?(helper)
244 extend helper unless self.instance_of?(helper)
250 helper.instance_method(:initial_page_content).bind(self).call(page)
245 helper.instance_method(:initial_page_content).bind(self).call(page)
251 end
246 end
247
248 # eager load information about last updates, without loading text
249 def load_pages_grouped_by_date_without_content
250 @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
251 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
252 :order => 'title'
253 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
254 @pages_by_parent_id = @pages.group_by(&:parent_id)
255 end
256
252 end
257 end
General Comments 0
You need to be logged in to leave comments. Login now