Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'diff' Chris@909: Chris@909: # The WikiController follows the Rails REST controller pattern but with Chris@909: # a few differences Chris@909: # Chris@909: # * index - shows a list of WikiPages grouped by page or date Chris@909: # * new - not used Chris@909: # * create - not used Chris@909: # * show - will also show the form for creating a new wiki page Chris@909: # * edit - used to edit an existing or new page Chris@909: # * update - used to save a wiki page update to the database, including new pages Chris@909: # * destroy - normal Chris@909: # Chris@909: # Other member and collection methods are also used Chris@909: # Chris@909: # TODO: still being worked on Chris@909: class WikiController < ApplicationController Chris@909: default_search_scope :wiki_pages Chris@909: before_filter :find_wiki, :authorize Chris@909: before_filter :find_existing_or_new_page, :only => [:show, :edit, :update] Chris@909: before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy] Chris@909: Chris@909: helper :attachments Chris@909: include AttachmentsHelper Chris@909: helper :watchers Chris@909: include Redmine::Export::PDF Chris@909: Chris@909: # List of pages, sorted alphabetically and by parent (hierarchy) Chris@909: def index Chris@909: load_pages_for_index Chris@909: @pages_by_parent_id = @pages.group_by(&:parent_id) Chris@909: end Chris@909: Chris@909: # List of page, by last update Chris@909: def date_index Chris@909: load_pages_for_index Chris@909: @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} Chris@909: end Chris@909: Chris@909: # display a page (in editing mode if it doesn't exist) Chris@909: def show Chris@909: if @page.new_record? Chris@909: if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? Chris@909: edit Chris@909: render :action => 'edit' Chris@909: else Chris@909: render_404 Chris@909: end Chris@909: return Chris@909: end Chris@909: if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project) Chris@909: # Redirects user to the current version if he's not allowed to view previous versions Chris@909: redirect_to :version => nil Chris@909: return Chris@909: end Chris@909: @content = @page.content_for_version(params[:version]) Chris@909: if User.current.allowed_to?(:export_wiki_pages, @project) Chris@909: if params[:format] == 'pdf' Chris@909: send_data(wiki_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf") Chris@909: return Chris@909: elsif params[:format] == 'html' Chris@909: export = render_to_string :action => 'export', :layout => false Chris@909: send_data(export, :type => 'text/html', :filename => "#{@page.title}.html") Chris@909: return Chris@909: elsif params[:format] == 'txt' Chris@909: send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt") Chris@909: return Chris@909: end Chris@909: end Chris@909: @editable = editable? Chris@909: @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) && Chris@909: @content.current_version? && Chris@909: Redmine::WikiFormatting.supports_section_edit? Chris@909: Chris@909: render :action => 'show' Chris@909: end Chris@909: Chris@909: # edit an existing page or a new one Chris@909: def edit Chris@909: return render_403 unless editable? Chris@909: @page.content = WikiContent.new(:page => @page) if @page.new_record? Chris@909: Chris@909: @content = @page.content_for_version(params[:version]) Chris@909: @content.text = initial_page_content(@page) if @content.text.blank? Chris@909: # don't keep previous comment Chris@909: @content.comments = nil Chris@909: Chris@909: # To prevent StaleObjectError exception when reverting to a previous version Chris@909: @content.version = @page.content.version Chris@909: Chris@909: @text = @content.text Chris@909: if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? Chris@909: @section = params[:section].to_i Chris@909: @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section) Chris@909: render_404 if @text.blank? Chris@909: end Chris@909: end Chris@909: Chris@909: verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } Chris@909: # Creates a new page or updates an existing one Chris@909: def update Chris@909: return render_403 unless editable? Chris@909: @page.content = WikiContent.new(:page => @page) if @page.new_record? Chris@909: Chris@909: @content = @page.content_for_version(params[:version]) Chris@909: @content.text = initial_page_content(@page) if @content.text.blank? Chris@909: # don't keep previous comment Chris@909: @content.comments = nil Chris@909: Chris@909: if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text] Chris@909: attachments = Attachment.attach_files(@page, params[:attachments]) Chris@909: render_attachment_warning_if_needed(@page) Chris@909: # don't save if text wasn't changed Chris@909: redirect_to :action => 'show', :project_id => @project, :id => @page.title Chris@909: return Chris@909: end Chris@909: Chris@909: @content.comments = params[:content][:comments] Chris@909: @text = params[:content][:text] Chris@909: if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? Chris@909: @section = params[:section].to_i Chris@909: @section_hash = params[:section_hash] Chris@909: @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash) Chris@909: else Chris@909: @content.version = params[:content][:version] Chris@909: @content.text = @text Chris@909: end Chris@909: @content.author = User.current Chris@909: # if page is new @page.save will also save content, but not if page isn't a new record Chris@909: if (@page.new_record? ? @page.save : @content.save) Chris@909: attachments = Attachment.attach_files(@page, params[:attachments]) Chris@909: render_attachment_warning_if_needed(@page) Chris@909: call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) Chris@909: redirect_to :action => 'show', :project_id => @project, :id => @page.title Chris@909: else Chris@909: render :action => 'edit' Chris@909: end Chris@909: Chris@909: rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError Chris@909: # Optimistic locking exception Chris@909: flash.now[:error] = l(:notice_locking_conflict) Chris@909: render :action => 'edit' Chris@909: end Chris@909: Chris@909: # rename a page Chris@909: def rename Chris@909: return render_403 unless editable? Chris@909: @page.redirect_existing_links = true Chris@909: # used to display the *original* title if some AR validation errors occur Chris@909: @original_title = @page.pretty_title Chris@909: if request.post? && @page.update_attributes(params[:wiki_page]) Chris@909: flash[:notice] = l(:notice_successful_update) Chris@909: redirect_to :action => 'show', :project_id => @project, :id => @page.title Chris@909: end Chris@909: end Chris@909: Chris@909: verify :method => :post, :only => :protect, :redirect_to => { :action => :show } Chris@909: def protect Chris@909: @page.update_attribute :protected, params[:protected] Chris@909: redirect_to :action => 'show', :project_id => @project, :id => @page.title Chris@909: end Chris@909: Chris@909: # show page history Chris@909: def history Chris@909: @version_count = @page.content.versions.count Chris@909: @version_pages = Paginator.new self, @version_count, per_page_option, params['p'] Chris@909: # don't load text Chris@909: @versions = @page.content.versions.find :all, Chris@909: :select => "id, author_id, comments, updated_on, version", Chris@909: :order => 'version DESC', Chris@909: :limit => @version_pages.items_per_page + 1, Chris@909: :offset => @version_pages.current.offset Chris@909: Chris@909: render :layout => false if request.xhr? Chris@909: end Chris@909: Chris@909: def diff Chris@909: @diff = @page.diff(params[:version], params[:version_from]) Chris@909: render_404 unless @diff Chris@909: end Chris@909: Chris@909: def annotate Chris@909: @annotate = @page.annotate(params[:version]) Chris@909: render_404 unless @annotate Chris@909: end Chris@909: Chris@909: verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show } Chris@909: # Removes a wiki page and its history Chris@909: # Children can be either set as root pages, removed or reassigned to another parent page Chris@909: def destroy Chris@909: return render_403 unless editable? Chris@909: Chris@909: @descendants_count = @page.descendants.size Chris@909: if @descendants_count > 0 Chris@909: case params[:todo] Chris@909: when 'nullify' Chris@909: # Nothing to do Chris@909: when 'destroy' Chris@909: # Removes all its descendants Chris@909: @page.descendants.each(&:destroy) Chris@909: when 'reassign' Chris@909: # Reassign children to another parent page Chris@909: reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i) Chris@909: return unless reassign_to Chris@909: @page.children.each do |child| Chris@909: child.update_attribute(:parent, reassign_to) Chris@909: end Chris@909: else Chris@909: @reassignable_to = @wiki.pages - @page.self_and_descendants Chris@909: return Chris@909: end Chris@909: end Chris@909: @page.destroy Chris@909: redirect_to :action => 'index', :project_id => @project Chris@909: end Chris@909: Chris@909: # Export wiki to a single html file Chris@909: def export Chris@909: if User.current.allowed_to?(:export_wiki_pages, @project) Chris@909: @pages = @wiki.pages.find :all, :order => 'title' Chris@909: export = render_to_string :action => 'export_multiple', :layout => false Chris@909: send_data(export, :type => 'text/html', :filename => "wiki.html") Chris@909: else Chris@909: redirect_to :action => 'show', :project_id => @project, :id => nil Chris@909: end Chris@909: end Chris@909: Chris@909: def preview Chris@909: page = @wiki.find_page(params[:id]) Chris@909: # page is nil when previewing a new page Chris@909: return render_403 unless page.nil? || editable?(page) Chris@909: if page Chris@909: @attachements = page.attachments Chris@909: @previewed = page.content Chris@909: end Chris@909: @text = params[:content][:text] Chris@909: render :partial => 'common/preview' Chris@909: end Chris@909: Chris@909: def add_attachment Chris@909: return render_403 unless editable? Chris@909: attachments = Attachment.attach_files(@page, params[:attachments]) Chris@909: render_attachment_warning_if_needed(@page) Chris@909: redirect_to :action => 'show', :id => @page.title, :project_id => @project Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: def find_wiki Chris@909: @project = Project.find(params[:project_id]) Chris@909: @wiki = @project.wiki Chris@909: render_404 unless @wiki Chris@909: rescue ActiveRecord::RecordNotFound Chris@909: render_404 Chris@909: end Chris@909: Chris@909: # Finds the requested page or a new page if it doesn't exist Chris@909: def find_existing_or_new_page Chris@909: @page = @wiki.find_or_new_page(params[:id]) Chris@909: if @wiki.page_found_with_redirect? Chris@909: redirect_to params.update(:id => @page.title) Chris@909: end Chris@909: end Chris@909: Chris@909: # Finds the requested page and returns a 404 error if it doesn't exist Chris@909: def find_existing_page Chris@909: @page = @wiki.find_page(params[:id]) Chris@909: if @page.nil? Chris@909: render_404 Chris@909: return Chris@909: end Chris@909: if @wiki.page_found_with_redirect? Chris@909: redirect_to params.update(:id => @page.title) Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns true if the current user is allowed to edit the page, otherwise false Chris@909: def editable?(page = @page) Chris@909: page.editable_by?(User.current) Chris@909: end Chris@909: Chris@909: # Returns the default content of a new wiki page Chris@909: def initial_page_content(page) Chris@909: helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) Chris@909: extend helper unless self.instance_of?(helper) Chris@909: helper.instance_method(:initial_page_content).bind(self).call(page) Chris@909: end Chris@909: Chris@909: def load_pages_for_index Chris@909: @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project}) Chris@909: end Chris@909: end