comparison app/controllers/.svn/text-base/wiki_controller.rb.svn-base @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents 8661b858af72
children
comparison
equal deleted inserted replaced
245:051f544170fe 441:cbce1fd3b1b7
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
129 render :action => 'edit' 128 render :action => 'edit'
130 end 129 end
131 130
132 rescue ActiveRecord::StaleObjectError 131 rescue ActiveRecord::StaleObjectError
133 # Optimistic locking exception 132 # Optimistic locking exception
134 flash[:error] = l(:notice_locking_conflict) 133 flash.now[:error] = l(:notice_locking_conflict)
134 render :action => 'edit'
135 end 135 end
136 136
137 # rename a page 137 # rename a page
138 def rename 138 def rename
139 return render_403 unless editable? 139 return render_403 unless editable?
143 if request.post? && @page.update_attributes(params[:wiki_page]) 143 if request.post? && @page.update_attributes(params[:wiki_page])
144 flash[:notice] = l(:notice_successful_update) 144 flash[:notice] = l(:notice_successful_update)
145 redirect_to :action => 'show', :project_id => @project, :id => @page.title 145 redirect_to :action => 'show', :project_id => @project, :id => @page.title
146 end 146 end
147 end 147 end
148 148
149 verify :method => :post, :only => :protect, :redirect_to => { :action => :show }
149 def protect 150 def protect
150 @page.update_attribute :protected, params[:protected] 151 @page.update_attribute :protected, params[:protected]
151 redirect_to :action => 'show', :project_id => @project, :id => @page.title 152 redirect_to :action => 'show', :project_id => @project, :id => @page.title
152 end 153 end
153 154
154 # show page history 155 # show page history
155 def history 156 def history
156 @version_count = @page.content.versions.count 157 @version_count = @page.content.versions.count
157 @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']
158 # don't load text 159 # don't load text
159 @versions = @page.content.versions.find :all, 160 @versions = @page.content.versions.find :all,
160 :select => "id, author_id, comments, updated_on, version", 161 :select => "id, author_id, comments, updated_on, version",
161 :order => 'version DESC', 162 :order => 'version DESC',
162 :limit => @version_pages.items_per_page + 1, 163 :limit => @version_pages.items_per_page + 1,
163 :offset => @version_pages.current.offset 164 :offset => @version_pages.current.offset
164 165
165 render :layout => false if request.xhr? 166 render :layout => false if request.xhr?
166 end 167 end
167 168
168 def diff 169 def diff
169 @diff = @page.diff(params[:version], params[:version_from]) 170 @diff = @page.diff(params[:version], params[:version_from])
170 render_404 unless @diff 171 render_404 unless @diff
171 end 172 end
172 173
173 def annotate 174 def annotate
174 @annotate = @page.annotate(params[:version]) 175 @annotate = @page.annotate(params[:version])
175 render_404 unless @annotate 176 render_404 unless @annotate
176 end 177 end
177 178
178 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show } 179 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
179 # Removes a wiki page and its history 180 # Removes a wiki page and its history
180 # 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
181 def destroy 182 def destroy
182 return render_403 unless editable? 183 return render_403 unless editable?
183 184
184 @descendants_count = @page.descendants.size 185 @descendants_count = @page.descendants.size
185 if @descendants_count > 0 186 if @descendants_count > 0
186 case params[:todo] 187 case params[:todo]
187 when 'nullify' 188 when 'nullify'
188 # Nothing to do 189 # Nothing to do
214 else 215 else
215 redirect_to :action => 'show', :project_id => @project, :id => nil 216 redirect_to :action => 'show', :project_id => @project, :id => nil
216 end 217 end
217 end 218 end
218 219
219 def date_index
220 load_pages_grouped_by_date_without_content
221 end
222
223 def preview 220 def preview
224 page = @wiki.find_page(params[:id]) 221 page = @wiki.find_page(params[:id])
225 # page is nil when previewing a new page 222 # page is nil when previewing a new page
226 return render_403 unless page.nil? || editable?(page) 223 return render_403 unless page.nil? || editable?(page)
227 if page 224 if page
238 render_attachment_warning_if_needed(@page) 235 render_attachment_warning_if_needed(@page)
239 redirect_to :action => 'show', :id => @page.title, :project_id => @project 236 redirect_to :action => 'show', :id => @page.title, :project_id => @project
240 end 237 end
241 238
242 private 239 private
243 240
244 def find_wiki 241 def find_wiki
245 @project = Project.find(params[:project_id]) 242 @project = Project.find(params[:project_id])
246 @wiki = @project.wiki 243 @wiki = @project.wiki
247 render_404 unless @wiki 244 render_404 unless @wiki
248 rescue ActiveRecord::RecordNotFound 245 rescue ActiveRecord::RecordNotFound
249 render_404 246 render_404
250 end 247 end
251 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
252 # 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
253 def find_existing_page 258 def find_existing_page
254 @page = @wiki.find_page(params[:id]) 259 @page = @wiki.find_page(params[:id])
255 render_404 if @page.nil? 260 if @page.nil?
256 end 261 render_404
257 262 return
263 end
264 if @wiki.page_found_with_redirect?
265 redirect_to params.update(:id => @page.title)
266 end
267 end
268
258 # 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
259 def editable?(page = @page) 270 def editable?(page = @page)
260 page.editable_by?(User.current) 271 page.editable_by?(User.current)
261 end 272 end
262 273
265 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) 276 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
266 extend helper unless self.instance_of?(helper) 277 extend helper unless self.instance_of?(helper)
267 helper.instance_method(:initial_page_content).bind(self).call(page) 278 helper.instance_method(:initial_page_content).bind(self).call(page)
268 end 279 end
269 280
270 # eager load information about last updates, without loading text 281 def load_pages_for_index
271 def load_pages_grouped_by_date_without_content 282 @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project})
272 @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on", 283 end
273 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
274 :order => 'title'
275 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
276 @pages_by_parent_id = @pages.group_by(&:parent_id)
277 end
278
279 end 284 end