annotate .svn/pristine/83/83c8ea6b826f97412b09501824a3127336754f22.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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