@@ -1,208 +1,211 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | class WikiController < ApplicationController |
|
21 | 21 | before_filter :find_wiki, :authorize |
|
22 | 22 | |
|
23 | 23 | verify :method => :post, :only => [:destroy, :destroy_attachment, :protect], :redirect_to => { :action => :index } |
|
24 | 24 | |
|
25 | 25 | helper :attachments |
|
26 | 26 | include AttachmentsHelper |
|
27 | 27 | |
|
28 | 28 | # display a page (in editing mode if it doesn't exist) |
|
29 | 29 | def index |
|
30 | 30 | page_title = params[:page] |
|
31 | 31 | @page = @wiki.find_or_new_page(page_title) |
|
32 | 32 | if @page.new_record? |
|
33 | 33 | if User.current.allowed_to?(:edit_wiki_pages, @project) |
|
34 | 34 | edit |
|
35 | 35 | render :action => 'edit' |
|
36 | 36 | else |
|
37 | 37 | render_404 |
|
38 | 38 | end |
|
39 | 39 | return |
|
40 | 40 | end |
|
41 | 41 | if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project) |
|
42 | 42 | # Redirects user to the current version if he's not allowed to view previous versions |
|
43 | 43 | redirect_to :version => nil |
|
44 | 44 | return |
|
45 | 45 | end |
|
46 | 46 | @content = @page.content_for_version(params[:version]) |
|
47 | 47 | if params[:export] == 'html' |
|
48 | 48 | export = render_to_string :action => 'export', :layout => false |
|
49 | 49 | send_data(export, :type => 'text/html', :filename => "#{@page.title}.html") |
|
50 | 50 | return |
|
51 | 51 | elsif params[:export] == 'txt' |
|
52 | 52 | send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt") |
|
53 | 53 | return |
|
54 | 54 | end |
|
55 | 55 | @editable = editable? |
|
56 | 56 | render :action => 'show' |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | # edit an existing page or a new one |
|
60 | 60 | def edit |
|
61 | 61 | @page = @wiki.find_or_new_page(params[:page]) |
|
62 | 62 | return render_403 unless editable? |
|
63 | 63 | @page.content = WikiContent.new(:page => @page) if @page.new_record? |
|
64 | 64 | |
|
65 | 65 | @content = @page.content_for_version(params[:version]) |
|
66 | 66 | @content.text = "h1. #{@page.pretty_title}" if @content.text.blank? |
|
67 | 67 | # don't keep previous comment |
|
68 | 68 | @content.comments = nil |
|
69 |
if request. |
|
|
69 | if request.get? | |
|
70 | # To prevent StaleObjectError exception when reverting to a previous version | |
|
71 | @content.version = @page.content.version | |
|
72 | else | |
|
70 | 73 | if !@page.new_record? && @content.text == params[:content][:text] |
|
71 | 74 | # don't save if text wasn't changed |
|
72 | 75 | redirect_to :action => 'index', :id => @project, :page => @page.title |
|
73 | 76 | return |
|
74 | 77 | end |
|
75 | 78 | #@content.text = params[:content][:text] |
|
76 | 79 | #@content.comments = params[:content][:comments] |
|
77 | 80 | @content.attributes = params[:content] |
|
78 | 81 | @content.author = User.current |
|
79 | 82 | # if page is new @page.save will also save content, but not if page isn't a new record |
|
80 | 83 | if (@page.new_record? ? @page.save : @content.save) |
|
81 | 84 | redirect_to :action => 'index', :id => @project, :page => @page.title |
|
82 | 85 | end |
|
83 | 86 | end |
|
84 | 87 | rescue ActiveRecord::StaleObjectError |
|
85 | 88 | # Optimistic locking exception |
|
86 | 89 | flash[:error] = l(:notice_locking_conflict) |
|
87 | 90 | end |
|
88 | 91 | |
|
89 | 92 | # rename a page |
|
90 | 93 | def rename |
|
91 | 94 | @page = @wiki.find_page(params[:page]) |
|
92 | 95 | return render_403 unless editable? |
|
93 | 96 | @page.redirect_existing_links = true |
|
94 | 97 | # used to display the *original* title if some AR validation errors occur |
|
95 | 98 | @original_title = @page.pretty_title |
|
96 | 99 | if request.post? && @page.update_attributes(params[:wiki_page]) |
|
97 | 100 | flash[:notice] = l(:notice_successful_update) |
|
98 | 101 | redirect_to :action => 'index', :id => @project, :page => @page.title |
|
99 | 102 | end |
|
100 | 103 | end |
|
101 | 104 | |
|
102 | 105 | def protect |
|
103 | 106 | page = @wiki.find_page(params[:page]) |
|
104 | 107 | page.update_attribute :protected, params[:protected] |
|
105 | 108 | redirect_to :action => 'index', :id => @project, :page => page.title |
|
106 | 109 | end |
|
107 | 110 | |
|
108 | 111 | # show page history |
|
109 | 112 | def history |
|
110 | 113 | @page = @wiki.find_page(params[:page]) |
|
111 | 114 | |
|
112 | 115 | @version_count = @page.content.versions.count |
|
113 | 116 | @version_pages = Paginator.new self, @version_count, per_page_option, params['p'] |
|
114 | 117 | # don't load text |
|
115 | 118 | @versions = @page.content.versions.find :all, |
|
116 | 119 | :select => "id, author_id, comments, updated_on, version", |
|
117 | 120 | :order => 'version DESC', |
|
118 | 121 | :limit => @version_pages.items_per_page + 1, |
|
119 | 122 | :offset => @version_pages.current.offset |
|
120 | 123 | |
|
121 | 124 | render :layout => false if request.xhr? |
|
122 | 125 | end |
|
123 | 126 | |
|
124 | 127 | def diff |
|
125 | 128 | @page = @wiki.find_page(params[:page]) |
|
126 | 129 | @diff = @page.diff(params[:version], params[:version_from]) |
|
127 | 130 | render_404 unless @diff |
|
128 | 131 | end |
|
129 | 132 | |
|
130 | 133 | def annotate |
|
131 | 134 | @page = @wiki.find_page(params[:page]) |
|
132 | 135 | @annotate = @page.annotate(params[:version]) |
|
133 | 136 | end |
|
134 | 137 | |
|
135 | 138 | # remove a wiki page and its history |
|
136 | 139 | def destroy |
|
137 | 140 | @page = @wiki.find_page(params[:page]) |
|
138 | 141 | return render_403 unless editable? |
|
139 | 142 | @page.destroy if @page |
|
140 | 143 | redirect_to :action => 'special', :id => @project, :page => 'Page_index' |
|
141 | 144 | end |
|
142 | 145 | |
|
143 | 146 | # display special pages |
|
144 | 147 | def special |
|
145 | 148 | page_title = params[:page].downcase |
|
146 | 149 | case page_title |
|
147 | 150 | # show pages index, sorted by title |
|
148 | 151 | when 'page_index', 'date_index' |
|
149 | 152 | # eager load information about last updates, without loading text |
|
150 | 153 | @pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on", |
|
151 | 154 | :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id", |
|
152 | 155 | :order => 'title' |
|
153 | 156 | @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} |
|
154 | 157 | @pages_by_parent_id = @pages.group_by(&:parent_id) |
|
155 | 158 | # export wiki to a single html file |
|
156 | 159 | when 'export' |
|
157 | 160 | @pages = @wiki.pages.find :all, :order => 'title' |
|
158 | 161 | export = render_to_string :action => 'export_multiple', :layout => false |
|
159 | 162 | send_data(export, :type => 'text/html', :filename => "wiki.html") |
|
160 | 163 | return |
|
161 | 164 | else |
|
162 | 165 | # requested special page doesn't exist, redirect to default page |
|
163 | 166 | redirect_to :action => 'index', :id => @project, :page => nil and return |
|
164 | 167 | end |
|
165 | 168 | render :action => "special_#{page_title}" |
|
166 | 169 | end |
|
167 | 170 | |
|
168 | 171 | def preview |
|
169 | 172 | page = @wiki.find_page(params[:page]) |
|
170 | 173 | # page is nil when previewing a new page |
|
171 | 174 | return render_403 unless page.nil? || editable?(page) |
|
172 | 175 | if page |
|
173 | 176 | @attachements = page.attachments |
|
174 | 177 | @previewed = page.content |
|
175 | 178 | end |
|
176 | 179 | @text = params[:content][:text] |
|
177 | 180 | render :partial => 'common/preview' |
|
178 | 181 | end |
|
179 | 182 | |
|
180 | 183 | def add_attachment |
|
181 | 184 | @page = @wiki.find_page(params[:page]) |
|
182 | 185 | return render_403 unless editable? |
|
183 | 186 | attach_files(@page, params[:attachments]) |
|
184 | 187 | redirect_to :action => 'index', :page => @page.title |
|
185 | 188 | end |
|
186 | 189 | |
|
187 | 190 | def destroy_attachment |
|
188 | 191 | @page = @wiki.find_page(params[:page]) |
|
189 | 192 | return render_403 unless editable? |
|
190 | 193 | @page.attachments.find(params[:attachment_id]).destroy |
|
191 | 194 | redirect_to :action => 'index', :page => @page.title |
|
192 | 195 | end |
|
193 | 196 | |
|
194 | 197 | private |
|
195 | 198 | |
|
196 | 199 | def find_wiki |
|
197 | 200 | @project = Project.find(params[:id]) |
|
198 | 201 | @wiki = @project.wiki |
|
199 | 202 | render_404 unless @wiki |
|
200 | 203 | rescue ActiveRecord::RecordNotFound |
|
201 | 204 | render_404 |
|
202 | 205 | end |
|
203 | 206 | |
|
204 | 207 | # Returns true if the current user is allowed to edit the page, otherwise false |
|
205 | 208 | def editable?(page = @page) |
|
206 | 209 | page.editable_by?(User.current) |
|
207 | 210 | end |
|
208 | 211 | end |
General Comments 0
You need to be logged in to leave comments.
Login now