annotate .svn/pristine/e2/e2daa7c7317910fcef5bddeb07c62bec3e3e95d8.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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