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