-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdmd-Preservation-DCmultidate
More file actions
executable file
·191 lines (149 loc) · 5.5 KB
/
dmd-Preservation-DCmultidate
File metadata and controls
executable file
·191 lines (149 loc) · 5.5 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
#!/usr/bin/env perl
package dmdPreservationDCMultiDate;
use strict;
use warnings;
use FindBin;
use lib "$FindBin::RealBin/../lib";
use Getopt::Long;
use Fcntl qw(:DEFAULT :flock);
use Try::Tiny;
use Log::Log4perl;
use File::Find;
use Data::Dumper;
use Digest::MD5;
use JSON;
use Poppler;
use XML::LibXML;
use CIHM::Swift::Client;
use URI::Escape;
use DateTime::Format::ISO8601;
use HTTP::Date qw(:DEFAULT time2isoz);
use File::Spec;
use File::stat;
use XML::LibXML;
use utf8;
use Switch;
use CIHM::Meta::DCCSV;
Log::Log4perl->init_once("/etc/canadiana/tdr/log4perl.conf");
my $logger = Log::Log4perl::get_logger("CIHM::TDR");
my $inThisFile;
sub log_warnings {
my $warning = shift;
chomp $warning;
# Strip wide characters before trying to log
( my $stripped = $warning ) =~ s/[^\x00-\x7f]//g;
$stripped =~ s/\R/ /g;
if ( defined $inThisFile ) {
print STDERR "$stripped in $inThisFile\n";
$logger->warn("$stripped in $inThisFile");
}
else {
$logger->warn($stripped);
print STDERR "$stripped\n";
}
}
local $SIG{__WARN__} = sub { &log_warnings };
my $dmddir = '/crkn-nas-wip/_Metadata_Synchronised';
my $csvdir = '/crkn-nas-wip/_Metadata_Synchronised/Fixed/dcDateRanges';
$logger->info("DMD Preservation DC MultiDate: start");
GetOptions(
'dmddir:s' => \$dmddir,
'csvdir:s' => \$csvdir,
);
# Used to show a different processname during processing
my $dmddcmultiprog = $0;
#
my $dccsv;
## Test an ID
#$dccsv = CIHM::Meta::DCCSV->new( { csvdir => $csvdir } );
#$dccsv->addElement( "oocihm.7a9790", "thisis" , "atest" );
#exit 0;
my $preservationdir = File::Spec->catfile( $dmddir, 'preservation' );
opendir( my $dh, $preservationdir ) || die "Can't open $preservationdir: $!";
while ( readdir $dh ) {
if ( substr( $_, 0, 1 ) ne "." ) {
my $prefixdir = $_;
my $preservationcsvdir =
File::Spec->catfile( $csvdir, 'preservation', $prefixdir );
my $preservationsubdir =
File::Spec->catfile( $dmddir, 'preservation', $prefixdir );
$dccsv = CIHM::Meta::DCCSV->new( { csvdir => $preservationcsvdir } );
$0 = $dmddcmultiprog . " find in $preservationsubdir";
print "Find for $prefixdir\n";
find( \&matching_dmd_file, $preservationsubdir );
print "Write for $prefixdir\n";
$dccsv->writeCSV();
undef $dccsv;
}
}
closedir $dh;
exit 0;
sub matching_dmd_file {
if ( -f $_ ) {
$0 = $dmddcmultiprog . " looking for DC in $File::Find::dir";
if (/^(.*)-(\w*)\.xml$/) {
my $id = $1;
my $dmdType = $2;
if ( $dmdType eq 'DC' ) {
$inThisFile = $File::Find::name;
open my $fh, '<:encoding(UTF-8)', $File::Find::name
or die "can't open DC file=$File::Find::name: $!\n";
my $xmlin = do { local $/; <$fh> };
close $fh;
undef $inThisFile;
# Ugly, but does the trick for now.
# Great when we can get rid of these odd hacks for odd data
$xmlin =~
s|<simpledc>|<simpledc xmlns:dc="http://purl.org/dc/elements/1.1/">|g;
my $doc = XML::LibXML->new->parse_string($xmlin);
my $xpc = XML::LibXML::XPathContext->new;
$xpc->registerNs( 'dc', 'http://purl.org/dc/elements/1.1/' );
my @datenodes = $xpc->findnodes( "//dc:date", $doc );
if ( scalar(@datenodes) > 2 ) {
my $warn = "$id has " . scalar(@datenodes) . " dates";
warn "$warn\n";
}
elsif ( scalar(@datenodes) == 2 ) {
# Add AIP ID as an identifier
$dccsv->addElement( $id, "identifier", $id );
# print "Orig $id:" . $doc->toString(1) . "\n";
# Append the second date to the first.
$datenodes[0]
->appendTextNode( "/" . $datenodes[1]->textContent );
# Clean newline too just to look right...
my $sibling = $datenodes[1]->nextSibling();
if ( $sibling->nodeType() == XML_TEXT_NODE ) {
my $text = $sibling->data();
if ( $text =~ /^\s*$/ ) {
$sibling->unbindNode();
}
}
# Remove the second date
$datenodes[1]->unbindNode();
#print "Fixed DC:" . $doc->toString(1) . "\n";
my @allnodes = $xpc->findnodes( "//*", $doc );
foreach my $node (@allnodes) {
my $content = normaliseSpace( $node->textContent );
if ( length($content) ) {
my $nodename = lc( $node->nodeName );
$nodename =~
s|dc:||g; # Strip namespace if it exists
if ( $nodename ne "simpledc" ) {
$dccsv->addElement( $id, $nodename, $content );
}
}
}
}
}
}
else {
warn $_ . " -- doesn't match pattern (in $File::Find::dir )\n";
}
}
}
sub normaliseSpace {
my $content = shift;
$content =~ s/^\s+|\s+$//g; # Trim space at end and beginning.
$content =~ s/\s+/ /g; # Remove extra spaces
return $content;
}