2012-04-03 18:04:38 +04:00
|
|
|
# Redmine - project management software
|
2014-01-30 02:45:39 +04:00
|
|
|
# Copyright (C) 2006-2014 Jean-Philippe Lang
|
2007-03-10 18:09:49 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-16 04:00:26 +04:00
|
|
|
#
|
2007-03-10 18:09:49 +03:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-05-16 04:00:26 +04:00
|
|
|
#
|
2007-03-10 18:09:49 +03:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
class WikiContent < ActiveRecord::Base
|
2012-04-27 20:28:35 +04:00
|
|
|
self.locking_column = 'version'
|
2007-03-10 18:09:49 +03:00
|
|
|
belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id'
|
|
|
|
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
|
|
|
|
validates_presence_of :text
|
2009-05-10 15:06:29 +04:00
|
|
|
validates_length_of :comments, :maximum => 255, :allow_nil => true
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
acts_as_versioned
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2013-07-14 18:26:27 +04:00
|
|
|
after_save :send_notification
|
|
|
|
|
2009-12-13 17:26:54 +03:00
|
|
|
def visible?(user=User.current)
|
|
|
|
page.visible?(user)
|
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2009-05-17 13:55:13 +04:00
|
|
|
def project
|
|
|
|
page.project
|
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2010-04-11 16:56:18 +04:00
|
|
|
def attachments
|
|
|
|
page.nil? ? [] : page.attachments
|
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2014-04-06 18:14:40 +04:00
|
|
|
# Returns the mail addresses of users that should be notified
|
2009-12-13 17:26:54 +03:00
|
|
|
def recipients
|
|
|
|
notified = project.notified_users
|
|
|
|
notified.reject! {|user| !visible?(user)}
|
|
|
|
notified.collect(&:mail)
|
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2011-11-29 00:12:03 +04:00
|
|
|
# Return true if the content is the current page content
|
|
|
|
def current_version?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
class Version
|
2007-12-15 14:57:48 +03:00
|
|
|
belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id'
|
|
|
|
belongs_to :author, :class_name => '::User', :foreign_key => 'author_id'
|
2007-03-10 18:09:49 +03:00
|
|
|
attr_protected :data
|
2007-08-29 20:52:35 +04:00
|
|
|
|
|
|
|
acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
|
|
|
|
:description => :comments,
|
|
|
|
:datetime => :updated_on,
|
2008-04-12 20:54:14 +04:00
|
|
|
:type => 'wiki-page',
|
2012-12-08 12:24:01 +04:00
|
|
|
:group => :page,
|
2010-10-27 20:27:06 +04:00
|
|
|
:url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
|
2007-08-29 20:52:35 +04:00
|
|
|
|
2008-09-13 20:45:01 +04:00
|
|
|
acts_as_activity_provider :type => 'wiki_edits',
|
2008-07-27 21:54:09 +04:00
|
|
|
:timestamp => "#{WikiContent.versioned_table_name}.updated_on",
|
2008-11-30 14:18:22 +03:00
|
|
|
:author_key => "#{WikiContent.versioned_table_name}.author_id",
|
2008-09-13 20:45:01 +04:00
|
|
|
:permission => :view_wiki_edits,
|
2008-07-27 21:54:09 +04:00
|
|
|
:find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
|
|
|
|
"#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
|
|
|
|
"#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
|
|
|
|
"#{WikiContent.versioned_table_name}.id",
|
|
|
|
:joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
|
|
|
|
"LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
|
|
|
|
"LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
|
|
|
|
|
2012-10-23 22:45:14 +04:00
|
|
|
after_destroy :page_update_after_destroy
|
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
def text=(plain)
|
|
|
|
case Setting.wiki_compression
|
|
|
|
when 'gzip'
|
|
|
|
begin
|
|
|
|
self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
|
|
|
|
self.compression = 'gzip'
|
|
|
|
rescue
|
|
|
|
self.data = plain
|
|
|
|
self.compression = ''
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.data = plain
|
|
|
|
self.compression = ''
|
|
|
|
end
|
|
|
|
plain
|
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2007-03-10 18:09:49 +03:00
|
|
|
def text
|
2012-04-06 11:21:51 +04:00
|
|
|
@text ||= begin
|
|
|
|
str = case compression
|
|
|
|
when 'gzip'
|
|
|
|
Zlib::Inflate.inflate(data)
|
|
|
|
else
|
|
|
|
# uncompressed data
|
|
|
|
data
|
|
|
|
end
|
2011-11-26 03:15:55 +04:00
|
|
|
str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
|
|
|
|
str
|
2011-05-16 04:00:26 +04:00
|
|
|
end
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2008-03-11 22:33:38 +03:00
|
|
|
def project
|
|
|
|
page.project
|
|
|
|
end
|
2011-05-16 04:00:26 +04:00
|
|
|
|
2011-11-29 00:12:03 +04:00
|
|
|
# Return true if the content is the current page content
|
|
|
|
def current_version?
|
|
|
|
page.content.version == self.version
|
|
|
|
end
|
|
|
|
|
2007-12-20 22:10:24 +03:00
|
|
|
# Returns the previous version or nil
|
|
|
|
def previous
|
2012-10-21 08:43:15 +04:00
|
|
|
@previous ||= WikiContent::Version.
|
|
|
|
reorder('version DESC').
|
|
|
|
includes(:author).
|
|
|
|
where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the next version or nil
|
|
|
|
def next
|
|
|
|
@next ||= WikiContent::Version.
|
|
|
|
reorder('version ASC').
|
|
|
|
includes(:author).
|
|
|
|
where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
|
2007-12-20 22:10:24 +03:00
|
|
|
end
|
2012-10-23 22:45:14 +04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Updates page's content if the latest version is removed
|
|
|
|
# or destroys the page if it was the only version
|
|
|
|
def page_update_after_destroy
|
|
|
|
latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
|
|
|
|
if latest && page.content.version != latest.version
|
|
|
|
raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
|
|
|
|
elsif latest.nil?
|
|
|
|
raise ActiveRecord::Rollback unless page.destroy
|
|
|
|
end
|
|
|
|
end
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|
2013-07-14 18:26:27 +04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def send_notification
|
|
|
|
# new_record? returns false in after_save callbacks
|
|
|
|
if id_changed?
|
|
|
|
if Setting.notified_events.include?('wiki_content_added')
|
|
|
|
Mailer.wiki_content_added(self).deliver
|
|
|
|
end
|
|
|
|
elsif text_changed?
|
|
|
|
if Setting.notified_events.include?('wiki_content_updated')
|
|
|
|
Mailer.wiki_content_updated(self).deliver
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|