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