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