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

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