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