##// END OF EJS Templates
Remove the limitation on characters that can be used in custom_field, issue_status, role, tracker, user names (#5152)....
Remove the limitation on characters that can be used in custom_field, issue_status, role, tracker, user names (#5152). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4599 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r4315:4a6a551d074c
r4479:44ffc5a3365f
Show More
wiki_controller.rb
279 lines | 9.9 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
wiki branch merged into trunk...
r320 # redMine - project management software
# Copyright (C) 2006-2007 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
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Jean-Philippe Lang
Added wiki diff....
r580 require 'diff'
Eric Davis
Refactor: Rename WikiController#page_index to #index...
r4176 # The WikiController follows the Rails REST controller pattern but with
# a few differences
#
# * index - shows a list of WikiPages grouped by page or date
# * new - not used
# * create - not used
# * show - will also show the form for creating a new wiki page
# * edit - used to edit an existing or new page
# * update - used to save a wiki page update to the database, including new pages
# * destroy - normal
#
# Other member and collection methods are also used
#
# TODO: still being worked on
Jean-Philippe Lang
wiki branch merged into trunk...
r320 class WikiController < ApplicationController
Jean-Philippe Lang
Contextual quick search (#3263)....
r2829 default_search_scope :wiki_pages
Jean-Philippe Lang
Merged 0.6 branch into trunk....
r663 before_filter :find_wiki, :authorize
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy]
Jean-Philippe Lang
Added the ability to destroy wiki pages (content and its history are deleted from the database)....
r537
Eric Davis
Refactor: convert WikiController#destroy to use HTTP DELETE...
r4181 verify :method => :post, :only => [:protect], :redirect_to => { :action => :show }
Jean-Philippe Lang
Attachments can now be added to wiki pages (original patch by Pavol Murin). Only authorized users can add/delete attachments....
r538
helper :attachments
include AttachmentsHelper
Jean-Philippe Lang
Ability to watch a wiki or a single wiki page (#413)....
r2666 helper :watchers
Eric Davis
Refactor: rename WikiController#index to #show, it's a single resource action...
r4152
Eric Davis
Refactor: Rename WikiController#page_index to #index...
r4176 # List of pages, sorted alphabetically and by parent (hierarchy)
def index
load_pages_grouped_by_date_without_content
end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 # display a page (in editing mode if it doesn't exist)
Eric Davis
Refactor: rename WikiController#index to #show, it's a single resource action...
r4152 def show
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 page_title = params[:id]
Jean-Philippe Lang
wiki branch merged into trunk...
r320 @page = @wiki.find_or_new_page(page_title)
if @page.new_record?
Jean-Philippe Lang
Makes the wiki sidebar editable (#5208)....
r3518 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable?
Jean-Philippe Lang
Fixed: a user not authorized to edit wiki pages gets the edit form if the page doesn't exist. He now gets a 404....
r812 edit
render :action => 'edit'
else
render_404
end
return
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Jean-Philippe Lang
Adds a permission 'view wiki edits' so that wiki history can be hidden to certain users (#1154)....
r1813 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
# Redirects user to the current version if he's not allowed to view previous versions
redirect_to :version => nil
return
end
Jean-Philippe Lang
Added the ability to easily rollback to a previous version of a wiki page....
r421 @content = @page.content_for_version(params[:version])
Jean-Philippe Lang
Adds a permission for exporting wiki pages....
r3257 if User.current.allowed_to?(:export_wiki_pages, @project)
if params[:format] == 'html'
export = render_to_string :action => 'export', :layout => false
send_data(export, :type => 'text/html', :filename => "#{@page.title}.html")
return
elsif params[:format] == 'txt'
send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt")
return
end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Jean-Philippe Lang
Adds a permission for exporting wiki pages....
r3257 @editable = editable?
Jean-Philippe Lang
wiki branch merged into trunk...
r320 render :action => 'show'
end
# edit an existing page or a new one
def edit
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 @page = @wiki.find_or_new_page(params[:id])
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400 return render_403 unless editable?
Jean-Philippe Lang
wiki branch merged into trunk...
r320 @page.content = WikiContent.new(:page => @page) if @page.new_record?
Jean-Philippe Lang
Added the ability to easily rollback to a previous version of a wiki page....
r421
@content = @page.content_for_version(params[:version])
Jean-Philippe Lang
Makes wiki text formatter pluggable....
r1953 @content.text = initial_page_content(@page) if @content.text.blank?
Jean-Philippe Lang
wiki branch merged into trunk...
r320 # don't keep previous comment
Jean-Philippe Lang
Fixed: 10342 Creation of Schema in Oracle...
r476 @content.comments = nil
Eric Davis
Refactor: split WikiController#edit into #update...
r4158
# To prevent StaleObjectError exception when reverting to a previous version
@content.version = @page.content.version
rescue ActiveRecord::StaleObjectError
# Optimistic locking exception
flash[:error] = l(:notice_locking_conflict)
end
Eric Davis
Refactor: convert WikiController to a REST resource...
r4189 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
Eric Davis
Refactor: split WikiController#edit into #update...
r4158 # Creates a new page or updates an existing one
def update
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 @page = @wiki.find_or_new_page(params[:id])
Eric Davis
Refactor: split WikiController#edit into #update...
r4158 return render_403 unless editable?
@page.content = WikiContent.new(:page => @page) if @page.new_record?
@content = @page.content_for_version(params[:version])
@content.text = initial_page_content(@page) if @content.text.blank?
# don't keep previous comment
@content.comments = nil
if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text]
attachments = Attachment.attach_files(@page, params[:attachments])
render_attachment_warning_if_needed(@page)
# don't save if text wasn't changed
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 redirect_to :action => 'show', :project_id => @project, :id => @page.title
Eric Davis
Refactor: split WikiController#edit into #update...
r4158 return
end
@content.attributes = params[:content]
@content.author = User.current
# if page is new @page.save will also save content, but not if page isn't a new record
if (@page.new_record? ? @page.save : @content.save)
attachments = Attachment.attach_files(@page, params[:attachments])
render_attachment_warning_if_needed(@page)
call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 redirect_to :action => 'show', :project_id => @project, :id => @page.title
Jean-Philippe Lang
Fixed: Missing template wiki/update.erb error introduced in r4272 (#6987)....
r4315 else
render :action => 'edit'
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Eric Davis
Refactor: split WikiController#edit into #update...
r4158
Jean-Philippe Lang
Optimistic locking added for wiki edits....
r542 rescue ActiveRecord::StaleObjectError
# Optimistic locking exception
Jean-Philippe Lang
Applied the flash notices patch by Matt Jones (slightly edited)....
r597 flash[:error] = l(:notice_locking_conflict)
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Eric Davis
Refactor: split WikiController#edit into #update...
r4158
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 # rename a page
def rename
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 return render_403 unless editable?
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 @page.redirect_existing_links = true
# used to display the *original* title if some AR validation errors occur
@original_title = @page.pretty_title
if request.post? && @page.update_attributes(params[:wiki_page])
flash[:notice] = l(:notice_successful_update)
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 redirect_to :action => 'show', :project_id => @project, :id => @page.title
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 end
end
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400 def protect
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 @page.update_attribute :protected, params[:protected]
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 redirect_to :action => 'show', :project_id => @project, :id => @page.title
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400 end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 # show page history
def history
Jean-Philippe Lang
Added pagination on wiki page history....
r568 @version_count = @page.content.versions.count
Jean-Philippe Lang
New setting added to specify how many objects should be displayed on most paginated lists....
r1013 @version_pages = Paginator.new self, @version_count, per_page_option, params['p']
Jean-Philippe Lang
Added pagination on wiki page history....
r568 # don't load text
Jean-Philippe Lang
wiki branch merged into trunk...
r320 @versions = @page.content.versions.find :all,
Jean-Philippe Lang
Fixed: 10342 Creation of Schema in Oracle...
r476 :select => "id, author_id, comments, updated_on, version",
Jean-Philippe Lang
Added pagination on wiki page history....
r568 :order => 'version DESC',
Jean-Philippe Lang
Added wiki diff....
r580 :limit => @version_pages.items_per_page + 1,
Jean-Philippe Lang
Added pagination on wiki page history....
r568 :offset => @version_pages.current.offset
render :layout => false if request.xhr?
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Jean-Philippe Lang
Added the ability to destroy wiki pages (content and its history are deleted from the database)....
r537
Jean-Philippe Lang
Added wiki diff....
r580 def diff
@diff = @page.diff(params[:version], params[:version_from])
render_404 unless @diff
end
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007 def annotate
@annotate = @page.annotate(params[:version])
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 render_404 unless @annotate
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007 end
Eric Davis
Refactor: convert WikiController#destroy to use HTTP DELETE...
r4181
verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show }
Jean-Philippe Lang
Ask user what to do with child pages when deleting a parent wiki page (#3202)....
r2584 # Removes a wiki page and its history
# Children can be either set as root pages, removed or reassigned to another parent page
Jean-Philippe Lang
Added the ability to destroy wiki pages (content and its history are deleted from the database)....
r537 def destroy
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 return render_403 unless editable?
Jean-Philippe Lang
Ask user what to do with child pages when deleting a parent wiki page (#3202)....
r2584
@descendants_count = @page.descendants.size
if @descendants_count > 0
case params[:todo]
when 'nullify'
# Nothing to do
when 'destroy'
# Removes all its descendants
@page.descendants.each(&:destroy)
when 'reassign'
# Reassign children to another parent page
reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i)
return unless reassign_to
@page.children.each do |child|
child.update_attribute(:parent, reassign_to)
end
else
@reassignable_to = @wiki.pages - @page.self_and_descendants
return
end
end
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 @page.destroy
Eric Davis
Refactor: Rename WikiController#page_index to #index...
r4176 redirect_to :action => 'index', :project_id => @project
Jean-Philippe Lang
Added the ability to destroy wiki pages (content and its history are deleted from the database)....
r537 end
Jean-Philippe Lang
wiki branch merged into trunk...
r320
Eric Davis
Refactor: extract method in WikiController#special to create a new #export method...
r4137 # Export wiki to a single html file
def export
if User.current.allowed_to?(:export_wiki_pages, @project)
@pages = @wiki.pages.find :all, :order => 'title'
export = render_to_string :action => 'export_multiple', :layout => false
send_data(export, :type => 'text/html', :filename => "wiki.html")
else
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 redirect_to :action => 'show', :project_id => @project, :id => nil
Eric Davis
Refactor: extract method in WikiController#special to create a new #export method...
r4137 end
end
Eric Davis
Refactor: extract #page_index from WikiController#special...
r4139
Eric Davis
Refactor: extract method from WikiController#special...
r4147 def date_index
load_pages_grouped_by_date_without_content
end
Jean-Philippe Lang
wiki branch merged into trunk...
r320
def preview
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 page = @wiki.find_page(params[:id])
Jean-Philippe Lang
Fixed: error when previewing a new wiki page (#1292) introduced in r1415....
r1431 # page is nil when previewing a new page
return render_403 unless page.nil? || editable?(page)
Jean-Philippe Lang
Adds child_pages macro for wiki pages (#528)....
r1690 if page
@attachements = page.attachments
@previewed = page.content
end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 @text = params[:content][:text]
Jean-Philippe Lang
Added preview on add/edit issue form....
r801 render :partial => 'common/preview'
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Jean-Philippe Lang
Attachments can now be added to wiki pages (original patch by Pavol Murin). Only authorized users can add/delete attachments....
r538 def add_attachment
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400 return render_403 unless editable?
Eric Davis
Refactor: Moved ApplicationController#attach_files to the Attachment model...
r3409 attachments = Attachment.attach_files(@page, params[:attachments])
Eric Davis
Refactor: Decouple failed attachments and the flash messages...
r3414 render_attachment_warning_if_needed(@page)
Eric Davis
Refactor: convert WikiController to a REST resource...
r4189 redirect_to :action => 'show', :id => @page.title, :project_id => @project
Jean-Philippe Lang
Attachments can now be added to wiki pages (original patch by Pavol Murin). Only authorized users can add/delete attachments....
r538 end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 private
def find_wiki
Eric Davis
Refactor: change :id on WikiController to use :project_id...
r4151 @project = Project.find(params[:project_id])
Jean-Philippe Lang
wiki branch merged into trunk...
r320 @wiki = @project.wiki
Jean-Philippe Lang
A wiki page can now be attached to each version....
r561 render_404 unless @wiki
Jean-Philippe Lang
wiki branch merged into trunk...
r320 rescue ActiveRecord::RecordNotFound
render_404
end
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 # Finds the requested page and returns a 404 error if it doesn't exist
def find_existing_page
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 @page = @wiki.find_page(params[:id])
Jean-Philippe Lang
Check that wiki page exists before processing (#2360)....
r2143 render_404 if @page.nil?
end
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400 # Returns true if the current user is allowed to edit the page, otherwise false
def editable?(page = @page)
page.editable_by?(User.current)
end
Jean-Philippe Lang
Makes wiki text formatter pluggable....
r1953
# Returns the default content of a new wiki page
def initial_page_content(page)
helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
extend helper unless self.instance_of?(helper)
helper.instance_method(:initial_page_content).bind(self).call(page)
end
Eric Davis
Refactor: extract finder to a utility method...
r4138
# eager load information about last updates, without loading text
def load_pages_grouped_by_date_without_content
@pages = @wiki.pages.find :all, :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
:joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id",
:order => 'title'
@pages_by_date = @pages.group_by {|p| p.updated_on.to_date}
@pages_by_parent_id = @pages.group_by(&:parent_id)
end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end