##// END OF EJS Templates
Don't add the inclusion error when tracker is not set, the blank error is enough....
Don't add the inclusion error when tracker is not set, the blank error is enough. git-svn-id: http://svn.redmine.org/redmine/trunk@15492 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r14856:cda9c63d9c21
r15110:90d14b71b365
Show More
wiki_page.rb
297 lines | 9.2 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
Ability to watch a wiki or a single wiki page (#413)....
r2666 # Redmine - project management software
Jean-Philippe Lang
Updates copyright for 2016....
r14856 # Copyright (C) 2006-2016 Jean-Philippe Lang
Jean-Philippe Lang
wiki branch merged into trunk...
r320 #
# 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.
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391 #
Jean-Philippe Lang
wiki branch merged into trunk...
r320 # 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.
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391 #
Jean-Philippe Lang
wiki branch merged into trunk...
r320 # 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'
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007 require 'enumerator'
Jean-Philippe Lang
Added wiki diff....
r580
Jean-Philippe Lang
wiki branch merged into trunk...
r320 class WikiPage < ActiveRecord::Base
Jean-Philippe Lang
Ability to edit a wiki page's parent on the edit page (#6449)....
r8667 include Redmine::SafeAttributes
Jean-Philippe Lang
wiki branch merged into trunk...
r320 belongs_to :wiki
has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
Jean-Philippe Lang
Replaces a piggy back query with an association for loading wiki pages updates....
r14243 has_one :content_without_text, lambda {without_text.readonly}, :class_name => 'WikiContent', :foreign_key => 'page_id'
Jean-Philippe Lang
AttachmentsController now handles attachments deletion....
r2114 acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
Jean-Philippe Lang
Ask user what to do with child pages when deleting a parent wiki page (#3202)....
r2584 acts_as_tree :dependent => :nullify, :order => 'title'
Jean-Philippe Lang
Ability to watch a wiki or a single wiki page (#413)....
r2666
acts_as_watchable
Jean-Philippe Lang
Search engines now supports pagination....
r755 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
:description => :text,
:datetime => :created_on,
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
Jean-Philippe Lang
Search engines now supports pagination....
r755
Etienne Massip
Qualify searchable @text@ column to prevent exception thrown when :content association is not eargerly fetched by AR (#9308)....
r7448 acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"],
Jean-Philippe Lang
Rewrites search engine to properly paginate results (#18631)....
r13357 :scope => joins(:content, {:wiki => :project}),
Jean-Philippe Lang
Preload wiki page content for search results....
r13432 :preload => [:content, {:wiki => :project}],
Jean-Philippe Lang
Add permission option to wiki page activity provider....
r5206 :permission => :view_wiki_pages,
Jean-Philippe Lang
Search engines now supports pagination....
r755 :project_key => "#{Wiki.table_name}.project_id"
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 attr_accessor :redirect_existing_links
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
wiki branch merged into trunk...
r320 validates_presence_of :title
Jean-Philippe Lang
Use \A and \z in validation regexps....
r10734 validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
Jean-Philippe Lang
wiki branch merged into trunk...
r320 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
validates_associated :content
Jean-Philippe Lang
Merged rails-4.1 branch (#14534)....
r13100 attr_protected :id
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Toshi MARUYAMA
Rails3: replace deprecated 'validate' method at WikiPage model....
r6714 validate :validate_parent_title
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 before_destroy :delete_redirects
before_save :handle_rename_or_move
after_save :handle_children_move
Toshi MARUYAMA
Rails3: replace deprecated 'validate' method at WikiPage model....
r6714
Jean-Philippe Lang
Moved wiki page updated_on eager load to a scope and fixed timestamp titles on wiki page index (#7818)....
r4978 # eager load information about last updates, without loading text
Jean-Philippe Lang
Replaces a piggy back query with an association for loading wiki pages updates....
r14243 scope :with_updated_on, lambda { preload(:content_without_text) }
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Makes the wiki sidebar editable (#5208)....
r3518 # Wiki pages that are protected by default
DEFAULT_PROTECTED_PAGES = %w(sidebar)
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 safe_attributes 'parent_id', 'parent_title', 'title', 'redirect_existing_links', 'wiki_id',
Jean-Philippe Lang
Ability to edit a wiki page's parent on the edit page (#6449)....
r8667 :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
Jean-Philippe Lang
Removed after_initialize methods....
r8168 def initialize(attributes=nil, *args)
super
Jean-Philippe Lang
Makes the wiki sidebar editable (#5208)....
r3518 if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
self.protected = true
end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Makes user unwatch what he can no longer view after its permissions have changed (#3589)....
r3053 def visible?(user=User.current)
!user.nil? && user.allowed_to?(:view_wiki_pages, project)
end
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709
def title=(value)
value = Wiki.titleize(value)
write_attribute(:title, value)
end
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 def safe_attributes=(attrs, user=User.current)
return unless attrs.is_a?(Hash)
attrs = attrs.deep_dup
# Project and Tracker must be set before since new_statuses_allowed_to depends on it.
if (w_id = attrs.delete('wiki_id')) && safe_attribute?('wiki_id')
if (w = Wiki.find_by_id(w_id)) && w.project && user.allowed_to?(:rename_wiki_pages, w.project)
self.wiki = w
end
end
super attrs, user
end
# Manages redirects if page is renamed or moved
def handle_rename_or_move
if !new_record? && (title_changed? || wiki_id_changed?)
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 # Update redirects that point to the old title
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 WikiRedirect.where(:redirects_to => title_was, :redirects_to_wiki_id => wiki_id_was).each do |r|
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 r.redirects_to = title
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 r.redirects_to_wiki_id = wiki_id
(r.title == r.redirects_to && r.wiki_id == r.redirects_to_wiki_id) ? r.destroy : r.save
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 end
# Remove redirects for the new title
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 WikiRedirect.where(:wiki_id => wiki_id, :title => title).delete_all
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 # Create a redirect to the new title
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 unless redirect_existing_links == "0"
WikiRedirect.create(
:wiki_id => wiki_id_was, :title => title_was,
:redirects_to_wiki_id => wiki_id, :redirects_to => title
)
end
end
if !new_record? && wiki_id_changed? && parent.present?
unless parent.wiki_id == wiki_id
self.parent_id = nil
end
Jean-Philippe Lang
Added the ability to rename wiki pages (specific permission required)....
r709 end
end
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 private :handle_rename_or_move
# Moves child pages if page was moved
def handle_children_move
if !new_record? && wiki_id_changed?
children.each do |child|
child.wiki_id = wiki_id
child.redirect_existing_links = redirect_existing_links
unless child.save
Jean-Philippe Lang
Fixed that moving a wiki page with a child raises an error when target wiki contains a page with the same name as the child (#21900)....
r14748 WikiPage.where(:id => child.id).update_all :parent_id => nil
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 end
end
end
end
private :handle_children_move
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 # Deletes redirects to this page
def delete_redirects
WikiRedirect.where(:redirects_to_wiki_id => wiki_id, :redirects_to => title).delete_all
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
wiki branch merged into trunk...
r320 def pretty_title
Jean-Philippe Lang
patch #9429 Display Wiki edits in activity log (Nick Read)...
r367 WikiPage.pretty_title(title)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Added the ability to easily rollback to a previous version of a wiki page....
r421 def content_for_version(version=nil)
Jean-Philippe Lang
Fixed that requesting a specific version of a non-existent wiki page raises an error (#16255)....
r12696 if content
result = content.versions.find_by_version(version.to_i) if version
result ||= content
result
end
Jean-Philippe Lang
Added the ability to easily rollback to a previous version of a wiki page....
r421 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Added wiki diff....
r580 def diff(version_to=nil, version_from=nil)
version_to = version_to ? version_to.to_i : self.content.version
content_to = content.versions.find_by_version(version_to)
Jean-Philippe Lang
Respond with 404 instead of 500 when requesting a wiki diff with invalid versions (#12434)....
r10650 content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
return nil unless content_to && content_from
Jean-Philippe Lang
Handle deleted wiki page versions (#10852)....
r10473
if content_from.version > content_to.version
content_to, content_from = content_from, content_to
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Added wiki diff....
r580 (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007 def annotate(version=nil)
version = version ? version.to_i : self.content.version
c = content.versions.find_by_version(version)
c ? WikiAnnotate.new(c) : nil
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
patch #9429 Display Wiki edits in activity log (Nick Read)...
r367 def self.pretty_title(str)
(str && str.is_a?(String)) ? str.tr('_', ' ') : str
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
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 project
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 wiki.try(: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
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Search engines now supports pagination....
r755 def text
content.text if content
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Moved wiki page updated_on eager load to a scope and fixed timestamp titles on wiki page index (#7818)....
r4978 def updated_on
Jean-Philippe Lang
Replaces a piggy back query with an association for loading wiki pages updates....
r14243 content_attribute(:updated_on)
end
def version
content_attribute(:version)
Jean-Philippe Lang
Moved wiki page updated_on eager load to a scope and fixed timestamp titles on wiki page index (#7818)....
r4978 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Wiki page protection (#851, patch #1146 by Mateo Murphy with slight changes)....
r1400 # Returns true if usr is allowed to edit the page, otherwise false
def editable_by?(usr)
!protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
AttachmentsController now handles attachments deletion....
r2114 def attachments_deletable?(usr=User.current)
editable_by?(usr) && super(usr)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Wiki page hierarchy (#528). Parent page can be assigned on Rename screen....
r1689 def parent_title
@parent_title || (self.parent && self.parent.pretty_title)
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Wiki page hierarchy (#528). Parent page can be assigned on Rename screen....
r1689 def parent_title=(t)
@parent_title = t
parent_page = t.blank? ? nil : self.wiki.find_page(t)
self.parent = parent_page
end
Eric Davis
Refactor: use :id instead of :page when linking to Wiki Pages...
r4182
Jean-Philippe Lang
Code cleanup....
r10615 # Saves the page and its content if text was changed
Jean-Philippe Lang
Rescue RecordNotSaved in #save_with_content....
r13314 # Return true if the page was saved
Jean-Philippe Lang
Fixed that viewing/editing a wiki page without WikiContent raises an error (#14986)....
r11990 def save_with_content(content)
Jean-Philippe Lang
Code cleanup....
r10615 ret = nil
transaction do
Jean-Philippe Lang
Merged rails-4.1 branch (#14534)....
r13100 ret = save
if content.text_changed?
Jean-Philippe Lang
Rescue RecordNotSaved in #save_with_content....
r13314 begin
self.content = content
ret = ret && content.changed?
rescue ActiveRecord::RecordNotSaved
ret = false
end
Jean-Philippe Lang
Code cleanup....
r10615 end
raise ActiveRecord::Rollback unless ret
end
ret
end
Jean-Philippe Lang
Wiki page hierarchy (#528). Parent page can be assigned on Rename screen....
r1689 protected
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Toshi MARUYAMA
Rails3: replace deprecated 'validate' method at WikiPage model....
r6714 def validate_parent_title
Jean-Philippe Lang
Merged Rails 2.2 branch. Redmine now requires Rails 2.2.2....
r2430 errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
Jean-Philippe Lang
Move wiki page to other project (#5450)....
r13261 if parent_id_changed? && parent && (parent.wiki_id != wiki_id)
errors.add(:parent_title, :not_same_project)
end
Jean-Philippe Lang
Wiki page hierarchy (#528). Parent page can be assigned on Rename screen....
r1689 end
Jean-Philippe Lang
Replaces a piggy back query with an association for loading wiki pages updates....
r14243
private
def content_attribute(name)
(association(:content).loaded? ? content : content_without_text).try(name)
end
Jean-Philippe Lang
wiki branch merged into trunk...
r320 end
Jean-Philippe Lang
Added wiki diff....
r580
Jean-Philippe Lang
Extracts a diff helper from the WikiDiff class....
r4832 class WikiDiff < Redmine::Helpers::Diff
attr_reader :content_to, :content_from
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Added wiki diff....
r580 def initialize(content_to, content_from)
@content_to = content_to
@content_from = content_from
Jean-Philippe Lang
Extracts a diff helper from the WikiDiff class....
r4832 super(content_to.text, content_from.text)
Jean-Philippe Lang
Added wiki diff....
r580 end
end
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007
class WikiAnnotate
attr_reader :lines, :content
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007 def initialize(content)
@content = content
current = content
current_lines = current.text.split(/\r?\n/)
@lines = current_lines.collect {|t| [nil, nil, t]}
positions = []
current_lines.size.times {|i| positions << i}
while (current.previous)
d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
d.each_slice(3) do |s|
sign, line = s[0], s[1]
if sign == '+' && positions[line] && positions[line] != -1
if @lines[positions[line]][0].nil?
@lines[positions[line]][0] = current.version
@lines[positions[line]][1] = current.author
end
end
end
d.each_slice(3) do |s|
sign, line = s[0], s[1]
if sign == '-'
positions.insert(line, -1)
else
positions[line] = nil
end
end
positions.compact!
# Stop if every line is annotated
break unless @lines.detect { |line| line[0].nil? }
current = current.previous
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/wiki_page.rb....
r6391 @lines.each { |line|
Jean-Philippe Lang
Fixed: Wiki annotated page does not display author of version 1 (#8449)....
r6086 line[0] ||= current.version
# if the last known version is > 1 (eg. history was cleared), we don't know the author
line[1] ||= current.author if current.version == 1
}
Jean-Philippe Lang
Added wiki annotate view. It's accessible for each version from the page history view. ...
r1007 end
end