comparison app/controllers/.svn/text-base/wiki_controller.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children 94944d00e43c
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require 'diff'
19
20 class WikiController < ApplicationController
21 default_search_scope :wiki_pages
22 before_filter :find_wiki, :authorize
23 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
24
25 verify :method => :post, :only => [:destroy, :protect], :redirect_to => { :action => :index }
26
27 helper :attachments
28 include AttachmentsHelper
29 helper :watchers
30
31 # display a page (in editing mode if it doesn't exist)
32 def index
33 page_title = params[:page]
34 @page = @wiki.find_or_new_page(page_title)
35 if @page.new_record?
36 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
37 edit
38 render :action => 'edit'
39 else
40 render_404
41 end
42 return
43 end
44 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
45 # Redirects user to the current version if he's not allowed to view previous versions
46 redirect_to :version => nil
47 return
48 end
49 @content = @page.content_for_version(params[:version])
50 if User.current.allowed_to?(:export_wiki_pages, @project)
51 if params[:format] == 'html'
52 export = render_to_string :action => 'export', :layout => false
53 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
54 return
55 elsif params[:format] == 'txt'
56 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
57 return
58 end
59 end
60 @editable = editable?
61 render :action => 'show'
62 end
63
64 # edit an existing page or a new one
65 def edit
66 @page = @wiki.find_or_new_page(params[:page])
67 return render_403 unless editable?
68 @page.content = WikiContent.new(:page => @page) if @page.new_record?
69
70 @content = @page.content_for_version(params[:version])
71 @content.text = initial_page_content(@page) if @content.text.blank?
72 # don't keep previous comment
73 @content.comments = nil
74 if request.get?
75 # To prevent StaleObjectError exception when reverting to a previous version
76 @content.version = @page.content.version
77 else
78 if !@page.new_record? && @content.text == params[:content][:text]
79 attachments = Attachment.attach_files(@page, params[:attachments])
80 render_attachment_warning_if_needed(@page)
81 # don't save if text wasn't changed
82 redirect_to :action => 'index', :id => @project, :page => @page.title
83 return
84 end
85 #@content.text = params[:content][:text]
86 #@content.comments = params[:content][:comments]
87 @content.attributes = params[:content]
88 @content.author = User.current
89 # if page is new @page.save will also save content, but not if page isn't a new record
90 if (@page.new_record? ? @page.save : @content.save)
91 attachments = Attachment.attach_files(@page, params[:attachments])
92 render_attachment_warning_if_needed(@page)
93 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
94 redirect_to :action => 'index', :id => @project, :page => @page.title
95 end
96 end
97 rescue ActiveRecord::StaleObjectError
98 # Optimistic locking exception
99 flash[:error] = l(:notice_locking_conflict)
100 end
101
102 # rename a page
103 def rename
104 return render_403 unless editable?
105 @page.redirect_existing_links = true
106 # used to display the *original* title if some AR validation errors occur
107 @original_title = @page.pretty_title
108 if request.post? && @page.update_attributes(params[:wiki_page])
109 flash[:notice] = l(:notice_successful_update)
110 redirect_to :action => 'index', :id => @project, :page => @page.title
111 end
112 end
113
114 def protect
115 @page.update_attribute :protected, params[:protected]
116 redirect_to :action => 'index', :id => @project, :page => @page.title
117 end
118
119 # show page history
120 def history
121 @version_count = @page.content.versions.count
122 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
123 # don't load text
124 @versions = @page.content.versions.find :all,
125 :select => "id, author_id, comments, updated_on, version",
126 :order => 'version DESC',
127 :limit => @version_pages.items_per_page + 1,
128 :offset => @version_pages.current.offset
129
130 render :layout => false if request.xhr?
131 end
132
133 def diff
134 @diff = @page.diff(params[:version], params[:version_from])
135 render_404 unless @diff
136 end
137
138 def annotate
139 @annotate = @page.annotate(params[:version])
140 render_404 unless @annotate
141 end
142
143 # Removes a wiki page and its history
144 # Children can be either set as root pages, removed or reassigned to another parent page
145 def destroy
146 return render_403 unless editable?
147
148 @descendants_count = @page.descendants.size
149 if @descendants_count > 0
150 case params[:todo]
151 when 'nullify'
152 # Nothing to do
153 when 'destroy'
154 # Removes all its descendants
155 @page.descendants.each(&:destroy)
156 when 'reassign'
157 # Reassign children to another parent page
158 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
159 return unless reassign_to
160 @page.children.each do |child|
161 child.update_attribute(:parent, reassign_to)
162 end
163 else
164 @reassignable_to = @wiki.pages - @page.self_and_descendants
165 return
166 end
167 end
168 @page.destroy
169 redirect_to :action => 'special', :id => @project, :page => 'Page_index'
170 end
171
172 # display special pages
173 def special
174 page_title = params[:page].downcase
175 case page_title
176 # show pages index, sorted by title
177 when 'page_index', 'date_index'
178 # eager load information about last updates, without loading text
179 @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
180 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
181 :order => 'title'
182 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
183 @pages_by_parent_id = @pages.group_by(&:parent_id)
184 # export wiki to a single html file
185 when 'export'
186 if User.current.allowed_to?(:export_wiki_pages, @project)
187 @pages = @wiki.pages.find :all, :order => 'title'
188 export = render_to_string :action => 'export_multiple', :layout => false
189 send_data(export, :type => 'text/html', :filename => "wiki.html")
190 else
191 redirect_to :action => 'index', :id => @project, :page => nil
192 end
193 return
194 else
195 # requested special page doesn't exist, redirect to default page
196 redirect_to :action => 'index', :id => @project, :page => nil
197 return
198 end
199 render :action => "special_#{page_title}"
200 end
201
202 def preview
203 page = @wiki.find_page(params[:page])
204 # page is nil when previewing a new page
205 return render_403 unless page.nil? || editable?(page)
206 if page
207 @attachements = page.attachments
208 @previewed = page.content
209 end
210 @text = params[:content][:text]
211 render :partial => 'common/preview'
212 end
213
214 def add_attachment
215 return render_403 unless editable?
216 attachments = Attachment.attach_files(@page, params[:attachments])
217 render_attachment_warning_if_needed(@page)
218 redirect_to :action => 'index', :page => @page.title
219 end
220
221 private
222
223 def find_wiki
224 @project = Project.find(params[:id])
225 @wiki = @project.wiki
226 render_404 unless @wiki
227 rescue ActiveRecord::RecordNotFound
228 render_404
229 end
230
231 # Finds the requested page and returns a 404 error if it doesn't exist
232 def find_existing_page
233 @page = @wiki.find_page(params[:page])
234 render_404 if @page.nil?
235 end
236
237 # Returns true if the current user is allowed to edit the page, otherwise false
238 def editable?(page = @page)
239 page.editable_by?(User.current)
240 end
241
242 # Returns the default content of a new wiki page
243 def initial_page_content(page)
244 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
245 extend helper unless self.instance_of?(helper)
246 helper.instance_method(:initial_page_content).bind(self).call(page)
247 end
248 end