wiki_content.rb 5.76 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2016  Jean-Philippe Lang
jplang's avatar
jplang committed
3 4 5 6 7
#
# 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.
8
#
jplang's avatar
jplang committed
9 10 11 12
# 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.
13
#
jplang's avatar
jplang committed
14 15 16 17 18 19 20
# 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
21
  self.locking_column = 'version'
22 23
  belongs_to :page, :class_name => 'WikiPage'
  belongs_to :author, :class_name => 'User'
jplang's avatar
jplang committed
24
  validates_presence_of :text
25
  validates_length_of :comments, :maximum => 1024, :allow_nil => true
jplang's avatar
jplang committed
26
  attr_protected :id
27

jplang's avatar
jplang committed
28
  acts_as_versioned
29

30 31
  after_save :send_notification

32 33
  scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)}

34 35 36
  def visible?(user=User.current)
    page.visible?(user)
  end
37

38 39 40
  def project
    page.project
  end
41

42 43 44
  def attachments
    page.nil? ? [] : page.attachments
  end
45

46 47 48 49
  def notified_users
    project.notified_users.reject {|user| !visible?(user)}
  end

50
  # Returns the mail addresses of users that should be notified
51
  def recipients
52
    notified_users.collect(&:mail)
53
  end
54

55 56 57 58 59
  # Return true if the content is the current page content
  def current_version?
    true
  end

jplang's avatar
jplang committed
60
  class Version
61 62
    belongs_to :page, :class_name => '::WikiPage'
    belongs_to :author, :class_name => '::User'
jplang's avatar
jplang committed
63
    attr_protected :data
jplang's avatar
jplang committed
64 65 66 67

    acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
                  :description => :comments,
                  :datetime => :updated_on,
68
                  :type => 'wiki-page',
69
                  :group => :page,
70
                  :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
jplang's avatar
jplang committed
71

72
    acts_as_activity_provider :type => 'wiki_edits',
jplang's avatar
jplang committed
73
                              :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
74
                              :author_key => "#{WikiContent.versioned_table_name}.author_id",
75
                              :permission => :view_wiki_edits,
jplang's avatar
jplang committed
76 77 78 79 80 81 82
                              :scope => 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")
jplang's avatar
jplang committed
83

84 85
    after_destroy :page_update_after_destroy

jplang's avatar
jplang committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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
102

jplang's avatar
jplang committed
103
    def text
104 105 106 107 108 109 110 111
      @text ||= begin
        str = case compression
              when 'gzip'
                Zlib::Inflate.inflate(data)
              else
                # uncompressed data
                data
              end
jplang's avatar
jplang committed
112
        str.force_encoding("UTF-8")
113
        str
114
      end
jplang's avatar
jplang committed
115
    end
116

jplang's avatar
jplang committed
117 118 119
    def project
      page.project
    end
120

121 122 123 124
    def attachments
      page.nil? ? [] : page.attachments
    end

125 126 127 128 129
    # Return true if the content is the current page content
    def current_version?
      page.content.version == self.version
    end

130 131
    # Returns the previous version or nil
    def previous
132 133 134 135 136 137 138 139 140 141 142 143
      @previous ||= WikiContent::Version.
        reorder('version DESC').
        includes(:author).
        where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
    end

    # Returns the next version or nil
    def next
      @next ||= WikiContent::Version.
        reorder('version ASC').
        includes(:author).
        where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
144
    end
145 146 147 148 149 150 151 152 153 154 155 156 157

    private

    # Updates page's content if the latest version is removed
    # or destroys the page if it was the only version
    def page_update_after_destroy
      latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
      if latest && page.content.version != latest.version
        raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
      elsif latest.nil?
        raise ActiveRecord::Rollback unless page.destroy
      end
    end
jplang's avatar
jplang committed
158
  end
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  private

  def send_notification
    # new_record? returns false in after_save callbacks
    if id_changed?
      if Setting.notified_events.include?('wiki_content_added')
        Mailer.wiki_content_added(self).deliver
      end
    elsif text_changed?
      if Setting.notified_events.include?('wiki_content_updated')
        Mailer.wiki_content_updated(self).deliver
      end
    end
  end
jplang's avatar
jplang committed
174
end