Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: require 'diff' Chris@1517: require 'enumerator' Chris@1517: Chris@1517: class WikiPage < ActiveRecord::Base Chris@1517: include Redmine::SafeAttributes Chris@1517: Chris@1517: belongs_to :wiki Chris@1517: has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy Chris@1517: acts_as_attachable :delete_permission => :delete_wiki_pages_attachments Chris@1517: acts_as_tree :dependent => :nullify, :order => 'title' Chris@1517: Chris@1517: acts_as_watchable Chris@1517: acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"}, Chris@1517: :description => :text, Chris@1517: :datetime => :created_on, Chris@1517: :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}} Chris@1517: Chris@1517: acts_as_searchable :columns => ['title', "#{WikiContent.table_name}.text"], Chris@1517: :include => [{:wiki => :project}, :content], Chris@1517: :permission => :view_wiki_pages, Chris@1517: :project_key => "#{Wiki.table_name}.project_id" Chris@1517: Chris@1517: attr_accessor :redirect_existing_links Chris@1517: Chris@1517: validates_presence_of :title Chris@1517: validates_format_of :title, :with => /\A[^,\.\/\?\;\|\s]*\z/ Chris@1517: validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false Chris@1517: validates_associated :content Chris@1517: Chris@1517: validate :validate_parent_title Chris@1517: before_destroy :remove_redirects Chris@1517: before_save :handle_redirects Chris@1517: Chris@1517: # eager load information about last updates, without loading text Chris@1517: scope :with_updated_on, lambda { Chris@1517: select("#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on, #{WikiContent.table_name}.version"). Chris@1517: joins("LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id") Chris@1517: } Chris@1517: Chris@1517: # Wiki pages that are protected by default Chris@1517: DEFAULT_PROTECTED_PAGES = %w(sidebar) Chris@1517: Chris@1517: safe_attributes 'parent_id', 'parent_title', Chris@1517: :if => lambda {|page, user| page.new_record? || user.allowed_to?(:rename_wiki_pages, page.project)} Chris@1517: Chris@1517: def initialize(attributes=nil, *args) Chris@1517: super Chris@1517: if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase) Chris@1517: self.protected = true Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def visible?(user=User.current) Chris@1517: !user.nil? && user.allowed_to?(:view_wiki_pages, project) Chris@1517: end Chris@1517: Chris@1517: def title=(value) Chris@1517: value = Wiki.titleize(value) Chris@1517: @previous_title = read_attribute(:title) if @previous_title.blank? Chris@1517: write_attribute(:title, value) Chris@1517: end Chris@1517: Chris@1517: def handle_redirects Chris@1517: self.title = Wiki.titleize(title) Chris@1517: # Manage redirects if the title has changed Chris@1517: if !@previous_title.blank? && (@previous_title != title) && !new_record? Chris@1517: # Update redirects that point to the old title Chris@1517: wiki.redirects.where(:redirects_to => @previous_title).each do |r| Chris@1517: r.redirects_to = title Chris@1517: r.title == r.redirects_to ? r.destroy : r.save Chris@1517: end Chris@1517: # Remove redirects for the new title Chris@1517: wiki.redirects.where(:title => title).each(&:destroy) Chris@1517: # Create a redirect to the new title Chris@1517: wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0" Chris@1517: @previous_title = nil Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def remove_redirects Chris@1517: # Remove redirects to this page Chris@1517: wiki.redirects.where(:redirects_to => title).each(&:destroy) Chris@1517: end Chris@1517: Chris@1517: def pretty_title Chris@1517: WikiPage.pretty_title(title) Chris@1517: end Chris@1517: Chris@1517: def content_for_version(version=nil) Chris@1517: if content Chris@1517: result = content.versions.find_by_version(version.to_i) if version Chris@1517: result ||= content Chris@1517: result Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def diff(version_to=nil, version_from=nil) Chris@1517: version_to = version_to ? version_to.to_i : self.content.version Chris@1517: content_to = content.versions.find_by_version(version_to) Chris@1517: content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous) Chris@1517: return nil unless content_to && content_from Chris@1517: Chris@1517: if content_from.version > content_to.version Chris@1517: content_to, content_from = content_from, content_to Chris@1517: end Chris@1517: Chris@1517: (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil Chris@1517: end Chris@1517: Chris@1517: def annotate(version=nil) Chris@1517: version = version ? version.to_i : self.content.version Chris@1517: c = content.versions.find_by_version(version) Chris@1517: c ? WikiAnnotate.new(c) : nil Chris@1517: end Chris@1517: Chris@1517: def self.pretty_title(str) Chris@1517: (str && str.is_a?(String)) ? str.tr('_', ' ') : str Chris@1517: end Chris@1517: Chris@1517: def project Chris@1517: wiki.project Chris@1517: end Chris@1517: Chris@1517: def text Chris@1517: content.text if content Chris@1517: end Chris@1517: Chris@1517: def updated_on Chris@1517: unless @updated_on Chris@1517: if time = read_attribute(:updated_on) Chris@1517: # content updated_on was eager loaded with the page Chris@1517: begin Chris@1517: @updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime) Chris@1517: rescue Chris@1517: end Chris@1517: else Chris@1517: @updated_on = content && content.updated_on Chris@1517: end Chris@1517: end Chris@1517: @updated_on Chris@1517: end Chris@1517: Chris@1517: # Returns true if usr is allowed to edit the page, otherwise false Chris@1517: def editable_by?(usr) Chris@1517: !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project) Chris@1517: end Chris@1517: Chris@1517: def attachments_deletable?(usr=User.current) Chris@1517: editable_by?(usr) && super(usr) Chris@1517: end Chris@1517: Chris@1517: def parent_title Chris@1517: @parent_title || (self.parent && self.parent.pretty_title) Chris@1517: end Chris@1517: Chris@1517: def parent_title=(t) Chris@1517: @parent_title = t Chris@1517: parent_page = t.blank? ? nil : self.wiki.find_page(t) Chris@1517: self.parent = parent_page Chris@1517: end Chris@1517: Chris@1517: # Saves the page and its content if text was changed Chris@1517: def save_with_content(content) Chris@1517: ret = nil Chris@1517: transaction do Chris@1517: self.content = content Chris@1517: if new_record? Chris@1517: # Rails automatically saves associated content Chris@1517: ret = save Chris@1517: else Chris@1517: ret = save && (content.text_changed? ? content.save : true) Chris@1517: end Chris@1517: raise ActiveRecord::Rollback unless ret Chris@1517: end Chris@1517: ret Chris@1517: end Chris@1517: Chris@1517: protected Chris@1517: Chris@1517: def validate_parent_title Chris@1517: errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil? Chris@1517: errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self)) Chris@1517: errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: class WikiDiff < Redmine::Helpers::Diff Chris@1517: attr_reader :content_to, :content_from Chris@1517: Chris@1517: def initialize(content_to, content_from) Chris@1517: @content_to = content_to Chris@1517: @content_from = content_from Chris@1517: super(content_to.text, content_from.text) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: class WikiAnnotate Chris@1517: attr_reader :lines, :content Chris@1517: Chris@1517: def initialize(content) Chris@1517: @content = content Chris@1517: current = content Chris@1517: current_lines = current.text.split(/\r?\n/) Chris@1517: @lines = current_lines.collect {|t| [nil, nil, t]} Chris@1517: positions = [] Chris@1517: current_lines.size.times {|i| positions << i} Chris@1517: while (current.previous) Chris@1517: d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten Chris@1517: d.each_slice(3) do |s| Chris@1517: sign, line = s[0], s[1] Chris@1517: if sign == '+' && positions[line] && positions[line] != -1 Chris@1517: if @lines[positions[line]][0].nil? Chris@1517: @lines[positions[line]][0] = current.version Chris@1517: @lines[positions[line]][1] = current.author Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: d.each_slice(3) do |s| Chris@1517: sign, line = s[0], s[1] Chris@1517: if sign == '-' Chris@1517: positions.insert(line, -1) Chris@1517: else Chris@1517: positions[line] = nil Chris@1517: end Chris@1517: end Chris@1517: positions.compact! Chris@1517: # Stop if every line is annotated Chris@1517: break unless @lines.detect { |line| line[0].nil? } Chris@1517: current = current.previous Chris@1517: end Chris@1517: @lines.each { |line| Chris@1517: line[0] ||= current.version Chris@1517: # if the last known version is > 1 (eg. history was cleared), we don't know the author Chris@1517: line[1] ||= current.author if current.version == 1 Chris@1517: } Chris@1517: end Chris@1517: end