annotate .svn/pristine/c8/c8161dc18d368b5a9f7435cd9c889d8ea63a8a29.svn-base @ 1628:9c5f8e24dadc live tip

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