diff app/controllers/wiki_controller.rb @ 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 433d4f72a19b
children e248c7af89ec
line wrap: on
line diff
--- a/app/controllers/wiki_controller.rb	Fri Jun 14 09:05:06 2013 +0100
+++ b/app/controllers/wiki_controller.rb	Tue Jan 14 14:37:42 2014 +0000
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2012  Jean-Philippe Lang
+# Copyright (C) 2006-2013  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -15,8 +15,6 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
-require 'diff'
-
 # The WikiController follows the Rails REST controller pattern but with
 # a few differences
 #
@@ -37,6 +35,7 @@
   before_filter :find_existing_or_new_page, :only => [:show, :edit, :update]
   before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version]
   accept_api_auth :index, :show, :update, :destroy
+  before_filter :find_attachments, :only => [:preview]
 
   helper :attachments
   include AttachmentsHelper
@@ -63,7 +62,12 @@
 
   # display a page (in editing mode if it doesn't exist)
   def show
-    if @page.new_record?
+    if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
+      deny_access
+      return
+    end
+    @content = @page.content_for_version(params[:version])
+    if @content.nil?
       if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
         edit
         render :action => 'edit'
@@ -72,11 +76,6 @@
       end
       return
     end
-    if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
-      deny_access
-      return
-    end
-    @content = @page.content_for_version(params[:version])
     if User.current.allowed_to?(:export_wiki_pages, @project)
       if params[:format] == 'pdf'
         send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
@@ -105,19 +104,19 @@
   def edit
     return render_403 unless editable?
     if @page.new_record?
-      @page.content = WikiContent.new(:page => @page)
       if params[:parent].present?
         @page.parent = @page.wiki.find_page(params[:parent].to_s)
       end
     end
 
     @content = @page.content_for_version(params[:version])
+    @content ||= WikiContent.new(:page => @page)
     @content.text = initial_page_content(@page) if @content.text.blank?
     # don't keep previous comment
     @content.comments = nil
 
     # To prevent StaleObjectError exception when reverting to a previous version
-    @content.version = @page.content.version
+    @content.version = @page.content.version if @page.content
 
     @text = @content.text
     if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
@@ -131,10 +130,9 @@
   def update
     return render_403 unless editable?
     was_new_page = @page.new_record?
-    @page.content = WikiContent.new(:page => @page) if @page.new_record?
     @page.safe_attributes = params[:wiki_page]
 
-    @content = @page.content
+    @content = @page.content || WikiContent.new(:page => @page)
     content_params = params[:content]
     if content_params.nil? && params[:wiki_page].is_a?(Hash)
       content_params = params[:wiki_page].slice(:text, :comments, :version)
@@ -146,23 +144,26 @@
     if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
       @section = params[:section].to_i
       @section_hash = params[:section_hash]
-      @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash)
+      @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(@section, @text, @section_hash)
     else
       @content.version = content_params[:version] if content_params[:version]
       @content.text = @text
     end
     @content.author = User.current
 
-    if @page.save_with_content
+    if @page.save_with_content(@content)
       attachments = Attachment.attach_files(@page, params[:attachments])
       render_attachment_warning_if_needed(@page)
       call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
 
       respond_to do |format|
-        format.html { redirect_to :action => 'show', :project_id => @project, :id => @page.title }
+        format.html {
+          anchor = @section ? "section-#{@section}" : nil
+          redirect_to project_wiki_page_path(@project, @page.title, :anchor => anchor)
+        }
         format.api {
           if was_new_page
-            render :action => 'show', :status => :created, :location => url_for(:controller => 'wiki', :action => 'show', :project_id => @project, :id => @page.title)
+            render :action => 'show', :status => :created, :location => project_wiki_page_path(@project, @page.title)
           else
             render_api_ok
           end
@@ -199,25 +200,26 @@
     @original_title = @page.pretty_title
     if request.post? && @page.update_attributes(params[:wiki_page])
       flash[:notice] = l(:notice_successful_update)
-      redirect_to :action => 'show', :project_id => @project, :id => @page.title
+      redirect_to project_wiki_page_path(@project, @page.title)
     end
   end
 
   def protect
     @page.update_attribute :protected, params[:protected]
-    redirect_to :action => 'show', :project_id => @project, :id => @page.title
+    redirect_to project_wiki_page_path(@project, @page.title)
   end
 
   # show page history
   def history
     @version_count = @page.content.versions.count
-    @version_pages = Paginator.new self, @version_count, per_page_option, params['page']
+    @version_pages = Paginator.new @version_count, per_page_option, params['page']
     # don't load text
-    @versions = @page.content.versions.find :all,
-                                            :select => "id, author_id, comments, updated_on, version",
-                                            :order => 'version DESC',
-                                            :limit  =>  @version_pages.items_per_page + 1,
-                                            :offset =>  @version_pages.current.offset
+    @versions = @page.content.versions.
+      select("id, author_id, comments, updated_on, version").
+      reorder('version DESC').
+      limit(@version_pages.per_page + 1).
+      offset(@version_pages.offset).
+      all
 
     render :layout => false if request.xhr?
   end
@@ -260,7 +262,7 @@
     end
     @page.destroy
     respond_to do |format|
-      format.html { redirect_to :action => 'index', :project_id => @project }
+      format.html { redirect_to project_wiki_index_path(@project) }
       format.api { render_api_ok }
     end
   end
@@ -270,7 +272,7 @@
 
     @content = @page.content_for_version(params[:version])
     @content.destroy
-    redirect_to_referer_or :action => 'history', :id => @page.title, :project_id => @project
+    redirect_to_referer_or history_project_wiki_page_path(@project, @page.title)
   end
 
   # Export wiki to a single pdf or html file
@@ -292,7 +294,7 @@
     # page is nil when previewing a new page
     return render_403 unless page.nil? || editable?(page)
     if page
-      @attachements = page.attachments
+      @attachments += page.attachments
       @previewed = page.content
     end
     @text = params[:content][:text]
@@ -349,6 +351,6 @@
   end
 
   def load_pages_for_index
-    @pages = @wiki.pages.with_updated_on.order("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all
+    @pages = @wiki.pages.with_updated_on.reorder("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all
   end
 end