-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvert.rb
More file actions
executable file
·91 lines (69 loc) · 2.18 KB
/
convert.rb
File metadata and controls
executable file
·91 lines (69 loc) · 2.18 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
#!/usr/bin/env ruby
# encoding: utf-8
require 'nokogiri'
require 'mongo'
# ath down
# backup db
# read in convert.xml to see which how to convert the string-arrays
# for each lang in db
# find the string-arrays that match the ones in convert.xml and for each
# add new string with name from conversions and value from old array
# drop array string
# ath up
# verify
def parse_string(element)
return nil if element.nil?
s = ''
# element.text strips HTML like <b> and/or <i> that we want to keep, so we loop over the children
# taking each child's to_xml to preserve them. Manually setting encoding seems to be necessary
# to preserve multi-byte characters.
element.children.each do |c|
s << c.to_xml(:encoding => 'utf-8')
end
s
end
conversions = {}
conv_doc = Nokogiri::XML(IO.read('./conversions.xml'))
conv_doc.xpath('//string-array').each do |sa_el|
a = []
sa_el.element_children.each_with_index do |item_el, i|
a[i] = parse_string(item_el)
end
conversions[sa_el.attr('name')] = a
end
db = Mongo::Connection.new.db('ath_bi')
langs = db.collection_names.to_a.delete_if {|i| i =~ /\./} - ['en']
langs.each do |lang|
existing = {}
ars = {}
c = db.collection(lang)
c.find.each_entry do |entry|
if ars[entry['name']].nil? || ars[entry['name']]['modified_at'] < entry['hash']['modified_at']
if entry['name'] =~ /\[/ and not entry['name'] =~ /:/
ars[entry['name']] = entry['hash']
end
end
end
ars.each do |old_name, a|
update = []
a_name = old_name.split("[")[0]
i = old_name.split("[")[1].split("]")[0].to_i
value = a['string']
new_name = conversions[a_name][i]
puts "#{old_name}: #{a}"
puts " Will be renamed to: #{new_name}"
if existing.has_key?(new_name)
puts "Name #{new_name} already exists with content: #{existing[new_name]}"
if existing[new_name] == value
puts " (Skipped) new content is the same: #{value}"
else
puts " (Skipped) new content is different: #{value}"
end
else
existing[new_name] = value
update.push('name' => new_name, 'hash' => a)
end
c.insert(update)
c.remove({"name" => old_name})
end
end