Skip to content

Instantly share code, notes, and snippets.

@CobyR
Created August 27, 2009 20:33
Show Gist options
  • Save CobyR/176539 to your computer and use it in GitHub Desktop.
Save CobyR/176539 to your computer and use it in GitHub Desktop.
require 'nokogiri'
namespace :import do
desc "Import XML file"
task :xml => :environment do
xml_file = File.dirname(__FILE__) + "/../../db/one.xml"
rtf_directory = File.dirname(__FILE__) + "/../../db/txt/"
puts "DESTROYING! "
File.unlink "db/development.sqlite3" rescue nil
system "rake db:migrate"
system "rake db:fixtures:load"
f = File.open(xml_file)
n = Nokogiri::XML.parse(f)
thoughts = n.xpath('*/thought')
puts "Importing: #{xml_file}"
puts "#{thoughts.size} thoughts to process."
print "Processing"
STDOUT.flush
# Loop through all of the thoughts and create the initial record set
thoughts.each_with_index do | thought, x |
t = Thought.new
t.original_id = thought.attributes['id'].to_s.to_i
t.name = thought.xpath('name').children[0].to_s
t.forgotten = thought.attributes['forgotten'].to_s
t.private = thought.attributes['private'].to_s
t.created_at = thought.attributes['createtimestamp'].to_s
t.updated_at = thought.attributes['modtimestamp'].to_s
if f = File.open("#{rtf_directory}#{t.original_id}.txt")
c = f.read
#c.gsub!('\par','<p>')
#c.gsub!('\r\n', "<br/>")
t.rtf = c
end
t.save
if x % 100 == 0
print '.'
STDOUT.flush
end
end
puts "\nAll thoughts imported into the database."
# Loop through a second time to establish relations
puts "Connecting the thoughts via their relations."
print "Processing"
STDOUT.flush
thoughts.each_with_index do | thought, x |
relations = thought.xpath('relations')
puts "=================================="
puts "#{x} - Relations #{relations.size}"
t = Thought.find_by_original_id(thought.attributes['id'].to_s.to_i)
relations.children.each_with_index do | relation, y |
unless relation.name == "text"
puts "\t#{y} - #{relation.name}"
rt = Thought.find_by_original_id(relation.attributes['id'].to_s.to_i)
r = Relation.new
r.thought_id = t.id
r.related_to_thought_id = rt.id
if relation.name == "jump"
r.relation_type_id = 1
elsif relation.name == "child"
r.relation_type_id = 2
elsif relation.name == "parent"
r.relation_type_id = 3
end
r.save
end
end
if x % 100 == 0
print '.'
STDOUT.flush
end
end
f.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment