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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'diff'
  18. require 'enumerator'
  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, :deleted_attachment_ids
  37. validates_presence_of :title
  38. validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
  39. validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
  40. validates_length_of :title, maximum: 255
  41. validates_associated :content
  42. validate :validate_parent_title
  43. before_destroy :delete_redirects
  44. before_save :handle_rename_or_move, :update_wiki_start_page
  45. after_save :handle_children_move, :delete_selected_attachments
  46. # eager load information about last updates, without loading text
  47. scope :with_updated_on, lambda { preload(:content_without_text) }
  48. # Wiki pages that are protected by default
  49. DEFAULT_PROTECTED_PAGES = %w(sidebar)
  50. safe_attributes 'parent_id', 'parent_title', 'title', 'redirect_existing_links', 'wiki_id',
  51. :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
  52. safe_attributes 'is_start_page',
  53. :if => lambda {|page, user| user.allowed_to?(:manage_wiki, page.project)}
  54. safe_attributes 'deleted_attachment_ids',
  55. :if => lambda {|page, user| page.attachments_deletable?(user)}
  56. def initialize(attributes=nil, *args)
  57. super
  58. if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
  59. self.protected = true
  60. end
  61. end
  62. def visible?(user=User.current)
  63. !user.nil? && user.allowed_to?(:view_wiki_pages, project)
  64. end
  65. def title=(value)
  66. value = Wiki.titleize(value)
  67. write_attribute(:title, value)
  68. end
  69. def safe_attributes=(attrs, user=User.current)
  70. if attrs.respond_to?(:to_unsafe_hash)
  71. attrs = attrs.to_unsafe_hash
  72. end
  73. return unless attrs.is_a?(Hash)
  74. attrs = attrs.deep_dup
  75. # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
  76. if (w_id = attrs.delete('wiki_id')) && safe_attribute?('wiki_id')
  77. if (w = Wiki.find_by_id(w_id)) && w.project && user.allowed_to?(:rename_wiki_pages, w.project)
  78. self.wiki = w
  79. end
  80. end
  81. super attrs, user
  82. end
  83. # Manages redirects if page is renamed or moved
  84. def handle_rename_or_move
  85. if !new_record? && (title_changed? || wiki_id_changed?)
  86. # Update redirects that point to the old title
  87. WikiRedirect.where(:redirects_to => title_was, :redirects_to_wiki_id => wiki_id_was).each do |r|
  88. r.redirects_to = title
  89. r.redirects_to_wiki_id = wiki_id
  90. (r.title == r.redirects_to && r.wiki_id == r.redirects_to_wiki_id) ? r.destroy : r.save
  91. end
  92. # Remove redirects for the new title
  93. WikiRedirect.where(:wiki_id => wiki_id, :title => title).delete_all
  94. # Create a redirect to the new title
  95. unless redirect_existing_links == "0"
  96. WikiRedirect.create(
  97. :wiki_id => wiki_id_was, :title => title_was,
  98. :redirects_to_wiki_id => wiki_id, :redirects_to => title
  99. )
  100. end
  101. end
  102. if !new_record? && wiki_id_changed? && parent.present?
  103. unless parent.wiki_id == wiki_id
  104. self.parent_id = nil
  105. end
  106. end
  107. end
  108. private :handle_rename_or_move
  109. # Moves child pages if page was moved
  110. def handle_children_move
  111. if !new_record? && saved_change_to_wiki_id?
  112. children.each do |child|
  113. child.wiki_id = wiki_id
  114. child.redirect_existing_links = redirect_existing_links
  115. unless child.save
  116. WikiPage.where(:id => child.id).update_all :parent_id => nil
  117. end
  118. end
  119. end
  120. end
  121. private :handle_children_move
  122. # Deletes redirects to this page
  123. def delete_redirects
  124. WikiRedirect.where(:redirects_to_wiki_id => wiki_id, :redirects_to => title).delete_all
  125. end
  126. def pretty_title
  127. WikiPage.pretty_title(title)
  128. end
  129. def content_for_version(version=nil)
  130. if content
  131. result = content.versions.find_by_version(version.to_i) if version
  132. result ||= content
  133. result
  134. end
  135. end
  136. def diff(version_to=nil, version_from=nil)
  137. version_to = version_to ? version_to.to_i : self.content.version
  138. content_to = content.versions.find_by_version(version_to)
  139. content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
  140. return nil unless content_to && content_from
  141. if content_from.version > content_to.version
  142. content_to, content_from = content_from, content_to
  143. end
  144. (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
  145. end
  146. def annotate(version=nil)
  147. version = version ? version.to_i : self.content.version
  148. c = content.versions.find_by_version(version)
  149. c ? WikiAnnotate.new(c) : nil
  150. end
  151. def self.pretty_title(str)
  152. (str && str.is_a?(String)) ? str.tr('_', ' ') : str
  153. end
  154. def project
  155. wiki.try(:project)
  156. end
  157. def text
  158. content.text if content
  159. end
  160. def updated_on
  161. content_attribute(:updated_on)
  162. end
  163. def version
  164. content_attribute(:version)
  165. end
  166. # Returns true if usr is allowed to edit the page, otherwise false
  167. def editable_by?(usr)
  168. !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
  169. end
  170. def attachments_deletable?(usr=User.current)
  171. editable_by?(usr) && super(usr)
  172. end
  173. def parent_title
  174. @parent_title || (self.parent && self.parent.pretty_title)
  175. end
  176. def parent_title=(t)
  177. @parent_title = t
  178. parent_page = t.blank? ? nil : self.wiki.find_page(t)
  179. self.parent = parent_page
  180. end
  181. def is_start_page
  182. if @is_start_page.nil?
  183. @is_start_page = wiki.try(:start_page) == title_was
  184. end
  185. @is_start_page
  186. end
  187. def is_start_page=(arg)
  188. @is_start_page = arg == '1' || arg == true
  189. end
  190. def update_wiki_start_page
  191. if is_start_page
  192. wiki.update_attribute :start_page, title
  193. end
  194. end
  195. private :update_wiki_start_page
  196. # Saves the page and its content if text was changed
  197. # Return true if the page was saved
  198. def save_with_content(content)
  199. ret = nil
  200. transaction do
  201. ret = save
  202. if content.text_changed?
  203. begin
  204. self.content = content
  205. rescue ActiveRecord::RecordNotSaved
  206. ret = false
  207. end
  208. end
  209. raise ActiveRecord::Rollback unless ret
  210. end
  211. ret
  212. end
  213. def deleted_attachment_ids
  214. Array(@deleted_attachment_ids).map(&:to_i)
  215. end
  216. def delete_selected_attachments
  217. if deleted_attachment_ids.present?
  218. objects = attachments.where(:id => deleted_attachment_ids.map(&:to_i))
  219. attachments.delete(objects)
  220. end
  221. end
  222. protected
  223. def validate_parent_title
  224. errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
  225. errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
  226. if parent_id_changed? && parent && (parent.wiki_id != wiki_id)
  227. errors.add(:parent_title, :not_same_project)
  228. end
  229. end
  230. private
  231. def content_attribute(name)
  232. (association(:content).loaded? ? content : content_without_text).try(name)
  233. end
  234. end
  235. class WikiDiff < Redmine::Helpers::Diff
  236. attr_reader :content_to, :content_from
  237. def initialize(content_to, content_from)
  238. @content_to = content_to
  239. @content_from = content_from
  240. super(content_to.text, content_from.text)
  241. end
  242. end
  243. class WikiAnnotate
  244. attr_reader :lines, :content
  245. def initialize(content)
  246. @content = content
  247. current = content
  248. current_lines = current.text.split(/\r?\n/)
  249. @lines = current_lines.collect {|t| [nil, nil, t]}
  250. positions = []
  251. current_lines.size.times {|i| positions << i}
  252. while (current.previous)
  253. d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
  254. d.each_slice(3) do |s|
  255. sign, line = s[0], s[1]
  256. if sign == '+' && positions[line] && positions[line] != -1
  257. if @lines[positions[line]][0].nil?
  258. @lines[positions[line]][0] = current.version
  259. @lines[positions[line]][1] = current.author
  260. end
  261. end
  262. end
  263. d.each_slice(3) do |s|
  264. sign, line = s[0], s[1]
  265. if sign == '-'
  266. positions.insert(line, -1)
  267. else
  268. positions[line] = nil
  269. end
  270. end
  271. positions.compact!
  272. # Stop if every line is annotated
  273. break unless @lines.detect { |line| line[0].nil? }
  274. current = current.previous
  275. end
  276. @lines.each { |line|
  277. line[0] ||= current.version
  278. # if the last known version is > 1 (eg. history was cleared), we don't know the author
  279. line[1] ||= current.author if current.version == 1
  280. }
  281. end
  282. end