2019-03-16 10:37:35 +01:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 02:32:57 +01:00
|
|
|
|
2012-04-03 16:04:38 +02:00
|
|
|
# Redmine - project management software
|
2022-01-02 06:29:10 +01:00
|
|
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
2007-03-10 16:09:49 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-16 02:00:26 +02:00
|
|
|
#
|
2007-03-10 16:09:49 +01:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-05-16 02:00:26 +02:00
|
|
|
#
|
2007-03-10 16:09:49 +01:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
class WikiContent < ActiveRecord::Base
|
2012-04-27 18:28:35 +02:00
|
|
|
self.locking_column = 'version'
|
2014-10-22 20:41:03 +02:00
|
|
|
belongs_to :page, :class_name => 'WikiPage'
|
|
|
|
belongs_to :author, :class_name => 'User'
|
2017-07-26 18:41:06 +02:00
|
|
|
has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all
|
2022-02-23 22:16:18 +01:00
|
|
|
|
|
|
|
acts_as_mentionable :attributes => ['text']
|
|
|
|
|
2007-03-10 16:09:49 +01:00
|
|
|
validates_presence_of :text
|
2015-09-21 23:13:06 +02:00
|
|
|
validates_length_of :comments, :maximum => 1024, :allow_nil => true
|
2011-05-16 02:00:26 +02:00
|
|
|
|
2017-07-26 18:41:06 +02:00
|
|
|
after_save :create_version
|
2018-10-10 19:13:09 +02:00
|
|
|
after_create_commit :send_notification_create
|
|
|
|
after_update_commit :send_notification_update
|
2013-07-14 16:26:27 +02:00
|
|
|
|
2015-09-28 20:39:07 +02:00
|
|
|
scope :without_text, lambda {select(:id, :page_id, :version, :updated_on)}
|
|
|
|
|
2017-07-26 18:41:06 +02:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
if new_record?
|
|
|
|
self.version = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-13 15:26:54 +01:00
|
|
|
def visible?(user=User.current)
|
|
|
|
page.visible?(user)
|
|
|
|
end
|
2011-05-16 02:00:26 +02:00
|
|
|
|
2009-05-17 11:55:13 +02:00
|
|
|
def project
|
|
|
|
page.project
|
|
|
|
end
|
2011-05-16 02:00:26 +02:00
|
|
|
|
2010-04-11 14:56:18 +02:00
|
|
|
def attachments
|
|
|
|
page.nil? ? [] : page.attachments
|
|
|
|
end
|
2011-05-16 02:00:26 +02:00
|
|
|
|
2015-01-17 15:14:12 +01:00
|
|
|
def notified_users
|
|
|
|
project.notified_users.reject {|user| !visible?(user)}
|
|
|
|
end
|
|
|
|
|
2014-04-06 16:14:40 +02:00
|
|
|
# Returns the mail addresses of users that should be notified
|
2009-12-13 15:26:54 +01:00
|
|
|
def recipients
|
2015-01-17 15:14:12 +01:00
|
|
|
notified_users.collect(&:mail)
|
2009-12-13 15:26:54 +01:00
|
|
|
end
|
2011-05-16 02:00:26 +02:00
|
|
|
|
2011-11-28 21:12:03 +01:00
|
|
|
# Return true if the content is the current page content
|
|
|
|
def current_version?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-07-26 18:41:06 +02:00
|
|
|
# Reverts the record to a previous version
|
|
|
|
def revert_to!(version)
|
|
|
|
if version.wiki_content_id == id
|
|
|
|
update_columns(
|
2020-10-27 16:18:25 +01:00
|
|
|
:author_id => version.author_id,
|
|
|
|
:text => version.text,
|
|
|
|
:comments => version.comments,
|
|
|
|
:version => version.version,
|
|
|
|
:updated_on => version.updated_on
|
|
|
|
) && reload
|
2012-10-23 20:45:14 +02:00
|
|
|
end
|
2007-03-10 16:09:49 +01:00
|
|
|
end
|
2013-07-14 16:26:27 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-07-26 18:41:06 +02:00
|
|
|
def create_version
|
|
|
|
versions << WikiContentVersion.new(attributes.except("id"))
|
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users that a wiki page was created
|
|
|
|
def send_notification_create
|
|
|
|
if Setting.notified_events.include?('wiki_content_added')
|
|
|
|
Mailer.deliver_wiki_content_added(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Notifies users that a wiki page was updated
|
|
|
|
def send_notification_update
|
|
|
|
if Setting.notified_events.include?('wiki_content_updated') && saved_change_to_text?
|
|
|
|
Mailer.deliver_wiki_content_updated(self)
|
2013-07-14 16:26:27 +02:00
|
|
|
end
|
|
|
|
end
|
2007-03-10 16:09:49 +01:00
|
|
|
end
|