forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_source_
More file actions
executable file
·473 lines (368 loc) · 13.3 KB
/
Copy pathfiles_source_
File metadata and controls
executable file
·473 lines (368 loc) · 13.3 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/perl
my $dvobjectoffset = shift @ARGV;
unless ($dvobjectoffset > 0)
{
print STDERR "Usage: ./files_source_ <DVOBJECT DB ID OFFSET>\n";
exit 1;
}
my $filecatid = 0; # file categories (this is a new object in 4.0, so there are no 3.6 IDs to reuse)
# (offset this if adding content content to an already populated db)
use DBI;
my $host = "localhost";
my $username = "xxxxx";
my $database = "xxxxx";
my $password = "xxxxx";
my $dbh = DBI->connect("DBI:Pg:dbname=$database;host=$host",$username,$password);
open PL, ">packlist.txt";
%STUDYMAP = {};
%STUDYFILEMAP = {};
while ( <> )
{
chop;
my ($globalid, $dsid, $dsvid, $dsvnum) = split("\t", $_);
%FILECATEGORIES = {}; # file categories for this dataset.
if ($globalid =~/^([a-z]*):(.*)\/([^\/]*)$/)
{
$protocol = $1;
$authority = $2;
$identifier = $3;
# print $protocol . " " . $authority . " " . $identifier . "\n";
}
else
{
print STDERR "WARNING! illegal global id: " . $globalid . "\n";
next;
}
my $sth;
#$sth = $dbh->prepare(qq {SELECT s.id, v.id FROM study s, studyversion v WHERE v.study_id = s.id AND s.protocol = '$protocol' AND s.authority='$authority' AND s.studyid = '$identifier' AND v.versionstate = 'RELEASED'});
$sth = $dbh->prepare(qq {SELECT s.id, v.id FROM study s, studyversion v WHERE v.study_id = s.id AND s.protocol = '$protocol' AND s.authority='$authority' AND s.studyid = '$identifier' AND v.versionnumber = $dsvnum});
$sth->execute();
my $vercount = 0;
my $sid;
my $svid;
while ( @foo = $sth->fetchrow() )
{
$sid = $foo[0];
$svid = $foo[1];
# print $sid . "\t" . $svid . "\n";
$vercount++;
}
$sth->finish;
unless ($vercount == 1)
{
print STDERR "WARNING: invalid number of versions for study " . $globalid . ", with version number " . $dsvnum . " (" . $vercount . ")!\n";
next;
}
$sth = $dbh->prepare(qq {SELECT fm.label, fm.category, fm.description, sf.filetype, sf.filesystemlocation, sf.md5, sf.restricted, sf.subsettable, sf.originalfiletype, sf.unf, sf.id, sf.fileclass, fm.id FROM filemetadata fm, studyfile sf WHERE fm.studyfile_id = sf.id AND fm.studyversion_id = $svid});
# print qq {SELECT fm.label, fm.category, fm.description, sf.filetype, sf.filesystemlocation, sf.md5, sf.restricted, sf.subsettable, sf.originalfiletype, sf.unf FROM filemetadata fm, studyfile sf WHERE fm.studyfile_id = sf.id AND fm.studyversion_id = $svid} . "\n";
$sth->execute();
my $newfile = 0;
while ( @foo = $sth->fetchrow() )
{
# new filemetadata fields:
$label = $foo[0];
$description = $foo[2];
$description =~s/\n/ /g;
$description = $dbh->quote($description);
# category:
$category = $foo[1];
# new datafile fields:
$type = $foo[3];
unless ($type =~m:/:)
{
$type = "application/octet-stream";
}
$md5 = $foo[5];
$restricted = $foo[6];
# location of the file, on the old filesystem:
$fslocation = $foo[4];
# additional info for subsettable files:
# (will go into the new datatable)
$subsettable = $foo[7];
$originalfiletype = $foo[8];
$unf = $foo[9];
# id of the existing studyfile:
$sfid = $foo[10];
# "class" of the existing studyfile:
# (tabular, "other", etc.)
$fileclass = $foo[11];
$fmid = $foo[12];
#print join ("\t", @_) . "\n";
if ($label =~/[\\\/:\*\?\"\<\>\|;\#]/)
{
$preservedlabel = $label;
$label=~s/[\\\/:\*\?\"\<\>\|;\#]//g;
print STDERR "LABEL REPLACED: (FILEMETA: " . $fmid . ", FILE: " . $sfid . ", STUDY: " . $sid . ", VERSION: " . $svid . ", GLOBALID: " . $globalid . ") OLD: \"" . $preservedlabel . "\", NEW: \"" . $label . "\"\n";
}
if ($label eq '')
{
$label = "UNKNOWN";
}
$label = $dbh->quote($label);
unless ($STUDYFILEMAP{$sfid})
{
$newfile = 1;
# Certain things only need to be done once per file -
# namely, each file needs one dvobject and datafile each;
# same for the datatables and variables.
# Other things, like filemetadatas, need to be created one
# per version.
$newdatafileid = ($dvobjectoffset+$sfid);
$STUDYFILEMAP{$sfid} = $newdatafileid;
##$dvobjectoffset++;
$fsname = $fslocation;
if ($fslocation =~/^http/ )
{
$fsize = 0;
$fmtime = &formatTimeStamp(time);
}
else
{
if ( -f $fslocation )
{
@fstats = stat($fslocation);
$fsize = $fstats[7];
$mtime = $fstats[9];
$fmtime = &formatTimeStamp($mtime);
$packlistentry = $fslocation;
$packlistentry =~s/.*\/DVN\/data\///;
print PL $packlistentry . "\n";
}
else
{
print STDERR "WARNING: file " . $fslocation . " not found!\n";
$fsize = 0;
$fmtime = &formatTimeStamp(time);
}
$fsname =~s/^.*\///g;
}
# dvobject:
print qq {INSERT INTO dvobject (id, dtype, owner_id, createdate, modificationtime) VALUES ($newdatafileid, 'DataFile', $dsid, '$fmtime', '$fmtime');} . "\n";
# datafile object:
print qq {INSERT INTO datafile (id, contenttype, filesystemname, filesize, md5, restricted) VALUES ($newdatafileid, '$type', '$fsname', $fsize, '$md5', TRUE);} . "\n";
}
else
{
$newdatafileid = $STUDYFILEMAP{$sfid};
$newfile = 0;
}
# file metadata object:
print qq {INSERT INTO filemetadata (id, description, label, restricted, version, datasetversion_id, datafile_id) VALUES ($fmid, $description, $label, TRUE, 1, $dsvid, $newdatafileid);} . "\n";
##print qq {INSERT INTO filemetadata (id, description, label, restricted, version, datasetversion_id, datafile_id) VALUES ($fmid, $description, $label, TRUE, 1, $dsvid, $dvobjectoffset);} . "\n";
# and the category, if exists:
if ($category && $category ne "")
{
$category = $dbh->quote($category);
unless ($FILECATEGORIES{$category})
{
# this is a new category (for this dataset),
# so it needs to be created:
$filecatid++;
print qq{INSERT INTO datafilecategory (id, name, dataset_id) VALUES ($filecatid, $category, $newdatafileid);} . "\n";
#print qq{INSERT INTO datafilecategory (id, name, dataset_id) VALUES ($filecatid, $category, $dvobjectoffset);} . "\n";
$FILECATEGORIES{$category} = $filecatid;
}
my $fcid = $FILECATEGORIES{$category};
print qq{INSERT INTO filemetadata_datafilecategory (filecategories_id, filemetadatas_id) VALUES ($fcid, $fmid);} . "\n";
}
# subsettable files:
# (again, this only needs to be done once per file!)
if ($newfile && ($fileclass eq "TabularDataFile"))
{
#print STDERR "this is a subsettable file.\n";
# NOTE:
# there's only one datatable per file - make sure to only run this once!
# (i.e., not for every version!)
$sth1 = $dbh->prepare(qq {SELECT id, varquantity, casequantity, unf, recordspercase FROM datatable WHERE studyfile_id = $sfid});
$sth1->execute();
$count = 0;
while ( @dt = $sth1->fetchrow() )
{
$dtid = $dt[0];
$varquantity = $dt[1];
$casequantity = $dt[2];
$dtunf = $dt[3];
$recordspercase = $dt[4];
$count++;
unless ($unf eq $dtunf)
{
print STDERR "WARNING: unf mismatch, between studyfile and datatable: " + $unf + ":" + $dtunf + "\n";
}
# datatable object:
if ($recordspercase)
{
print qq {INSERT INTO datatable (id, varquantity, casequantity, unf, originalfileformat, recordspercase, datafile_id) VALUES ($dtid, $varquantity, $casequantity, '$unf', '$originalfiletype', $recordspercase, $newdatafileid);} . "\n";
#print qq {INSERT INTO datatable (id, varquantity, casequantity, unf, originalfileformat, recordspercase, datafile_id) VALUES ($dtid, $varquantity, $casequantity, '$unf', '$originalfiletype', $recordspercase, $dvobjectoffset);} . "\n";
}
else
{
print qq {INSERT INTO datatable (id, varquantity, casequantity, unf, originalfileformat, datafile_id) VALUES ($dtid, $varquantity, $casequantity, '$unf', '$originalfiletype', $newdatafileid);} . "\n";
#print qq {INSERT INTO datatable (id, varquantity, casequantity, unf, originalfileformat, datafile_id) VALUES ($dtid, $varquantity, $casequantity, '$unf', '$originalfiletype', $dvobjectoffset);} . "\n";
}
}
$sth1->finish;
unless ($count == 1)
{
print STDERR "WARNING: invalid numbe of datatables: " + $count +".\n";
}
else
{
# variables:
$sth1 = $dbh->prepare(qq {SELECT name, label, variableformattype_id, variableintervaltype_id, formatcategory, formatschema, formatschemaname, unf, fileorder, weighted, orderedfactor, numberofdecimalpoints, universe, filestartposition, fileendposition, recordsegmentnumber, id FROM datavariable WHERE datatable_id = $dtid});
$sth1->execute();
while ( @dv = $sth1->fetchrow() )
{
$varname = $dv[0];
$varname = $dbh->quote($varname);
$varlabel = $dv[1];
$varlabel = $dbh->quote($varlabel);
$variableformattype_id = $dv[2];
# the old school formattype_id and
# intervaltype_id need to be adjusted by 1,
# to match the new enum values used in the
# 4.0 datavariables:
$variableformattype_id--;
$variableintervaltype_id = $dv[3];
$variableintervaltype_id--;
$varformatcategory = $dv[4];
$varformatschema = $dv[5];
$varformatschemaname = $dv[6];
$varunf = $dv[7];
$varfileorder = $dv[8];
$varweighted = $dv[9];
if ($varweighted)
{
$varweighted = "TRUE";
}
else
{
$varweighted = "FALSE";
}
$varorderedfactor = $dv[10];
if ($varorderedfactor)
{
$varorderedfactor = "TRUE";
}
else
{
$varorderedfactor = "FALSE";
}
$varnumberofdecimalpoints = $dv[11];
$varuniverse = $dv[12];
$varfilestartposition = $dv[13];
$varfileendposition = $dv[14];
$varrecordsegmentnumber = $dv[15];
$varid = $dv[16];
# new datavariable object:
$newdvfields = "id, name, label, interval, type, unf, fileorder, orderedfactor, weighted, datatable_id";
$newdvvalues = qq {$varid, $varname, $varlabel, $variableintervaltype_id, $variableformattype_id, '$varunf', $varfileorder, $varorderedfactor, $varweighted, $dtid};
if ($varformatschemaname)
{
# becomes "format":
$newdvfields = $newdvfields . ", format";
$newdvvalues = qq{$newdvvalues, '$varformatschemaname'};
}
if ($varformatcategory)
{
$newdvfields = $newdvfields . ", formatcategory";
$newdvvalues = qq{$newdvvalues, '$varformatcategory'};
}
if ($varfilestartposition)
{
$newdvfields = $newdvfields . ", filestartposition";
$newdvvalues = qq{$newdvvalues, $varfilestartposition};
}
if ($varfileendposition)
{
$newdvfields = $newdvfields . ", fileendposition";
$newdvvalues = qq{$newdvvalues, $varfileendposition};
}
if ($varrecordsegmentnumber)
{
$newdvfields = $newdvfields . ", recordsegmentnumber";
$newdvvalues = qq{$newdvvalues, $varrecordsegmentnumber};
}
if ($varuniverse)
{
$newdvfields = $newdvfields . ", universe";
$newdvvalues = qq{$newdvvalues, '$varuniverse'};
}
if ($varnumberofdecimalpoints)
{
$newdvfields = $newdvfields . ", numberofdecimalpoints";
$newdvvalues = qq{$newdvvalues, $numberofdecimalpoints};
}
print qq {INSERT INTO datavariable ($newdvfields) VALUES ($newdvvalues);} . "\n";
# variable categories:
$sth2 = $dbh->prepare(qq {SELECT id, label, value, missing, catorder, frequency FROM variablecategory WHERE datavariable_id = $varid});
$sth2->execute();
while ( @vc = $sth2->fetchrow() )
{
$varcatid = $vc[0];
$varcatlabel = $vc[1];
$varcatvalue = $vc[2];
$varcatmissing = $vc[3];
if ($varcatmissing)
{
$varcatmissing = "true";
}
else
{
$varcatmissing = "false";
}
$varcatorder = $vc[4];
unless ($varcatorder)
{
if ($varcatorder eq "" || $varcatorder != 0)
{
$varcatorder = "null";
}
}
$varcatfreq = $vc[5];
unless ($varcatfreq)
{
if ($varcatfreq eq "" || $varcatfreq != 0)
{
$varcatfreq = "null";
}
}
# only migrate the *real* categories:
if ($varcatlabel)
{
$varcatlabel = $dbh->quote($varcatlabel);
unless ($varcatvalue || ($varcatvalue eq "") || ($varcatvalue == 0))
{
#print STDERR "WARNING: empty var cat value (" . $varcatlabel . ")\n";
print STDERR qq {INSERT INTO variablecategory (id, label, value, missing, catorder, frequency, datavariable_id) VALUES ($varcatid, $varcatlabel, $varcatvalue, $varcatmissing, $varcatorder, $varcatfreq, $varid);} . "\n";
}
else
{
$varcatvalue = $dbh->quote($varcatvalue);
print qq {INSERT INTO variablecategory (id, label, value, missing, catorder, frequency, datavariable_id) VALUES ($varcatid, $varcatlabel, $varcatvalue, $varcatmissing, $varcatorder, $varcatfreq, $varid);} . "\n";
}
}
else
{
#print STDERR "empty var cat label.\n";
}
}
$sth2->finish;
}
$sth1->finish;
}
}
}
$sth->finish;
}
$dbh->disconnect;
close PL;
exit 0;
sub formatTimeStamp () {
my ($mtime) = (@_);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
$year+=1900;
$mon++;
$fmt = $year . "-" . sprintf("%02d",$mon) . "-" . sprintf("%02d",$mday) . " " .
sprintf("%02d", $hour) . ":" . sprintf("%02d",$min) . ":" . sprintf("%02d",$sec);
return $fmt;
}