Chris@909: # RedMine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'zlib' Chris@909: Chris@909: class WikiContent < ActiveRecord::Base Chris@909: set_locking_column :version Chris@909: belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id' Chris@909: belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' Chris@909: validates_presence_of :text Chris@909: validates_length_of :comments, :maximum => 255, :allow_nil => true Chris@909: Chris@909: acts_as_versioned Chris@909: Chris@909: def visible?(user=User.current) Chris@909: page.visible?(user) Chris@909: end Chris@909: Chris@909: def project Chris@909: page.project Chris@909: end Chris@909: Chris@909: def attachments Chris@909: page.nil? ? [] : page.attachments Chris@909: end Chris@909: Chris@909: # Returns the mail adresses of users that should be notified Chris@909: def recipients Chris@909: notified = project.notified_users Chris@909: notified.reject! {|user| !visible?(user)} Chris@909: notified.collect(&:mail) Chris@909: end Chris@909: Chris@909: # Return true if the content is the current page content Chris@909: def current_version? Chris@909: true Chris@909: end Chris@909: Chris@909: class Version Chris@909: belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id' Chris@909: belongs_to :author, :class_name => '::User', :foreign_key => 'author_id' Chris@909: attr_protected :data Chris@909: Chris@909: acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"}, Chris@909: :description => :comments, Chris@909: :datetime => :updated_on, Chris@909: :type => 'wiki-page', Chris@909: :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}} Chris@909: Chris@909: acts_as_activity_provider :type => 'wiki_edits', Chris@909: :timestamp => "#{WikiContent.versioned_table_name}.updated_on", Chris@909: :author_key => "#{WikiContent.versioned_table_name}.author_id", Chris@909: :permission => :view_wiki_edits, Chris@909: :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " + Chris@909: "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " + Chris@909: "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " + Chris@909: "#{WikiContent.versioned_table_name}.id", Chris@909: :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " + Chris@909: "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " + Chris@909: "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"} Chris@909: Chris@909: def text=(plain) Chris@909: case Setting.wiki_compression Chris@909: when 'gzip' Chris@909: begin Chris@909: self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION) Chris@909: self.compression = 'gzip' Chris@909: rescue Chris@909: self.data = plain Chris@909: self.compression = '' Chris@909: end Chris@909: else Chris@909: self.data = plain Chris@909: self.compression = '' Chris@909: end Chris@909: plain Chris@909: end Chris@909: Chris@909: def text Chris@909: @text ||= case compression Chris@909: when 'gzip' Chris@909: str = Zlib::Inflate.inflate(data) Chris@909: str.force_encoding("UTF-8") if str.respond_to?(:force_encoding) Chris@909: str Chris@909: else Chris@909: # uncompressed data Chris@909: data Chris@909: end Chris@909: end Chris@909: Chris@909: def project Chris@909: page.project Chris@909: end Chris@909: Chris@909: # Return true if the content is the current page content Chris@909: def current_version? Chris@909: page.content.version == self.version Chris@909: end Chris@909: Chris@909: # Returns the previous version or nil Chris@909: def previous Chris@909: @previous ||= WikiContent::Version.find(:first, Chris@909: :order => 'version DESC', Chris@909: :include => :author, Chris@909: :conditions => ["wiki_content_id = ? AND version < ?", wiki_content_id, version]) Chris@909: end Chris@909: end Chris@909: end