annotate .svn/pristine/4d/4dbc763764f2013267075c3f49bdd12a87fc0d1b.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 require 'diff'
Chris@1464 19 require 'enumerator'
Chris@1464 20
Chris@1464 21 class WikiPage < ActiveRecord::Base
Chris@1464 22 include Redmine::SafeAttributes
Chris@1464 23
Chris@1464 24 belongs_to :wiki
Chris@1464 25 has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
Chris@1464 26 acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
Chris@1464 27 acts_as_tree :dependent => :nullify, :order => 'title'
Chris@1464 28
Chris@1464 29 acts_as_watchable
Chris@1464 30 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
Chris@1464 31 :description => :text,
Chris@1464 32 :datetime => :created_on,
Chris@1464 33 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
Chris@1464 34
Chris@1464 35 acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"],
Chris@1464 36 :include => [{:wiki => :project}, :content],
Chris@1464 37 :permission => :view_wiki_pages,
Chris@1464 38 :project_key => "#{Wiki.table_name}.project_id"
Chris@1464 39
Chris@1464 40 attr_accessor :redirect_existing_links
Chris@1464 41
Chris@1464 42 validates_presence_of :title
Chris@1464 43 validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
Chris@1464 44 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
Chris@1464 45 validates_associated :content
Chris@1464 46
Chris@1464 47 validate :validate_parent_title
Chris@1464 48 before_destroy :remove_redirects
Chris@1464 49 before_save :handle_redirects
Chris@1464 50
Chris@1464 51 # eager load information about last updates, without loading text
Chris@1464 52 scope :with_updated_on, lambda {
Chris@1464 53 select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
Chris@1464 54 joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
Chris@1464 55 }
Chris@1464 56
Chris@1464 57 # Wiki pages that are protected by default
Chris@1464 58 DEFAULT_PROTECTED_PAGES = %w(sidebar)
Chris@1464 59
Chris@1464 60 safe_attributes 'parent_id', 'parent_title',
Chris@1464 61 :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
Chris@1464 62
Chris@1464 63 def initialize(attributes=nil, *args)
Chris@1464 64 super
Chris@1464 65 if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
Chris@1464 66 self.protected = true
Chris@1464 67 end
Chris@1464 68 end
Chris@1464 69
Chris@1464 70 def visible?(user=User.current)
Chris@1464 71 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
Chris@1464 72 end
Chris@1464 73
Chris@1464 74 def title=(value)
Chris@1464 75 value = Wiki.titleize(value)
Chris@1464 76 @previous_title = read_attribute(:title) if @previous_title.blank?
Chris@1464 77 write_attribute(:title, value)
Chris@1464 78 end
Chris@1464 79
Chris@1464 80 def handle_redirects
Chris@1464 81 self.title = Wiki.titleize(title)
Chris@1464 82 # Manage redirects if the title has changed
Chris@1464 83 if !@previous_title.blank? && (@previous_title != title) && !new_record?
Chris@1464 84 # Update redirects that point to the old title
Chris@1464 85 wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
Chris@1464 86 r.redirects_to = title
Chris@1464 87 r.title == r.redirects_to ? r.destroy : r.save
Chris@1464 88 end
Chris@1464 89 # Remove redirects for the new title
Chris@1464 90 wiki.redirects.find_all_by_title(title).each(&:destroy)
Chris@1464 91 # Create a redirect to the new title
Chris@1464 92 wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
Chris@1464 93 @previous_title = nil
Chris@1464 94 end
Chris@1464 95 end
Chris@1464 96
Chris@1464 97 def remove_redirects
Chris@1464 98 # Remove redirects to this page
Chris@1464 99 wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
Chris@1464 100 end
Chris@1464 101
Chris@1464 102 def pretty_title
Chris@1464 103 WikiPage.pretty_title(title)
Chris@1464 104 end
Chris@1464 105
Chris@1464 106 def content_for_version(version=nil)
Chris@1464 107 result = content.versions.find_by_version(version.to_i) if version
Chris@1464 108 result ||= content
Chris@1464 109 result
Chris@1464 110 end
Chris@1464 111
Chris@1464 112 def diff(version_to=nil, version_from=nil)
Chris@1464 113 version_to = version_to ? version_to.to_i : self.content.version
Chris@1464 114 content_to = content.versions.find_by_version(version_to)
Chris@1464 115 content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
Chris@1464 116 return nil unless content_to && content_from
Chris@1464 117
Chris@1464 118 if content_from.version > content_to.version
Chris@1464 119 content_to, content_from = content_from, content_to
Chris@1464 120 end
Chris@1464 121
Chris@1464 122 (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
Chris@1464 123 end
Chris@1464 124
Chris@1464 125 def annotate(version=nil)
Chris@1464 126 version = version ? version.to_i : self.content.version
Chris@1464 127 c = content.versions.find_by_version(version)
Chris@1464 128 c ? WikiAnnotate.new(c) : nil
Chris@1464 129 end
Chris@1464 130
Chris@1464 131 def self.pretty_title(str)
Chris@1464 132 (str && str.is_a?(String)) ? str.tr('_', ' ') : str
Chris@1464 133 end
Chris@1464 134
Chris@1464 135 def project
Chris@1464 136 wiki.project
Chris@1464 137 end
Chris@1464 138
Chris@1464 139 def text
Chris@1464 140 content.text if content
Chris@1464 141 end
Chris@1464 142
Chris@1464 143 def updated_on
Chris@1464 144 unless @updated_on
Chris@1464 145 if time = read_attribute(:updated_on)
Chris@1464 146 # content updated_on was eager loaded with the page
Chris@1464 147 begin
Chris@1464 148 @updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime)
Chris@1464 149 rescue
Chris@1464 150 end
Chris@1464 151 else
Chris@1464 152 @updated_on = content && content.updated_on
Chris@1464 153 end
Chris@1464 154 end
Chris@1464 155 @updated_on
Chris@1464 156 end
Chris@1464 157
Chris@1464 158 # Returns true if usr is allowed to edit the page, otherwise false
Chris@1464 159 def editable_by?(usr)
Chris@1464 160 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
Chris@1464 161 end
Chris@1464 162
Chris@1464 163 def attachments_deletable?(usr=User.current)
Chris@1464 164 editable_by?(usr) && super(usr)
Chris@1464 165 end
Chris@1464 166
Chris@1464 167 def parent_title
Chris@1464 168 @parent_title || (self.parent && self.parent.pretty_title)
Chris@1464 169 end
Chris@1464 170
Chris@1464 171 def parent_title=(t)
Chris@1464 172 @parent_title = t
Chris@1464 173 parent_page = t.blank? ? nil : self.wiki.find_page(t)
Chris@1464 174 self.parent = parent_page
Chris@1464 175 end
Chris@1464 176
Chris@1464 177 # Saves the page and its content if text was changed
Chris@1464 178 def save_with_content(content)
Chris@1464 179 ret = nil
Chris@1464 180 transaction do
Chris@1464 181 self.content = content
Chris@1464 182 if new_record?
Chris@1464 183 # Rails automatically saves associated content
Chris@1464 184 ret = save
Chris@1464 185 else
Chris@1464 186 ret = save && (content.text_changed? ? content.save : true)
Chris@1464 187 end
Chris@1464 188 raise ActiveRecord::Rollback unless ret
Chris@1464 189 end
Chris@1464 190 ret
Chris@1464 191 end
Chris@1464 192
Chris@1464 193 protected
Chris@1464 194
Chris@1464 195 def validate_parent_title
Chris@1464 196 errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
Chris@1464 197 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
Chris@1464 198 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
Chris@1464 199 end
Chris@1464 200 end
Chris@1464 201
Chris@1464 202 class WikiDiff < Redmine::Helpers::Diff
Chris@1464 203 attr_reader :content_to, :content_from
Chris@1464 204
Chris@1464 205 def initialize(content_to, content_from)
Chris@1464 206 @content_to = content_to
Chris@1464 207 @content_from = content_from
Chris@1464 208 super(content_to.text, content_from.text)
Chris@1464 209 end
Chris@1464 210 end
Chris@1464 211
Chris@1464 212 class WikiAnnotate
Chris@1464 213 attr_reader :lines, :content
Chris@1464 214
Chris@1464 215 def initialize(content)
Chris@1464 216 @content = content
Chris@1464 217 current = content
Chris@1464 218 current_lines = current.text.split(/\r?\n/)
Chris@1464 219 @lines = current_lines.collect {|t| [nil, nil, t]}
Chris@1464 220 positions = []
Chris@1464 221 current_lines.size.times {|i| positions << i}
Chris@1464 222 while (current.previous)
Chris@1464 223 d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
Chris@1464 224 d.each_slice(3) do |s|
Chris@1464 225 sign, line = s[0], s[1]
Chris@1464 226 if sign == '+' && positions[line] && positions[line] != -1
Chris@1464 227 if @lines[positions[line]][0].nil?
Chris@1464 228 @lines[positions[line]][0] = current.version
Chris@1464 229 @lines[positions[line]][1] = current.author
Chris@1464 230 end
Chris@1464 231 end
Chris@1464 232 end
Chris@1464 233 d.each_slice(3) do |s|
Chris@1464 234 sign, line = s[0], s[1]
Chris@1464 235 if sign == '-'
Chris@1464 236 positions.insert(line, -1)
Chris@1464 237 else
Chris@1464 238 positions[line] = nil
Chris@1464 239 end
Chris@1464 240 end
Chris@1464 241 positions.compact!
Chris@1464 242 # Stop if every line is annotated
Chris@1464 243 break unless @lines.detect { |line| line[0].nil? }
Chris@1464 244 current = current.previous
Chris@1464 245 end
Chris@1464 246 @lines.each { |line|
Chris@1464 247 line[0] ||= current.version
Chris@1464 248 # if the last known version is > 1 (eg. history was cleared), we don't know the author
Chris@1464 249 line[1] ||= current.author if current.version == 1
Chris@1464 250 }
Chris@1464 251 end
Chris@1464 252 end