]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/marc_export
add 901-overwriting to exported records
[Evergreen.git] / Open-ILS / src / support-scripts / marc_export
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use OpenSRF::System;
6 use OpenSRF::EX qw/:try/;
7 use OpenSRF::AppSession;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenILS::Application::AppUtils;
10 use OpenILS::Utils::Fieldmapper;
11
12 use MARC::Record;
13 use MARC::File::XML;
14 use UNIVERSAL::require;
15
16 use Getopt::Long;
17
18 my @formats = qw/USMARC UNIMARC XML/;
19
20 my ($config,$format,$encoding,$help) = ('/openils/conf/bootstrap.conf','USMARC','MARC8');
21
22 GetOptions(
23         'help'      => \$help,
24         'config=s'      => \$config,
25         'format=s'      => \$format,
26         'encoding=s'      => \$encoding,
27 );
28
29 if ($help) {
30         print <<"       HELP";
31 Usage: $0 [options]
32  --help or -h           This screen.
33  --config or -c         Configuration file [/openils/conf/bootstrap.conf]
34  --format or -f         Output format (USMARC, UNIMARC, XML) [USMARC]
35  --encoding or -e       Output Encoding (UTF-8, ISO-8859-?, MARC8) [MARC8]
36
37 Example:
38
39   cat list_of_ids | $0 > output_file
40
41         HELP
42         exit;
43 }
44
45 $format = uc($format);
46 $encoding = uc($encoding);
47
48 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
49 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
50
51 if (!grep { $format eq $_ } @formats) {
52         die     "Please select a supported format.  ".
53                 "Right now that means one of [".
54                 join('|',@formats). "]\n";
55 }
56
57 if ($format ne 'XML') {
58         my $type = 'MARC::File::' . $format;
59         $type->require;
60 }
61
62 OpenSRF::System->bootstrap_client( config_file => $config );
63 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
64
65 my $ses = OpenSRF::AppSession->connect('open-ils.cstore');
66
67 print <<HEADER if ($format eq 'XML');
68 <?xml version="1.0" encoding="$encoding"?>
69 <collection xmlns='http://www.loc.gov/MARC21/slim'>
70 HEADER
71
72 while ( my $i = <> ) {
73         my $bib = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i )->gather(1);
74
75         next unless $bib;
76
77         my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
78         $r->delete_field( $_ ) for ($r->field(901));
79
80         $r->append_fields(
81                 MARC::Field->new(
82                         901, '', '', 
83                         a => $bib->tcn_value,
84                         b => $bib->tcn_source
85                 )
86         );
87
88         if (uc($format) eq 'XML') {
89                 print $r->as_xml_record;
90         } elsif (uc($format) eq 'UNIMARC') {
91                 print $r->as_unimarc
92         } elsif (uc($format) eq 'USMARC') {
93                 print $r->as_usmarc
94         }
95 }
96
97 print "</collection>\n" if ($format eq 'XML');
98
99 $ses->disconnect;
100