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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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. acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
  24. acts_as_tree :dependent => :nullify, :order => 'title'
  25. acts_as_watchable
  26. acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
  27. :description => :text,
  28. :datetime => :created_on,
  29. :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
  30. acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"],
  31. :include => [{:wiki => :project}, :content],
  32. :permission => :view_wiki_pages,
  33. :project_key => "#{Wiki.table_name}.project_id"
  34. attr_accessor :redirect_existing_links
  35. validates_presence_of :title
  36. validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/
  37. validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
  38. validates_associated :content
  39. validate :validate_parent_title
  40. before_destroy :remove_redirects
  41. before_save :handle_redirects
  42. # eager load information about last updates, without loading text
  43. scope :with_updated_on, lambda {
  44. select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version").
  45. joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id")
  46. }
  47. # Wiki pages that are protected by default
  48. DEFAULT_PROTECTED_PAGES = %w(sidebar)
  49. safe_attributes 'parent_id', 'parent_title',
  50. :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)}
  51. def initialize(attributes=nil, *args)
  52. super
  53. if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
  54. self.protected = true
  55. end
  56. end
  57. def visible?(user=User.current)
  58. !user.nil? && user.allowed_to?(:view_wiki_pages, project)
  59. end
  60. def title=(value)
  61. value = Wiki.titleize(value)
  62. @previous_title = read_attribute(:title) if @previous_title.blank?
  63. write_attribute(:title, value)
  64. end
  65. def handle_redirects
  66. self.title = Wiki.titleize(title)
  67. # Manage redirects if the title has changed
  68. if !@previous_title.blank? && (@previous_title != title) && !new_record?
  69. # Update redirects that point to the old title
  70. wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
  71. r.redirects_to = title
  72. r.title == r.redirects_to ? r.destroy : r.save
  73. end
  74. # Remove redirects for the new title
  75. wiki.redirects.find_all_by_title(title).each(&:destroy)
  76. # Create a redirect to the new title
  77. wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
  78. @previous_title = nil
  79. end
  80. end
  81. def remove_redirects
  82. # Remove redirects to this page
  83. wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
  84. end
  85. def pretty_title
  86. WikiPage.pretty_title(title)
  87. end
  88. def content_for_version(version=nil)
  89. result = content.versions.find_by_version(version.to_i) if version
  90. result ||= content
  91. result
  92. end
  93. def diff(version_to=nil, version_from=nil)
  94. version_to = version_to ? version_to.to_i : self.content.version
  95. content_to = content.versions.find_by_version(version_to)
  96. content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
  97. return nil unless content_to && content_from
  98. if content_from.version > content_to.version
  99. content_to, content_from = content_from, content_to
  100. end
  101. (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
  102. end
  103. def annotate(version=nil)
  104. version = version ? version.to_i : self.content.version
  105. c = content.versions.find_by_version(version)
  106. c ? WikiAnnotate.new(c) : nil
  107. end
  108. def self.pretty_title(str)
  109. (str && str.is_a?(String)) ? str.tr('_', ' ') : str
  110. end
  111. def project
  112. wiki.project
  113. end
  114. def text
  115. content.text if content
  116. end
  117. def updated_on
  118. unless @updated_on
  119. if time = read_attribute(:updated_on)
  120. # content updated_on was eager loaded with the page
  121. begin
  122. @updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime)
  123. rescue
  124. end
  125. else
  126. @updated_on = content && content.updated_on
  127. end
  128. end
  129. @updated_on
  130. end
  131. # Returns true if usr is allowed to edit the page, otherwise false
  132. def editable_by?(usr)
  133. !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
  134. end
  135. def attachments_deletable?(usr=User.current)
  136. editable_by?(usr) && super(usr)
  137. end
  138. def parent_title
  139. @parent_title || (self.parent && self.parent.pretty_title)
  140. end
  141. def parent_title=(t)
  142. @parent_title = t
  143. parent_page = t.blank? ? nil : self.wiki.find_page(t)
  144. self.parent = parent_page
  145. end
  146. # Saves the page and its content if text was changed
  147. def save_with_content
  148. ret = nil
  149. transaction do
  150. if new_record?
  151. # Rails automatically saves associated content
  152. ret = save
  153. else
  154. ret = save && (content.text_changed? ? content.save : true)
  155. end
  156. raise ActiveRecord::Rollback unless ret
  157. end
  158. ret
  159. end
  160. protected
  161. def validate_parent_title
  162. errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
  163. errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
  164. errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
  165. end
  166. end
  167. class WikiDiff < Redmine::Helpers::Diff
  168. attr_reader :content_to, :content_from
  169. def initialize(content_to, content_from)
  170. @content_to = content_to
  171. @content_from = content_from
  172. super(content_to.text, content_from.text)
  173. end
  174. end
  175. class WikiAnnotate
  176. attr_reader :lines, :content
  177. def initialize(content)
  178. @content = content
  179. current = content
  180. current_lines = current.text.split(/\r?\n/)
  181. @lines = current_lines.collect {|t| [nil, nil, t]}
  182. positions = []
  183. current_lines.size.times {|i| positions << i}
  184. while (current.previous)
  185. d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
  186. d.each_slice(3) do |s|
  187. sign, line = s[0], s[1]
  188. if sign == '+' && positions[line] && positions[line] != -1
  189. if @lines[positions[line]][0].nil?
  190. @lines[positions[line]][0] = current.version
  191. @lines[positions[line]][1] = current.author
  192. end
  193. end
  194. end
  195. d.each_slice(3) do |s|
  196. sign, line = s[0], s[1]
  197. if sign == '-'
  198. positions.insert(line, -1)
  199. else
  200. positions[line] = nil
  201. end
  202. end
  203. positions.compact!
  204. # Stop if every line is annotated
  205. break unless @lines.detect { |line| line[0].nil? }
  206. current = current.previous
  207. end
  208. @lines.each { |line|
  209. line[0] ||= current.version
  210. # if the last known version is > 1 (eg. history was cleared), we don't know the author
  211. line[1] ||= current.author if current.version == 1
  212. }
  213. end
  214. end