Defect #18658 » migrate_from_trac_1_0.rake.patch
| migrate_from_trac.rake.0.12.new 2014-12-16 09:30:45.990139244 -0500 | ||
|---|---|---|
|
require 'active_record'
|
||
|
require 'iconv' if RUBY_VERSION < '1.9'
|
||
|
require 'pp'
|
||
|
require 'digest/sha1'
|
||
|
namespace :redmine do
|
||
|
desc 'Trac migration script'
|
||
| ... | ... | |
|
real_now - @fake_diff.to_i
|
||
|
end
|
||
|
def fake(time)
|
||
|
time = 0 if time.nil?
|
||
|
@fake_diff = real_now - time
|
||
|
res = yield
|
||
|
@fake_diff = 0
|
||
| ... | ... | |
|
# If this attribute is set a milestone has a defined target timepoint
|
||
|
def due
|
||
|
if read_attribute(:due) && read_attribute(:due) > 0
|
||
|
Time.at(read_attribute(:due)).to_date
|
||
|
Time.at(0,read_attribute(:due)).to_date
|
||
|
else
|
||
|
nil
|
||
|
end
|
||
| ... | ... | |
|
# This is the real timepoint at which the milestone has finished.
|
||
|
def completed
|
||
|
if read_attribute(:completed) && read_attribute(:completed) > 0
|
||
|
Time.at(read_attribute(:completed)).to_date
|
||
|
Time.at(0,read_attribute(:completed)).to_date
|
||
|
else
|
||
|
nil
|
||
|
end
|
||
| ... | ... | |
|
self.table_name = :attachment
|
||
|
set_inheritance_column :none
|
||
|
def time; Time.at(read_attribute(:time)) end
|
||
|
def time; Time.at(0,read_attribute(:time)) end
|
||
|
def original_filename
|
||
|
filename
|
||
| ... | ... | |
|
end
|
||
|
private
|
||
|
def trac_fullpath
|
||
|
attachment_type = read_attribute(:type)
|
||
|
#replace exotic characters with their hex representation to avoid invalid filenames
|
||
|
trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) do |x|
|
||
|
codepoint = RUBY_VERSION < '1.9' ? x[0] : x.codepoints.to_a[0]
|
||
|
sprintf('%%%02x', codepoint)
|
||
|
end
|
||
|
"#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}"
|
||
|
def sha1(s)
|
||
|
return Digest::SHA1.hexdigest(s)
|
||
|
end
|
||
|
|
||
|
def get_path(ticket_id, filename)
|
||
|
t = sha1(ticket_id.to_s)
|
||
|
f = sha1(filename)
|
||
|
ext = File.extname(filename)
|
||
|
a = [ t[0..2], "/", t, "/", f, ext ]
|
||
|
return a.join("")
|
||
|
end
|
||
|
def trac_fullpath
|
||
|
attachment_type = read_attribute(:type)
|
||
|
ticket_id = read_attribute(:id)
|
||
|
filename = read_attribute(:filename)
|
||
|
path = get_path(ticket_id, filename)
|
||
|
"#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{path}"
|
||
|
end
|
||
|
end
|
||
| ... | ... | |
|
read_attribute(:description).blank? ? summary : read_attribute(:description)
|
||
|
end
|
||
|
def time; Time.at(read_attribute(:time)) end
|
||
|
def changetime; Time.at(read_attribute(:changetime)) end
|
||
|
def time; Time.at(0,read_attribute(:time)) end
|
||
|
def changetime; Time.at(0,read_attribute(:changetime)) end
|
||
|
end
|
||
|
class TracTicketChange < ActiveRecord::Base
|
||
| ... | ... | |
|
super.select {|column| column.name.to_s != 'field'}
|
||
|
end
|
||
|
def time; Time.at(read_attribute(:time)) end
|
||
|
def time; Time.at(0,read_attribute(:time)) end
|
||
|
end
|
||
|
TRAC_WIKI_PAGES = %w(InterMapTxt InterTrac InterWiki RecentChanges SandBox TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset \
|
||
| ... | ... | |
|
TracMigrate::TracAttachment.all(:conditions => ["type = 'wiki' AND id = ?", self.id.to_s])
|
||
|
end
|
||
|
def time; Time.at(read_attribute(:time)) end
|
||
|
def time; Time.at(0,read_attribute(:time)) end
|
||
|
end
|
||
|
class TracPermission < ActiveRecord::Base
|
||
| ... | ... | |
|
text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
|
||
|
# External Links
|
||
|
text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
|
||
|
# PageOutline
|
||
|
text = text.gsub(/\[\[PageOutline\]\]/,"{{>toc}}")
|
||
|
# Images
|
||
|
text = text.gsub(/\[\[Image\((.*)\)\]\]/,'!\1!')
|
||
|
# Ticket links:
|
||
|
# [ticket:234 Text],[ticket:234 This is a test]
|
||
|
text = text.gsub(/\[ticket\:([^\ ]+)\ (.+?)\]/, '"\2":/issues/show/\1')
|
||
| ... | ... | |
|
# We would like to convert the Code highlighting too
|
||
|
# This will go into the next line.
|
||
|
shebang_line = false
|
||
|
# Regular expression for start of code
|
||
|
pre_re = /\{\{\{/
|
||
|
# Code highlighting...
|
||
|
pre_line = false
|
||
|
# Reguar expression for start of code
|
||
|
pre_re = /^\s*\{\{\{/
|
||
|
# Code hightlighing...
|
||
|
shebang_re = /^\#\!([a-z]+)/
|
||
|
# Regular expression for end of code
|
||
|
pre_end_re = /\}\}\}/
|
||
| ... | ... | |
|
m_pre = pre_re.match(line)
|
||
|
if m_pre
|
||
|
line = '<pre>'
|
||
|
pre_line = true
|
||
|
else
|
||
|
m_sl = shebang_re.match(line)
|
||
|
if m_sl
|
||
|
shebang_line = true
|
||
|
line = '<code class="' + m_sl[1] + '">'
|
||
|
end
|
||
|
m_pre_end = pre_end_re.match(line)
|
||
|
if m_pre_end
|
||
|
line = '</pre>'
|
||
|
if shebang_line
|
||
|
line = '</code>' + line
|
||
|
if pre_line
|
||
|
m_sl = shebang_re.match(line)
|
||
|
if m_sl
|
||
|
shebang_line = true
|
||
|
line = '<code class="' + m_sl[1] + '">'
|
||
|
end
|
||
|
m_pre_end = pre_end_re.match(line)
|
||
|
if m_pre_end
|
||
|
line = '</pre>'
|
||
|
if shebang_line
|
||
|
line = '</code>' + line
|
||
|
end
|
||
|
pre_line = false
|
||
|
end
|
||
|
end
|
||
|
end
|
||
| ... | ... | |
|
# Attachments
|
||
|
page.attachments.each do |attachment|
|
||
|
next unless attachment.exist?
|
||
|
next if p.attachments.find_by_filename(attachment.filename.gsub(/^.*(\\|\/)/, '').gsub(/[^\w\.\-]/,'_')) #add only once per page
|
||
|
next if p.attachments.find_by_filename(attachment.filename.gsub(/[\/\?\%\*\:\|\"\'<>\n\r]+/, '_')) #add only once per page
|
||
|
attachment.open {
|
||
|
a = Attachment.new :created_on => attachment.time
|
||
|
a.file = attachment
|
||
| ... | ... | |
|
mattr_reader :trac_directory, :trac_adapter, :trac_db_host, :trac_db_port, :trac_db_name, :trac_db_schema, :trac_db_username, :trac_db_password
|
||
|
def self.trac_db_path; "#{trac_directory}/db/trac.db" end
|
||
|
def self.trac_attachments_directory; "#{trac_directory}/attachments" end
|
||
|
def self.trac_attachments_directory; "#{trac_directory}/files/attachments" end
|
||
|
def self.target_project_identifier(identifier)
|
||
|
project = Project.find_by_identifier(identifier)
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
end
|
||