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