Chris@441: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 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@441: # 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@441: # 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@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@441: before_filter :find_existing_or_new_page, :only => [:show, :edit, :update] Chris@1115: before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version] Chris@1115: accept_api_auth :index, :show, :update, :destroy Chris@1464: before_filter :find_attachments, :only => [:preview] Chris@0: Chris@0: helper :attachments Chris@441: include AttachmentsHelper Chris@0: helper :watchers Chris@909: include Redmine::Export::PDF chris@37: chris@37: # List of pages, sorted alphabetically and by parent (hierarchy) chris@37: def index Chris@441: load_pages_for_index Chris@1115: Chris@1115: respond_to do |format| Chris@1115: format.html { Chris@1115: @pages_by_parent_id = @pages.group_by(&:parent_id) Chris@1115: } Chris@1115: format.api Chris@1115: end Chris@441: end Chris@441: Chris@441: # List of page, by last update Chris@441: def date_index Chris@441: load_pages_for_index Chris@441: @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} chris@37: end chris@37: Chris@0: # display a page (in editing mode if it doesn't exist) chris@37: def show Chris@1464: if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project) Chris@1464: deny_access Chris@1464: return Chris@1464: end Chris@1464: @content = @page.content_for_version(params[:version]) Chris@1464: if @content.nil? Chris@1115: if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request? 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 User.current.allowed_to?(:export_wiki_pages, @project) Chris@909: if params[:format] == 'pdf' Chris@1115: send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf") Chris@909: return Chris@909: elsif 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@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@1115: respond_to do |format| Chris@1115: format.html Chris@1115: format.api Chris@1115: end Chris@0: end Chris@441: Chris@0: # edit an existing page or a new one Chris@0: def edit Chris@0: return render_403 unless editable? Chris@1115: if @page.new_record? Chris@1115: if params[:parent].present? Chris@1115: @page.parent = @page.wiki.find_page(params[:parent].to_s) Chris@1115: end Chris@1115: end Chris@441: Chris@0: @content = @page.content_for_version(params[:version]) Chris@1464: @content ||= WikiContent.new(:page => @page) 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@1464: @content.version = @page.content.version if @page.content Chris@1115: 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@0: end chris@37: chris@37: # Creates a new page or updates an existing one chris@37: def update chris@37: return render_403 unless editable? Chris@1115: was_new_page = @page.new_record? Chris@1115: @page.safe_attributes = params[:wiki_page] Chris@441: Chris@1464: @content = @page.content || WikiContent.new(:page => @page) Chris@1115: content_params = params[:content] Chris@1115: if content_params.nil? && params[:wiki_page].is_a?(Hash) Chris@1115: content_params = params[:wiki_page].slice(:text, :comments, :version) Chris@1115: end Chris@1115: content_params ||= {} chris@37: Chris@1115: @content.comments = content_params[:comments] Chris@1115: @text = content_params[: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@1464: @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(@section, @text, @section_hash) Chris@909: else Chris@1115: @content.version = content_params[:version] if content_params[:version] Chris@909: @content.text = @text Chris@909: end chris@37: @content.author = User.current Chris@1115: Chris@1464: if @page.save_with_content(@content) 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@1115: Chris@1115: respond_to do |format| Chris@1464: format.html { Chris@1464: anchor = @section ? "section-#{@section}" : nil Chris@1464: redirect_to project_wiki_page_path(@project, @page.title, :anchor => anchor) Chris@1464: } Chris@1115: format.api { Chris@1115: if was_new_page Chris@1464: render :action => 'show', :status => :created, :location => project_wiki_page_path(@project, @page.title) Chris@1115: else Chris@1115: render_api_ok Chris@1115: end Chris@1115: } Chris@1115: end Chris@119: else Chris@1115: respond_to do |format| Chris@1115: format.html { render :action => 'edit' } Chris@1115: format.api { render_validation_errors(@content) } Chris@1115: end chris@37: end chris@37: Chris@909: rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError chris@37: # Optimistic locking exception Chris@1115: respond_to do |format| Chris@1115: format.html { Chris@1115: flash.now[:error] = l(:notice_locking_conflict) Chris@1115: render :action => 'edit' Chris@1115: } Chris@1115: format.api { render_api_head :conflict } Chris@1115: end Chris@1115: rescue ActiveRecord::RecordNotSaved Chris@1115: respond_to do |format| Chris@1115: format.html { render :action => 'edit' } Chris@1115: format.api { render_validation_errors(@content) } Chris@1115: end 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@1464: redirect_to project_wiki_page_path(@project, @page.title) Chris@0: end Chris@0: end Chris@441: Chris@0: def protect Chris@0: @page.update_attribute :protected, params[:protected] Chris@1464: redirect_to project_wiki_page_path(@project, @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@1464: @version_pages = Paginator.new @version_count, per_page_option, params['page'] Chris@441: # don't load text Chris@1464: @versions = @page.content.versions. Chris@1464: select("id, author_id, comments, updated_on, version"). Chris@1464: reorder('version DESC'). Chris@1464: limit(@version_pages.per_page + 1). Chris@1464: offset(@version_pages.offset). Chris@1464: all Chris@0: Chris@0: render :layout => false if request.xhr? Chris@0: end Chris@441: Chris@0: def diff Chris@0: @diff = @page.diff(params[:version], params[:version_from]) Chris@0: render_404 unless @diff Chris@0: end Chris@441: Chris@0: def annotate Chris@0: @annotate = @page.annotate(params[:version]) Chris@0: render_404 unless @annotate Chris@0: end chris@37: 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@441: 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@1115: # display the destroy form if it's a user request Chris@1115: return unless api_request? Chris@0: end Chris@0: end Chris@0: @page.destroy Chris@1115: respond_to do |format| Chris@1464: format.html { redirect_to project_wiki_index_path(@project) } Chris@1115: format.api { render_api_ok } Chris@1115: end Chris@0: end Chris@0: Chris@1115: def destroy_version Chris@1115: return render_403 unless editable? Chris@1115: Chris@1115: @content = @page.content_for_version(params[:version]) Chris@1115: @content.destroy Chris@1464: redirect_to_referer_or history_project_wiki_page_path(@project, @page.title) Chris@1115: end Chris@1115: Chris@1115: # Export wiki to a single pdf or html file chris@37: def export Chris@1517: @pages = @wiki.pages. Chris@1517: order('title'). Chris@1517: includes([:content, {:attachments => :author}]). Chris@1517: all Chris@1115: respond_to do |format| Chris@1115: format.html { Chris@1115: export = render_to_string :action => 'export_multiple', :layout => false Chris@1115: send_data(export, :type => 'text/html', :filename => "wiki.html") Chris@1115: } Chris@1115: format.pdf { Chris@1517: send_data(wiki_pages_to_pdf(@pages, @project), Chris@1517: :type => 'application/pdf', Chris@1517: :filename => "#{@project.identifier}.pdf") Chris@1115: } Chris@0: end chris@37: end chris@37: 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@1464: @attachments += 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@441: 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@441: Chris@441: # Finds the requested page or a new page if it doesn't exist Chris@441: def find_existing_or_new_page Chris@441: @page = @wiki.find_or_new_page(params[:id]) Chris@441: if @wiki.page_found_with_redirect? Chris@441: redirect_to params.update(:id => @page.title) Chris@441: end Chris@441: end Chris@441: 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@441: if @page.nil? Chris@441: render_404 Chris@441: return Chris@441: end Chris@441: if @wiki.page_found_with_redirect? Chris@441: redirect_to params.update(:id => @page.title) Chris@441: end Chris@0: end Chris@441: 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@441: def load_pages_for_index Chris@1517: @pages = @wiki.pages.with_updated_on. Chris@1517: reorder("#{WikiPage.table_name}.title"). Chris@1517: includes(:wiki => :project). Chris@1517: includes(:parent). Chris@1517: all chris@37: end Chris@0: end