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