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.1KB

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