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