You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki_content_version.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'zlib'
  19. class WikiContentVersion < ActiveRecord::Base
  20. belongs_to :page, :class_name => 'WikiPage'
  21. belongs_to :author, :class_name => 'User'
  22. acts_as_event(
  23. :title =>
  24. Proc.new do |o|
  25. "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"
  26. end,
  27. :description => :comments,
  28. :datetime => :updated_on,
  29. :type => 'wiki-page',
  30. :group => :page,
  31. :url =>
  32. Proc.new do |o|
  33. {:controller => 'wiki', :action => 'show',
  34. :project_id => o.page.wiki.project,
  35. :id => o.page.title, :version => o.version}
  36. end
  37. )
  38. acts_as_activity_provider(
  39. :type => 'wiki_edits',
  40. :timestamp => "#{table_name}.updated_on",
  41. :author_key => "#{table_name}.author_id",
  42. :permission => :view_wiki_edits,
  43. :scope =>
  44. proc do
  45. select("#{table_name}.updated_on, #{table_name}.comments, " \
  46. "#{table_name}.version, #{WikiPage.table_name}.title, " \
  47. "#{table_name}.page_id, #{table_name}.author_id, " \
  48. "#{table_name}.id").
  49. joins("LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{table_name}.page_id " \
  50. "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " \
  51. "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id")
  52. end
  53. )
  54. after_destroy :page_update_after_destroy
  55. def text=(plain)
  56. case Setting.wiki_compression
  57. when 'gzip'
  58. begin
  59. self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
  60. self.compression = 'gzip'
  61. rescue
  62. self.data = plain
  63. self.compression = ''
  64. end
  65. else
  66. self.data = plain
  67. self.compression = ''
  68. end
  69. plain
  70. end
  71. def text
  72. @text ||= begin
  73. str = case compression
  74. when 'gzip'
  75. Zlib::Inflate.inflate(data)
  76. else
  77. # uncompressed data
  78. data
  79. end
  80. str.force_encoding("UTF-8")
  81. str
  82. end
  83. end
  84. def project
  85. page.project
  86. end
  87. def attachments
  88. page.nil? ? [] : page.attachments
  89. end
  90. # Return true if the content is the current page content
  91. def current_version?
  92. page.content.version == self.version
  93. end
  94. # Returns the previous version or nil
  95. def previous
  96. @previous ||= WikiContentVersion.
  97. reorder(version: :desc).
  98. includes(:author).
  99. where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
  100. end
  101. # Returns the next version or nil
  102. def next
  103. @next ||= WikiContentVersion.
  104. reorder(version: :asc).
  105. includes(:author).
  106. where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
  107. end
  108. private
  109. # Updates page's content if the latest version is removed
  110. # or destroys the page if it was the only version
  111. def page_update_after_destroy
  112. latest = page.content.versions.reorder(version: :desc).first
  113. if latest && page.content.version != latest.version
  114. raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
  115. elsif latest.nil?
  116. raise ActiveRecord::Rollback unless page.destroy
  117. end
  118. end
  119. end