Commit c36b97ff2c8d07cffaec8d85ef6eade4190ce18f
1 parent
6cc68ec3
Exists in
master
and in
29 other branches
Removing old scripts
Showing
3 changed files
with
0 additions
and
214 deletions
Show diff stats
script/import-cooperation.net
| @@ -1,212 +0,0 @@ | @@ -1,212 +0,0 @@ | ||
| 1 | -#!/usr/bin/ruby | ||
| 2 | -require File.dirname(__FILE__) + '/../config/environment' | ||
| 3 | - | ||
| 4 | -include ActionController::UrlWriter | ||
| 5 | - | ||
| 6 | -SCRIPT_TYPE = File.basename($PROGRAM_NAME) | ||
| 7 | - | ||
| 8 | -IMPORT_DIR = ENV['IMPORT_DIR'] || '/home/terceiro/src/cooperation-migration/data/export' | ||
| 9 | - | ||
| 10 | -if ARGV.size == 0 | ||
| 11 | - puts "usage: %s <username> [ <username> [ <username> ... ] ]" % $PROGRAM_NAME | ||
| 12 | - exit(1) | ||
| 13 | -end | ||
| 14 | - | ||
| 15 | -def import_environment(domain_name) | ||
| 16 | - env = Environment.default | ||
| 17 | - env.domains << get_domain(domain_name) | ||
| 18 | - return Environment.default | ||
| 19 | -end | ||
| 20 | - | ||
| 21 | -def get_domain(envname) | ||
| 22 | - suffix = (ENV['RAILS_ENV'] == 'production') ? '' : '.local' | ||
| 23 | - domain = Domain.find_or_create_by_name(envname + suffix) | ||
| 24 | -end | ||
| 25 | - | ||
| 26 | -TinyMceArticle # forces loading the Noosfero class before adding stuff to it | ||
| 27 | -class TinyMceArticle | ||
| 28 | - attr_accessor :is_homepage | ||
| 29 | -end | ||
| 30 | -UploadedFile # force loading Noosfero class before addinf stuff to it | ||
| 31 | -class UploadedFile | ||
| 32 | - attr_accessor :is_homepage, :filesystem_location | ||
| 33 | -end | ||
| 34 | - | ||
| 35 | -Person | ||
| 36 | - | ||
| 37 | -class FileData < StringIO | ||
| 38 | - attr_reader :original_filename, :content_type | ||
| 39 | - def initialize(actual_filename, name, content_type) | ||
| 40 | - @original_filename = name | ||
| 41 | - @content_type = content_type | ||
| 42 | - super(File.read(actual_filename)) | ||
| 43 | - end | ||
| 44 | -end | ||
| 45 | - | ||
| 46 | -class Progress | ||
| 47 | - DEBUG = true | ||
| 48 | - class << self | ||
| 49 | - def start(n) | ||
| 50 | - @total = n | ||
| 51 | - @current = 0 | ||
| 52 | - end | ||
| 53 | - def step | ||
| 54 | - @current += 1 | ||
| 55 | - end | ||
| 56 | - def step_done | ||
| 57 | - say('=> done', true) | ||
| 58 | - end | ||
| 59 | - def step_failed | ||
| 60 | - say('=> FAILED', true) | ||
| 61 | - end | ||
| 62 | - def say(msg, force = Progress::DEBUG) | ||
| 63 | - puts("[%d/%d] %s" % [@current, @total, msg]) if force | ||
| 64 | - end | ||
| 65 | - end | ||
| 66 | -end | ||
| 67 | - | ||
| 68 | -FORBIDDEN_LOGINS = %w[ | ||
| 69 | - info | ||
| 70 | -] | ||
| 71 | -Progress.start(ARGV.size) | ||
| 72 | -for username in ARGV | ||
| 73 | - Progress.step | ||
| 74 | - begin | ||
| 75 | - User.transaction do | ||
| 76 | - # guess environment | ||
| 77 | - domain_name = username.gsub(/^.*@/, '') | ||
| 78 | - environment = import_environment(domain_name) | ||
| 79 | - | ||
| 80 | - login = username.gsub(/@.*$/, '') | ||
| 81 | - if FORBIDDEN_LOGINS.include?(login) | ||
| 82 | - $stderr.puts "E: not importing #{username}, #{login} is not an allowed login" | ||
| 83 | - next | ||
| 84 | - end | ||
| 85 | - | ||
| 86 | - profile = nil | ||
| 87 | - if SCRIPT_TYPE == 'import-cooperation.net-groups' | ||
| 88 | - # import as a group | ||
| 89 | - imported_data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))['person'] | ||
| 90 | - profile = Community.create!( | ||
| 91 | - :identifier => login, | ||
| 92 | - :name => login, | ||
| 93 | - :contact_email => imported_data['email'], | ||
| 94 | - :address => imported_data['address'], | ||
| 95 | - :environment => environment, | ||
| 96 | - :preferred_domain_id => get_domain(domain_name).id | ||
| 97 | - ) | ||
| 98 | - elsif SCRIPT_TYPE == 'import-cooperation.net-articles' | ||
| 99 | - # only load the articles | ||
| 100 | - profile = Profile[login] | ||
| 101 | - else | ||
| 102 | - # import as a user | ||
| 103 | - | ||
| 104 | - # create user | ||
| 105 | - user = User.new(:login => login, :email => username, :environment => environment) | ||
| 106 | - user.enable_email = true | ||
| 107 | - | ||
| 108 | - # import person data | ||
| 109 | - imported_data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))['person'] | ||
| 110 | - user.crypted_password = imported_data.delete(:crypted_password.to_s) | ||
| 111 | - user.salt = imported_data.delete(:salt.to_s) | ||
| 112 | - user.password_type = imported_data.delete(:password_type.to_s) | ||
| 113 | - | ||
| 114 | - user.person_data = imported_data.merge('preferred_domain_id' => get_domain(domain_name).id) | ||
| 115 | - user.save! | ||
| 116 | - Progress.say "I: #{username} data imported" | ||
| 117 | - | ||
| 118 | - profile = user.person | ||
| 119 | - end | ||
| 120 | - | ||
| 121 | - # import articles | ||
| 122 | - Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| | ||
| 123 | - Progress.say "I: Trying to import #{username}'s article in #{xml} ..." | ||
| 124 | - article = TinyMceArticle.new | ||
| 125 | - article.from_xml(File.read(xml)) | ||
| 126 | - article.profile = profile | ||
| 127 | - if article.valid? | ||
| 128 | - article.save! | ||
| 129 | - Progress.say "I: #{username}'s article #{article.name.inspect} imported" | ||
| 130 | - else | ||
| 131 | - $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" | ||
| 132 | - next | ||
| 133 | - end | ||
| 134 | - | ||
| 135 | - if article.is_homepage | ||
| 136 | - profile.home_page = article | ||
| 137 | - profile.save! | ||
| 138 | - Progress.say "I: Article #{article.name.inspect} is #{username}'s homepage!" | ||
| 139 | - end | ||
| 140 | - | ||
| 141 | - orig_article_number = File.basename(xml).sub(/\.xml$/, '') | ||
| 142 | - | ||
| 143 | - File.open("tmp/rewrite-articles.txt", 'a') do |file| | ||
| 144 | - file.puts("#{orig_article_number}\t\t\t#{url_for(article.url)}") | ||
| 145 | - Progress.say "I: Article with id = #{article.id} redirected to #{url_for(article.url)}" | ||
| 146 | - end | ||
| 147 | - | ||
| 148 | - # import attachments | ||
| 149 | - attachments_dir = xml.gsub(/\.xml$/, '') | ||
| 150 | - Dir.glob(File.join(attachments_dir, 'attachments', '*.xml')).each do |attachment_xml| | ||
| 151 | - file = UploadedFile.new | ||
| 152 | - file.from_xml(File.read(attachment_xml)) | ||
| 153 | - if !File.exist?(file.filesystem_location) | ||
| 154 | - Progress.say "W: skipping attachment pointing to unexisting file" | ||
| 155 | - next | ||
| 156 | - end | ||
| 157 | - Progress.say "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" | ||
| 158 | - file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) | ||
| 159 | - file.parent = article | ||
| 160 | - file.profile = profile | ||
| 161 | - Progress.say "I: Trying to save attachment \"#{file.filename}/#{file.slug}\"" | ||
| 162 | - file.save! | ||
| 163 | - Progress.say "I: attachment added to article #{article.id}" | ||
| 164 | - | ||
| 165 | - file.reload | ||
| 166 | - if file.image? | ||
| 167 | - file.parent.body ||= '' | ||
| 168 | - file.parent.body = "<img align='left' style='margin-right: 5px; margin-bottom: 5px;' src='/#{login}/#{file.path}'/> " + file.parent.body | ||
| 169 | - else | ||
| 170 | - if file.parent.body | ||
| 171 | - file.parent.body += "<hr/><div><a href='/#{login}/#{file.path}'>#{file.abstract}</a></div>" | ||
| 172 | - end | ||
| 173 | - end | ||
| 174 | - file.parent.save | ||
| 175 | - | ||
| 176 | - orig_file_number = File.basename(attachment_xml).gsub(/\.xml$/, '') | ||
| 177 | - File.open('tmp/rewrite-files.txt', 'a') do |outfile| | ||
| 178 | - outfile.puts("#{orig_file_number}\t\t\t#{url_for(file.url)}") | ||
| 179 | - Progress.say "I: Attachment with id = #{orig_article_number} redirected to #{url_for(file.url)}" | ||
| 180 | - end | ||
| 181 | - | ||
| 182 | - end | ||
| 183 | - end | ||
| 184 | - | ||
| 185 | - if SCRIPT_TYPE != 'import-cooperation.net-articles' | ||
| 186 | - # import menus | ||
| 187 | - for i in [1,2] | ||
| 188 | - links = [] | ||
| 189 | - data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username, "menu#{i}.xml"))) | ||
| 190 | - data['menu']['items'].each do |item| | ||
| 191 | - links << { :name => item['title'], :address => item['url'] } | ||
| 192 | - end | ||
| 193 | - block = profile.blocks.select { |block| block.class == LinkListBlock }[i-1] | ||
| 194 | - block.title = data['menu']['title'] | ||
| 195 | - block.links = links | ||
| 196 | - block.save! | ||
| 197 | - Progress.say "imported links: #{links.inspect}" | ||
| 198 | - end | ||
| 199 | - end | ||
| 200 | - | ||
| 201 | - end | ||
| 202 | - Progress.step_done | ||
| 203 | - rescue Exception => e | ||
| 204 | - $stderr.puts "==================================" | ||
| 205 | - $stderr.puts "E: importing <#{username}> failed." | ||
| 206 | - $stderr.puts e | ||
| 207 | - $stderr.puts e.backtrace | ||
| 208 | - $stderr.puts "==================================" | ||
| 209 | - $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." | ||
| 210 | - Progress.step_failed | ||
| 211 | - end | ||
| 212 | -end |
script/import-cooperation.net-articles
script/import-cooperation.net-groups