Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 171 lines (127 sloc) 4.535 kB
6b27c92 @MagnusEnger Introducing serials.pl for generating SQL for subscriptions.
MagnusEnger authored
1 #!/usr/bin/perl -w
2
3 # serials.pl
4 # Copyright 2009 Magnus Enger
5
6 # This is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This file is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this file; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20 use Getopt::Long;
21 use Pod::Usage;
22 use MARC::File::USMARC;
23 use Template;
24 use strict;
25
26 ## Redirect STDERR to STDOUT
27 open STDERR, ">&STDOUT" or die "cannot dup STDERR to STDOUT: $!\n";
28
29 ## get command line options
30 my ($input_file, $system, $limit, $debug) = get_options();
31 print "\nStarting serials.pl\n" if $debug;
32 print "Input File: $input_file\n" if $debug;
33 print "Stopping after $limit records\n" if $debug && $limit;
34
35 if (!-e $input_file) {
36 die "Couldn't find input file $input_file\n";
37 }
38
39 # Configure the Template Toolkit
40 my $config = {
41 INCLUDE_PATH => 'tt2', # or list ref
42 INTERPOLATE => 1, # expand "$var" in plain text
43 POST_CHOMP => 0, # cleanup whitespace
44 };
45 # create Template object
46 my $tt2 = Template->new($config) || die Template->error(), "\n";
47
48 my $batch = MARC::File::USMARC->in($input_file);
49 my $count = 0;
50
51 # Variables to collect output
52 my @serials;
53
54 print "Starting records iteration\n" if $debug;
55 ## iterate through our marc files and do stuff
56 while (my $record = $batch->next()) {
57
58 # only process serials
59 if (lc($record->subfield('245', 'h')) ne 'tidsskrift') {
60 next;
61 }
62
63 print "\n########################################\n" if $debug;
64 print $record->title(), "\n" if $debug;
65 # print $record->as_formatted(), "\n" if $debug;
66
67 my $callnumber = $record->subfield('942', 'k');
68 if ($record->subfield('942', 'm')) {
69 $callnumber .= " " . $record->subfield('942', 'm');
70 }
71
72 my $serial = {
73 'biblio' => $record->subfield('999', 'c'),
74 'callnumber' => $callnumber
75 };
76
77 if (my @field952s = $record->field('952')) {
78 foreach my $field952 (@field952s) {
79 if (my $field952h = $field952->subfield('h')) {
80 # print "$field952h\n";
81 # print ".";
82 }
83 }
84 }
85
86 # Check if --limit is set and we need to stop processing
87 if ($limit > 0 && $count == $limit) {
88 last;
89 }
90 $count++;
91
92 push(@serials, $serial);
93
94 }
95 print "\n";
96 print "End of records - $count records\n" if $debug;
97
98 # make sure there weren't any problems
99 if ( my @warnings = $batch->warnings() ) {
100 die "\nWarnings were detected!\n", @warnings if $debug;
101 }
102
103 # Output
104 my $template = 'serials.tt2';
105 my $vars = {
106 'serials' => \@serials,
107 };
108 $tt2->process($template, $vars) || die $tt2->error();
109
110 ### SUBROUTINES ###
111
112 # Get commandline options
113 sub get_options {
114 my $input_file = '';
115 my $system = '';
116 my $debug = '';
117 my $limit = 0;
118 my $xml = '';
119 my $help = '';
120
121 GetOptions('i|infile=s' => \$input_file,
122 's|system=s' => \$system,
123 'd|debug!' => \$debug,
124 'l|limit=s' => \$limit,
125 'h|?|help' => \$help
126 );
127
128 pod2usage(-exitval => 0) if $help;
129 pod2usage( -msg => "\nMissing argument: -i, --infile required\n", -exitval => 1) if !$input_file;
130 pod2usage( -msg => "\nMissing argument: -s, --system required\n", -exitval => 1) if !$system;
131
132 return ($input_file, $system, $limit, $debug);
133 }
134
135 __END__
136
137 =head1 NAME
138
139 serials.pl - Processes MARC and produce SQL needed for serials.
140
141 =head1 SYNOPSIS
142
143 n4k.pl -i inputfile -s system [-d] [-l] [-h] > outputfile
144
145 =head1 OPTIONS
146
147 =over 8
148
149 =item B<-i, --infile>
150
151 Name of the MARC file to be read.
152
153 =item B<-s, --system>
154
155 Name of the system that produced the records. ("tidemann" supported so far)
156
157 =item B<-d, --debug>
158
159 Print out debug info.
160
161 =item B<-l, --limit>
162
163 Stop processing after n records.
164
165 =item B<-h, -?, --help>
166
167 Prints this help message and exits.
168
169 =back
170
171 =cut
Something went wrong with that request. Please try again.