To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / wiki_controller.rb @ 1040:ff783f2cc500

History | View | Annotate | Download (10.9 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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
# The WikiController follows the Rails REST controller pattern but with
21
# a few differences
22
#
23
# * index - shows a list of WikiPages grouped by page or date
24
# * new - not used
25
# * create - not used
26
# * show - will also show the form for creating a new wiki 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
29
# * destroy - normal
30
#
31
# Other member and collection methods are also used
32
#
33
# TODO: still being worked on
34
class WikiController < ApplicationController
35
  default_search_scope :wiki_pages
36
  before_filter :find_wiki, :authorize
37
  before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
38
  before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
39

    
40
  helper :attachments
41
  include AttachmentsHelper
42
  helper :watchers
43
  include Redmine::Export::PDF
44

    
45
  # List of pages, sorted alphabetically and by parent (hierarchy)
46
  def index
47
    load_pages_for_index
48
    @pages_by_parent_id = @pages.group_by(&:parent_id)
49
  end
50

    
51
  # List of page, by last update
52
  def date_index
53
    load_pages_for_index
54
    @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
55
  end
56

    
57
  # display a page (in editing mode if it doesn't exist)
58
  def show
59
    if @page.new_record?
60
      if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
61
        edit
62
        render :action => 'edit'
63
      else
64
        render_404
65
      end
66
      return
67
    end
68
    if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
69
      # Redirects user to the current version if he's not allowed to view previous versions
70
      redirect_to :version => nil
71
      return
72
    end
73
    @content = @page.content_for_version(params[:version])
74
    if User.current.allowed_to?(:export_wiki_pages, @project)
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'
79
        export = render_to_string :action => 'export', :layout => false
80
        send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
81
        return
82
      elsif params[:format] == 'txt'
83
        send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
84
        return
85
      end
86
    end
87
    @editable = editable?
88
    @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) &&
89
      @content.current_version? &&
90
      Redmine::WikiFormatting.supports_section_edit?
91

    
92
    render :action => 'show'
93
  end
94

    
95
  # edit an existing page or a new one
96
  def edit
97
    return render_403 unless editable?
98
    @page.content = WikiContent.new(:page => @page) if @page.new_record?
99

    
100
    @content = @page.content_for_version(params[:version])
101
    @content.text = initial_page_content(@page) if @content.text.blank?
102
    # don't keep previous comment
103
    @content.comments = nil
104

    
105
    # To prevent StaleObjectError exception when reverting to a previous version
106
    @content.version = @page.content.version
107
    
108
    @text = @content.text
109
    if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
110
      @section = params[:section].to_i
111
      @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
112
      render_404 if @text.blank?
113
    end
114
  end
115

    
116
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
117
  # Creates a new page or updates an existing one
118
  def update
119
    return render_403 unless editable?
120
    @page.content = WikiContent.new(:page => @page) if @page.new_record?
121

    
122
    @content = @page.content_for_version(params[:version])
123
    @content.text = initial_page_content(@page) if @content.text.blank?
124
    # don't keep previous comment
125
    @content.comments = nil
126

    
127
    if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
128
      attachments = Attachment.attach_files(@page, params[:attachments])
129
      render_attachment_warning_if_needed(@page)
130
      # don't save if text wasn't changed
131
      redirect_to :action => 'show', :project_id => @project, :id => @page.title
132
      return
133
    end
134
    
135
    @content.comments = params[:content][:comments]
136
    @text = params[:content][:text]
137
    if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
138
      @section = params[:section].to_i
139
      @section_hash = params[:section_hash]
140
      @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
141
    else
142
      @content.version = params[:content][:version]
143
      @content.text = @text
144
    end
145
    @content.author = User.current
146
    # if page is new @page.save will also save content, but not if page isn't a new record
147
    if (@page.new_record? ? @page.save : @content.save)
148
      attachments = Attachment.attach_files(@page, params[:attachments])
149
      render_attachment_warning_if_needed(@page)
150
      call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
151
      redirect_to :action => 'show', :project_id => @project, :id => @page.title
152
    else
153
      render :action => 'edit'
154
    end
155

    
156
  rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
157
    # Optimistic locking exception
158
    flash.now[:error] = l(:notice_locking_conflict)
159
    render :action => 'edit'
160
  end
161

    
162
  # rename a page
163
  def rename
164
    return render_403 unless editable?
165
    @page.redirect_existing_links = true
166
    # used to display the *original* title if some AR validation errors occur
167
    @original_title = @page.pretty_title
168
    if request.post? && @page.update_attributes(params[:wiki_page])
