# 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.
:project_key => "#{Wiki.table_name}.project_id"
attr_accessor :redirect_existing_links
-
+
validates_presence_of :title
validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
validates_associated :content
-
+
# eager load information about last updates, without loading text
named_scope :with_updated_on, {
: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"
}
-
+
# Wiki pages that are protected by default
DEFAULT_PROTECTED_PAGES = %w(sidebar)
-
+
def after_initialize
if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
self.protected = true
end
end
-
+
def visible?(user=User.current)
!user.nil? && user.allowed_to?(:view_wiki_pages, project)
end
end
def before_save
- self.title = Wiki.titleize(title)
+ self.title = Wiki.titleize(title)
# Manage redirects if the title has changed
if !@previous_title.blank? && (@previous_title != title) && !new_record?
# Update redirects that point to the old title
@previous_title = nil
end
end
-
+
def before_destroy
# Remove redirects to this page
wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
end
-
+
def pretty_title
WikiPage.pretty_title(title)
end
-
+
def content_for_version(version=nil)
result = content.versions.find_by_version(version.to_i) if version
result ||= content
result
end
-
+
def diff(version_to=nil, version_from=nil)
version_to = version_to ? version_to.to_i : self.content.version
version_from = version_from ? version_from.to_i : version_to - 1
version_to, version_from = version_from, version_to unless version_from < version_to
-
+
content_to = content.versions.find_by_version(version_to)
content_from = content.versions.find_by_version(version_from)
-
+
(content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
end
-
+
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
-
+
def self.pretty_title(str)
(str && str.is_a?(String)) ? str.tr('_', ' ') : str
end
-
+
def project
wiki.project
end
-
+
def text
content.text if content
end
-
+
def updated_on
unless @updated_on
if time = read_attribute(:updated_on)
end
@updated_on
end
-
+
# 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
-
+
def attachments_deletable?(usr=User.current)
editable_by?(usr) && super(usr)
end
-
+
def parent_title
@parent_title || (self.parent && self.parent.pretty_title)
end
-
+
def parent_title=(t)
@parent_title = t
parent_page = t.blank? ? nil : self.wiki.find_page(t)
end
protected
-
+
def validate
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))
class WikiDiff < Redmine::Helpers::Diff
attr_reader :content_to, :content_from
-
+
def initialize(content_to, content_from)
@content_to = content_to
@content_from = content_from
class WikiAnnotate
attr_reader :lines, :content
-
+
def initialize(content)
@content = content
current = content
break unless @lines.detect { |line| line[0].nil? }
current = current.previous
end
- @lines.each { |line|
+ @lines.each { |line|
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