forked from eprints/eprints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport
executable file
·478 lines (364 loc) · 10.9 KB
/
import
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
474
475
476
477
#!/usr/bin/perl -w
use FindBin;
use lib "$FindBin::Bin/../perl_lib";
######################################################################
#
#
######################################################################
=pod
=for Pod2Wiki
=head1 NAME
B<import> - Import a file using an import plugin.
=head1 SYNOPSIS
B<import> I<repository_id> [B<options>] I<dataset>
B<import> I<repository_id> [B<options>] I<dataset> I<plugin> I<filename>
=head1 DESCRIPTION
This command imports a set of EPrints from a file into the given dataset.
=head1 ARGUMENTS
=over 8
=item I<repository_id>
The ID of the EPrint repository to import to.
=item I<dataset>
The name of the dataset to import into, such as "eprint","archive", "subject" or "user".
Please note that for the "subject" dataset, you are probably better off using the import_subjects tool which will empty the dataset before importing.
=item I<plugin>
The id of the input plugin to use. This should not include the leading "Import::". Examples: BibTeX, XML.
If this is omitted or an invalid plugin is requested, then 'import' will list all plugins compatible with the dataset and exit.
=back
=head1 OPTIONS
=over 8
=item B<--user USERID/USERNAME>
For eprint datasets only. (not user or subject).
Sets the userid/username of the user in the system who will own the imported records.
Usually required for importing EPrint records. This may not be required if the import format contains the userid value, eg. an import in the EPrints 3 XML format.
If this is an integer then it is assumed to be the userid of the user, otherwise it is assumed to be the username.
You may wish to create one or more "bulk import" users and make imported eprint records belong to them.
=item B<--arg key=value>
Add an argument to the import plugin. May be repeated. Effects depend on the import plugin.
=item B<--parse-only>
Don't import, just check the file.
=item B<--dump>
Dump epdata to STDERR while importing (for debugging).
=item B<--migration>
Turn on all the options needed to correctly import an XML data file exported from version 2, using the migration toolkit. Implies --enable-web-imports --enable-file-imports --force
=item B<--enable-import-fields>
Allow the import to set fields that are set as not to be imported. This is typically used to specify the imported object id.
=item B<--enable-file-imports>
Allow the imported data to import files from the local filesystem. This can obviously be seen as a security hole if you don't trust the data you are importing.
This sets the "enable_file_imports" configuration option for this session only.
=item B<--enable-web-imports>
Allow the imported data to import files from the Web. This can obviously be seen as a security hole if you don't trust the data you are importing.
This sets the "enable_web_imports" configuration option for this session only.
=item B<--update>
Normally you can not import a new item with the same id as an existing item. With this option enabled existing items will be updated with the new item. Combine with --enable-import-fields to update all field values.
=item B<--force>
Don't ask any questions, just do it!
=item B<--help>
Print a brief help message and exit.
=item B<--man>
Print the full manual page and then exit.
=item B<--quiet>
Be vewwy vewwy quiet. This option will suppress all output unless an error occurs.
=item B<--verbose>
Explain in detail what is going on.
May be repeated for greater effect.
Shows why a plugin is disabled.
=item B<--version>
Output version information and exit.
=back
=cut
use Getopt::Long;
use Pod::Usage;
use strict;
use EPrints;
my $version = 0;
my $verbose = 0;
my $quiet = 0;
my $purge = 1;
my $help = 0;
my $man = 0;
my $force = 0;
my $single = 0;
my $scripted = 0;
my $user = undef;
my $parse_only = 0;
my $enable_file_imports = 0;
my $enable_web_imports = 0;
my $enable_import_fields = 0;
my $migration = 0;
my $update = 0;
my %opt_args;
my @actions;
my $dump = 0;
Getopt::Long::Configure("permute");
GetOptions(
'help|?' => \$help,
'man' => \$man,
'force' => \$force,
'user=s' => \$user,
'version' => \$version,
'verbose+' => \$verbose,
'silent' => \$quiet,
'quiet' => \$quiet,
'scripted' => \$scripted,
'single' => \$single,
'parse-only' => \$parse_only,
'enable-import-fields' => \$enable_import_fields,
'enable-file-imports' => \$enable_file_imports,
'enable-web-imports' => \$enable_web_imports,
'migration' => \$migration,
'update' => \$update,
'arg=s' => \%opt_args,
'action=s' => \@actions,
'dump' => \$dump,
) || pod2usage( 2 );
EPrints::Utils::cmd_version() if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 2 && scalar @ARGV !=4 );
my $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );
if( $migration )
{
$force = 1;
$enable_file_imports = 1;
$enable_web_imports = 1;
$enable_import_fields = 1;
}
if( $scripted ) { $noise = 0; }
# Set STDOUT to auto flush (without needing a \n)
$|=1;
my $repoid = shift @ARGV;
my $datasetid = shift @ARGV;
my $ipluginid = shift @ARGV;
my $filename = shift @ARGV;
my $session = new EPrints::Session( 1, $repoid, $noise );
if( !defined $session )
{
print STDERR "Failed to load repository: $repoid\n";
exit 1;
}
if( $enable_file_imports )
{
# doesn't use a setter method!
$session->get_repository->{config}->{enable_file_imports} = 1;
}
if( $enable_web_imports )
{
# doesn't use a setter method!
$session->get_repository->{config}->{enable_web_imports} = 1;
}
if( $enable_import_fields )
{
$session->get_repository->{config}->{enable_import_fields} = 1;
}
my $ds = $session->dataset( $datasetid ) ;
if( !defined $ds )
{
print STDERR "Unknown Dataset ID: $datasetid\n";
$session->terminate;
exit 1;
}
my $userobj;
if( defined $user )
{
if( $user =~ m/^\d+/ )
{
$userobj = EPrints::DataObj::User->new( $session, $user );
}
else
{
$userobj = EPrints::DataObj::User::user_with_username( $session, $user );
}
if( !defined $userobj )
{
print STDERR "Can't find user with userid/username [$user]\n";
exit 1;
}
}
if( !defined $ipluginid )
{
my @list_plugins = $session->plugin_list(
type=>"Import",
can_produce=>"list/".$ds->confid );
my @dataobj_plugins = $session->plugin_list(
type=>"Import",
can_produce=>"dataobj/".$ds->confid );
my %p = ();
my %l = ();
my %d = ();
foreach( @list_plugins ) { $p{$_} = $_; $l{$_} = 1; }
foreach( @dataobj_plugins ) { $p{$_} = 1; $d{$_} = 1; }
print "Available input formats:\n";
foreach my $a_plugin_id ( sort keys %p )
{
my $a_plugin = $session->plugin( $a_plugin_id );
printf( "% 16s", $a_plugin->get_subtype);
print ": ".$a_plugin->get_name();
if( $l{$a_plugin_id} && !$d{$a_plugin_id} )
{
print " (List input only)";
}
if( $d{$a_plugin_id} && !$l{$a_plugin_id} )
{
print " (Single object input only)";
}
if( $a_plugin->broken )
{
print " (DISABLED)";
print "\n** Disabled because: ".$a_plugin->error_message if( $noise > 1 );
}
print "\n";
}
$session->terminate();
exit;
}
if( !defined $user && $ds->confid eq "eprint" && !$force )
{
print <<END;
Warning! You haven't specified a user id to own these eprints,
That's OK, assuming the input file specifies the userid for each eprint.
(you can suppress this check with --force).
END
unless( EPrints::Utils::get_input_confirm( "Continue?", 1 ) )
{
$session->terminate();
exit 1;
}
}
my $count = 0;
my $key_field = $ds->key_field;
my $handler = EPrints::CLIProcessor->new(
session => $session,
epdata_to_dataobj => sub {
my( $epdata, %opts ) = @_;
if( $opts{dataset}->base_id eq "eprint" )
{
$epdata->{userid} = $userobj->id if defined $userobj;
}
my $id = $epdata->{$opts{dataset}->key_field->name};
print "EPRINTS_IMPORT: ITEM_PARSED".(defined $id ? " $id" : "")."\n" if $scripted && $parse_only;
EPrints->dump($epdata) if $dump;
$count++, return if $parse_only;
my $item;
if(
$update &&
EPrints::Utils::is_set( my $id = $epdata->{$key_field->name} )
)
{
$item = $ds->dataobj( $id );
}
if( $update && defined $item )
{
$item->update( $epdata,
include_subdataobjs => 1,
);
$item->commit;
print "EPRINTS_IMPORT: ITEM_UPDATED ".$item->get_id."\n" if $scripted;
}
elsif( defined($item = $opts{dataset}->create_dataobj( $epdata )) )
{
print "EPRINTS_IMPORT: ITEM_IMPORTED ".$item->get_id."\n" if $scripted;
}
else
{
print "EPRINTS_IMPORT: ITEM_FAILED".(defined $id ? " $id" : "")."\n" if $scripted;
}
return if !defined $item;
$count++, return $item;
},
);
my $pluginid = "Import::".$ipluginid;
my $plugin = $session->plugin( $pluginid, parse_only=>$parse_only, Handler=>$handler );
if( !defined $plugin )
{
# This warning is already generated by the plugin
# code.
#print STDERR "Plugin $pluginid not found.\n";
$session->terminate();
exit 1;
}
my $req_plugin_type = ($single ? "dataobj/" : "list/").$ds->confid;
unless( $plugin->can_produce( $req_plugin_type ) )
{
print STDERR "Plugin $pluginid can't process $req_plugin_type data.\n";
$session->terminate();
exit 1;
}
if( $plugin->broken )
{
print STDERR "Plugin $pluginid could not run because:\n";
print STDERR $plugin->error_message."\n";
$session->terminate();
exit 1;
}
my %arguments = %{$plugin->param( "arguments" )};
foreach my $argid ( keys %opt_args )
{
if( !exists $arguments{$argid} )
{
print STDERR "Plugin $pluginid does not accept argument $argid\n";
$session->terminate();
exit 1;
}
$arguments{$argid} = $opt_args{$argid};
}
if( $scripted )
{
print "EPRINTS_IMPORT: BEGIN\n";
}
# improve ordervalues insertion performance
$session->cache_subjects;
my $list = eval { $plugin->input_file(
%arguments,
dataset=>$ds,
filename=>$filename,
user=>$userobj,
actions=>\@actions,
) };
if( $@ )
{
$handler->message( "error", $session->make_text( "Unhandled exception in ".$plugin->{id}.": $@" ) );
}
if( $scripted )
{
print "EPRINTS_IMPORT: DONE $count\n";
}
if( $count > 0 )
{
if( $noise > 0 )
{
if( $parse_only )
{
print "Would have imported $count records\n";
}
else
{
print "Number of records imported: $count\n";
if( $noise > 1 )
{
print join( ",", @{$list->ids})."\n";
}
}
}
}
$session->terminate();
exit;
=head1 COPYRIGHT
=for COPYRIGHT BEGIN
Copyright 2000-2011 University of Southampton.
=for COPYRIGHT END
=for LICENSE BEGIN
This file is part of EPrints L<http://www.eprints.org/>.
EPrints is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EPrints is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with EPrints. If not, see L<http://www.gnu.org/licenses/>.
=for LICENSE END