import-cooperation.net
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/ruby
require File.dirname(__FILE__) + '/../config/environment'
IMPORT_DIR = '/home/terceiro/src/cooperation-migration/data/export'
if ARGV.size == 0
puts "usage: %s <username> [ <username> [ <username> ... ] ]" % $PROGRAM_NAME
exit(1)
end
def password(username)
# FIXME
'123456'
end
def domainname(envname)
# FIXME remove the suffix
envname + '.local'
end
TinyMceArticle # forces loading the Noosfero class before adding stuff to it
class TinyMceArticle
attr_accessor :is_homepage
end
UploadedFile # force loading Noosfero class before addinf stuff to it
class UploadedFile
attr_accessor :is_homepage, :filesystem_location
end
class FileData < StringIO
attr_reader :original_filename, :content_type
def initialize(actual_filename, name, content_type)
@original_filename = name
@content_type = content_type
super(File.read(actual_filename))
end
end
def import_environment(name)
environment = Environment.find_by_name(name)
if environment
puts "I: environment <#{name}> already exists, loading it."
else
environment = Environment.create!(:name => name)
Domain.create!(:name => domainname(name), :owner => environment)
puts "I: environment <#{name}> created."
end
environment
end
for username in ARGV
begin
User.transaction do
# guess environment
envname = username.gsub(/^.*@/, '')
environment = import_environment(envname)
# create user
login = username.gsub(/@.*$/, '')
user = User.create!(:login => login, :email => username, :environment => environment, :password => password(username), :password_confirmation => password(username))
puts "I: user account for #{username} created"
# import person data
person = user.person
person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))
puts "I: #{username} data imported"
# import articles
Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml|
puts "I: Trying to import #{username}'s article in #{xml} ..."
article = TinyMceArticle.new
article.from_xml(File.read(xml))
article.profile = person
if article.valid?
article.save!
puts "I: #{username}'s article #{article.name.inspect} imported"
else
$stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}"
next
end
if article.is_homepage
person.home_page = article
person.save!
puts "I: Article #{article.name.inspect} is #{username}'s homepage!"
end
# import attachments
attachments_dir = xml.gsub(/\.xml$/, '')
Dir.glob(File.join(attachments_dir, 'attachments', '*.xml')).each do |attachment_xml|
file = UploadedFile.new
file.from_xml(File.read(attachment_xml))
puts "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})"
file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type)
file.parent = article
file.profile = person
file.save!
puts "I: attachment added to article #{article.id}"
file.reload
if file.image?
file.parent.body = "<img align='left' style='margin-right: 5px; margin-bottom: 5px;' src='/#{login}/#{file.path}'/> " + file.parent.body
else
if file.parent.body
file.parent.body += "<hr/><div><a href='/#{login}/#{file.path}'>#{file.abstract}</a></div>"
end
end
file.parent.save
end
end
end
rescue Exception => e
$stderr.puts "=================================="
$stderr.puts "E: importing <#{username}> failed."
$stderr.puts e
$stderr.puts e.backtrace
$stderr.puts "=================================="
$stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors."
end
end