annotate .svn/pristine/fa/fafc777bb67f933889320268007fe8033f0b89cb.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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