annotate app/models/wiki_page.rb @ 1478:5ca1f4a47171 bibplugin_db_migrations

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