]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/marc_export
some reporting and error protection
[working/Evergreen.git] / Open-ILS / src / support-scripts / marc_export
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use bytes;
5
6 use OpenSRF::System;
7 use OpenSRF::EX qw/:try/;
8 use OpenSRF::AppSession;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Application::AppUtils;
11 use OpenILS::Utils::Fieldmapper;
12
13 use MARC::Record;
14 use MARC::File::XML;
15 use UNIVERSAL::require;
16
17 use Getopt::Long;
18
19
20 my @formats = qw/USMARC UNIMARC XML/;
21
22 my ($config,$format,$encoding,$location,$dollarsign,$idl,$help,$holdings) = ('/openils/conf/bootstrap.conf','USMARC','MARC8','','$');
23
24 GetOptions(
25         'help'      => \$help,
26         'items'      => \$holdings,
27         'location=s'      => \$location,
28         'money=s'      => \$dollarsign,
29         'config=s'      => \$config,
30         'format=s'      => \$format,
31         'xml-idl=s'      => \$idl,
32         'encoding=s'      => \$encoding,
33 );
34
35 if ($help) {
36         print <<"       HELP";
37 Usage: $0 [options]
38  --help or -h           This screen.
39  --config or -c         Configuration file [/openils/conf/bootstrap.conf]
40  --format or -f         Output format (USMARC, UNIMARC, XML) [USMARC]
41  --encoding or -e       Output Encoding (UTF-8, ISO-8859-?, MARC8) [MARC8]
42  --items or -i          Include items (holdings) in the output
43  --xml-idl or -x        Location of the IDL XML
44  --location or -l       MARC Location Code for holdings from
45                         http://www.loc.gov/marc/organizations/orgshome.html
46
47 Example:
48
49   cat list_of_ids | $0 > output_file
50
51         HELP
52         exit;
53 }
54
55 $format = uc($format);
56 $encoding = uc($encoding);
57
58 binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
59 binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
60
61 if (!grep { $format eq $_ } @formats) {
62         die     "Please select a supported format.  ".
63                 "Right now that means one of [".
64                 join('|',@formats). "]\n";
65 }
66
67 if ($format ne 'XML') {
68         my $type = 'MARC::File::' . $format;
69         $type->require;
70 }
71
72 OpenSRF::System->bootstrap_client( config_file => $config );
73
74 if (!$idl) {
75         $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
76 }
77
78 Fieldmapper->import(IDL => $idl);
79
80 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
81
82 print <<HEADER if ($format eq 'XML');
83 <?xml version="1.0" encoding="$encoding"?>
84 <collection xmlns='http://www.loc.gov/MARC21/slim'>
85 HEADER
86
87 my %orgs;
88 my %shelves;
89 if ($holdings) {
90         my $o = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search.atomic', { id => { '!=' => undef } } )->gather(1);
91         %orgs = map { ( $_->id => $_ ) } @$o;
92
93         my $s = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search.atomic', { id => { '!=' => undef } } )->gather(1);
94         %shelves = map { ( $_->id => $_ ) } @$s;
95 }
96
97 my $start = time;
98 my $count = 1;
99 while ( my $i = <> ) {
100         my $bib = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i )->gather(1);
101
102         next unless $bib;
103
104         try {
105                 my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
106                 $r->delete_field( $_ ) for ($r->field(901));
107
108                 $r->append_fields(
109                         MARC::Field->new(
110                                 901, '', '', 
111                                 a => $bib->tcn_value,
112                                 b => $bib->tcn_source
113                         )
114                 );
115
116                 if ($holdings) {
117                         my $cn_list = $ses->request(
118                                 'open-ils.cstore.direct.asset.call_number.search.atomic',
119                                 { record => $i, deleted => 'f' }
120                         )->gather(1);
121
122
123                         my $cp_list = $ses->request(
124                                 'open-ils.cstore.direct.asset.copy.search.atomic',
125                                 { call_number => [ map { $_->id } @$cn_list ], deleted => 'f' }
126                         )->gather(1);
127
128                         my %cn_map;
129                         push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
130
131                         for my $cn ( @$cn_list ) {
132                                 my $cn_map_list = $cn_map{$cn->id};
133                                 for my $cp ( @$cn_map_list ) {
134                                         $r->append_fields(
135                                                 MARC::Field->new(
136                                                         852, '4', '', 
137                                                         a => $location,
138                                                         b => $orgs{$cn->owning_lib}->shortname,
139                                                         b => $orgs{$cp->circ_lib}->shortname,
140                                                         c => $shelves{$cp->location}->name,
141                                                         j => $cn->label,
142                                                         ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
143                                                         p => $cp->barcode,
144                                                         ($cp->price ? ( y => $dollarsign.$cp->price ) : ()),
145                                                         ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
146                                                         ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
147                                                         ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
148                                                         ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
149                                                         ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
150                                                 )
151                                         );
152                                 }
153                         }
154                 }
155
156                 if (uc($format) eq 'XML') {
157                         print $r->as_xml_record;
158                 } elsif (uc($format) eq 'UNIMARC') {
159                         print $r->as_unimarc
160                 } elsif (uc($format) eq 'USMARC') {
161                         print $r->as_usmarc
162                 }
163         } catch {
164                 my $e = shift;
165                 warn "\n$e\n";
166         };
167
168         if (! ($count % 50 )) {
169                 my $speed = sprintf( '%0.4f', $count / time - $start );
170                 print STDERR "\r $speed / second       \r";
171         }
172 }
173
174 print "</collection>\n" if ($format eq 'XML');
175