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@0
|
85 wiki.redirects.find_all_by_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@0
|
90 wiki.redirects.find_all_by_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@0
|
99 wiki.redirects.find_all_by_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@0
|
107 result = content.versions.find_by_version(version.to_i) if version
|
Chris@0
|
108 result ||= content
|
Chris@0
|
109 result
|
Chris@0
|
110 end
|
Chris@909
|
111
|
Chris@0
|
112 def diff(version_to=nil, version_from=nil)
|
Chris@0
|
113 version_to = version_to ? version_to.to_i : self.content.version
|
Chris@1115
|
114 content_to = content.versions.find_by_version(version_to)
|
Chris@1115
|
115 content_from = version_from ? content.versions.find_by_version(version_from.to_i) : content_to.try(:previous)
|
Chris@1115
|
116 return nil unless content_to && content_from
|
Chris@909
|
117
|
Chris@1115
|
118 if content_from.version > content_to.version
|
Chris@1115
|
119 content_to, content_from = content_from, content_to
|
Chris@1115
|
120 end
|
Chris@909
|
121
|
Chris@0
|
122 (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
|
Chris@0
|
123 end
|
Chris@909
|
124
|
Chris@0
|
125 def annotate(version=nil)
|
Chris@0
|
126 version = version ? version.to_i : self.content.version
|
Chris@0
|
127 c = content.versions.find_by_version(version)
|
Chris@0
|
128 c ? WikiAnnotate.new(c) : nil
|
Chris@0
|
129 end
|
Chris@909
|
130
|
Chris@0
|
131 def self.pretty_title(str)
|
Chris@0
|
132 (str && str.is_a?(String)) ? str.tr('_', ' ') : str
|
Chris@0
|
133 end
|
Chris@909
|
134
|
Chris@0
|
135 def project
|
Chris@0
|
136 wiki.project
|
Chris@0
|
137 end
|
Chris@909
|
138
|
Chris@0
|
139 def text
|
Chris@0
|
140 content.text if content
|
Chris@0
|
141 end
|
Chris@909
|
142
|
Chris@441
|
143 def updated_on
|
Chris@441
|
144 unless @updated_on
|
Chris@441
|
145 if time = read_attribute(:updated_on)
|
Chris@441
|
146 # content updated_on was eager loaded with the page
|
Chris@1115
|
147 begin
|
Chris@1115
|
148 @updated_on = (self.class.default_timezone == :utc ? Time.parse(time.to_s).utc : Time.parse(time.to_s).localtime)
|
Chris@1115
|
149 rescue
|
Chris@1115
|
150 end
|
Chris@441
|
151 else
|
Chris@441
|
152 @updated_on = content && content.updated_on
|
Chris@441
|
153 end
|
Chris@441
|
154 end
|
Chris@441
|
155 @updated_on
|
Chris@441
|
156 end
|
Chris@909
|
157
|
Chris@0
|
158 # Returns true if usr is allowed to edit the page, otherwise false
|
Chris@0
|
159 def editable_by?(usr)
|
Chris@0
|
160 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
|
Chris@0
|
161 end
|
Chris@909
|
162
|
Chris@0
|
163 def attachments_deletable?(usr=User.current)
|
Chris@0
|
164 editable_by?(usr) && super(usr)
|
Chris@0
|
165 end
|
Chris@909
|
166
|
Chris@0
|
167 def parent_title
|
Chris@0
|
168 @parent_title || (self.parent && self.parent.pretty_title)
|
Chris@0
|
169 end
|
Chris@909
|
170
|
Chris@0
|
171 def parent_title=(t)
|
Chris@0
|
172 @parent_title = t
|
Chris@0
|
173 parent_page = t.blank? ? nil : self.wiki.find_page(t)
|
Chris@0
|
174 self.parent = parent_page
|
Chris@0
|
175 end
|
chris@37
|
176
|
Chris@1115
|
177 # Saves the page and its content if text was changed
|
Chris@1464
|
178 def save_with_content(content)
|
Chris@1115
|
179 ret = nil
|
Chris@1115
|
180 transaction do
|
Chris@1464
|
181 self.content = content
|
Chris@1115
|
182 if new_record?
|
Chris@1115
|
183 # Rails automatically saves associated content
|
Chris@1115
|
184 ret = save
|
Chris@1115
|
185 else
|
Chris@1115
|
186 ret = save && (content.text_changed? ? content.save : true)
|
Chris@1115
|
187 end
|
Chris@1115
|
188 raise ActiveRecord::Rollback unless ret
|
Chris@1115
|
189 end
|
Chris@1115
|
190 ret
|
Chris@1115
|
191 end
|
Chris@1115
|
192
|
Chris@0
|
193 protected
|
Chris@909
|
194
|
Chris@909
|
195 def validate_parent_title
|
Chris@0
|
196 errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
|
Chris@0
|
197 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
|
Chris@0
|
198 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
|
Chris@0
|
199 end
|
Chris@0
|
200 end
|
Chris@0
|
201
|
Chris@245
|
202 class WikiDiff < Redmine::Helpers::Diff
|
Chris@245
|
203 attr_reader :content_to, :content_from
|
Chris@909
|
204
|
Chris@0
|
205 def initialize(content_to, content_from)
|
Chris@0
|
206 @content_to = content_to
|
Chris@0
|
207 @content_from = content_from
|
Chris@245
|
208 super(content_to.text, content_from.text)
|
Chris@0
|
209 end
|
Chris@0
|
210 end
|
Chris@0
|
211
|
Chris@0
|
212 class WikiAnnotate
|
Chris@0
|
213 attr_reader :lines, :content
|
Chris@909
|
214
|
Chris@0
|
215 def initialize(content)
|
Chris@0
|
216 @content = content
|
Chris@0
|
217 current = content
|
Chris@0
|
218 current_lines = current.text.split(/\r?\n/)
|
Chris@0
|
219 @lines = current_lines.collect {|t| [nil, nil, t]}
|
Chris@0
|
220 positions = []
|
Chris@0
|
221 current_lines.size.times {|i| positions << i}
|
Chris@0
|
222 while (current.previous)
|
Chris@0
|
223 d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
|
Chris@0
|
224 d.each_slice(3) do |s|
|
Chris@0
|
225 sign, line = s[0], s[1]
|
Chris@0
|
226 if sign == '+' && positions[line] && positions[line] != -1
|
Chris@0
|
227 if @lines[positions[line]][0].nil?
|
Chris@0
|
228 @lines[positions[line]][0] = current.version
|
Chris@0
|
229 @lines[positions[line]][1] = current.author
|
Chris@0
|
230 end
|
Chris@0
|
231 end
|
Chris@0
|
232 end
|
Chris@0
|
233 d.each_slice(3) do |s|
|
Chris@0
|
234 sign, line = s[0], s[1]
|
Chris@0
|
235 if sign == '-'
|
Chris@0
|
236 positions.insert(line, -1)
|
Chris@0
|
237 else
|
Chris@0
|
238 positions[line] = nil
|
Chris@0
|
239 end
|
Chris@0
|
240 end
|
Chris@0
|
241 positions.compact!
|
Chris@0
|
242 # Stop if every line is annotated
|
Chris@0
|
243 break unless @lines.detect { |line| line[0].nil? }
|
Chris@0
|
244 current = current.previous
|
Chris@0
|
245 end
|
Chris@909
|
246 @lines.each { |line|
|
Chris@507
|
247 line[0] ||= current.version
|
Chris@507
|
248 # if the last known version is > 1 (eg. history was cleared), we don't know the author
|
Chris@507
|
249 line[1] ||= current.author if current.version == 1
|
Chris@507
|
250 }
|
Chris@0
|
251 end
|
Chris@0
|
252 end
|