comparison app/controllers/wiki_controller.rb @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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'
32 # 32 #
33 # TODO: still being worked on 33 # TODO: still being worked on
34 class WikiController < ApplicationController 34 class WikiController < ApplicationController
35 default_search_scope :wiki_pages 35 default_search_scope :wiki_pages
36 before_filter :find_wiki, :authorize 36 before_filter :find_wiki, :authorize
37 before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
37 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy] 38 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
38
39 verify :method => :post, :only => [:protect], :redirect_to => { :action => :show }
40 39
41 helper :attachments 40 helper :attachments
42 include AttachmentsHelper 41 include AttachmentsHelper
43 helper :watchers 42 helper :watchers
44 43
45 # List of pages, sorted alphabetically and by parent (hierarchy) 44 # List of pages, sorted alphabetically and by parent (hierarchy)
46 def index 45 def index
47 load_pages_grouped_by_date_without_content 46 load_pages_for_index
47 @pages_by_parent_id = @pages.group_by(&:parent_id)
48 end
49
50 # List of page, by last update
51 def date_index
52 load_pages_for_index
53 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
48 end 54 end
49 55
50 # display a page (in editing mode if it doesn't exist) 56 # display a page (in editing mode if it doesn't exist)
51 def show 57 def show
52 page_title = params[:id]
53 @page = @wiki.find_or_new_page(page_title)
54 if @page.new_record? 58 if @page.new_record?
55 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? 59 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
56 edit 60 edit
57 render :action => 'edit' 61 render :action => 'edit'
58 else 62 else
77 end 81 end
78 end 82 end
79 @editable = editable? 83 @editable = editable?
80 render :action => 'show' 84 render :action => 'show'
81 end 85 end
82 86
83 # edit an existing page or a new one 87 # edit an existing page or a new one
84 def edit 88 def edit
85 @page = @wiki.find_or_new_page(params[:id])
86 return render_403 unless editable? 89 return render_403 unless editable?
87 @page.content = WikiContent.new(:page => @page) if @page.new_record? 90 @page.content = WikiContent.new(:page => @page) if @page.new_record?
88 91
89 @content = @page.content_for_version(params[:version]) 92 @content = @page.content_for_version(params[:version])
90 @content.text = initial_page_content(@page) if @content.text.blank? 93 @content.text = initial_page_content(@page) if @content.text.blank?
91 # don't keep previous comment 94 # don't keep previous comment
92 @content.comments = nil 95 @content.comments = nil
93 96
94 # To prevent StaleObjectError exception when reverting to a previous version 97 # To prevent StaleObjectError exception when reverting to a previous version
95 @content.version = @page.content.version 98 @content.version = @page.content.version
96 rescue ActiveRecord::StaleObjectError
97 # Optimistic locking exception
98 flash[:error] = l(:notice_locking_conflict)
99 end 99 end
100 100
101 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } 101 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
102 # Creates a new page or updates an existing one 102 # Creates a new page or updates an existing one
103 def update 103 def update
104 @page = @wiki.find_or_new_page(params[:id])
105 return render_403 unless editable? 104 return render_403 unless editable?
106 @page.content = WikiContent.new(:page => @page) if @page.new_record? 105 @page.content = WikiContent.new(:page => @page) if @page.new_record?
107 106
108 @content = @page.content_for_version(params[:version]) 107 @content = @page.content_for_version(params[:version])
109 @content.text = initial_page_content(@page) if @content.text.blank? 108 @content.text = initial_page_content(@page) if @content.text.blank?
110 # don't keep previous comment 109 # don't keep previous comment
111 @content.comments = nil 110 @content.comments = nil
112 111
123 if (@page.new_record? ? @page.save : @content.save) 122 if (@page.new_record? ? @page.save : @content.save)
124 attachments = Attachment.attach_files(@page, params[:attachments]) 123 attachments = Attachment.attach_files(@page, params[:attachments])
125 render_attachment_warning_if_needed(@page) 124 render_attachment_warning_if_needed(@page)
126 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) 125 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
127 redirect_to :action => 'show', :project_id => @project, :id => @page.title 126 redirect_to :action => 'show', :project_id => @project, :id => @page.title
127 else
128 render :action => 'edit'
128 end 129 end
129 130
130 rescue ActiveRecord::StaleObjectError 131 rescue ActiveRecord::StaleObjectError
131 # Optimistic locking exception 132 # Optimistic locking exception
132 flash[:error] = l(:notice_locking_conflict) 133 flash.now[:error] = l(:notice_locking_conflict)
134 render :action => 'edit'
133 end 135 end
134 136
135 # rename a page 137 # rename a page
136 def rename 138 def rename
137 return render_403 unless editable? 139 return render_403 unless editable?
141 if request.post? && @page.update_attributes(params[:wiki_page]) 143 if request.post? && @page.update_attributes(params[:wiki_page])
142 flash[:notice] = l(:notice_successful_update) 144 flash[:notice] = l(:notice_successful_update)
143 redirect_to :action => 'show', :project_id => @project, :id => @page.title 145 redirect_to :action => 'show', :project_id => @project, :id => @page.title
144 end 146 end
145 end 147 end
146 148
149 verify :method => :post, :only => :protect, :redirect_to => { :action => :show }
147 def protect 150 def protect
148 @page.update_attribute :protected, params[:protected] 151 @page.update_attribute :protected, params[:protected]
149 redirect_to :action => 'show', :project_id => @project, :id => @page.title 152 redirect_to :action => 'show', :project_id => @project, :id => @page.title
150 end 153 end
151 154
152 # show page history 155 # show page history
153 def history 156 def history
154 @version_count = @page.content.versions.count 157 @version_count = @page.content.versions.count
155 @version_pages = Paginator.new self, @version_count, per_page_option, params['p'] 158 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
156 # don't load text 159 # don't load text
157 @versions = @page.content.versions.find :all, 160 @versions = @page.content.versions.find :all,
158 :select => "id, author_id, comments, updated_on, version", 161 :select => "id, author_id, comments, updated_on, version",
159 :order => 'version DESC', 162 :order => 'version DESC',
160 :limit => @version_pages.items_per_page + 1, 163 :limit => @version_pages.items_per_page + 1,
161 :offset => @version_pages.current.offset 164 :offset => @version_pages.current.offset
162 165
163 render :layout => false if request.xhr? 166 render :layout => false if request.xhr?
164 end 167 end
165 168
166 def diff 169 def diff
167 @diff = @page.diff(params[:version], params[:version_from]) 170 @diff = @page.diff(params[:version], params[:version_from])
168 render_404 unless @diff 171 render_404 unless @diff
169 end 172 end
170 173
171 def annotate 174 def annotate
172 @annotate = @page.annotate(params[:version]) 175 @annotate = @page.annotate(params[:version])
173 render_404 unless @annotate 176 render_404 unless @annotate
174 end 177 end
175 178
176 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show } 179 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
177 # Removes a wiki page and its history 180 # Removes a wiki page and its history
178 # Children can be either set as root pages, removed or reassigned to another parent page 181 # Children can be either set as root pages, removed or reassigned to another parent page
179 def destroy 182 def destroy
180 return render_403 unless editable? 183 return render_403 unless editable?
181 184
182 @descendants_count = @page.descendants.size 185 @descendants_count = @page.descendants.size
183 if @descendants_count > 0 186 if @descendants_count > 0
184 case params[:todo] 187 case params[:todo]
185 when 'nullify' 188 when 'nullify'
186 # Nothing to do 189 # Nothing to do
212 else 215 else
213 redirect_to :action => 'show', :project_id => @project, :id => nil 216 redirect_to :action => 'show', :project_id => @project, :id => nil
214 end 217 end
215 end 218 end
216 219
217 def date_index
218 load_pages_grouped_by_date_without_content
219 end
220
221 def preview 220 def preview
222 page = @wiki.find_page(params[:id]) 221 page = @wiki.find_page(params[:id])
223 # page is nil when previewing a new page 222 # page is nil when previewing a new page
224 return render_403 unless page.nil? || editable?(page) 223 return render_403 unless page.nil? || editable?(page)
225 if page 224 if page
236 render_attachment_warning_if_needed(@page) 235 render_attachment_warning_if_needed(@page)
237 redirect_to :action => 'show', :id => @page.title, :project_id => @project 236 redirect_to :action => 'show', :id => @page.title, :project_id => @project
238 end 237 end
239 238
240 private 239 private
241 240
242 def find_wiki 241 def find_wiki
243 @project = Project.find(params[:project_id]) 242 @project = Project.find(params[:project_id])
244 @wiki = @project.wiki 243 @wiki = @project.wiki
245 render_404 unless @wiki 244 render_404 unless @wiki
246 rescue ActiveRecord::RecordNotFound 245 rescue ActiveRecord::RecordNotFound
247 render_404 246 render_404
248 end 247 end
249 248
249 # Finds the requested page or a new page if it doesn't exist
250 def find_existing_or_new_page
251 @page = @wiki.find_or_new_page(params[:id])
252 if @wiki.page_found_with_redirect?
253 redirect_to params.update(:id => @page.title)
254 end
255 end
256
250 # Finds the requested page and returns a 404 error if it doesn't exist 257 # Finds the requested page and returns a 404 error if it doesn't exist
251 def find_existing_page 258 def find_existing_page
252 @page = @wiki.find_page(params[:id]) 259 @page = @wiki.find_page(params[:id])
253 render_404 if @page.nil? 260 if @page.nil?
254 end 261 render_404
255 262 return
263 end
264 if @wiki.page_found_with_redirect?
265 redirect_to params.update(:id => @page.title)
266 end
267 end
268
256 # Returns true if the current user is allowed to edit the page, otherwise false 269 # Returns true if the current user is allowed to edit the page, otherwise false
257 def editable?(page = @page) 270 def editable?(page = @page)
258 page.editable_by?(User.current) 271 page.editable_by?(User.current)
259 end 272 end
260 273
263 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) 276 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
264 extend helper unless self.instance_of?(helper) 277 extend helper unless self.instance_of?(helper)
265 helper.instance_method(:initial_page_content).bind(self).call(page) 278 helper.instance_method(:initial_page_content).bind(self).call(page)
266 end 279 end
267 280
268 # eager load information about last updates, without loading text 281 def load_pages_for_index
269 def load_pages_grouped_by_date_without_content 282 @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project})
270 @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on", 283 end
271 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
272 :order => 'title'
273 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
274 @pages_by_parent_id = @pages.group_by(&:parent_id)
275 end
276
277 end 284 end