@@ -1,170 +1,174 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 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' |
|
22 | belongs_to :page, :class_name => 'WikiPage' | |
23 | belongs_to :author, :class_name => 'User' |
|
23 | belongs_to :author, :class_name => 'User' | |
24 | validates_presence_of :text |
|
24 | validates_presence_of :text | |
25 | validates_length_of :comments, :maximum => 1024, :allow_nil => true |
|
25 | validates_length_of :comments, :maximum => 1024, :allow_nil => true | |
26 | attr_protected :id |
|
26 | attr_protected :id | |
27 |
|
27 | |||
28 | acts_as_versioned |
|
28 | acts_as_versioned | |
29 |
|
29 | |||
30 | after_save :send_notification |
|
30 | after_save :send_notification | |
31 |
|
31 | |||
32 | scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)} |
|
32 | scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)} | |
33 |
|
33 | |||
34 | def visible?(user=User.current) |
|
34 | def visible?(user=User.current) | |
35 | page.visible?(user) |
|
35 | page.visible?(user) | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def project |
|
38 | def project | |
39 | page.project |
|
39 | page.project | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def attachments |
|
42 | def attachments | |
43 | page.nil? ? [] : page.attachments |
|
43 | page.nil? ? [] : page.attachments | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def notified_users |
|
46 | def notified_users | |
47 | project.notified_users.reject {|user| !visible?(user)} |
|
47 | project.notified_users.reject {|user| !visible?(user)} | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | # Returns the mail addresses of users that should be notified |
|
50 | # Returns the mail addresses of users that should be notified | |
51 | def recipients |
|
51 | def recipients | |
52 | notified_users.collect(&:mail) |
|
52 | notified_users.collect(&:mail) | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | # Return true if the content is the current page content |
|
55 | # Return true if the content is the current page content | |
56 | def current_version? |
|
56 | def current_version? | |
57 | true |
|
57 | true | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | class Version |
|
60 | class Version | |
61 | belongs_to :page, :class_name => '::WikiPage' |
|
61 | belongs_to :page, :class_name => '::WikiPage' | |
62 | belongs_to :author, :class_name => '::User' |
|
62 | belongs_to :author, :class_name => '::User' | |
63 | attr_protected :data |
|
63 | attr_protected :data | |
64 |
|
64 | |||
65 | acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"}, |
|
65 | acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"}, | |
66 | :description => :comments, |
|
66 | :description => :comments, | |
67 | :datetime => :updated_on, |
|
67 | :datetime => :updated_on, | |
68 | :type => 'wiki-page', |
|
68 | :type => 'wiki-page', | |
69 | :group => :page, |
|
69 | :group => :page, | |
70 | :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}} |
|
70 | :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}} | |
71 |
|
71 | |||
72 | acts_as_activity_provider :type => 'wiki_edits', |
|
72 | acts_as_activity_provider :type => 'wiki_edits', | |
73 | :timestamp => "#{WikiContent.versioned_table_name}.updated_on", |
|
73 | :timestamp => "#{WikiContent.versioned_table_name}.updated_on", | |
74 | :author_key => "#{WikiContent.versioned_table_name}.author_id", |
|
74 | :author_key => "#{WikiContent.versioned_table_name}.author_id", | |
75 | :permission => :view_wiki_edits, |
|
75 | :permission => :view_wiki_edits, | |
76 | :scope => select("#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " + |
|
76 | :scope => select("#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " + | |
77 | "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " + |
|
77 | "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " + | |
78 | "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " + |
|
78 | "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " + | |
79 | "#{WikiContent.versioned_table_name}.id"). |
|
79 | "#{WikiContent.versioned_table_name}.id"). | |
80 | joins("LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " + |
|
80 | joins("LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " + | |
81 | "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " + |
|
81 | "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " + | |
82 | "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id") |
|
82 | "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id") | |
83 |
|
83 | |||
84 | after_destroy :page_update_after_destroy |
|
84 | after_destroy :page_update_after_destroy | |
85 |
|
85 | |||
86 | def text=(plain) |
|
86 | def text=(plain) | |
87 | case Setting.wiki_compression |
|
87 | case Setting.wiki_compression | |
88 | when 'gzip' |
|
88 | when 'gzip' | |
89 | begin |
|
89 | begin | |
90 | self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION) |
|
90 | self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION) | |
91 | self.compression = 'gzip' |
|
91 | self.compression = 'gzip' | |
92 | rescue |
|
92 | rescue | |
93 | self.data = plain |
|
93 | self.data = plain | |
94 | self.compression = '' |
|
94 | self.compression = '' | |
95 | end |
|
95 | end | |
96 | else |
|
96 | else | |
97 | self.data = plain |
|
97 | self.data = plain | |
98 | self.compression = '' |
|
98 | self.compression = '' | |
99 | end |
|
99 | end | |
100 | plain |
|
100 | plain | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | def text |
|
103 | def text | |
104 | @text ||= begin |
|
104 | @text ||= begin | |
105 | str = case compression |
|
105 | str = case compression | |
106 | when 'gzip' |
|
106 | when 'gzip' | |
107 | Zlib::Inflate.inflate(data) |
|
107 | Zlib::Inflate.inflate(data) | |
108 | else |
|
108 | else | |
109 | # uncompressed data |
|
109 | # uncompressed data | |
110 | data |
|
110 | data | |
111 | end |
|
111 | end | |
112 | str.force_encoding("UTF-8") |
|
112 | str.force_encoding("UTF-8") | |
113 | str |
|
113 | str | |
114 | end |
|
114 | end | |
115 | end |
|
115 | end | |
116 |
|
116 | |||
117 | def project |
|
117 | def project | |
118 | page.project |
|
118 | page.project | |
119 | end |
|
119 | end | |
120 |
|
120 | |||
|
121 | def attachments | |||
|
122 | page.nil? ? [] : page.attachments | |||
|
123 | end | |||
|
124 | ||||
121 | # Return true if the content is the current page content |
|
125 | # Return true if the content is the current page content | |
122 | def current_version? |
|
126 | def current_version? | |
123 | page.content.version == self.version |
|
127 | page.content.version == self.version | |
124 | end |
|
128 | end | |
125 |
|
129 | |||
126 | # Returns the previous version or nil |
|
130 | # Returns the previous version or nil | |
127 | def previous |
|
131 | def previous | |
128 | @previous ||= WikiContent::Version. |
|
132 | @previous ||= WikiContent::Version. | |
129 | reorder('version DESC'). |
|
133 | reorder('version DESC'). | |
130 | includes(:author). |
|
134 | includes(:author). | |
131 | where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first |
|
135 | where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first | |
132 | end |
|
136 | end | |
133 |
|
137 | |||
134 | # Returns the next version or nil |
|
138 | # Returns the next version or nil | |
135 | def next |
|
139 | def next | |
136 | @next ||= WikiContent::Version. |
|
140 | @next ||= WikiContent::Version. | |
137 | reorder('version ASC'). |
|
141 | reorder('version ASC'). | |
138 | includes(:author). |
|
142 | includes(:author). | |
139 | where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first |
|
143 | where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first | |
140 | end |
|
144 | end | |
141 |
|
145 | |||
142 | private |
|
146 | private | |
143 |
|
147 | |||
144 | # Updates page's content if the latest version is removed |
|
148 | # Updates page's content if the latest version is removed | |
145 | # or destroys the page if it was the only version |
|
149 | # or destroys the page if it was the only version | |
146 | def page_update_after_destroy |
|
150 | def page_update_after_destroy | |
147 | latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first |
|
151 | latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first | |
148 | if latest && page.content.version != latest.version |
|
152 | if latest && page.content.version != latest.version | |
149 | raise ActiveRecord::Rollback unless page.content.revert_to!(latest) |
|
153 | raise ActiveRecord::Rollback unless page.content.revert_to!(latest) | |
150 | elsif latest.nil? |
|
154 | elsif latest.nil? | |
151 | raise ActiveRecord::Rollback unless page.destroy |
|
155 | raise ActiveRecord::Rollback unless page.destroy | |
152 | end |
|
156 | end | |
153 | end |
|
157 | end | |
154 | end |
|
158 | end | |
155 |
|
159 | |||
156 | private |
|
160 | private | |
157 |
|
161 | |||
158 | def send_notification |
|
162 | def send_notification | |
159 | # new_record? returns false in after_save callbacks |
|
163 | # new_record? returns false in after_save callbacks | |
160 | if id_changed? |
|
164 | if id_changed? | |
161 | if Setting.notified_events.include?('wiki_content_added') |
|
165 | if Setting.notified_events.include?('wiki_content_added') | |
162 | Mailer.wiki_content_added(self).deliver |
|
166 | Mailer.wiki_content_added(self).deliver | |
163 | end |
|
167 | end | |
164 | elsif text_changed? |
|
168 | elsif text_changed? | |
165 | if Setting.notified_events.include?('wiki_content_updated') |
|
169 | if Setting.notified_events.include?('wiki_content_updated') | |
166 | Mailer.wiki_content_updated(self).deliver |
|
170 | Mailer.wiki_content_updated(self).deliver | |
167 | end |
|
171 | end | |
168 | end |
|
172 | end | |
169 | end |
|
173 | end | |
170 | end |
|
174 | end |
General Comments 0
You need to be logged in to leave comments.
Login now