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 @ 1532:a0460a3d154f
History | View | Annotate | Download (8.1 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 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 | 1115:433d4f72a19b | Chris | include Redmine::SafeAttributes |
| 23 | |||
| 24 | 0:513646585e45 | Chris | belongs_to :wiki
|
| 25 | has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy |
||
| 26 | acts_as_attachable :delete_permission => :delete_wiki_pages_attachments |
||
| 27 | acts_as_tree :dependent => :nullify, :order => 'title' |
||
| 28 | |||
| 29 | acts_as_watchable |
||
| 30 | acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"}, |
||
| 31 | :description => :text, |
||
| 32 | :datetime => :created_on, |
||
| 33 | 37:94944d00e43c | chris | :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}} |
| 34 | 0:513646585e45 | Chris | |
| 35 | 909:cbb26bc654de | Chris | acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"], |
| 36 | 0:513646585e45 | Chris | :include => [{:wiki => :project}, :content], |
| 37 | 441:cbce1fd3b1b7 | Chris | :permission => :view_wiki_pages, |
| 38 | 0:513646585e45 | Chris | :project_key => "#{Wiki.table_name}.project_id" |
| 39 | |||
| 40 | attr_accessor :redirect_existing_links
|
||
| 41 | 909:cbb26bc654de | Chris | |
| 42 | 0:513646585e45 | Chris | validates_presence_of :title
|
| 43 | 1464:261b3d9a4903 | Chris | validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/ |
| 44 | 0:513646585e45 | Chris | validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false |
| 45 | validates_associated :content
|
||
| 46 | 909:cbb26bc654de | Chris | |
| 47 | validate :validate_parent_title
|
||
| 48 | before_destroy :remove_redirects
|
||
| 49 | before_save :handle_redirects
|
||
| 50 | |||
| 51 | 441:cbce1fd3b1b7 | Chris | # eager load information about last updates, without loading text
|
| 52 | 1464:261b3d9a4903 | Chris | scope :with_updated_on, lambda {
|
| 53 | select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
|
||
| 54 | joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
|
||
| 55 | 441:cbce1fd3b1b7 | Chris | } |
| 56 | 909:cbb26bc654de | Chris | |
| 57 | 0:513646585e45 | Chris | # Wiki pages that are protected by default
|
| 58 | DEFAULT_PROTECTED_PAGES = %w(sidebar) |
||
| 59 | 909:cbb26bc654de | Chris | |
| 60 | 1115:433d4f72a19b | Chris | safe_attributes 'parent_id', 'parent_title', |
| 61 | :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)} |
||
| 62 | |||
| 63 | def initialize(attributes=nil, *args) |
||
| 64 | super
|
||
| 65 | 0:513646585e45 | Chris | if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase) |
| 66 | self.protected = true |
||
| 67 | end
|
||
| 68 | end
|
||
| 69 | 909:cbb26bc654de | Chris | |
| 70 | 0:513646585e45 | Chris | def visible?(user=User.current) |
| 71 | !user.nil? && user.allowed_to?(:view_wiki_pages, project)
|
||
| 72 | end
|
||
| 73 | |||
| 74 | def title=(value) |
||
| 75 | value = Wiki.titleize(value)
|
||
| 76 | @previous_title = read_attribute(:title) if @previous_title.blank? |
||
| 77 | write_attribute(:title, value)
|
||
| 78 | end
|
||
| 79 | |||
| 80 | 909:cbb26bc654de | Chris | def handle_redirects |
| 81 | self.title = Wiki.titleize(title) |
||
| 82 | 0:513646585e45 | Chris | # Manage redirects if the title has changed
|
| 83 | if !@previous_title.blank? && (@previous_title != title) && !new_record? |
||
| 84 | # Update redirects that point to the old title
|
||
| 85 | 1517:dffacf8a6908 | Chris | wiki.redirects.where(:redirects_to => @previous_title).each do |r| |
| 86 | 0:513646585e45 | Chris | r.redirects_to = title |
| 87 | r.title == r.redirects_to ? r.destroy : r.save |
||
| 88 | end
|
||
| 89 | # Remove redirects for the new title
|
||
| 90 | 1517:dffacf8a6908 | Chris | wiki.redirects.where(:title => title).each(&:destroy) |
| 91 | 0:513646585e45 | Chris | # Create a redirect to the new title
|
| 92 | wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0" |
||
| 93 | @previous_title = nil |
||
| 94 | end
|
||
| 95 | end
|
||
| 96 | 909:cbb26bc654de | Chris | |
| 97 | def remove_redirects |
||
| 98 | 0:513646585e45 | Chris | # Remove redirects to this page
|
| 99 | 1517:dffacf8a6908 | Chris | wiki.redirects.where(:redirects_to => title).each(&:destroy) |
| 100 | 0:513646585e45 | Chris | end
|
| 101 | 909:cbb26bc654de | Chris | |
| 102 | 0:513646585e45 | Chris | def pretty_title |
| 103 | WikiPage.pretty_title(title)
|
||
| 104 | end
|
||
| 105 | 909:cbb26bc654de | Chris | |
| 106 | 0:513646585e45 | Chris | def content_for_version(version=nil) |
| 107 | 1517:dffacf8a6908 | Chris | if content
|
| 108 | result = content.versions.find_by_version(version.to_i) if version
|
||
| 109 | result ||= content |
||
| 110 | result |
||
| 111 | end
|
||
| 112 | 0:513646585e45 | Chris | end
|
| 113 | 909:cbb26bc654de | Chris | |
| 114 | 0:513646585e45 | Chris | def diff(version_to=nil, version_from=nil) |
| 115 | version_to = version_to ? version_to.to_i : self.content.version
|
||
| 116 | 1115:433d4f72a19b | Chris | content_to = content.versions.find_by_version(version_to) |
| 117 | content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
|
||
| 118 | return nil unless content_to && content_from |
||
| 119 | 909:cbb26bc654de | Chris | |
| 120 | 1115:433d4f72a19b | Chris | if content_from.version > content_to.version
|
| 121 | content_to, content_from = content_from, content_to |
||
| 122 | end
|
||
| 123 | 909:cbb26bc654de | Chris | |
| 124 | 0:513646585e45 | Chris | (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil |
| 125 | end
|
||
| 126 | 909:cbb26bc654de | Chris | |
| 127 | 0:513646585e45 | Chris | def annotate(version=nil) |
| 128 | version = version ? version.to_i : self.content.version
|
||
| 129 | c = content.versions.find_by_version(version) |
||
| 130 | c ? WikiAnnotate.new(c) : nil |
||
| 131 | end
|
||
| 132 | 909:cbb26bc654de | Chris | |
| 133 | 0:513646585e45 | Chris | def self.pretty_title(str) |
| 134 | (str && str.is_a?(String)) ? str.tr('_', ' ') : str |
||
| 135 | end
|
||
| 136 | 909:cbb26bc654de | Chris | |
| 137 | 0:513646585e45 | Chris | def project |
| 138 | wiki.project |
||
| 139 | end
|
||
| 140 | 909:cbb26bc654de | Chris | |
| 141 | 0:513646585e45 | Chris | def text |
| 142 | content.text if content
|
||
| 143 | end
|
||
| 144 | 909:cbb26bc654de | Chris | |
| 145 | 441:cbce1fd3b1b7 | Chris | def updated_on |
| 146 | unless @updated_on |
||
| 147 | if time = read_attribute(:updated_on) |
||
| 148 | # content updated_on was eager loaded with the page
|
||
| 149 | 1115:433d4f72a19b | Chris | begin
|
| 150 | @updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime) |
||
| 151 | rescue
|
||
| 152 | end
|
||
| 153 | 441:cbce1fd3b1b7 | Chris | else
|
| 154 | @updated_on = content && content.updated_on
|
||
| 155 | end
|
||
| 156 | end
|
||
| 157 | @updated_on
|
||
| 158 | end
|
||
| 159 | 909:cbb26bc654de | Chris | |
| 160 | 0:513646585e45 | Chris | # Returns true if usr is allowed to edit the page, otherwise false
|
| 161 | def editable_by?(usr) |
||
| 162 | !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
|
||
| 163 | end
|
||
| 164 | 909:cbb26bc654de | Chris | |
| 165 | 0:513646585e45 | Chris | def attachments_deletable?(usr=User.current) |
| 166 | editable_by?(usr) && super(usr)
|
||
| 167 | end
|
||
| 168 | 909:cbb26bc654de | Chris | |
| 169 | 0:513646585e45 | Chris | def parent_title |
| 170 | @parent_title || (self.parent && self.parent.pretty_title) |
||
| 171 | end
|
||
| 172 | 909:cbb26bc654de | Chris | |
| 173 | 0:513646585e45 | Chris | def parent_title=(t) |
| 174 | @parent_title = t
|
||
| 175 | parent_page = t.blank? ? nil : self.wiki.find_page(t) |
||
| 176 | self.parent = parent_page
|
||
| 177 | end
|
||
| 178 | 37:94944d00e43c | chris | |
| 179 | 1115:433d4f72a19b | Chris | # Saves the page and its content if text was changed
|
| 180 | 1464:261b3d9a4903 | Chris | def save_with_content(content) |
| 181 | 1115:433d4f72a19b | Chris | ret = nil
|
| 182 | transaction do
|
||
| 183 | 1464:261b3d9a4903 | Chris | self.content = content
|
| 184 | 1115:433d4f72a19b | Chris | if new_record?
|
| 185 | # Rails automatically saves associated content
|
||
| 186 | ret = save |
||
| 187 | else
|
||
| 188 | ret = save && (content.text_changed? ? content.save : true)
|
||
| 189 | end
|
||
| 190 | raise ActiveRecord::Rollback unless ret |
||
| 191 | end
|
||
| 192 | ret |
||
| 193 | end
|
||
| 194 | |||
| 195 | 0:513646585e45 | Chris | protected |
| 196 | 909:cbb26bc654de | Chris | |
| 197 | def validate_parent_title |
||
| 198 | 0:513646585e45 | Chris | errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil? |
| 199 | errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self)) |
||
| 200 | errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id) |
||
| 201 | end
|
||
| 202 | end
|
||
| 203 | |||
| 204 | 245:051f544170fe | Chris | class WikiDiff < Redmine::Helpers::Diff |
| 205 | attr_reader :content_to, :content_from |
||
| 206 | 909:cbb26bc654de | Chris | |
| 207 | 0:513646585e45 | Chris | def initialize(content_to, content_from) |
| 208 | @content_to = content_to
|
||
| 209 | @content_from = content_from
|
||
| 210 | 245:051f544170fe | Chris | super(content_to.text, content_from.text)
|
| 211 | 0:513646585e45 | Chris | end
|
| 212 | end
|
||
| 213 | |||
| 214 | class WikiAnnotate |
||
| 215 | attr_reader :lines, :content |
||
| 216 | 909:cbb26bc654de | Chris | |
| 217 | 0:513646585e45 | Chris | def initialize(content) |
| 218 | @content = content
|
||
| 219 | current = content |
||
| 220 | current_lines = current.text.split(/\r?\n/)
|
||
| 221 | @lines = current_lines.collect {|t| [nil, nil, t]} |
||
| 222 | positions = [] |
||
| 223 | current_lines.size.times {|i| positions << i}
|
||
| 224 | while (current.previous)
|
||
| 225 | d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten |
||
| 226 | d.each_slice(3) do |s| |
||
| 227 | sign, line = s[0], s[1] |
||
| 228 | if sign == '+' && positions[line] && positions[line] != -1 |
||
| 229 | if @lines[positions[line]][0].nil? |
||
| 230 | @lines[positions[line]][0] = current.version |
||
| 231 | @lines[positions[line]][1] = current.author |
||
| 232 | end
|
||
| 233 | end
|
||
| 234 | end
|
||
| 235 | d.each_slice(3) do |s| |
||
| 236 | sign, line = s[0], s[1] |
||
| 237 | if sign == '-' |
||
| 238 | positions.insert(line, -1)
|
||
| 239 | else
|
||
| 240 | positions[line] = nil
|
||
| 241 | end
|
||
| 242 | end
|
||
| 243 | positions.compact! |
||
| 244 | # Stop if every line is annotated
|
||
| 245 | break unless @lines.detect { |line| line[0].nil? } |
||
| 246 | current = current.previous |
||
| 247 | end
|
||
| 248 | 909:cbb26bc654de | Chris | @lines.each { |line|
|
| 249 | 507:0c939c159af4 | Chris | line[0] ||= current.version
|
| 250 | # if the last known version is > 1 (eg. history was cleared), we don't know the author
|
||
| 251 | line[1] ||= current.author if current.version == 1 |
||
| 252 | } |
||
| 253 | 0:513646585e45 | Chris | end
|
| 254 | end |