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