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