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 @ 1298:4f746d8966dd

History | View | Annotate | Download (5.39 KB)

1 1115:433d4f72a19b Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  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 0:513646585e45 Chris
  def visible?(user=User.current)
30
    page.visible?(user)
31
  end
32 441:cbce1fd3b1b7 Chris
33 0:513646585e45 Chris
  def project
34
    page.project
35
  end
36 441:cbce1fd3b1b7 Chris
37 0:513646585e45 Chris
  def attachments
38
    page.nil? ? [] : page.attachments
39
  end
40 441:cbce1fd3b1b7 Chris
41 0:513646585e45 Chris
  # Returns the mail adresses of users that should be notified
42
  def recipients
43
    notified = project.notified_users
44
    notified.reject! {|user| !visible?(user)}
45
    notified.collect(&:mail)
46
  end
47 441:cbce1fd3b1b7 Chris
48 909:cbb26bc654de Chris
  # Return true if the content is the current page content
49
  def current_version?
50
    true
51
  end
52
53 0:513646585e45 Chris
  class Version
54
    belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id'
55
    belongs_to :author, :class_name => '::User', :foreign_key => 'author_id'
56
    attr_protected :data
57
58
    acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
59
                  :description => :comments,
60
                  :datetime => :updated_on,
61
                  :type => 'wiki-page',
62 1295:622f24f53b42 Chris
                  :group => :page,
63 37:94944d00e43c chris
                  :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
64 0:513646585e45 Chris
65
    acts_as_activity_provider :type => 'wiki_edits',
66
                              :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
67
                              :author_key => "#{WikiContent.versioned_table_name}.author_id",
68
                              :permission => :view_wiki_edits,
69
                              :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
70
                                                           "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
71
                                                           "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
72
                                                           "#{WikiContent.versioned_table_name}.id",
73
                                                :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
74
                                                          "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
75
                                                          "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
76
77 1115:433d4f72a19b Chris
    after_destroy :page_update_after_destroy
78
79 0:513646585e45 Chris
    def text=(plain)
80
      case Setting.wiki_compression
81
      when 'gzip'
82
      begin
83
        self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
84
        self.compression = 'gzip'
85
      rescue
86
        self.data = plain
87
        self.compression = ''
88
      end
89
      else
90
        self.data = plain
91
        self.compression = ''
92
      end
93
      plain
94
    end
95 441:cbce1fd3b1b7 Chris
96 0:513646585e45 Chris
    def text
97 1115:433d4f72a19b Chris
      @text ||= begin
98
        str = case compression
99
              when 'gzip'
100
                Zlib::Inflate.inflate(data)
101
              else
102
                # uncompressed data
103
                data
104
              end
105 909:cbb26bc654de Chris
        str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
106
        str
107 441:cbce1fd3b1b7 Chris
      end
108 0:513646585e45 Chris
    end
109 441:cbce1fd3b1b7 Chris
110 0:513646585e45 Chris
    def project
111
      page.project
112
    end
113 441:cbce1fd3b1b7 Chris
114 909:cbb26bc654de Chris
    # Return true if the content is the current page content
115
    def current_version?
116
      page.content.version == self.version
117
    end
118
119 0:513646585e45 Chris
    # Returns the previous version or nil
120
    def previous
121 1115:433d4f72a19b Chris
      @previous ||= WikiContent::Version.
122
        reorder('version DESC').
123
        includes(:author).
124
        where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
125
    end
126
127
    # Returns the next version or nil
128
    def next
129
      @next ||= WikiContent::Version.
130
        reorder('version ASC').
131
        includes(:author).
132
        where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
133
    end
134
135
    private
136
137
    # Updates page's content if the latest version is removed
138
    # or destroys the page if it was the only version
139
    def page_update_after_destroy
140
      latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
141
      if latest && page.content.version != latest.version
142
        raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
143
      elsif latest.nil?
144
        raise ActiveRecord::Rollback unless page.destroy
145
      end
146 0:513646585e45 Chris
    end
147
  end
148
end