annotate .svn/pristine/20/2069c7a6894bd57d0791bf6d175776952425d0ca.svn-base @ 1295:622f24f53b42 redmine-2.3

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