2011-05-16 04:00:26 +04:00
|
|
|
# RedMine - project management software
|
|
|
|
# Copyright (C) 2006-2011 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
|
2007-05-26 21:22:27 +04:00
|
|
|
set_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
|
|
|
|
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
|
|
|
|
2009-12-13 17:26:54 +03:00
|
|
|
# Returns the mail adresses of users that should be notified
|
|
|
|
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',
|
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"}
|
|
|
|
|
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
|
|
|
|
@text ||= case compression
|
|
|
|
when 'gzip'
|
2011-11-26 03:15:55 +04:00
|
|
|
str = Zlib::Inflate.inflate(data)
|
|
|
|
str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
|
|
|
|
str
|
2007-03-10 18:09:49 +03:00
|
|
|
else
|
|
|
|
# uncompressed data
|
|
|
|
data
|
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
|
2011-05-16 04:00:26 +04:00
|
|
|
@previous ||= WikiContent::Version.find(:first,
|
2007-12-20 22:10:24 +03:00
|
|
|
:order => 'version DESC',
|
|
|
|
:include => :author,
|
|
|
|
:conditions => ["wiki_content_id = ? AND version < ?", wiki_content_id, version])
|
|
|
|
end
|
2007-03-10 18:09:49 +03:00
|
|
|
end
|
|
|
|
end
|