Chris@0
|
1 # Redmine - project management software
|
Chris@245
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'diff'
|
Chris@0
|
19 require 'enumerator'
|
Chris@0
|
20
|
Chris@0
|
21 class WikiPage < ActiveRecord::Base
|
Chris@0
|
22 belongs_to :wiki
|
Chris@0
|
23 has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
|
Chris@0
|
24 acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
|
Chris@0
|
25 acts_as_tree :dependent => :nullify, :order => 'title'
|
Chris@0
|
26
|
Chris@0
|
27 acts_as_watchable
|
Chris@0
|
28 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
|
Chris@0
|
29 :description => :text,
|
Chris@0
|
30 :datetime => :created_on,
|
chris@37
|
31 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
|
Chris@0
|
32
|
Chris@0
|
33 acts_as_searchable :columns => ['title', 'text'],
|
Chris@0
|
34 :include => [{:wiki => :project}, :content],
|
Chris@441
|
35 :permission => :view_wiki_pages,
|
Chris@0
|
36 :project_key => "#{Wiki.table_name}.project_id"
|
Chris@0
|
37
|
Chris@0
|
38 attr_accessor :redirect_existing_links
|
Chris@0
|
39
|
Chris@0
|
40 validates_presence_of :title
|
Chris@0
|
41 validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
|
Chris@0
|
42 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
|
Chris@0
|
43 validates_associated :content
|
Chris@0
|
44
|
Chris@441
|
45 # eager load information about last updates, without loading text
|
Chris@441
|
46 named_scope :with_updated_on, {
|
Chris@441
|
47 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
|
Chris@441
|
48 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
|
Chris@441
|
49 }
|
Chris@441
|
50
|
Chris@0
|
51 # Wiki pages that are protected by default
|
Chris@0
|
52 DEFAULT_PROTECTED_PAGES = %w(sidebar)
|
Chris@0
|
53
|
Chris@0
|
54 def after_initialize
|
Chris@0
|
55 if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
|
Chris@0
|
56 self.protected = true
|
Chris@0
|
57 end
|
Chris@0
|
58 end
|
Chris@0
|
59
|
Chris@0
|
60 def visible?(user=User.current)
|
Chris@0
|
61 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
|
Chris@0
|
62 end
|
Chris@0
|
63
|
Chris@0
|
64 def title=(value)
|
Chris@0
|
65 value = Wiki.titleize(value)
|
Chris@0
|
66 @previous_title = read_attribute(:title) if @previous_title.blank?
|
Chris@0
|
67 write_attribute(:title, value)
|
Chris@0
|
68 end
|
Chris@0
|
69
|
Chris@0
|
70 def before_save
|
Chris@0
|
71 self.title = Wiki.titleize(title)
|
Chris@0
|
72 # Manage redirects if the title has changed
|
Chris@0
|
73 if !@previous_title.blank? && (@previous_title != title) && !new_record?
|
Chris@0
|
74 # Update redirects that point to the old title
|
Chris@0
|
75 wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
|
Chris@0
|
76 r.redirects_to = title
|
Chris@0
|
77 r.title == r.redirects_to ? r.destroy : r.save
|
Chris@0
|
78 end
|
Chris@0
|
79 # Remove redirects for the new title
|
Chris@0
|
80 wiki.redirects.find_all_by_title(title).each(&:destroy)
|
Chris@0
|
81 # Create a redirect to the new title
|
Chris@0
|
82 wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
|
Chris@0
|
83 @previous_title = nil
|
Chris@0
|
84 end
|
Chris@0
|
85 end
|
Chris@0
|
86
|
Chris@0
|
87 def before_destroy
|
Chris@0
|
88 # Remove redirects to this page
|
Chris@0
|
89 wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
|
Chris@0
|
90 end
|
Chris@0
|
91
|
Chris@0
|
92 def pretty_title
|
Chris@0
|
93 WikiPage.pretty_title(title)
|
Chris@0
|
94 end
|
Chris@0
|
95
|
Chris@0
|
96 def content_for_version(version=nil)
|
Chris@0
|
97 result = content.versions.find_by_version(version.to_i) if version
|
Chris@0
|
98 result ||= content
|
Chris@0
|
99 result
|
Chris@0
|
100 end
|
Chris@0
|
101
|
Chris@0
|
102 def diff(version_to=nil, version_from=nil)
|
Chris@0
|
103 version_to = version_to ? version_to.to_i : self.content.version
|
Chris@0
|
104 version_from = version_from ? version_from.to_i : version_to - 1
|
Chris@0
|
105 version_to, version_from = version_from, version_to unless version_from < version_to
|
Chris@0
|
106
|
Chris@0
|
107 content_to = content.versions.find_by_version(version_to)
|
Chris@0
|
108 content_from = content.versions.find_by_version(version_from)
|
Chris@0
|
109
|
Chris@0
|
110 (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
|
Chris@0
|
111 end
|
Chris@0
|
112
|
Chris@0
|
113 def annotate(version=nil)
|
Chris@0
|
114 version = version ? version.to_i : self.content.version
|
Chris@0
|
115 c = content.versions.find_by_version(version)
|
Chris@0
|
116 c ? WikiAnnotate.new(c) : nil
|
Chris@0
|
117 end
|
Chris@0
|
118
|
Chris@0
|
119 def self.pretty_title(str)
|
Chris@0
|
120 (str && str.is_a?(String)) ? str.tr('_', ' ') : str
|
Chris@0
|
121 end
|
Chris@0
|
122
|
Chris@0
|
123 def project
|
Chris@0
|
124 wiki.project
|
Chris@0
|
125 end
|
Chris@0
|
126
|
Chris@0
|
127 def text
|
Chris@0
|
128 content.text if content
|
Chris@0
|
129 end
|
Chris@0
|
130
|
Chris@441
|
131 def updated_on
|
Chris@441
|
132 unless @updated_on
|
Chris@441
|
133 if time = read_attribute(:updated_on)
|
Chris@441
|
134 # content updated_on was eager loaded with the page
|
Chris@441
|
135 @updated_on = Time.parse(time) rescue nil
|
Chris@441
|
136 else
|
Chris@441
|
137 @updated_on = content && content.updated_on
|
Chris@441
|
138 end
|
Chris@441
|
139 end
|
Chris@441
|
140 @updated_on
|
Chris@441
|
141 end
|
Chris@441
|
142
|
Chris@0
|
143 # Returns true if usr is allowed to edit the page, otherwise false
|
Chris@0
|
144 def editable_by?(usr)
|
Chris@0
|
145 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
|
Chris@0
|
146 end
|
Chris@0
|
147
|
Chris@0
|
148 def attachments_deletable?(usr=User.current)
|
Chris@0
|
149 editable_by?(usr) && super(usr)
|
Chris@0
|
150 end
|
Chris@0
|
151
|
Chris@0
|
152 def parent_title
|
Chris@0
|
153 @parent_title || (self.parent && self.parent.pretty_title)
|
Chris@0
|
154 end
|
Chris@0
|
155
|
Chris@0
|
156 def parent_title=(t)
|
Chris@0
|
157 @parent_title = t
|
Chris@0
|
158 parent_page = t.blank? ? nil : self.wiki.find_page(t)
|
Chris@0
|
159 self.parent = parent_page
|
Chris@0
|
160 end
|
chris@37
|
161
|
Chris@0
|
162 protected
|
Chris@0
|
163
|
Chris@0
|
164 def validate
|
Chris@0
|
165 errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
|
Chris@0
|
166 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
|
Chris@0
|
167 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
|
Chris@0
|
168 end
|
Chris@0
|
169 end
|
Chris@0
|
170
|
Chris@245
|
171 class WikiDiff < Redmine::Helpers::Diff
|
Chris@245
|
172 attr_reader :content_to, :content_from
|
Chris@0
|
173
|
Chris@0
|
174 def initialize(content_to, content_from)
|
Chris@0
|
175 @content_to = content_to
|
Chris@0
|
176 @content_from = content_from
|
Chris@245
|
177 super(content_to.text, content_from.text)
|
Chris@0
|
178 end
|
Chris@0
|
179 end
|
Chris@0
|
180
|
Chris@0
|
181 class WikiAnnotate
|
Chris@0
|
182 attr_reader :lines, :content
|
Chris@0
|
183
|
Chris@0
|
184 def initialize(content)
|
Chris@0
|
185 @content = content
|
Chris@0
|
186 current = content
|
Chris@0
|
187 current_lines = current.text.split(/\r?\n/)
|
Chris@0
|
188 @lines = current_lines.collect {|t| [nil, nil, t]}
|
Chris@0
|
189 positions = []
|
Chris@0
|
190 current_lines.size.times {|i| positions << i}
|
Chris@0
|
191 while (current.previous)
|
Chris@0
|
192 d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
|
Chris@0
|
193 d.each_slice(3) do |s|
|
Chris@0
|
194 sign, line = s[0], s[1]
|
Chris@0
|
195 if sign == '+' && positions[line] && positions[line] != -1
|
Chris@0
|
196 if @lines[positions[line]][0].nil?
|
Chris@0
|
197 @lines[positions[line]][0] = current.version
|
Chris@0
|
198 @lines[positions[line]][1] = current.author
|
Chris@0
|
199 end
|
Chris@0
|
200 end
|
Chris@0
|
201 end
|
Chris@0
|
202 d.each_slice(3) do |s|
|
Chris@0
|
203 sign, line = s[0], s[1]
|
Chris@0
|
204 if sign == '-'
|
Chris@0
|
205 positions.insert(line, -1)
|
Chris@0
|
206 else
|
Chris@0
|
207 positions[line] = nil
|
Chris@0
|
208 end
|
Chris@0
|
209 end
|
Chris@0
|
210 positions.compact!
|
Chris@0
|
211 # Stop if every line is annotated
|
Chris@0
|
212 break unless @lines.detect { |line| line[0].nil? }
|
Chris@0
|
213 current = current.previous
|
Chris@0
|
214 end
|
Chris@507
|
215 @lines.each { |line|
|
Chris@507
|
216 line[0] ||= current.version
|
Chris@507
|
217 # if the last known version is > 1 (eg. history was cleared), we don't know the author
|
Chris@507
|
218 line[1] ||= current.author if current.version == 1
|
Chris@507
|
219 }
|
Chris@0
|
220 end
|
Chris@0
|
221 end
|