##// END OF EJS Templates
Do not show section edit links for wiki page history (#2222)....
Jean-Philippe Lang -
r7710:2770e285358a
parent child
Show More
@@ -1,305 +1,306
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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'
19
19
20 # The WikiController follows the Rails REST controller pattern but with
20 # The WikiController follows the Rails REST controller pattern but with
21 # a few differences
21 # a few differences
22 #
22 #
23 # * index - shows a list of WikiPages grouped by page or date
23 # * index - shows a list of WikiPages grouped by page or date
24 # * new - not used
24 # * new - not used
25 # * create - not used
25 # * create - not used
26 # * show - will also show the form for creating a new wiki page
26 # * show - will also show the form for creating a new wiki page
27 # * edit - used to edit an existing or new page
27 # * edit - used to edit an existing or new page
28 # * update - used to save a wiki page update to the database, including new pages
28 # * update - used to save a wiki page update to the database, including new pages
29 # * destroy - normal
29 # * destroy - normal
30 #
30 #
31 # Other member and collection methods are also used
31 # Other member and collection methods are also used
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_or_new_page, :only => [:show, :edit, :update]
38 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]
39
39
40 helper :attachments
40 helper :attachments
41 include AttachmentsHelper
41 include AttachmentsHelper
42 helper :watchers
42 helper :watchers
43 include Redmine::Export::PDF
43 include Redmine::Export::PDF
44
44
45 # List of pages, sorted alphabetically and by parent (hierarchy)
45 # List of pages, sorted alphabetically and by parent (hierarchy)
46 def index
46 def index
47 load_pages_for_index
47 load_pages_for_index
48 @pages_by_parent_id = @pages.group_by(&:parent_id)
48 @pages_by_parent_id = @pages.group_by(&:parent_id)
49 end
49 end
50
50
51 # List of page, by last update
51 # List of page, by last update
52 def date_index
52 def date_index
53 load_pages_for_index
53 load_pages_for_index
54 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
54 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
55 end
55 end
56
56
57 # display a page (in editing mode if it doesn't exist)
57 # display a page (in editing mode if it doesn't exist)
58 def show
58 def show
59 if @page.new_record?
59 if @page.new_record?
60 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
60 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
61 edit
61 edit
62 render :action => 'edit'
62 render :action => 'edit'
63 else
63 else
64 render_404
64 render_404
65 end
65 end
66 return
66 return
67 end
67 end
68 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
68 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
69 # Redirects user to the current version if he's not allowed to view previous versions
69 # Redirects user to the current version if he's not allowed to view previous versions
70 redirect_to :version => nil
70 redirect_to :version => nil
71 return
71 return
72 end
72 end
73 @content = @page.content_for_version(params[:version])
73 @content = @page.content_for_version(params[:version])
74 if User.current.allowed_to?(:export_wiki_pages, @project)
74 if User.current.allowed_to?(:export_wiki_pages, @project)
75 if params[:format] == 'pdf'
75 if params[:format] == 'pdf'
76 send_data(wiki_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
76 send_data(wiki_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
77 return
77 return
78 elsif params[:format] == 'html'
78 elsif params[:format] == 'html'
79 export = render_to_string :action => 'export', :layout => false
79 export = render_to_string :action => 'export', :layout => false
80 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
80 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
81 return
81 return
82 elsif params[:format] == 'txt'
82 elsif params[:format] == 'txt'
83 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
83 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
84 return
84 return
85 end
85 end
86 end
86 end
87 @editable = editable?
87 @editable = editable?
88 @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) && params[:version].nil?
88 render :action => 'show'
89 render :action => 'show'
89 end
90 end
90
91
91 # edit an existing page or a new one
92 # edit an existing page or a new one
92 def edit
93 def edit
93 return render_403 unless editable?
94 return render_403 unless editable?
94 @page.content = WikiContent.new(:page => @page) if @page.new_record?
95 @page.content = WikiContent.new(:page => @page) if @page.new_record?
95
96
96 @content = @page.content_for_version(params[:version])
97 @content = @page.content_for_version(params[:version])
97 @content.text = initial_page_content(@page) if @content.text.blank?
98 @content.text = initial_page_content(@page) if @content.text.blank?
98 # don't keep previous comment
99 # don't keep previous comment
99 @content.comments = nil
100 @content.comments = nil
100
101
101 # To prevent StaleObjectError exception when reverting to a previous version
102 # To prevent StaleObjectError exception when reverting to a previous version
102 @content.version = @page.content.version
103 @content.version = @page.content.version
103
104
104 @text = @content.text
105 @text = @content.text
105 if params[:section].present?
106 if params[:section].present?
106 @section = params[:section].to_i
107 @section = params[:section].to_i
107 @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
108 @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
108 render_404 if @text.blank?
109 render_404 if @text.blank?
109 end
110 end
110 end
111 end
111
112
112 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
113 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
113 # Creates a new page or updates an existing one
114 # Creates a new page or updates an existing one
114 def update
115 def update
115 return render_403 unless editable?
116 return render_403 unless editable?
116 @page.content = WikiContent.new(:page => @page) if @page.new_record?
117 @page.content = WikiContent.new(:page => @page) if @page.new_record?
117
118
118 @content = @page.content_for_version(params[:version])
119 @content = @page.content_for_version(params[:version])
119 @content.text = initial_page_content(@page) if @content.text.blank?
120 @content.text = initial_page_content(@page) if @content.text.blank?
120 # don't keep previous comment
121 # don't keep previous comment
121 @content.comments = nil
122 @content.comments = nil
122
123
123 if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
124 if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
124 attachments = Attachment.attach_files(@page, params[:attachments])
125 attachments = Attachment.attach_files(@page, params[:attachments])
125 render_attachment_warning_if_needed(@page)
126 render_attachment_warning_if_needed(@page)
126 # don't save if text wasn't changed
127 # don't save if text wasn't changed
127 redirect_to :action => 'show', :project_id => @project, :id => @page.title
128 redirect_to :action => 'show', :project_id => @project, :id => @page.title
128 return
129 return
129 end
130 end
130
131
131 @content.comments = params[:content][:comments]
132 @content.comments = params[:content][:comments]
132 @text = params[:content][:text]
133 @text = params[:content][:text]
133 if params[:section].present?
134 if params[:section].present?
134 @section = params[:section].to_i
135 @section = params[:section].to_i
135 @section_hash = params[:section_hash]
136 @section_hash = params[:section_hash]
136 @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
137 @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
137 else
138 else
138 @content.version = params[:content][:version]
139 @content.version = params[:content][:version]
139 @content.text = @text
140 @content.text = @text
140 end
141 end
141 @content.author = User.current
142 @content.author = User.current
142 # if page is new @page.save will also save content, but not if page isn't a new record
143 # if page is new @page.save will also save content, but not if page isn't a new record
143 if (@page.new_record? ? @page.save : @content.save)
144 if (@page.new_record? ? @page.save : @content.save)
144 attachments = Attachment.attach_files(@page, params[:attachments])
145 attachments = Attachment.attach_files(@page, params[:attachments])
145 render_attachment_warning_if_needed(@page)
146 render_attachment_warning_if_needed(@page)
146 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
147 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
147 redirect_to :action => 'show', :project_id => @project, :id => @page.title
148 redirect_to :action => 'show', :project_id => @project, :id => @page.title
148 else
149 else
149 render :action => 'edit'
150 render :action => 'edit'
150 end
151 end
151
152
152 rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
153 rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
153 # Optimistic locking exception
154 # Optimistic locking exception
154 flash.now[:error] = l(:notice_locking_conflict)
155 flash.now[:error] = l(:notice_locking_conflict)
155 render :action => 'edit'
156 render :action => 'edit'
156 end
157 end
157
158
158 # rename a page
159 # rename a page
159 def rename
160 def rename
160 return render_403 unless editable?
161 return render_403 unless editable?
161 @page.redirect_existing_links = true
162 @page.redirect_existing_links = true
162 # used to display the *original* title if some AR validation errors occur
163 # used to display the *original* title if some AR validation errors occur
163 @original_title = @page.pretty_title
164 @original_title = @page.pretty_title
164 if request.post? && @page.update_attributes(params[:wiki_page])
165 if request.post? && @page.update_attributes(params[:wiki_page])
165 flash[:notice] = l(:notice_successful_update)
166 flash[:notice] = l(:notice_successful_update)
166 redirect_to :action => 'show', :project_id => @project, :id => @page.title
167 redirect_to :action => 'show', :project_id => @project, :id => @page.title
167 end
168 end
168 end
169 end
169
170
170 verify :method => :post, :only => :protect, :redirect_to => { :action => :show }
171 verify :method => :post, :only => :protect, :redirect_to => { :action => :show }
171 def protect
172 def protect
172 @page.update_attribute :protected, params[:protected]
173 @page.update_attribute :protected, params[:protected]
173 redirect_to :action => 'show', :project_id => @project, :id => @page.title
174 redirect_to :action => 'show', :project_id => @project, :id => @page.title
174 end
175 end
175
176
176 # show page history
177 # show page history
177 def history
178 def history
178 @version_count = @page.content.versions.count
179 @version_count = @page.content.versions.count
179 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
180 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
180 # don't load text
181 # don't load text
181 @versions = @page.content.versions.find :all,
182 @versions = @page.content.versions.find :all,
182 :select => "id, author_id, comments, updated_on, version",
183 :select => "id, author_id, comments, updated_on, version",
183 :order => 'version DESC',
184 :order => 'version DESC',
184 :limit => @version_pages.items_per_page + 1,
185 :limit => @version_pages.items_per_page + 1,
185 :offset => @version_pages.current.offset
186 :offset => @version_pages.current.offset
186
187
187 render :layout => false if request.xhr?
188 render :layout => false if request.xhr?
188 end
189 end
189
190
190 def diff
191 def diff
191 @diff = @page.diff(params[:version], params[:version_from])
192 @diff = @page.diff(params[:version], params[:version_from])
192 render_404 unless @diff
193 render_404 unless @diff
193 end
194 end
194
195
195 def annotate
196 def annotate
196 @annotate = @page.annotate(params[:version])
197 @annotate = @page.annotate(params[:version])
197 render_404 unless @annotate
198 render_404 unless @annotate
198 end
199 end
199
200
200 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
201 verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
201 # Removes a wiki page and its history
202 # Removes a wiki page and its history
202 # Children can be either set as root pages, removed or reassigned to another parent page
203 # Children can be either set as root pages, removed or reassigned to another parent page
203 def destroy
204 def destroy
204 return render_403 unless editable?
205 return render_403 unless editable?
205
206
206 @descendants_count = @page.descendants.size
207 @descendants_count = @page.descendants.size
207 if @descendants_count > 0
208 if @descendants_count > 0
208 case params[:todo]
209 case params[:todo]
209 when 'nullify'
210 when 'nullify'
210 # Nothing to do
211 # Nothing to do
211 when 'destroy'
212 when 'destroy'
212 # Removes all its descendants
213 # Removes all its descendants
213 @page.descendants.each(&:destroy)
214 @page.descendants.each(&:destroy)
214 when 'reassign'
215 when 'reassign'
215 # Reassign children to another parent page
216 # Reassign children to another parent page
216 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
217 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
217 return unless reassign_to
218 return unless reassign_to
218 @page.children.each do |child|
219 @page.children.each do |child|
219 child.update_attribute(:parent, reassign_to)
220 child.update_attribute(:parent, reassign_to)
220 end
221 end
221 else
222 else
222 @reassignable_to = @wiki.pages - @page.self_and_descendants
223 @reassignable_to = @wiki.pages - @page.self_and_descendants
223 return
224 return
224 end
225 end
225 end
226 end
226 @page.destroy
227 @page.destroy
227 redirect_to :action => 'index', :project_id => @project
228 redirect_to :action => 'index', :project_id => @project
228 end
229 end
229
230
230 # Export wiki to a single html file
231 # Export wiki to a single html file
231 def export
232 def export
232 if User.current.allowed_to?(:export_wiki_pages, @project)
233 if User.current.allowed_to?(:export_wiki_pages, @project)
233 @pages = @wiki.pages.find :all, :order => 'title'
234 @pages = @wiki.pages.find :all, :order => 'title'
234 export = render_to_string :action => 'export_multiple', :layout => false
235 export = render_to_string :action => 'export_multiple', :layout => false
235 send_data(export, :type => 'text/html', :filename => "wiki.html")
236 send_data(export, :type => 'text/html', :filename => "wiki.html")
236 else
237 else
237 redirect_to :action => 'show', :project_id => @project, :id => nil
238 redirect_to :action => 'show', :project_id => @project, :id => nil
238 end
239 end
239 end
240 end
240
241
241 def preview
242 def preview
242 page = @wiki.find_page(params[:id])
243 page = @wiki.find_page(params[:id])
243 # page is nil when previewing a new page
244 # page is nil when previewing a new page
244 return render_403 unless page.nil? || editable?(page)
245 return render_403 unless page.nil? || editable?(page)
245 if page
246 if page
246 @attachements = page.attachments
247 @attachements = page.attachments
247 @previewed = page.content
248 @previewed = page.content
248 end
249 end
249 @text = params[:content][:text]
250 @text = params[:content][:text]
250 render :partial => 'common/preview'
251 render :partial => 'common/preview'
251 end
252 end
252
253
253 def add_attachment
254 def add_attachment
254 return render_403 unless editable?
255 return render_403 unless editable?
255 attachments = Attachment.attach_files(@page, params[:attachments])
256 attachments = Attachment.attach_files(@page, params[:attachments])
256 render_attachment_warning_if_needed(@page)
257 render_attachment_warning_if_needed(@page)
257 redirect_to :action => 'show', :id => @page.title, :project_id => @project
258 redirect_to :action => 'show', :id => @page.title, :project_id => @project
258 end
259 end
259
260
260 private
261 private
261
262
262 def find_wiki
263 def find_wiki
263 @project = Project.find(params[:project_id])
264 @project = Project.find(params[:project_id])
264 @wiki = @project.wiki
265 @wiki = @project.wiki
265 render_404 unless @wiki
266 render_404 unless @wiki
266 rescue ActiveRecord::RecordNotFound
267 rescue ActiveRecord::RecordNotFound
267 render_404
268 render_404
268 end
269 end
269
270
270 # Finds the requested page or a new page if it doesn't exist
271 # Finds the requested page or a new page if it doesn't exist
271 def find_existing_or_new_page
272 def find_existing_or_new_page
272 @page = @wiki.find_or_new_page(params[:id])
273 @page = @wiki.find_or_new_page(params[:id])
273 if @wiki.page_found_with_redirect?
274 if @wiki.page_found_with_redirect?
274 redirect_to params.update(:id => @page.title)
275 redirect_to params.update(:id => @page.title)
275 end
276 end
276 end
277 end
277
278
278 # Finds the requested page and returns a 404 error if it doesn't exist
279 # Finds the requested page and returns a 404 error if it doesn't exist
279 def find_existing_page
280 def find_existing_page
280 @page = @wiki.find_page(params[:id])
281 @page = @wiki.find_page(params[:id])
281 if @page.nil?
282 if @page.nil?
282 render_404
283 render_404
283 return
284 return
284 end
285 end
285 if @wiki.page_found_with_redirect?
286 if @wiki.page_found_with_redirect?
286 redirect_to params.update(:id => @page.title)
287 redirect_to params.update(:id => @page.title)
287 end
288 end
288 end
289 end
289
290
290 # Returns true if the current user is allowed to edit the page, otherwise false
291 # Returns true if the current user is allowed to edit the page, otherwise false
291 def editable?(page = @page)
292 def editable?(page = @page)
292 page.editable_by?(User.current)
293 page.editable_by?(User.current)
293 end
294 end
294
295
295 # Returns the default content of a new wiki page
296 # Returns the default content of a new wiki page
296 def initial_page_content(page)
297 def initial_page_content(page)
297 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
298 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
298 extend helper unless self.instance_of?(helper)
299 extend helper unless self.instance_of?(helper)
299 helper.instance_method(:initial_page_content).bind(self).call(page)
300 helper.instance_method(:initial_page_content).bind(self).call(page)
300 end
301 end
301
302
302 def load_pages_for_index
303 def load_pages_for_index
303 @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project})
304 @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project})
304 end
305 end
305 end
306 end
@@ -1,4 +1,4
1 <div class="wiki wiki-page">
1 <div class="wiki wiki-page">
2 <%= textilizable content, :text, :attachments => content.page.attachments,
2 <%= textilizable content, :text, :attachments => content.page.attachments,
3 :edit_section_links => (content.is_a?(WikiContent) && @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) && {:controller => 'wiki', :action => 'edit', :project_id => @page.project, :id => @page.title}) %>
3 :edit_section_links => (@sections_editable && {:controller => 'wiki', :action => 'edit', :project_id => @page.project, :id => @page.title}) %>
4 </div>
4 </div>
General Comments 0
You need to be logged in to leave comments. Login now