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_content.rb @ 1592:72d9219f2f19
History | View | Annotate | Download (5.81 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2014 Jean-Philippe Lang
|
| 3 |
#
|
| 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 |
#
|
| 9 |
# 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 |
#
|
| 14 |
# 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 'zlib'
|
| 19 |
|
| 20 |
class WikiContent < ActiveRecord::Base |
| 21 |
self.locking_column = 'version' |
| 22 |
belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id' |
| 23 |
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
| 24 |
validates_presence_of :text
|
| 25 |
validates_length_of :comments, :maximum => 255, :allow_nil => true |
| 26 |
|
| 27 |
acts_as_versioned |
| 28 |
|
| 29 |
after_save :send_notification
|
| 30 |
|
| 31 |
def visible?(user=User.current) |
| 32 |
page.visible?(user) |
| 33 |
end
|
| 34 |
|
| 35 |
def project |
| 36 |
page.project |
| 37 |
end
|
| 38 |
|
| 39 |
def attachments |
| 40 |
page.nil? ? [] : page.attachments |
| 41 |
end
|
| 42 |
|
| 43 |
# Returns the mail adresses of users that should be notified
|
| 44 |
def recipients |
| 45 |
notified = project.notified_users |
| 46 |
notified.reject! {|user| !visible?(user)}
|
| 47 |
notified.collect(&:mail)
|
| 48 |
end
|
| 49 |
|
| 50 |
# Return true if the content is the current page content
|
| 51 |
def current_version? |
| 52 |
true
|
| 53 |
end
|
| 54 |
|
| 55 |
class Version |
| 56 |
belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id' |
| 57 |
belongs_to :author, :class_name => '::User', :foreign_key => 'author_id' |
| 58 |
attr_protected :data
|
| 59 |
|
| 60 |
acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"}, |
| 61 |
:description => :comments, |
| 62 |
:datetime => :updated_on, |
| 63 |
:type => 'wiki-page', |
| 64 |
:group => :page, |
| 65 |
:url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}} |
| 66 |
|
| 67 |
acts_as_activity_provider :type => 'wiki_edits', |
| 68 |
:timestamp => "#{WikiContent.versioned_table_name}.updated_on", |
| 69 |
:author_key => "#{WikiContent.versioned_table_name}.author_id", |
| 70 |
:permission => :view_wiki_edits, |
| 71 |
:find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " + |
| 72 |
"#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
|
| 73 |
"#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
|
| 74 |
"#{WikiContent.versioned_table_name}.id",
|
| 75 |
:joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " + |
| 76 |
"LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
|
| 77 |
"LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
|
| 78 |
|
| 79 |
after_destroy :page_update_after_destroy
|
| 80 |
|
| 81 |
def text=(plain) |
| 82 |
case Setting.wiki_compression |
| 83 |
when 'gzip' |
| 84 |
begin
|
| 85 |
self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION) |
| 86 |
self.compression = 'gzip' |
| 87 |
rescue
|
| 88 |
self.data = plain
|
| 89 |
self.compression = '' |
| 90 |
end
|
| 91 |
else
|
| 92 |
self.data = plain
|
| 93 |
self.compression = '' |
| 94 |
end
|
| 95 |
plain |
| 96 |
end
|
| 97 |
|
| 98 |
def text |
| 99 |
@text ||= begin |
| 100 |
str = case compression
|
| 101 |
when 'gzip' |
| 102 |
Zlib::Inflate.inflate(data) |
| 103 |
else
|
| 104 |
# uncompressed data
|
| 105 |
data |
| 106 |
end
|
| 107 |
str.force_encoding("UTF-8") if str.respond_to?(:force_encoding) |
| 108 |
str |
| 109 |
end
|
| 110 |
end
|
| 111 |
|
| 112 |
def project |
| 113 |
page.project |
| 114 |
end
|
| 115 |
|
| 116 |
# Return true if the content is the current page content
|
| 117 |
def current_version? |
| 118 |
page.content.version == self.version
|
| 119 |
end
|
| 120 |
|
| 121 |
# Returns the previous version or nil
|
| 122 |
def previous |
| 123 |
@previous ||= WikiContent::Version. |
| 124 |
reorder('version DESC').
|
| 125 |
includes(:author).
|
| 126 |
where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
|
| 127 |
end
|
| 128 |
|
| 129 |
# Returns the next version or nil
|
| 130 |
def next |
| 131 |
@next ||= WikiContent::Version. |
| 132 |
reorder('version ASC').
|
| 133 |
includes(:author).
|
| 134 |
where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
|
| 135 |
end
|
| 136 |
|
| 137 |
private |
| 138 |
|
| 139 |
# Updates page's content if the latest version is removed
|
| 140 |
# or destroys the page if it was the only version
|
| 141 |
def page_update_after_destroy |
| 142 |
latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
|
| 143 |
if latest && page.content.version != latest.version
|
| 144 |
raise ActiveRecord::Rollback unless page.content.revert_to!(latest) |
| 145 |
elsif latest.nil?
|
| 146 |
raise ActiveRecord::Rollback unless page.destroy |
| 147 |
end
|
| 148 |
end
|
| 149 |
end
|
| 150 |
|
| 151 |
private |
| 152 |
|
| 153 |
def send_notification |
| 154 |
# new_record? returns false in after_save callbacks
|
| 155 |
if id_changed?
|
| 156 |
if Setting.notified_events.include?('wiki_content_added') |
| 157 |
Mailer.wiki_content_added(self).deliver |
| 158 |
end
|
| 159 |
elsif text_changed?
|
| 160 |
if Setting.notified_events.include?('wiki_content_updated') |
| 161 |
Mailer.wiki_content_updated(self).deliver |
| 162 |
end
|
| 163 |
end
|
| 164 |
end
|
| 165 |
end
|