forked from USGCRP/gcis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-ref.pl
More file actions
executable file
·139 lines (90 loc) · 2.7 KB
/
add-ref.pl
File metadata and controls
executable file
·139 lines (90 loc) · 2.7 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
#!/usr/bin/env perl
=head1 NAME
update-attrs.pl -- update citation attributes
=head1 DESCRIPTION
update-attrs.pl updates the attributes of a citation.
An attribute is deleted if the input value is null. No update is made if the
existing and the update values are the same.
=head1 SYNOPSIS
./update-attrs.pl [OPTIONS]
=head1 OPTIONS
=over
=item B<--url>
GCIS url, e.g. http://data-stage.globalchange.gov
=item B<--file>
File containing the citation to be updated and the updated information (yaml
format, more informatoin in example)
=item B<--max_updates>
Maximum number of update (defaults to 20)
=item B<--verbose>
Verbose option
=item B<--dry_run>
Set to perform dry run (no actual update)
=back
=head1 EXAMPLES
./update-attrs.pl -u http://data-stage.globalchange.gov < update_list.yaml
Example input file (yaml format):
---
- uri: /reference/11112222-3333-4444-5555-666677778888
URL: http://new_www.org/data/doc/12345.6789
=cut
use Getopt::Long qw/GetOptions/; use Pod::Usage qw/pod2usage/;
use Gcis::Client; use YAML::XS; use Data::Dumper; use Clone::PP qw(clone);
# use Tuba::Util qw(new_uuid);
use strict; use v5.14;
GetOptions(
'url=s' => \(my $url),
'file=s' => \(my $file),
'max_updates=i' => \(my $max_updates = 20),
'verbose' => \(my $verbose),
'dry_run|n' => \(my $dry_run),
'help|?' => sub { pod2usage(verbose => 2) }, ) or die pod2usage(verbose =>
1);
pod2usage(msg => "missing url", verbose => 1) unless $url; pod2usage(msg =>
"missing file", verbose => 1) unless $file;
my $n_updates = 0;
&main;
sub main {
say " updating attributes";
say " url : $url";
say " file : $file";
say " max updates : $max_updates";
say " verbose on" if $verbose;
say " dry run" if $dry_run;
my $g = $dry_run ? Gcis::Client->new(url => $url) :
Gcis::Client->connect(url => $url);
my $y = load_ref($file);
say " uri: $y->{uri}";
say " y :\n".Dumper($y) if $verbose;
add_ref($g, $y);
say "done";
}
sub load_ref {
my $file = shift;
open my $f, '<:encoding(UTF-8)', $file or die "can't open file : $file";
my $yml = do { local $/; <$f> };
my $y = Load($yml);
return $y;
}
sub add_ref {
my ($g, $u) = @_;
my $uri = $u->{uri};
if ($g->get($uri)) {
say " reference already exist : $uri";
return 0;
};
my $r = {
attrs => $u->{attrs},
identifier => $u->{identifier},
};
say " r :\n".Dumper($r) if $verbose;
if ($dry_run) {
say " would update reference for : $uri";
return 0;
}
say " updating reference for : $uri";
$g->post($r) or
die " unable to add reference for : $uri";
$n_updates++;
return 1;
}