annotate .svn/pristine/20/2069c7a6894bd57d0791bf6d175776952425d0ca.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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