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_page.rb 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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 'redmine/string_array_diff/diff'
  19. class WikiPage < ApplicationRecord
  20. include Redmine::SafeAttributes
  21. belongs_to :wiki
  22. has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id',
  23. :dependent => :destroy
  24. has_one :content_without_text, lambda {without_text.readonly},
  25. :class_name => 'WikiContent', :foreign_key => 'page_id'
  26. acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
  27. acts_as_tree :dependent => :nullify, :order => 'title'
  28. acts_as_watchable
  29. acts_as_event(
  30. :title => proc {|o| "#{l(:label_wiki)}: #{o.title}"},
  31. :description => :text,
  32. :datetime => :created_on,
  33. :url =>
  34. proc do |o|
  35. {:controller => 'wiki', :action => 'show',
  36. :project_id => o.wiki.project, :id => o.title}
  37. end
  38. )
  39. acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"],
  40. :scope => joins(:content, {:wiki => :project}),
  41. :preload => [:content, {:wiki => :project}],
  42. :permission => :view_wiki_pages,
  43. :project_key => "#{Wiki.table_name}.project_id"
  44. attr_accessor :redirect_existing_links
  45. attr_writer :deleted_attachment_ids
  46. validates_presence_of :title
  47. validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
  48. validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
  49. validates_length_of :title, maximum: 255
  50. validates_associated :content
  51. validate :validate_parent_title
  52. before_destroy :delete_redirects
  53. before_save :handle_rename_or_move, :update_wiki_start_page
  54. after_save :handle_children_move, :delete_selected_attachments
  55. # eager load information about last updates, without loading text
  56. scope :with_updated_on, lambda {preload(:content_without_text)}
  57. # Wiki pages that are protected by default
  58. DEFAULT_PROTECTED_PAGES = %w(sidebar)
  59. safe_attributes 'parent_id', 'parent_title', 'title', 'redirect_existing_links', 'wiki_id',
  60. :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
  61. safe_attributes 'is_start_page',
  62. :if => lambda {|page, user| user.allowed_to?(:manage_wiki, page.project)}
  63. safe_attributes 'deleted_attachment_ids',
  64. :if => lambda {|page, user| page.attachments_deletable?(user)}
  65. def initialize(attributes=nil, *args)
  66. super
  67. if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
  68. self.protected = true
  69. end
  70. end
  71. def visible?(user=User.current)
  72. !user.nil? && user.allowed_to?(:view_wiki_pages, project)
  73. end
  74. def title=(value)
  75. value = Wiki.titleize(value)
  76. write_attribute(:title, value)
  77. end
  78. def safe_attributes=(attrs, user=User.current)
  79. if attrs.respond_to?(:to_unsafe_hash)
  80. attrs = attrs.to_unsafe_hash
  81. end
  82. return unless attrs.is_a?(Hash)
  83. attrs = attrs.deep_dup
  84. # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
  85. if (w_id = attrs.delete('wiki_id')) && safe_attribute?('wiki_id')
  86. if (w = Wiki.find_by_id(w_id)) && w.project && user.allowed_to?(:rename_wiki_pages, w.project)
  87. self.wiki = w
  88. end
  89. end
  90. super(attrs, user)
  91. end
  92. # Manages redirects if page is renamed or moved
  93. def handle_rename_or_move
  94. if !new_record? && (title_changed? || wiki_id_changed?)
  95. # Update redirects that point to the old title
  96. WikiRedirect.where(:redirects_to => title_was, :redirects_to_wiki_id => wiki_id_was).each do |r|
  97. r.redirects_to = title
  98. r.redirects_to_wiki_id = wiki_id
  99. (r.title == r.redirects_to && r.wiki_id == r.redirects_to_wiki_id) ? r.destroy : r.save
  100. end
  101. # Remove redirects for the new title
  102. WikiRedirect.where(:wiki_id => wiki_id, :title => title).delete_all
  103. # Create a redirect to the new title
  104. unless redirect_existing_links == "0"
  105. WikiRedirect.create(
  106. :wiki_id => wiki_id_was, :title => title_was,
  107. :redirects_to_wiki_id => wiki_id, :redirects_to => title
  108. )
  109. end
  110. end
  111. if !new_record? && wiki_id_changed? && parent.present?
  112. unless parent.wiki_id == wiki_id
  113. self.parent_id = nil
  114. end
  115. end
  116. end
  117. private :handle_rename_or_move
  118. # Moves child pages if page was moved
  119. def handle_children_move
  120. if !new_record? && saved_change_to_wiki_id?
  121. children.each do |child|
  122. child.wiki_id = wiki_id
  123. child.redirect_existing_links = redirect_existing_links
  124. unless child.save
  125. WikiPage.where(:id => child.id).update_all :parent_id => nil
  126. end
  127. end
  128. end
  129. end
  130. private :handle_children_move
  131. # Deletes redirects to this page
  132. def delete_redirects
  133. WikiRedirect.where(:redirects_to_wiki_id => wiki_id, :redirects_to => title).delete_all
  134. end
  135. def pretty_title
  136. WikiPage.pretty_title(title)
  137. end
  138. def content_for_version(version=nil)
  139. (content && version) ? content.versions.find_by_version(version.to_i) : content
  140. end
  141. def diff(version_to=nil, version_from=nil)
  142. version_to = version_to ? version_to.to_i : self.content.version
  143. content_to = content.versions.find_by_version(version_to)
  144. content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
  145. return nil unless content_to && content_from
  146. if content_from.version > content_to.version
  147. content_to, content_from = content_from, content_to
  148. end
  149. (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
  150. end
  151. def annotate(version=nil)
  152. version = version ? version.to_i : self.content.version
  153. c = content.versions.find_by_version(version)
  154. c ? WikiAnnotate.new(c) : nil
  155. end
  156. def self.pretty_title(str)
  157. (str && str.is_a?(String)) ? str.tr('_', ' ') : str
  158. end
  159. def project
  160. wiki.try(:project)
  161. end
  162. def text
  163. content.text if content
  164. end
  165. def updated_on
  166. content_attribute(:updated_on)
  167. end
  168. def version
  169. content_attribute(:version)
  170. end
  171. # Returns true if usr is allowed to edit the page, otherwise false
  172. def editable_by?(usr)
  173. !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
  174. end
  175. def attachments_deletable?(usr=User.current)
  176. editable_by?(usr) && super(usr)
  177. end
  178. def parent_title
  179. @parent_title || (self.parent && self.parent.pretty_title)
  180. end
  181. def parent_title=(t)
  182. @parent_title = t
  183. parent_page = t.blank? ? nil : self.wiki.find_page(t)
  184. self.parent = parent_page
  185. end
  186. def is_start_page
  187. if @is_start_page.nil?
  188. @is_start_page = wiki.try(:start_page) == title_was
  189. end
  190. @is_start_page
  191. end
  192. def is_start_page=(arg)
  193. @is_start_page = arg == '1' || arg == true
  194. end
  195. def update_wiki_start_page
  196. if is_start_page
  197. wiki.update_attribute :start_page, title
  198. end
  199. end
  200. private :update_wiki_start_page
  201. # Saves the page and its content if text was changed
  202. # Return true if the page was saved
  203. def save_with_content(content)
  204. ret = nil
  205. transaction do
  206. ret = save
  207. if content.text_changed?
  208. begin
  209. self.content = content
  210. rescue ActiveRecord::RecordNotSaved
  211. ret = false
  212. end
  213. end
  214. raise ActiveRecord::Rollback unless ret
  215. end
  216. ret
  217. end
  218. def deleted_attachment_ids
  219. Array(@deleted_attachment_ids).map(&:to_i)
  220. end
  221. def delete_selected_attachments
  222. if deleted_attachment_ids.present?
  223. objects = attachments.where(:id => deleted_attachment_ids.map(&:to_i))
  224. attachments.delete(objects)
  225. end
  226. end
  227. protected
  228. def validate_parent_title
  229. errors.add(:parent_title, :invalid) if @parent_title.present? && parent.nil?
  230. errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
  231. if parent_id_changed? && parent && (parent.wiki_id != wiki_id)
  232. errors.add(:parent_title, :not_same_project)
  233. end
  234. end
  235. private
  236. def content_attribute(name)
  237. (association(:content).loaded? ? content : content_without_text).try(name)
  238. end
  239. end