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