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