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 @ 1361:7c0909052511

History | View | Annotate | Download (5.36 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
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
#
9
# 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
#
14
# 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
  self.locking_column = 'version'
22
  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

    
27
  acts_as_versioned
28

    
29
  def visible?(user=User.current)
30
    page.visible?(user)
31
  end
32

    
33
  def project
34
    page.project
35
  end
36

    
37
  def attachments
38
    page.nil? ? [] : page.attachments
39
  end
40

    
41
  # 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

    
48
  # Return true if the content is the current page content
49
  def current_version?
50
    true
51
  end
52

    
53
  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
                  :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
63

    
64
    acts_as_activity_provider :type => 'wiki_edits',
65
                              :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
66
                              :author_key => "#{WikiContent.versioned_table_name}.author_id",
67
                              :permission => :view_wiki_edits,
68
                              :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
69
                                                           "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
70
                                                           "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
71
                                                           "#{WikiContent.versioned_table_name}.id",
72
                                                :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
73
                                                          "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
74
                                                          "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
75

    
76
    after_destroy :page_update_after_destroy
77

    
78
    def text=(plain)
79
      case Setting.wiki_compression
80
      when 'gzip'
81
      begin
82
        self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
83
        self.compression = 'gzip'
84
      rescue
85
        self.data = plain
86
        self.compression = ''
87
      end
88
      else
89
        self.data = plain
90
        self.compression = ''
91
      end
92
      plain
93
    end
94

    
95
    def text
96
      @text ||= begin
97
        str = case compression
98
              when 'gzip'
99
                Zlib::Inflate.inflate(data)
100
              else
101
                # uncompressed data
102
                data
103
              end
104
        str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
105
        str
106
      end
107
    end
108

    
109
    def project
110
      page.project
111
    end
112

    
113
    # Return true if the content is the current page content
114
    def current_version?
115
      page.content.version == self.version
116
    end
117

    
118
    # Returns the previous version or nil
119
    def previous
120
      @previous ||= WikiContent::Version.
121
        reorder('version DESC').
122
        includes(:author).
123
        where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
124
    end
125

    
126
    # Returns the next version or nil
127
    def next
128
      @next ||= WikiContent::Version.
129
        reorder('version ASC').
130
        includes(:author).
131
        where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
132
    end
133

    
134
    private
135

    
136
    # Updates page's content if the latest version is removed
137
    # or destroys the page if it was the only version
138
    def page_update_after_destroy
139
      latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
140
      if latest && page.content.version != latest.version
141
        raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
142
      elsif latest.nil?
143
        raise ActiveRecord::Rollback unless page.destroy
144
      end
145
    end
146
  end
147
end