Chris@441
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@441
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@441
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'diff'
|
Chris@0
|
19
|
chris@37
|
20 # The WikiController follows the Rails REST controller pattern but with
|
chris@37
|
21 # a few differences
|
chris@37
|
22 #
|
chris@37
|
23 # * index - shows a list of WikiPages grouped by page or date
|
chris@37
|
24 # * new - not used
|
chris@37
|
25 # * create - not used
|
chris@37
|
26 # * show - will also show the form for creating a new wiki page
|
chris@37
|
27 # * edit - used to edit an existing or new page
|
chris@37
|
28 # * update - used to save a wiki page update to the database, including new pages
|
chris@37
|
29 # * destroy - normal
|
chris@37
|
30 #
|
chris@37
|
31 # Other member and collection methods are also used
|
chris@37
|
32 #
|
chris@37
|
33 # TODO: still being worked on
|
Chris@0
|
34 class WikiController < ApplicationController
|
Chris@0
|
35 default_search_scope :wiki_pages
|
Chris@0
|
36 before_filter :find_wiki, :authorize
|
Chris@441
|
37 before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
|
Chris@1115
|
38 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version]
|
Chris@1115
|
39 accept_api_auth :index, :show, :update, :destroy
|
Chris@0
|
40
|
Chris@0
|
41 helper :attachments
|
Chris@441
|
42 include AttachmentsHelper
|
Chris@0
|
43 helper :watchers
|
Chris@909
|
44 include Redmine::Export::PDF
|
chris@37
|
45
|
chris@37
|
46 # List of pages, sorted alphabetically and by parent (hierarchy)
|
chris@37
|
47 def index
|
Chris@441
|
48 load_pages_for_index
|
Chris@1115
|
49
|
Chris@1115
|
50 respond_to do |format|
|
Chris@1115
|
51 format.html {
|
Chris@1115
|
52 @pages_by_parent_id = @pages.group_by(&:parent_id)
|
Chris@1115
|
53 }
|
Chris@1115
|
54 format.api
|
Chris@1115
|
55 end
|
Chris@441
|
56 end
|
Chris@441
|
57
|
Chris@441
|
58 # List of page, by last update
|
Chris@441
|
59 def date_index
|
Chris@441
|
60 load_pages_for_index
|
Chris@441
|
61 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
|
chris@37
|
62 end
|
chris@37
|
63
|
Chris@0
|
64 # display a page (in editing mode if it doesn't exist)
|
chris@37
|
65 def show
|
Chris@0
|
66 if @page.new_record?
|
Chris@1115
|
67 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
|
Chris@0
|
68 edit
|
Chris@0
|
69 render :action => 'edit'
|
Chris@0
|
70 else
|
Chris@0
|
71 render_404
|
Chris@0
|
72 end
|
Chris@0
|
73 return
|
Chris@0
|
74 end
|
Chris@0
|
75 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
|
Chris@1115
|
76 deny_access
|
Chris@0
|
77 return
|
Chris@0
|
78 end
|
Chris@0
|
79 @content = @page.content_for_version(params[:version])
|
Chris@0
|
80 if User.current.allowed_to?(:export_wiki_pages, @project)
|
Chris@909
|
81 if params[:format] == 'pdf'
|
Chris@1115
|
82 send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
|
Chris@909
|
83 return
|
Chris@909
|
84 elsif params[:format] == 'html'
|
Chris@0
|
85 export = render_to_string :action => 'export', :layout => false
|
Chris@0
|
86 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
|
Chris@0
|
87 return
|
Chris@0
|
88 elsif params[:format] == 'txt'
|
Chris@0
|
89 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
|
Chris@0
|
90 return
|
Chris@0
|
91 end
|
Chris@0
|
92 end
|
Chris@0
|
93 @editable = editable?
|
Chris@909
|
94 @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) &&
|
Chris@909
|
95 @content.current_version? &&
|
Chris@909
|
96 Redmine::WikiFormatting.supports_section_edit?
|
Chris@909
|
97
|
Chris@1115
|
98 respond_to do |format|
|
Chris@1115
|
99 format.html
|
Chris@1115
|
100 format.api
|
Chris@1115
|
101 end
|
Chris@0
|
102 end
|
Chris@441
|
103
|
Chris@0
|
104 # edit an existing page or a new one
|
Chris@0
|
105 def edit
|
Chris@0
|
106 return render_403 unless editable?
|
Chris@1115
|
107 if @page.new_record?
|
Chris@1115
|
108 @page.content = WikiContent.new(:page => @page)
|
Chris@1115
|
109 if params[:parent].present?
|
Chris@1115
|
110 @page.parent = @page.wiki.find_page(params[:parent].to_s)
|
Chris@1115
|
111 end
|
Chris@1115
|
112 end
|
Chris@441
|
113
|
Chris@0
|
114 @content = @page.content_for_version(params[:version])
|
Chris@0
|
115 @content.text = initial_page_content(@page) if @content.text.blank?
|
Chris@0
|
116 # don't keep previous comment
|
Chris@0
|
117 @content.comments = nil
|
chris@37
|
118
|
chris@37
|
119 # To prevent StaleObjectError exception when reverting to a previous version
|
chris@37
|
120 @content.version = @page.content.version
|
Chris@1115
|
121
|
Chris@909
|
122 @text = @content.text
|
Chris@909
|
123 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
|
Chris@909
|
124 @section = params[:section].to_i
|
Chris@909
|
125 @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
|
Chris@909
|
126 render_404 if @text.blank?
|
Chris@909
|
127 end
|
Chris@0
|
128 end
|
chris@37
|
129
|
chris@37
|
130 # Creates a new page or updates an existing one
|
chris@37
|
131 def update
|
chris@37
|
132 return render_403 unless editable?
|
Chris@1115
|
133 was_new_page = @page.new_record?
|
chris@37
|
134 @page.content = WikiContent.new(:page => @page) if @page.new_record?
|
Chris@1115
|
135 @page.safe_attributes = params[:wiki_page]
|
Chris@441
|
136
|
Chris@1115
|
137 @content = @page.content
|
Chris@1115
|
138 content_params = params[:content]
|
Chris@1115
|
139 if content_params.nil? && params[:wiki_page].is_a?(Hash)
|
Chris@1115
|
140 content_params = params[:wiki_page].slice(:text, :comments, :version)
|
Chris@1115
|
141 end
|
Chris@1115
|
142 content_params ||= {}
|
chris@37
|
143
|
Chris@1115
|
144 @content.comments = content_params[:comments]
|
Chris@1115
|
145 @text = content_params[:text]
|
Chris@909
|
146 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
|
Chris@909
|
147 @section = params[:section].to_i
|
Chris@909
|
148 @section_hash = params[:section_hash]
|
Chris@909
|
149 @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
|
Chris@909
|
150 else
|
Chris@1115
|
151 @content.version = content_params[:version] if content_params[:version]
|
Chris@909
|
152 @content.text = @text
|
Chris@909
|
153 end
|
chris@37
|
154 @content.author = User.current
|
Chris@1115
|
155
|
Chris@1115
|
156 if @page.save_with_content
|
chris@37
|
157 attachments = Attachment.attach_files(@page, params[:attachments])
|
chris@37
|
158 render_attachment_warning_if_needed(@page)
|
chris@37
|
159 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
|
Chris@1115
|
160
|
Chris@1115
|
161 respond_to do |format|
|
Chris@1115
|
162 format.html { redirect_to :action => 'show', :project_id => @project, :id => @page.title }
|
Chris@1115
|
163 format.api {
|
Chris@1115
|
164 if was_new_page
|
Chris@1115
|
165 render :action => 'show', :status => :created, :location => url_for(:controller => 'wiki', :action => 'show', :project_id => @project, :id => @page.title)
|
Chris@1115
|
166 else
|
Chris@1115
|
167 render_api_ok
|
Chris@1115
|
168 end
|
Chris@1115
|
169 }
|
Chris@1115
|
170 end
|
Chris@119
|
171 else
|
Chris@1115
|
172 respond_to do |format|
|
Chris@1115
|
173 format.html { render :action => 'edit' }
|
Chris@1115
|
174 format.api { render_validation_errors(@content) }
|
Chris@1115
|
175 end
|
chris@37
|
176 end
|
chris@37
|
177
|
Chris@909
|
178 rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
|
chris@37
|
179 # Optimistic locking exception
|
Chris@1115
|
180 respond_to do |format|
|
Chris@1115
|
181 format.html {
|
Chris@1115
|
182 flash.now[:error] = l(:notice_locking_conflict)
|
Chris@1115
|
183 render :action => 'edit'
|
Chris@1115
|
184 }
|
Chris@1115
|
185 format.api { render_api_head :conflict }
|
Chris@1115
|
186 end
|
Chris@1115
|
187 rescue ActiveRecord::RecordNotSaved
|
Chris@1115
|
188 respond_to do |format|
|
Chris@1115
|
189 format.html { render :action => 'edit' }
|
Chris@1115
|
190 format.api { render_validation_errors(@content) }
|
Chris@1115
|
191 end
|
chris@37
|
192 end
|
chris@37
|
193
|
Chris@0
|
194 # rename a page
|
Chris@0
|
195 def rename
|
Chris@0
|
196 return render_403 unless editable?
|
Chris@0
|
197 @page.redirect_existing_links = true
|
Chris@0
|
198 # used to display the *original* title if some AR validation errors occur
|
Chris@0
|
199 @original_title = @page.pretty_title
|
Chris@0
|
200 if request.post? && @page.update_attributes(params[:wiki_page])
|
Chris@0
|
201 flash[:notice] = l(:notice_successful_update)
|
chris@37
|
202 redirect_to :action => 'show', :project_id => @project, :id => @page.title
|
Chris@0
|
203 end
|
Chris@0
|
204 end
|
Chris@441
|
205
|
Chris@0
|
206 def protect
|
Chris@0
|
207 @page.update_attribute :protected, params[:protected]
|
chris@37
|
208 redirect_to :action => 'show', :project_id => @project, :id => @page.title
|
Chris@0
|
209 end
|
Chris@0
|
210
|
Chris@0
|
211 # show page history
|
Chris@0
|
212 def history
|
Chris@0
|
213 @version_count = @page.content.versions.count
|
Chris@1115
|
214 @version_pages = Paginator.new self, @version_count, per_page_option, params['page']
|
Chris@441
|
215 # don't load text
|
Chris@441
|
216 @versions = @page.content.versions.find :all,
|
Chris@0
|
217 :select => "id, author_id, comments, updated_on, version",
|
Chris@0
|
218 :order => 'version DESC',
|
Chris@0
|
219 :limit => @version_pages.items_per_page + 1,
|
Chris@0
|
220 :offset => @version_pages.current.offset
|
Chris@0
|
221
|
Chris@0
|
222 render :layout => false if request.xhr?
|
Chris@0
|
223 end
|
Chris@441
|
224
|
Chris@0
|
225 def diff
|
Chris@0
|
226 @diff = @page.diff(params[:version], params[:version_from])
|
Chris@0
|
227 render_404 unless @diff
|
Chris@0
|
228 end
|
Chris@441
|
229
|
Chris@0
|
230 def annotate
|
Chris@0
|
231 @annotate = @page.annotate(params[:version])
|
Chris@0
|
232 render_404 unless @annotate
|
Chris@0
|
233 end
|
chris@37
|
234
|
Chris@0
|
235 # Removes a wiki page and its history
|
Chris@0
|
236 # Children can be either set as root pages, removed or reassigned to another parent page
|
Chris@0
|
237 def destroy
|
Chris@0
|
238 return render_403 unless editable?
|
Chris@441
|
239
|
Chris@0
|
240 @descendants_count = @page.descendants.size
|
Chris@0
|
241 if @descendants_count > 0
|
Chris@0
|
242 case params[:todo]
|
Chris@0
|
243 when 'nullify'
|
Chris@0
|
244 # Nothing to do
|
Chris@0
|
245 when 'destroy'
|
Chris@0
|
246 # Removes all its descendants
|
Chris@0
|
247 @page.descendants.each(&:destroy)
|
Chris@0
|
248 when 'reassign'
|
Chris@0
|
249 # Reassign children to another parent page
|
Chris@0
|
250 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
|
Chris@0
|
251 return unless reassign_to
|
Chris@0
|
252 @page.children.each do |child|
|
Chris@0
|
253 child.update_attribute(:parent, reassign_to)
|
Chris@0
|
254 end
|
Chris@0
|
255 else
|
Chris@0
|
256 @reassignable_to = @wiki.pages - @page.self_and_descendants
|
Chris@1115
|
257 # display the destroy form if it's a user request
|
Chris@1115
|
258 return unless api_request?
|
Chris@0
|
259 end
|
Chris@0
|
260 end
|
Chris@0
|
261 @page.destroy
|
Chris@1115
|
262 respond_to do |format|
|
Chris@1115
|
263 format.html { redirect_to :action => 'index', :project_id => @project }
|
Chris@1115
|
264 format.api { render_api_ok }
|
Chris@1115
|
265 end
|
Chris@0
|
266 end
|
Chris@0
|
267
|
Chris@1115
|
268 def destroy_version
|
Chris@1115
|
269 return render_403 unless editable?
|
Chris@1115
|
270
|
Chris@1115
|
271 @content = @page.content_for_version(params[:version])
|
Chris@1115
|
272 @content.destroy
|
Chris@1115
|
273 redirect_to_referer_or :action => 'history', :id => @page.title, :project_id => @project
|
Chris@1115
|
274 end
|
Chris@1115
|
275
|
Chris@1115
|
276 # Export wiki to a single pdf or html file
|
chris@37
|
277 def export
|
Chris@1115
|
278 @pages = @wiki.pages.all(:order => 'title', :include => [:content, {:attachments => :author}])
|
Chris@1115
|
279 respond_to do |format|
|
Chris@1115
|
280 format.html {
|
Chris@1115
|
281 export = render_to_string :action => 'export_multiple', :layout => false
|
Chris@1115
|
282 send_data(export, :type => 'text/html', :filename => "wiki.html")
|
Chris@1115
|
283 }
|
Chris@1115
|
284 format.pdf {
|
Chris@1115
|
285 send_data(wiki_pages_to_pdf(@pages, @project), :type => 'application/pdf', :filename => "#{@project.identifier}.pdf")
|
Chris@1115
|
286 }
|
Chris@0
|
287 end
|
chris@37
|
288 end
|
chris@37
|
289
|
Chris@0
|
290 def preview
|
chris@37
|
291 page = @wiki.find_page(params[:id])
|
Chris@0
|
292 # page is nil when previewing a new page
|
Chris@0
|
293 return render_403 unless page.nil? || editable?(page)
|
Chris@0
|
294 if page
|
Chris@0
|
295 @attachements = page.attachments
|
Chris@0
|
296 @previewed = page.content
|
Chris@0
|
297 end
|
Chris@0
|
298 @text = params[:content][:text]
|
Chris@0
|
299 render :partial => 'common/preview'
|
Chris@0
|
300 end
|
Chris@0
|
301
|
Chris@0
|
302 def add_attachment
|
Chris@0
|
303 return render_403 unless editable?
|
Chris@0
|
304 attachments = Attachment.attach_files(@page, params[:attachments])
|
Chris@0
|
305 render_attachment_warning_if_needed(@page)
|
chris@37
|
306 redirect_to :action => 'show', :id => @page.title, :project_id => @project
|
Chris@0
|
307 end
|
Chris@0
|
308
|
Chris@0
|
309 private
|
Chris@441
|
310
|
Chris@0
|
311 def find_wiki
|
chris@37
|
312 @project = Project.find(params[:project_id])
|
Chris@0
|
313 @wiki = @project.wiki
|
Chris@0
|
314 render_404 unless @wiki
|
Chris@0
|
315 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
316 render_404
|
Chris@0
|
317 end
|
Chris@441
|
318
|
Chris@441
|
319 # Finds the requested page or a new page if it doesn't exist
|
Chris@441
|
320 def find_existing_or_new_page
|
Chris@441
|
321 @page = @wiki.find_or_new_page(params[:id])
|
Chris@441
|
322 if @wiki.page_found_with_redirect?
|
Chris@441
|
323 redirect_to params.update(:id => @page.title)
|
Chris@441
|
324 end
|
Chris@441
|
325 end
|
Chris@441
|
326
|
Chris@0
|
327 # Finds the requested page and returns a 404 error if it doesn't exist
|
Chris@0
|
328 def find_existing_page
|
chris@37
|
329 @page = @wiki.find_page(params[:id])
|
Chris@441
|
330 if @page.nil?
|
Chris@441
|
331 render_404
|
Chris@441
|
332 return
|
Chris@441
|
333 end
|
Chris@441
|
334 if @wiki.page_found_with_redirect?
|
Chris@441
|
335 redirect_to params.update(:id => @page.title)
|
Chris@441
|
336 end
|
Chris@0
|
337 end
|
Chris@441
|
338
|
Chris@0
|
339 # Returns true if the current user is allowed to edit the page, otherwise false
|
Chris@0
|
340 def editable?(page = @page)
|
Chris@0
|
341 page.editable_by?(User.current)
|
Chris@0
|
342 end
|
Chris@0
|
343
|
Chris@0
|
344 # Returns the default content of a new wiki page
|
Chris@0
|
345 def initial_page_content(page)
|
Chris@0
|
346 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
|
Chris@0
|
347 extend helper unless self.instance_of?(helper)
|
Chris@0
|
348 helper.instance_method(:initial_page_content).bind(self).call(page)
|
Chris@0
|
349 end
|
chris@37
|
350
|
Chris@441
|
351 def load_pages_for_index
|
Chris@1115
|
352 @pages = @wiki.pages.with_updated_on.order("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all
|
chris@37
|
353 end
|
Chris@0
|
354 end
|