169
      flash[:notice] = l(:notice_successful_update)
170
      redirect_to :action => 'show', :project_id => @project, :id => @page.title
171
    end
172
  end
173

    
174
  verify :method => :post, :only => :protect, :redirect_to => { :action => :show }
175
  def protect
176
    @page.update_attribute :protected, params[:protected]
177
    redirect_to :action => 'show', :project_id => @project, :id => @page.title
178
  end
179

    
180
  # show page history
181
  def history
182
    @version_count = @page.content.versions.count
183
    @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
184
    # don't load text
185
    @versions = @page.content.versions.find :all,
186
                                            :select => "id, author_id, comments, updated_on, version",
187
                                            :order => 'version DESC',
188
                                            :limit  =>  @version_pages.items_per_page + 1,
189
                                            :offset =>  @version_pages.current.offset
190

    
191
    render :layout => false if request.xhr?
192
  end
193

    
194
  def diff
195
    @diff = @page.diff(params[:version], params[:version_from])
196
    render_404 unless @diff
197
  end
198

    
199
  def annotate
200
    @annotate = @page.annotate(params[:version])
201
    render_404 unless @annotate
202
  end
203

    
204
  verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
205
  # Removes a wiki page and its history
206
  # Children can be either set as root pages, removed or reassigned to another parent page
207
  def destroy
208
    return render_403 unless editable?
209

    
210
    @descendants_count = @page.descendants.size
211
    if @descendants_count > 0
212
      case params[:todo]
213
      when 'nullify'
214
        # Nothing to do
215
      when 'destroy'
216
        # Removes all its descendants
217
        @page.descendants.each(&:destroy)
218
      when 'reassign'
219
        # Reassign children to another parent page
220
        reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
221
        return unless reassign_to
222
        @page.children.each do |child|
223
          child.update_attribute(:parent, reassign_to)
224
        end
225
      else
226
        @reassignable_to = @wiki.pages - @page.self_and_descendants
227
        return
228
      end
229
    end
230
    @page.destroy
231
    redirect_to :action => 'index', :project_id => @project
232
  end
233

    
234
  # Export wiki to a single html file
235
  def export
236
    if User.current.allowed_to?(:export_wiki_pages, @project)
237
      @pages = @wiki.pages.find :all, :order => 'title'
238
      export = render_to_string :action => 'export_multiple', :layout => false
239
      send_data(export, :type => 'text/html', :filename => "wiki.html")
240
    else
241
      redirect_to :action => 'show', :project_id => @project, :id => nil
242
    end
243
  end
244

    
245
  def preview
246
    page = @wiki.find_page(params[:id])
247
    # page is nil when previewing a new page
248
    return render_403 unless page.nil? || editable?(page)
249
    if page
250
      @attachements = page.attachments
251
      @previewed = page.content
252
    end
253
    @text = params[:content][:text]
254
    render :partial => 'common/preview'
255
  end
256

    
257
  def add_attachment
258
    return render_403 unless editable?
259
    attachments = Attachment.attach_files(@page, params[:attachments])
260
    render_attachment_warning_if_needed(@page)
261
    redirect_to :action => 'show', :id => @page.title, :project_id => @project
262
  end
263

    
264
private
265

    
266
  def find_wiki
267
    @project = Project.find(params[:project_id])
268
    @wiki = @project.wiki
269
    render_404 unless @wiki
270
  rescue ActiveRecord::RecordNotFound
271
    render_404
272
  end
273

    
274
  # Finds the requested page or a new page if it doesn't exist
275
  def find_existing_or_new_page
276
    @page = @wiki.find_or_new_page(params[:id])
277
    if @wiki.page_found_with_redirect?
278
      redirect_to params.update(:id => @page.title)
279
    end
280
  end
281

    
282
  # Finds the requested page and returns a 404 error if it doesn't exist
283
  def find_existing_page
284
    @page = @wiki.find_page(params[:id])
285
    if @page.nil?
286
      render_404
287
      return
288
    end
289
    if @wiki.page_found_with_redirect?
290
      redirect_to params.update(:id => @page.title)
291
    end
292
  end
293

    
294
  # Returns true if the current user is allowed to edit the page, otherwise false
295
  def editable?(page = @page)
296
    page.editable_by?(User.current)
297
  end
298

    
299
  # Returns the default content of a new wiki page
300
  def initial_page_content(page)
301
    helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
302
    extend helper unless self.instance_of?(helper)
303
    helper.instance_method(:initial_page_content).bind(self).call(page)
304
  end
305

    
306
  def load_pages_for_index
307
    @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project})
308
  end
309
end