annotate .svn/pristine/17/1786faee3d0cf0b377ade4249d7c22ac019c3500.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 require 'diff'
Chris@1464 19
Chris@1464 20 # The WikiController follows the Rails REST controller pattern but with
Chris@1464 21 # a few differences
Chris@1464 22 #
Chris@1464 23 # * index - shows a list of WikiPages grouped by page or date
Chris@1464 24 # * new - not used
Chris@1464 25 # * create - not used
Chris@1464 26 # * show - will also show the form for creating a new wiki page
Chris@1464 27 # * edit - used to edit an existing or new page
Chris@1464 28 # * update - used to save a wiki page update to the database, including new pages
Chris@1464 29 # * destroy - normal
Chris@1464 30 #
Chris@1464 31 # Other member and collection methods are also used
Chris@1464 32 #
Chris@1464 33 # TODO: still being worked on
Chris@1464 34 class WikiController < ApplicationController
Chris@1464 35 default_search_scope :wiki_pages
Chris@1464 36 before_filter :find_wiki, :authorize
Chris@1464 37 before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
Chris@1464 38 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version]
Chris@1464 39 accept_api_auth :index, :show, :update, :destroy
Chris@1464 40 before_filter :find_attachments, :only => [:preview]
Chris@1464 41
Chris@1464 42 helper :attachments
Chris@1464 43 include AttachmentsHelper
Chris@1464 44 helper :watchers
Chris@1464 45 include Redmine::Export::PDF
Chris@1464 46
Chris@1464 47 # List of pages, sorted alphabetically and by parent (hierarchy)
Chris@1464 48 def index
Chris@1464 49 load_pages_for_index
Chris@1464 50
Chris@1464 51 respond_to do |format|
Chris@1464 52 format.html {
Chris@1464 53 @pages_by_parent_id = @pages.group_by(&:parent_id)
Chris@1464 54 }
Chris@1464 55 format.api
Chris@1464 56 end
Chris@1464 57 end
Chris@1464 58
Chris@1464 59 # List of page, by last update
Chris@1464 60 def date_index
Chris@1464 61 load_pages_for_index
Chris@1464 62 @pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
Chris@1464 63 end
Chris@1464 64
Chris@1464 65 # display a page (in editing mode if it doesn't exist)
Chris@1464 66 def show
Chris@1464 67 if @page.new_record?
Chris@1464 68 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
Chris@1464 69 edit
Chris@1464 70 render :action => 'edit'
Chris@1464 71 else
Chris@1464 72 render_404
Chris@1464 73 end
Chris@1464 74 return
Chris@1464 75 end
Chris@1464 76 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
Chris@1464 77 deny_access
Chris@1464 78 return
Chris@1464 79 end
Chris@1464 80 @content = @page.content_for_version(params[:version])
Chris@1464 81 if User.current.allowed_to?(:export_wiki_pages, @project)
Chris@1464 82 if params[:format] == 'pdf'
Chris@1464 83 send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
Chris@1464 84 return
Chris@1464 85 elsif params[:format] == 'html'
Chris@1464 86 export = render_to_string :action => 'export', :layout => false
Chris@1464 87 send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
Chris@1464 88 return
Chris@1464 89 elsif params[:format] == 'txt'
Chris@1464 90 send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
Chris@1464 91 return
Chris@1464 92 end
Chris@1464 93 end
Chris@1464 94 @editable = editable?
Chris@1464 95 @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) &&
Chris@1464 96 @content.current_version? &&
Chris@1464 97 Redmine::WikiFormatting.supports_section_edit?
Chris@1464 98
Chris@1464 99 respond_to do |format|
Chris@1464 100 format.html
Chris@1464 101 format.api
Chris@1464 102 end
Chris@1464 103 end
Chris@1464 104
Chris@1464 105 # edit an existing page or a new one
Chris@1464 106 def edit
Chris@1464 107 return render_403 unless editable?
Chris@1464 108 if @page.new_record?
Chris@1464 109 @page.content = WikiContent.new(:page => @page)
Chris@1464 110 if params[:parent].present?
Chris@1464 111 @page.parent = @page.wiki.find_page(params[:parent].to_s)
Chris@1464 112 end
Chris@1464 113 end
Chris@1464 114
Chris@1464 115 @content = @page.content_for_version(params[:version])
Chris@1464 116 @content.text = initial_page_content(@page) if @content.text.blank?
Chris@1464 117 # don't keep previous comment
Chris@1464 118 @content.comments = nil
Chris@1464 119
Chris@1464 120 # To prevent StaleObjectError exception when reverting to a previous version
Chris@1464 121 @content.version = @page.content.version
Chris@1464 122
Chris@1464 123 @text = @content.text
Chris@1464 124 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
Chris@1464 125 @section = params[:section].to_i
Chris@1464 126 @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section)
Chris@1464 127 render_404 if @text.blank?
Chris@1464 128 end
Chris@1464 129 end
Chris@1464 130
Chris@1464 131 # Creates a new page or updates an existing one
Chris@1464 132 def update
Chris@1464 133 return render_403 unless editable?
Chris@1464 134 was_new_page = @page.new_record?
Chris@1464 135 @page.content = WikiContent.new(:page => @page) if @page.new_record?
Chris@1464 136 @page.safe_attributes = params[:wiki_page]
Chris@1464 137
Chris@1464 138 @content = @page.content
Chris@1464 139 content_params = params[:content]
Chris@1464 140 if content_params.nil? && params[:wiki_page].is_a?(Hash)
Chris@1464 141 content_params = params[:wiki_page].slice(:text, :comments, :version)
Chris@1464 142 end
Chris@1464 143 content_params ||= {}
Chris@1464 144
Chris@1464 145 @content.comments = content_params[:comments]
Chris@1464 146 @text = content_params[:text]
Chris@1464 147 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
Chris@1464 148 @section = params[:section].to_i
Chris@1464 149 @section_hash = params[:section_hash]
Chris@1464 150 @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
Chris@1464 151 else
Chris@1464 152 @content.version = content_params[:version] if content_params[:version]
Chris@1464 153 @content.text = @text
Chris@1464 154 end
Chris@1464 155 @content.author = User.current
Chris@1464 156
Chris@1464 157 if @page.save_with_content
Chris@1464 158 attachments = Attachment.attach_files(@page, params[:attachments])
Chris@1464 159 render_attachment_warning_if_needed(@page)
Chris@1464 160 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
Chris@1464 161
Chris@1464 162 respond_to do |format|
Chris@1464 163 format.html { redirect_to project_wiki_page_path(@project, @page.title) }
Chris@1464 164 format.api {
Chris@1464 165 if was_new_page
Chris@1464 166 render :action => 'show', :status => :created, :location => project_wiki_page_path(@project, @page.title)
Chris@1464 167 else
Chris@1464 168 render_api_ok
Chris@1464 169 end
Chris@1464 170 }
Chris@1464 171 end
Chris@1464 172 else
Chris@1464 173 respond_to do |format|
Chris@1464 174 format.html { render :action => 'edit' }
Chris@1464 175 format.api { render_validation_errors(@content) }
Chris@1464 176 end
Chris@1464 177 end
Chris@1464 178
Chris@1464 179 rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError
Chris@1464 180 # Optimistic locking exception
Chris@1464 181 respond_to do |format|
Chris@1464 182 format.html {
Chris@1464 183 flash.now[:error] = l(:notice_locking_conflict)
Chris@1464 184 render :action => 'edit'
Chris@1464 185 }
Chris@1464 186 format.api { render_api_head :conflict }
Chris@1464 187 end
Chris@1464 188 rescue ActiveRecord::RecordNotSaved
Chris@1464 189 respond_to do |format|
Chris@1464 190 format.html { render :action => 'edit' }
Chris@1464 191 format.api { render_validation_errors(@content) }
Chris@1464 192 end
Chris@1464 193 end
Chris@1464 194
Chris@1464 195 # rename a page
Chris@1464 196 def rename
Chris@1464 197 return render_403 unless editable?
Chris@1464 198 @page.redirect_existing_links = true
Chris@1464 199 # used to display the *original* title if some AR validation errors occur
Chris@1464 200 @original_title = @page.pretty_title
Chris@1464 201 if request.post? && @page.update_attributes(params[:wiki_page])
Chris@1464 202 flash[:notice] = l(:notice_successful_update)
Chris@1464 203 redirect_to project_wiki_page_path(@project, @page.title)
Chris@1464 204 end
Chris@1464 205 end
Chris@1464 206
Chris@1464 207 def protect
Chris@1464 208 @page.update_attribute :protected, params[:protected]
Chris@1464 209 redirect_to project_wiki_page_path(@project, @page.title)
Chris@1464 210 end
Chris@1464 211
Chris@1464 212 # show page history
Chris@1464 213 def history
Chris@1464 214 @version_count = @page.content.versions.count
Chris@1464 215 @version_pages = Paginator.new @version_count, per_page_option, params['page']
Chris@1464 216 # don't load text
Chris@1464 217 @versions = @page.content.versions.
Chris@1464 218 select("id, author_id, comments, updated_on, version").
Chris@1464 219 reorder('version DESC').
Chris@1464 220 limit(@version_pages.per_page + 1).
Chris@1464 221 offset(@version_pages.offset).
Chris@1464 222 all
Chris@1464 223
Chris@1464 224 render :layout => false if request.xhr?
Chris@1464 225 end
Chris@1464 226
Chris@1464 227 def diff
Chris@1464 228 @diff = @page.diff(params[:version], params[:version_from])
Chris@1464 229 render_404 unless @diff
Chris@1464 230 end
Chris@1464 231
Chris@1464 232 def annotate
Chris@1464 233 @annotate = @page.annotate(params[:version])
Chris@1464 234 render_404 unless @annotate
Chris@1464 235 end
Chris@1464 236
Chris@1464 237 # Removes a wiki page and its history
Chris@1464 238 # Children can be either set as root pages, removed or reassigned to another parent page
Chris@1464 239 def destroy
Chris@1464 240 return render_403 unless editable?
Chris@1464 241
Chris@1464 242 @descendants_count = @page.descendants.size
Chris@1464 243 if @descendants_count > 0
Chris@1464 244 case params[:todo]
Chris@1464 245 when 'nullify'
Chris@1464 246 # Nothing to do
Chris@1464 247 when 'destroy'
Chris@1464 248 # Removes all its descendants
Chris@1464 249 @page.descendants.each(&:destroy)
Chris@1464 250 when 'reassign'
Chris@1464 251 # Reassign children to another parent page
Chris@1464 252 reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
Chris@1464 253 return unless reassign_to
Chris@1464 254 @page.children.each do |child|
Chris@1464 255 child.update_attribute(:parent, reassign_to)
Chris@1464 256 end
Chris@1464 257 else
Chris@1464 258 @reassignable_to = @wiki.pages - @page.self_and_descendants
Chris@1464 259 # display the destroy form if it's a user request
Chris@1464 260 return unless api_request?
Chris@1464 261 end
Chris@1464 262 end
Chris@1464 263 @page.destroy
Chris@1464 264 respond_to do |format|
Chris@1464 265 format.html { redirect_to project_wiki_index_path(@project) }
Chris@1464 266 format.api { render_api_ok }
Chris@1464 267 end
Chris@1464 268 end
Chris@1464 269
Chris@1464 270 def destroy_version
Chris@1464 271 return render_403 unless editable?
Chris@1464 272
Chris@1464 273 @content = @page.content_for_version(params[:version])
Chris@1464 274 @content.destroy
Chris@1464 275 redirect_to_referer_or history_project_wiki_page_path(@project, @page.title)
Chris@1464 276 end
Chris@1464 277
Chris@1464 278 # Export wiki to a single pdf or html file
Chris@1464 279 def export
Chris@1464 280 @pages = @wiki.pages.all(:order => 'title', :include => [:content, {:attachments => :author}])
Chris@1464 281 respond_to do |format|
Chris@1464 282 format.html {
Chris@1464 283 export = render_to_string :action => 'export_multiple', :layout => false
Chris@1464 284 send_data(export, :type => 'text/html', :filename => "wiki.html")
Chris@1464 285 }
Chris@1464 286 format.pdf {
Chris@1464 287 send_data(wiki_pages_to_pdf(@pages, @project), :type => 'application/pdf', :filename => "#{@project.identifier}.pdf")
Chris@1464 288 }
Chris@1464 289 end
Chris@1464 290 end
Chris@1464 291
Chris@1464 292 def preview
Chris@1464 293 page = @wiki.find_page(params[:id])
Chris@1464 294 # page is nil when previewing a new page
Chris@1464 295 return render_403 unless page.nil? || editable?(page)
Chris@1464 296 if page
Chris@1464 297 @attachments += page.attachments
Chris@1464 298 @previewed = page.content
Chris@1464 299 end
Chris@1464 300 @text = params[:content][:text]
Chris@1464 301 render :partial => 'common/preview'
Chris@1464 302 end
Chris@1464 303
Chris@1464 304 def add_attachment
Chris@1464 305 return render_403 unless editable?
Chris@1464 306 attachments = Attachment.attach_files(@page, params[:attachments])
Chris@1464 307 render_attachment_warning_if_needed(@page)
Chris@1464 308 redirect_to :action => 'show', :id => @page.title, :project_id => @project
Chris@1464 309 end
Chris@1464 310
Chris@1464 311 private
Chris@1464 312
Chris@1464 313 def find_wiki
Chris@1464 314 @project = Project.find(params[:project_id])
Chris@1464 315 @wiki = @project.wiki
Chris@1464 316 render_404 unless @wiki
Chris@1464 317 rescue ActiveRecord::RecordNotFound
Chris@1464 318 render_404
Chris@1464 319 end
Chris@1464 320
Chris@1464 321 # Finds the requested page or a new page if it doesn't exist
Chris@1464 322 def find_existing_or_new_page
Chris@1464 323 @page = @wiki.find_or_new_page(params[:id])
Chris@1464 324 if @wiki.page_found_with_redirect?
Chris@1464 325 redirect_to params.update(:id => @page.title)
Chris@1464 326 end
Chris@1464 327 end
Chris@1464 328
Chris@1464 329 # Finds the requested page and returns a 404 error if it doesn't exist
Chris@1464 330 def find_existing_page
Chris@1464 331 @page = @wiki.find_page(params[:id])
Chris@1464 332 if @page.nil?
Chris@1464 333 render_404
Chris@1464 334 return
Chris@1464 335 end
Chris@1464 336 if @wiki.page_found_with_redirect?
Chris@1464 337 redirect_to params.update(:id => @page.title)
Chris@1464 338 end
Chris@1464 339 end
Chris@1464 340
Chris@1464 341 # Returns true if the current user is allowed to edit the page, otherwise false
Chris@1464 342 def editable?(page = @page)
Chris@1464 343 page.editable_by?(User.current)
Chris@1464 344 end
Chris@1464 345
Chris@1464 346 # Returns the default content of a new wiki page
Chris@1464 347 def initial_page_content(page)
Chris@1464 348 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
Chris@1464 349 extend helper unless self.instance_of?(helper)
Chris@1464 350 helper.instance_method(:initial_page_content).bind(self).call(page)
Chris@1464 351 end
Chris@1464 352
Chris@1464 353 def load_pages_for_index
Chris@1464 354 @pages = @wiki.pages.with_updated_on.reorder("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all
Chris@1464 355 end
Chris@1464 356 end