Project

General

Profile

Patch #619 ยป migrate_from_trac.patch

Pirmin Kalberer, 2008-02-08 11:14

View differences:

lib/tasks/migrate_from_trac.rake
'low' => priorities[0],
'normal' => priorities[1],
'high' => priorities[2],
'highest' => priorities[3]
'highest' => priorities[3],
# ---
'trivial' => priorities[0],
'minor' => priorities[1],
'major' => priorities[2],
'critical' => priorities[3],
'blocker' => priorities[4]
}
TRACKER_BUG = Tracker.find_by_position(1)
......
end
class TracWikiPage < ActiveRecord::Base
set_table_name :wiki
set_table_name :wiki
set_primary_key :name
has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "#{TracMigrate::TracAttachment.table_name}.type = 'wiki'"
def time; Time.at(read_attribute(:time)) end
def self.columns
# Hides readonly Trac field to prevent clash with AR readonly? method (Rails 2.0)
super.select {|column| column.name.to_s != 'readonly'}
......
# Basic wiki syntax conversion
def self.convert_wiki_text(text)
# Titles
text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
text = text.gsub(/^(\=+)\s(.+?)\s(\=+)/) {|s| "\nh#{$1.length}. #{$2}\n"}
# External Links
text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"}
# Internal Links
text = text.gsub(/\[\[BR\]\]/, "\n") # This has to go before the rules below
text = text.gsub(/\[\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:\"(.+)\".*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:([^\s\]]+).*\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[\[PageOutline\(?.*\)?\]\]/, "{{>toc}}\n")
# * Images
text = text.gsub(/\[\[Image\((.+?)\)\]\]/) {|s| "!#{$1}!"}
# * Wiki links
text = text.gsub(/\[wiki:\"(.+?)\".*?\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:([^ ]+?)\]/) {|s| "[[#{$1.delete(',./?;|:')}]]"}
text = text.gsub(/\[wiki:(.+?) (.+?)\]/) {|s| "[[#{$1.delete(',./?;|:')}|#{$2}]]"}
# * CamelCase links
text = text.gsub(/\[([A-Z][a-z]+[A-Z][a-z]+[A-Za-z]*) (.+?)\]/) {|s| "[[#{$1}|#{$2}]]"}
text = text.gsub(/(\s)([A-Z][a-z]+[A-Z][a-z]+[A-Za-z]*)/) {|s| "#{$1}[[#{$2}]]"}
# * Attachments
text = text.gsub(/\[attachment:\"(.+?)\"(.*?)\]/) {|s| "attachment:#{$1.delete(',?;|:')} #{$2}"}
# Revisions links
text = text.gsub(/\[(\d+)\]/, 'r\1')
# Remaining External Links
text = text.gsub(/([^\[])\[(.+?) (.+?)\]/) {|s| "#{$1}\"#{$3}\":#{$2}"}
# Ticket number re-writing
text = text.gsub(/#(\d+)/) do |s|
TICKET_MAP[$1.to_i] ||= $1
......
text
end
TRAC_WIKI_PAGES = %w(TracAccessibility TracAdmin TracBackup TracBrowser TracCgi TracChangeset TracEnvironment TracFastCgi TracGuide TracImport TracIni TracInstall TracInterfaceCustomization TracLinks TracLogging TracModPython TracNotification TracPermissions TracPlugins TracQuery TracReports TracRoadmap TracRss TracSearch TracStandalone TracSupport TracSyntaxColoring TracTickets TracTicketsCustomFields TracTimeline TracUnicode TracUpgrade TracWiki WikiDeletePage WikiFormatting WikiHtml WikiMacros WikiNewPage WikiPageNames WikiProcessors WikiRestructuredText WikiRestructuredTextLinks CamelCase TitleIndex)
def self.migrate
establish_connection
......
migrated_custom_values = 0
migrated_ticket_attachments = 0
migrated_wiki_edits = 0
migrated_wiki_attachments = 0
# Components
print "Migrating components"
......
TracWikiPage.find(:all, :order => 'name, version').each do |page|
print '.'
STDOUT.flush
next if TRAC_WIKI_PAGES.include?(page.name)
p = wiki.find_or_new_page(page.name)
p.content = WikiContent.new(:page => p) if p.new_record?
p.content.text = page.text
......
p.content.comments = page.comment
p.new_record? ? p.save : p.content.save
migrated_wiki_edits += 1 unless p.content.new_record?
# 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
a = Attachment.new :created_on => attachment.time
a.file = attachment
a.author = find_or_create_user(attachment.author)
a.container = p
migrated_wiki_attachments += 1 if a.save
end
end
wiki.reload
......
puts "Components: #{migrated_components}/#{TracComponent.count}"
puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}"
puts "Tickets: #{migrated_tickets}/#{TracTicket.count}"
puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s
puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count(:conditions => "type = 'ticket'").to_s
puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}"
puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}"
puts "Wiki files: #{migrated_wiki_attachments}/" + TracAttachment.count(:conditions => "type = 'wiki'").to_s
end
def self.limit_for(klass, attribute)
    (1-1/1)