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