To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / wiki_content.rb @ 1533:59e13100ea95

History | View | Annotate | Download (5.81 KB)

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