2009-05-25 23:02:28 +04:00
|
|
|
# Redmine - project management software
|
2014-01-30 02:45:39 +04:00
|
|
|
# Copyright (C) 2006-2014 Jean-Philippe Lang
|
2007-03-10 18:09:49 +03:00
|
|
|
#
|
|
|
|
# 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.
|
2011-08-21 06:00:35 +04:00
|
|
|
#
|
2007-03-10 18:09:49 +03:00
|
|
|
# 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.
|
2011-08-21 06:00:35 +04:00
|
|
|
#
|
2007-03-10 18:09:49 +03:00
|
|
|
# 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.
|
|
|
|
|
2007-07-14 15:25:03 +04:00
|
|
|
require 'diff'
|
2007-12-20 22:10:24 +03:00
|
|
|
require 'enumerator'
|
2007-07-14 15:25:03 +04:00
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
class WikiPage < ActiveRecord::Base
|
2012-02-05 18:35:27 +04:00
|
|
|
include Redmine::SafeAttributes
|
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
belongs_to :wiki
|
|
|
|
has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
|
2008-12-09 19:54:46 +03:00
|
|
|
acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
|
2009-04-21 16:19:56 +04:00
|
|
|
acts_as_tree :dependent => :nullify, :order => 'title'
|
2009-05-25 23:02:28 +04:00
|
|
|
|
|
|
|
acts_as_watchable
|
2007-09-27 21:28:22 +04:00
|
|
|
acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
|
|
|
|
:description => :text,
|
|
|
|
:datetime => :created_on,
|
2010-10-27 20:27:06 +04:00
|
|
|
:url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
|
2007-09-27 21:28:22 +04:00
|
|
|
|
2011-10-04 00:17:01 +04:00
|
|
|
acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"],
|
2008-05-18 20:15:22 +04:00
|
|
|
:include => [{:wiki => :project}, :content],
|
2011-04-05 16:52:57 +04:00
|
|
|
:permission => :view_wiki_pages,
|
2007-09-27 21:28:22 +04:00
|
|
|
:project_key => "#{Wiki.table_name}.project_id"
|
|
|
|
|
2007-09-09 21:05:38 +04:00
|
|
|
attr_accessor :redirect_existing_links
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
validates_presence_of :title
|
2012-12-08 15:06:53 +04:00
|
|
|
validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
|
2007-03-10 18:09:49 +03:00
|
|
|
validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
|
|
|
|
validates_associated :content
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2011-08-31 08:07:15 +04:00
|
|
|
validate :validate_parent_title
|
2011-09-22 04:53:53 +04:00
|
|
|
before_destroy :remove_redirects
|
2011-09-22 04:54:37 +04:00
|
|
|
before_save :handle_redirects
|
2011-08-31 08:07:15 +04:00
|
|
|
|
2011-03-12 21:09:46 +03:00
|
|
|
# eager load information about last updates, without loading text
|
2012-12-07 21:59:20 +04:00
|
|
|
scope :with_updated_on, lambda {
|
|
|
|
select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
|
|
|
|
joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
|
|
|
|
}
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2010-04-11 16:56:18 +04:00
|
|
|
# Wiki pages that are protected by default
|
|
|
|
DEFAULT_PROTECTED_PAGES = %w(sidebar)
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2012-10-26 00:38:29 +04:00
|
|
|
safe_attributes 'parent_id', 'parent_title',
|
2012-02-05 18:35:27 +04:00
|
|
|
:if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
|
|
|
|
|
2011-12-18 18:57:58 +04:00
|
|
|
def initialize(attributes=nil, *args)
|
|
|
|
super
|
2010-04-11 16:56:18 +04:00
|
|
|
if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
|
|
|
|
self.protected = true
|
|
|
|
end
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2009-12-13 15:39:22 +03:00
|
|
|
def visible?(user=User.current)
|
|
|
|
!user.nil? && user.allowed_to?(:view_wiki_pages, project)
|
|
|
|
end
|
2007-09-09 21:05:38 +04:00
|
|
|
|
|
|
|
def title=(value)
|
|
|
|
value = Wiki.titleize(value)
|
|
|
|
@previous_title = read_attribute(:title) if @previous_title.blank?
|
|
|
|
write_attribute(:title, value)
|
|
|
|
end
|
|
|
|
|
2011-09-22 04:54:37 +04:00
|
|
|
def handle_redirects
|
2011-08-21 06:00:35 +04:00
|
|
|
self.title = Wiki.titleize(title)
|
2007-09-09 21:05:38 +04:00
|
|
|
# Manage redirects if the title has changed
|
|
|
|
if !@previous_title.blank? && (@previous_title != title) && !new_record?
|
|
|
|
# Update redirects that point to the old title
|
2014-01-08 16:42:53 +04:00
|
|
|
wiki.redirects.where(:redirects_to => @previous_title).each do |r|
|
2007-09-09 21:05:38 +04:00
|
|
|
r.redirects_to = title
|
|
|
|
r.title == r.redirects_to ? r.destroy : r.save
|
|
|
|
end
|
|
|
|
# Remove redirects for the new title
|
2014-01-08 16:43:10 +04:00
|
|
|
wiki.redirects.where(:title => title).each(&:destroy)
|
2007-09-09 21:05:38 +04:00
|
|
|
# Create a redirect to the new title
|
|
|
|
wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
|
|
|
|
@previous_title = nil
|
|
|
|
end
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2011-09-22 04:53:53 +04:00
|
|
|
def remove_redirects
|
2007-09-09 21:05:38 +04:00
|
|
|
# Remove redirects to this page
|
2014-01-08 16:42:53 +04:00
|
|
|
wiki.redirects.where(:redirects_to => title).each(&:destroy)
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
def pretty_title
|
2007-03-23 18:58:02 +03:00
|
|
|
WikiPage.pretty_title(title)
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-04-05 18:45:44 +04:00
|
|
|
def content_for_version(version=nil)
|
2014-03-15 15:41:24 +04:00
|
|
|
if content
|
|
|
|
result = content.versions.find_by_version(version.to_i) if version
|
|
|
|
result ||= content
|
|
|
|
result
|
|
|
|
end
|
2007-04-05 18:45:44 +04:00
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-07-14 15:25:03 +04:00
|
|
|
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)
|
2012-11-24 16:27:00 +04:00
|
|
|
content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
|
|
|
|
return nil unless content_to && content_from
|
2012-10-21 08:43:15 +04:00
|
|
|
|
|
|
|
if content_from.version > content_to.version
|
|
|
|
content_to, content_from = content_from, content_to
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-07-14 15:25:03 +04:00
|
|
|
(content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-12-20 22:10:24 +03:00
|
|
|
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
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-03-23 18:58:02 +03:00
|
|
|
def self.pretty_title(str)
|
|
|
|
(str && str.is_a?(String)) ? str.tr('_', ' ') : str
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-05-26 19:42:37 +04:00
|
|
|
def project
|
|
|
|
wiki.project
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-09-27 21:28:22 +04:00
|
|
|
def text
|
|
|
|
content.text if content
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2011-03-12 21:09:46 +03:00
|
|
|
def updated_on
|
|
|
|
unless @updated_on
|
|
|
|
if time = read_attribute(:updated_on)
|
|
|
|
# content updated_on was eager loaded with the page
|
2012-01-01 21:58:14 +04:00
|
|
|
begin
|
2012-05-26 23:35:25 +04:00
|
|
|
@updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime)
|
2012-01-01 21:58:14 +04:00
|
|
|
rescue
|
|
|
|
end
|
2011-03-12 21:09:46 +03:00
|
|
|
else
|
|
|
|
@updated_on = content && content.updated_on
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@updated_on
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2008-05-04 19:05:38 +04:00
|
|
|
# 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
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2008-12-09 19:54:46 +03:00
|
|
|
def attachments_deletable?(usr=User.current)
|
|
|
|
editable_by?(usr) && super(usr)
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2008-07-26 15:46:24 +04:00
|
|
|
def parent_title
|
|
|
|
@parent_title || (self.parent && self.parent.pretty_title)
|
|
|
|
end
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2008-07-26 15:46:24 +04:00
|
|
|
def parent_title=(t)
|
|
|
|
@parent_title = t
|
|
|
|
parent_page = t.blank? ? nil : self.wiki.find_page(t)
|
|
|
|
self.parent = parent_page
|
|
|
|
end
|
2010-10-27 20:27:06 +04:00
|
|
|
|
2012-11-18 20:01:07 +04:00
|
|
|
# Saves the page and its content if text was changed
|
2013-10-13 15:19:47 +04:00
|
|
|
def save_with_content(content)
|
2012-11-18 20:01:07 +04:00
|
|
|
ret = nil
|
|
|
|
transaction do
|
2013-10-13 15:19:47 +04:00
|
|
|
self.content = content
|
2012-11-18 20:01:07 +04:00
|
|
|
if new_record?
|
|
|
|
# Rails automatically saves associated content
|
|
|
|
ret = save
|
|
|
|
else
|
|
|
|
ret = save && (content.text_changed? ? content.save : true)
|
|
|
|
end
|
|
|
|
raise ActiveRecord::Rollback unless ret
|
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2008-07-26 15:46:24 +04:00
|
|
|
protected
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2011-08-31 08:07:15 +04:00
|
|
|
def validate_parent_title
|
2009-02-21 14:04:50 +03:00
|
|
|
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))
|
|
|
|
errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
|
2008-07-26 15:46:24 +04:00
|
|
|
end
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|
2007-07-14 15:25:03 +04:00
|
|
|
|
2011-02-27 15:35:31 +03:00
|
|
|
class WikiDiff < Redmine::Helpers::Diff
|
|
|
|
attr_reader :content_to, :content_from
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-07-14 15:25:03 +04:00
|
|
|
def initialize(content_to, content_from)
|
|
|
|
@content_to = content_to
|
|
|
|
@content_from = content_from
|
2011-02-27 15:35:31 +03:00
|
|
|
super(content_to.text, content_from.text)
|
2007-07-14 15:25:03 +04:00
|
|
|
end
|
|
|
|
end
|
2007-12-20 22:10:24 +03:00
|
|
|
|
|
|
|
class WikiAnnotate
|
|
|
|
attr_reader :lines, :content
|
2011-08-21 06:00:35 +04:00
|
|
|
|
2007-12-20 22:10:24 +03:00
|
|
|
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
|
2011-08-21 06:00:35 +04:00
|
|
|
@lines.each { |line|
|
2011-07-09 16:01:08 +04:00
|
|
|
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
|
|
|
|
}
|
2007-12-20 22:10:24 +03:00
|
|
|
end
|
|
|
|
end
|