Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 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@0
|
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@0
|
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@0
|
21 set_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@0
|
26
|
Chris@0
|
27 acts_as_versioned
|
Chris@0
|
28
|
Chris@0
|
29 def visible?(user=User.current)
|
Chris@0
|
30 page.visible?(user)
|
Chris@0
|
31 end
|
Chris@0
|
32
|
Chris@0
|
33 def project
|
Chris@0
|
34 page.project
|
Chris@0
|
35 end
|
Chris@0
|
36
|
Chris@0
|
37 def attachments
|
Chris@0
|
38 page.nil? ? [] : page.attachments
|
Chris@0
|
39 end
|
Chris@0
|
40
|
Chris@0
|
41 # Returns the mail adresses of users that should be notified
|
Chris@0
|
42 def recipients
|
Chris@0
|
43 notified = project.notified_users
|
Chris@0
|
44 notified.reject! {|user| !visible?(user)}
|
Chris@0
|
45 notified.collect(&:mail)
|
Chris@0
|
46 end
|
Chris@0
|
47
|
Chris@0
|
48 class Version
|
Chris@0
|
49 belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id'
|
Chris@0
|
50 belongs_to :author, :class_name => '::User', :foreign_key => 'author_id'
|
Chris@0
|
51 attr_protected :data
|
Chris@0
|
52
|
Chris@0
|
53 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
|
Chris@0
|
54 :description => :comments,
|
Chris@0
|
55 :datetime => :updated_on,
|
Chris@0
|
56 :type => 'wiki-page',
|
chris@37
|
57 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
|
Chris@0
|
58
|
Chris@0
|
59 acts_as_activity_provider :type => 'wiki_edits',
|
Chris@0
|
60 :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
|
Chris@0
|
61 :author_key => "#{WikiContent.versioned_table_name}.author_id",
|
Chris@0
|
62 :permission => :view_wiki_edits,
|
Chris@0
|
63 :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
|
Chris@0
|
64 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
|
Chris@0
|
65 "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
|
Chris@0
|
66 "#{WikiContent.versioned_table_name}.id",
|
Chris@0
|
67 :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
|
Chris@0
|
68 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
|
Chris@0
|
69 "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
|
Chris@0
|
70
|
Chris@0
|
71 def text=(plain)
|
Chris@0
|
72 case Setting.wiki_compression
|
Chris@0
|
73 when 'gzip'
|
Chris@0
|
74 begin
|
Chris@0
|
75 self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
|
Chris@0
|
76 self.compression = 'gzip'
|
Chris@0
|
77 rescue
|
Chris@0
|
78 self.data = plain
|
Chris@0
|
79 self.compression = ''
|
Chris@0
|
80 end
|
Chris@0
|
81 else
|
Chris@0
|
82 self.data = plain
|
Chris@0
|
83 self.compression = ''
|
Chris@0
|
84 end
|
Chris@0
|
85 plain
|
Chris@0
|
86 end
|
Chris@0
|
87
|
Chris@0
|
88 def text
|
Chris@0
|
89 @text ||= case compression
|
Chris@0
|
90 when 'gzip'
|
Chris@0
|
91 Zlib::Inflate.inflate(data)
|
Chris@0
|
92 else
|
Chris@0
|
93 # uncompressed data
|
Chris@0
|
94 data
|
Chris@0
|
95 end
|
Chris@0
|
96 end
|
Chris@0
|
97
|
Chris@0
|
98 def project
|
Chris@0
|
99 page.project
|
Chris@0
|
100 end
|
Chris@0
|
101
|
Chris@0
|
102 # Returns the previous version or nil
|
Chris@0
|
103 def previous
|
Chris@0
|
104 @previous ||= WikiContent::Version.find(:first,
|
Chris@0
|
105 :order => 'version DESC',
|
Chris@0
|
106 :include => :author,
|
Chris@0
|
107 :conditions => ["wiki_content_id = ? AND version < ?", wiki_content_id, version])
|
Chris@0
|
108 end
|
Chris@0
|
109 end
|
Chris@0
|
110 end
|