]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/marc_export
explicit IDL location support
[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 my $sc = OpenSRF::Utils::SettingsClient->new;
74 $idl ||= $sc->config_value("IDL");
75
76 Fieldmapper->import(IDL => $idl);
77
78 my $ses = OpenSRF::AppSession->create('open-ils.cstore');
79
80 print <<HEADER if ($format eq 'XML');
81 <?xml version="1.0" encoding="$encoding"?>
82 <collection xmlns='http://www.loc.gov/MARC21/slim'>
83 HEADER
84
85 my %orgs;
86 if ($holdings) {
87         my $o = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search.atomic', { id => { '!=' => undef } } )->gather(1);
88         %orgs = map { ( $_->id => $_ ) } @$o;
89 }
90
91 my %shelves;
92 if ($holdings) {
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 while ( my $i = <> ) {
98         my $bib = $ses->request( 'open-ils.cstore.direct.biblio.record_entry.retrieve', $i )->gather(1);
99
100         next unless $bib;
101
102         my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
103         $r->delete_field( $_ ) for ($r->field(901));
104
105         $r->append_fields(
106                 MARC::Field->new(
107                         901, '', '', 
108                         a => $bib->tcn_value,
109                         b => $bib->tcn_source
110                 )
111         );
112
113         if ($holdings) {
114                 my $cn_list = $ses->request(
115                         'open-ils.cstore.direct.asset.call_number.search.atomic',
116                         { record => $i, deleted => 'f' }
117                 )->gather(1);
118
119
120                 my $cp_list = $ses->request(
121                         'open-ils.cstore.direct.asset.copy.search.atomic',
122                         { call_number => [ map { $_->id } @$cn_list ], deleted => 'f' }
123                 )->gather(1);
124
125                 my %cn_map;
126                 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
127
128                 for my $cn ( @$cn_list ) {
129                         my $cn_map_list = $cn_map{$cn->id};
130                         for my $cp ( @$cn_map_list ) {
131                                 $r->append_fields(
132                                         MARC::Field->new(
133                                                 852, '4', '', 
134                                                 a => $location,
135                                                 b => $orgs{$cn->owning_lib}->shortname,
136                                                 b => $orgs{$cp->circ_lib}->shortname,
137                                                 c => $shelves{$cp->location}->name,
138                                                 j => $cn->label,
139                                                 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
140                                                 p => $cp->barcode,
141                                                 ($cp->price ? ( y => $dollarsign.$cp->price ) : ()),
142                                                 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
143                                                 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
144                                                 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
145                                                 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
146                                                 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
147                                         )
148                                 );
149                         }
150                 }
151         }
152
153         if (uc($format) eq 'XML') {
154                 print $r->as_xml_record;
155         } elsif (uc($format) eq 'UNIMARC') {
156                 print $r->as_unimarc
157         } elsif (uc($format) eq 'USMARC') {
158                 print $r->as_usmarc
159         }
160 }
161
162 print "</collection>\n" if ($format eq 'XML');
163