annotate app/models/wiki_content.rb @ 1621:3a510bf6a9bc

Merge from live branch
author Chris Cannam
date Fri, 13 Jul 2018 10:44:33 +0100
parents e248c7af89ec
children
rev   line source
Chris@1115 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'zlib'
Chris@0 19
Chris@0 20 class WikiContent < ActiveRecord::Base
Chris@1115 21 self.locking_column = 'version'
Chris@0 22 belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id'
Chris@0 23 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
Chris@0 24 validates_presence_of :text
Chris@0 25 validates_length_of :comments, :maximum => 255, :allow_nil => true
Chris@441 26
Chris@0 27 acts_as_versioned
Chris@441 28
Chris@1464 29 after_save :send_notification
Chris@1464 30
Chris@0 31 def visible?(user=User.current)
Chris@0 32 page.visible?(user)
Chris@0 33 end
Chris@441 34
Chris@0 35 def project
Chris@0 36 page.project
Chris@0 37 end
Chris@441 38
Chris@0 39 def attachments
Chris@0 40 page.nil? ? [] : page.attachments
Chris@0 41 end
Chris@441 42
Chris@0 43 # Returns the mail adresses of users that should be notified
Chris@0 44 def recipients
Chris@0 45 notified = project.notified_users
Chris@0 46 notified.reject! {|user| !visible?(user)}
Chris@0 47 notified.collect(&:mail)
Chris@0 48 end
Chris@441 49
Chris@909 50 # Return true if the content is the current page content
Chris@909 51 def current_version?
Chris@909 52 true
Chris@909 53 end
Chris@909 54
Chris@0 55 class Version
Chris@0 56 belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id'
Chris@0 57 belongs_to :author, :class_name => '::User', :foreign_key => 'author_id'
Chris@0 58 attr_protected :data
Chris@0 59
Chris@0 60 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
Chris@0 61 :description => :comments,
Chris@0 62 :datetime => :updated_on,
Chris@0 63 :type => 'wiki-page',
Chris@1464 64 :group => :page,
chris@37 65 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
Chris@0 66
Chris@0 67 acts_as_activity_provider :type => 'wiki_edits',
Chris@0 68 :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
Chris@0 69 :author_key => "#{WikiContent.versioned_table_name}.author_id",
Chris@0 70 :permission => :view_wiki_edits,
Chris@0 71 :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
Chris@0 72 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
Chris@0 73 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
Chris@0 74 "#{WikiContent.versioned_table_name}.id",
Chris@0 75 :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
Chris@0 76 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
Chris@0 77 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
Chris@0 78
Chris@1115 79 after_destroy :page_update_after_destroy
Chris@1115 80
Chris@0 81 def text=(plain)
Chris@0 82 case Setting.wiki_compression
Chris@0 83 when 'gzip'
Chris@0 84 begin
Chris@0 85 self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
Chris@0 86 self.compression = 'gzip'
Chris@0 87 rescue
Chris@0 88 self.data = plain
Chris@0 89 self.compression = ''
Chris@0 90 end
Chris@0 91 else
Chris@0 92 self.data = plain
Chris@0 93 self.compression = ''
Chris@0 94 end
Chris@0 95 plain
Chris@0 96 end
Chris@441 97
Chris@0 98 def text
Chris@1115 99 @text ||= begin
Chris@1115 100 str = case compression
Chris@1115 101 when 'gzip'
Chris@1115 102 Zlib::Inflate.inflate(data)
Chris@1115 103 else
Chris@1115 104 # uncompressed data
Chris@1115 105 data
Chris@1115 106 end
Chris@909 107 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
Chris@909 108 str
Chris@441 109 end
Chris@0 110 end
Chris@441 111
Chris@0 112 def project
Chris@0 113 page.project
Chris@0 114 end
Chris@441 115
Chris@909 116 # Return true if the content is the current page content
Chris@909 117 def current_version?
Chris@909 118 page.content.version == self.version
Chris@909 119 end
Chris@909 120
Chris@0 121 # Returns the previous version or nil
Chris@0 122 def previous
Chris@1115 123 @previous ||= WikiContent::Version.
Chris@1115 124 reorder('version DESC').
Chris@1115 125 includes(:author).
Chris@1115 126 where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
Chris@1115 127 end
Chris@1115 128
Chris@1115 129 # Returns the next version or nil
Chris@1115 130 def next
Chris@1115 131 @next ||= WikiContent::Version.
Chris@1115 132 reorder('version ASC').
Chris@1115 133 includes(:author).
Chris@1115 134 where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
Chris@1115 135 end
Chris@1115 136
Chris@1115 137 private
Chris@1115 138
Chris@1115 139 # Updates page's content if the latest version is removed
Chris@1115 140 # or destroys the page if it was the only version
Chris@1115 141 def page_update_after_destroy
Chris@1115 142 latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
Chris@1115 143 if latest && page.content.version != latest.version
Chris@1115 144 raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
Chris@1115 145 elsif latest.nil?
Chris@1115 146 raise ActiveRecord::Rollback unless page.destroy
Chris@1115 147 end
Chris@0 148 end
Chris@0 149 end
Chris@1464 150
Chris@1464 151 private
Chris@1464 152
Chris@1464 153 def send_notification
Chris@1464 154 # new_record? returns false in after_save callbacks
Chris@1464 155 if id_changed?
Chris@1464 156 if Setting.notified_events.include?('wiki_content_added')
Chris@1464 157 Mailer.wiki_content_added(self).deliver
Chris@1464 158 end
Chris@1464 159 elsif text_changed?
Chris@1464 160 if Setting.notified_events.include?('wiki_content_updated')
Chris@1464 161 Mailer.wiki_content_updated(self).deliver
Chris@1464 162 end
Chris@1464 163 end
Chris@1464 164 end
Chris@0 165 end