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