To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / wiki_page.rb @ 440:6253d777aa12

History | View | Annotate | Download (6.56 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2009  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

    
18
require 'diff'
19
require 'enumerator'
20

    
21
class WikiPage < ActiveRecord::Base
22
  belongs_to :wiki
23
  has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
24
  acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
25
  acts_as_tree :dependent => :nullify, :order => 'title'
26

    
27
  acts_as_watchable
28
  acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
29
                :description => :text,
30
                :datetime => :created_on,
31
                :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
32

    
33
  acts_as_searchable :columns => ['title', 'text'],
34
                     :include => [{:wiki => :project}, :content],
35
                     :project_key => "#{Wiki.table_name}.project_id"
36

    
37
  attr_accessor :redirect_existing_links
38
  
39
  validates_presence_of :title
40
  validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
41
  validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
42
  validates_associated :content
43
  
44
  # Wiki pages that are protected by default
45
  DEFAULT_PROTECTED_PAGES = %w(sidebar)
46
  
47
  def after_initialize
48
    if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
49
      self.protected = true
50
    end
51
  end
52
  
53
  def visible?(user=User.current)
54
    !user.nil? && user.allowed_to?(:view_wiki_pages, project)
55
  end
56

    
57
  def title=(value)
58
    value = Wiki.titleize(value)
59
    @previous_title = read_attribute(:title) if @previous_title.blank?
60
    write_attribute(:title, value)
61
  end
62

    
63
  def before_save
64
    self.title = Wiki.titleize(title)    
65
    # Manage redirects if the title has changed
66
    if !@previous_title.blank? && (@previous_title != title) && !new_record?
67
      # Update redirects that point to the old title
68
      wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
69
        r.redirects_to = title
70
        r.title == r.redirects_to ? r.destroy : r.save
71
      end
72
      # Remove redirects for the new title
73
      wiki.redirects.find_all_by_title(title).each(&:destroy)
74
      # Create a redirect to the new title
75
      wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
76
      @previous_title = nil
77
    end
78
  end
79
  
80
  def before_destroy
81
    # Remove redirects to this page
82
    wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
83
  end
84
  
85
  def pretty_title
86
    WikiPage.pretty_title(title)
87
  end
88
  
89
  def content_for_version(version=nil)
90
    result = content.versions.find_by_version(version.to_i) if version
91
    result ||= content
92
    result
93
  end
94
  
95
  def diff(version_to=nil, version_from=nil)
96
    version_to = version_to ? version_to.to_i : self.content.version
97
    version_from = version_from ? version_from.to_i : version_to - 1
98
    version_to, version_from = version_from, version_to unless version_from < version_to
99
    
100
    content_to = content.versions.find_by_version(version_to)
101
    content_from = content.versions.find_by_version(version_from)
102
    
103
    (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
104
  end
105
  
106
  def annotate(version=nil)
107
    version = version ? version.to_i : self.content.version
108
    c = content.versions.find_by_version(version)
109
    c ? WikiAnnotate.new(c) : nil
110
  end
111
  
112
  def self.pretty_title(str)
113
    (str && str.is_a?(String)) ? str.tr('_', ' ') : str
114
  end
115
  
116
  def project
117
    wiki.project
118
  end
119
  
120
  def text
121
    content.text if content
122
  end
123
  
124
  # Returns true if usr is allowed to edit the page, otherwise false
125
  def editable_by?(usr)
126
    !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
127
  end
128
        
129
  def attachments_deletable?(usr=User.current)
130
    editable_by?(usr) && super(usr)
131
  end
132
  
133
  def parent_title
134
    @parent_title || (self.parent && self.parent.pretty_title)
135
  end
136
  
137
  def parent_title=(t)
138
    @parent_title = t
139
    parent_page = t.blank? ? nil : self.wiki.find_page(t)
140
    self.parent = parent_page
141
  end
142

    
143
  protected
144
  
145
  def validate
146
    errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
147
    errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
148
    errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
149
  end
150
end
151

    
152
class WikiDiff
153
  attr_reader :diff, :words, :content_to, :content_from
154
  
155
  def initialize(content_to, content_from)
156
    @content_to = content_to
157
    @content_from = content_from
158
    @words = content_to.text.split(/(\s+)/)
159
    @words = @words.select {|word| word != ' '}
160
    words_from = content_from.text.split(/(\s+)/)
161
    words_from = words_from.select {|word| word != ' '}    
162
    @diff = words_from.diff @words
163
  end
164
end
165

    
166
class WikiAnnotate
167
  attr_reader :lines, :content
168
  
169
  def initialize(content)
170
    @content = content
171
    current = content
172
    current_lines = current.text.split(/\r?\n/)
173
    @lines = current_lines.collect {|t| [nil, nil, t]}
174
    positions = []
175
    current_lines.size.times {|i| positions << i}
176
    while (current.previous)
177
      d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
178
      d.each_slice(3) do |s|
179
        sign, line = s[0], s[1]
180
        if sign == '+' && positions[line] && positions[line] != -1
181
          if @lines[positions[line]][0].nil?
182
            @lines[positions[line]][0] = current.version
183
            @lines[positions[line]][1] = current.author
184
          end
185
        end
186
      end
187
      d.each_slice(3) do |s|
188
        sign, line = s[0], s[1]
189
        if sign == '-'
190
          positions.insert(line, -1)
191
        else
192
          positions[line] = nil
193
        end
194
      end
195
      positions.compact!
196
      # Stop if every line is annotated
197
      break unless @lines.detect { |line| line[0].nil? }
198
      current = current.previous
199
    end
200
    @lines.each { |line| line[0] ||= current.version }
201
  end
202
end