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