annotate .svn/pristine/86/862ff7186dec5a0dff7f7729e38725b70d1ecb29.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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