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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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 :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
  23. :description => :comments,
  24. :datetime => :updated_on,
  25. :type => 'wiki-page',
  26. :group => :page,
  27. :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
  28. acts_as_activity_provider :type => 'wiki_edits',
  29. :timestamp => "#{table_name}.updated_on",
  30. :author_key => "#{table_name}.author_id",
  31. :permission => :view_wiki_edits,
  32. :scope => proc {
  33. select("#{table_name}.updated_on, #{table_name}.comments, " +
  34. "#{table_name}.version, #{WikiPage.table_name}.title, " +
  35. "#{table_name}.page_id, #{table_name}.author_id, " +
  36. "#{table_name}.id").
  37. joins("LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{table_name}.page_id " +
  38. "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
  39. "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id")
  40. }
  41. after_destroy :page_update_after_destroy
  42. def text=(plain)
  43. case Setting.wiki_compression
  44. when 'gzip'
  45. begin
  46. self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
  47. self.compression = 'gzip'
  48. rescue
  49. self.data = plain
  50. self.compression = ''
  51. end
  52. else
  53. self.data = plain
  54. self.compression = ''
  55. end
  56. plain
  57. end
  58. def text
  59. @text ||= begin
  60. str = case compression
  61. when 'gzip'
  62. Zlib::Inflate.inflate(data)
  63. else
  64. # uncompressed data
  65. data
  66. end
  67. str.force_encoding("UTF-8")
  68. str
  69. end
  70. end
  71. def project
  72. page.project
  73. end
  74. def attachments
  75. page.nil? ? [] : page.attachments
  76. end
  77. # Return true if the content is the current page content
  78. def current_version?
  79. page.content.version == self.version
  80. end
  81. # Returns the previous version or nil
  82. def previous
  83. @previous ||= WikiContentVersion.
  84. reorder(version: :desc).
  85. includes(:author).
  86. where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
  87. end
  88. # Returns the next version or nil
  89. def next
  90. @next ||= WikiContentVersion.
  91. reorder(version: :asc).
  92. includes(:author).
  93. where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
  94. end
  95. private
  96. # Updates page's content if the latest version is removed
  97. # or destroys the page if it was the only version
  98. def page_update_after_destroy
  99. latest = page.content.versions.reorder(version: :desc).first
  100. if latest && page.content.version != latest.version
  101. raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
  102. elsif latest.nil?
  103. raise ActiveRecord::Rollback unless page.destroy
  104. end
  105. end
  106. end