-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter.rb
110 lines (84 loc) · 2.66 KB
/
exporter.rb
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
require_relative 'service'
require 'securerandom'
def show_usage
raise "Usage: #{$0} <backend URL> <repo id> <username> <password>"
end
$backend_url = ARGV.fetch(0) { show_usage }
$repo_id = ARGV.fetch(1) { show_usage }
$user = ARGV.fetch(2) { show_usage }
$password = ARGV.fetch(3) { show_usage }
$basedir = File.expand_path(File.join(File.dirname(__FILE__)))
@service = Service.new($backend_url, $repo_id, $user, $password)
export_file = File.join($basedir, "exported_#{$repo_id}.json")
@exported_uris = []
@linked_uris = []
def prepare_record_for_export(record)
record.delete('id')
record
end
def extract_links(hash_or_array)
uris = []
if hash_or_array.kind_of? Array
hash_or_array.map{|v|
uris = uris.concat(extract_links(v))
}
elsif hash_or_array.kind_of? Hash
hash_or_array.each do |k, v|
if v.kind_of? Array
uris = uris.concat(extract_links(v))
elsif v.kind_of? Hash
uris = uris.concat(extract_links(v))
elsif k == 'ref'
uris << v
end
end
end
uris
end
File.open(export_file, "w") do |out|
exported_records = []
records_to_import = ['resource', 'archival_object', 'digital_object', 'digital_object_component', 'accession', 'classification']
records_to_import.each do |record_type|
p "-- Get all of type: #{record_type}"
ids = @service.get_ids_for_type(record_type)
ids.each_slice(50).each do |id_set|\
p id_set
records = @service.get_records_for_type(record_type, id_set)
records.map {|record|
@exported_uris << record['uri']
extract_links(record).each do |uri|
unless @exported_uris.include?(uri)
@linked_uris << uri
end
end
exported_records << prepare_record_for_export(record)
}
end
end
while(true)
@linked_uris.clone.each do |uri|
if @exported_uris.include?(uri) || uri == @service.repo_uri || uri =~ /tree/
@linked_uris.delete(uri)
next
end
p "-- linked record: #{uri}"
record = @service.get_record(uri)
@exported_uris << record['uri']
@linked_uris.delete(record['uri'])
extract_links(record).each do |uri|
unless @exported_uris.include?(uri)
@linked_uris << uri
end
end
exported_records << prepare_record_for_export(record)
end
break if @linked_uris.empty?
p "-- #{@linked_uris.length} linked records to go"
end
out.puts exported_records.to_json
end
file_contents = File.read(export_file)
file_contents.gsub!(/#{@service.repo_uri}/, "/repositories/REPO_ID_GOES_HERE")
File.open(export_file, "w") {|file| file.puts file_contents }
p "--"
p "-- Output file: #{export_file}"