--- migrate_from_trac.rake.bk 2014-10-21 15:13:10.000000000 -0400 +++ migrate_from_trac.rake.0.12.new 2014-12-16 09:30:45.990139244 -0500 @@ -18,6 +18,7 @@ require 'active_record' require 'iconv' if RUBY_VERSION < '1.9' require 'pp' +require 'digest/sha1' namespace :redmine do desc 'Trac migration script' @@ -76,6 +77,7 @@ 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 @@ -93,7 +95,7 @@ # 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 @@ -101,7 +103,7 @@ # 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 @@ -121,7 +123,7 @@ 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 @@ -151,14 +153,24 @@ 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 @@ -186,8 +198,8 @@ 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 @@ -198,7 +210,7 @@ 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 \ @@ -222,7 +234,7 @@ 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 @@ -283,6 +295,10 @@ 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') @@ -327,9 +343,11 @@ # 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 = /\}\}\}/ @@ -339,17 +357,21 @@ m_pre = pre_re.match(line) if m_pre line = '
'
+            pre_line = true
           else
-            m_sl = shebang_re.match(line)
-            if m_sl
-              shebang_line = true
-              line = ''
-            end
-            m_pre_end = pre_end_re.match(line)
-            if m_pre_end
-              line = '
' - if shebang_line - line = '' + line + if pre_line + m_sl = shebang_re.match(line) + if m_sl + shebang_line = true + line = '' + end + m_pre_end = pre_end_re.match(line) + if m_pre_end + line = '' + if shebang_line + line = '' + line + end + pre_line = false end end end @@ -572,7 +594,8 @@ # 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 @@ -665,7 +688,7 @@ 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) @@ -781,3 +804,4 @@ end end end +