my @formats = qw/USMARC UNIMARC XML BRE ARE/;
my ($config,$format,$encoding,$location,$dollarsign,$idl,$help,$holdings,$timeout,$export_mfhd,$type,$all_records) = ('/openils/conf/opensrf_core.xml','USMARC','MARC8','','$',0,undef,undef,0,undef,'biblio',undef);
-my ($exclusion_ini,$collapse_to_depth);
+my ($exclusion_ini,$collapse_to_depth, $output_file);
my $cfg;
my $force901;
'force901' => \$force901,
'exclusion_ini=s' => \$exclusion_ini,
'collapse_to_depth=i' => \$collapse_to_depth,
-
+ 'output-file=s' => \$output_file,
);
$cfg = new Config::Simple($exclusion_ini) if ($exclusion_ini);
$format = uc($format);
$encoding = uc($encoding);
-binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
-binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
+my $outfh;
+my $real_stdout;
+open($real_stdout, ">&STDOUT") or die "Can't dup STDOUT: $!";
+if($output_file) {
+ open($outfh, '>', $output_file) or die "Can't open file for output $output_file: $!";
+} else {
+ $outfh = $real_stdout;
+}
+
+binmode($outfh, ':raw') if ($encoding ne 'UTF-8');
+binmode($outfh, ':utf8') if ($encoding eq 'UTF-8');
if (!grep { $format eq $_ } @formats) {
die "Please select a supported format. ".
OpenILS::Utils::CStoreEditor::init();
my $editor = OpenILS::Utils::CStoreEditor->new();
-print <<HEADER if ($format eq 'XML');
+print $outfh <<HEADER if ($format eq 'XML');
<?xml version="1.0" encoding="$encoding"?>
<collection xmlns='http://www.loc.gov/MARC21/slim'>
HEADER
}
}
-print "</collection>\n" if ($format eq 'XML');
+print $outfh "</collection>\n" if ($format eq 'XML');
$speed = $count{did} / (time - $start);
my $time = time - $start;
return unless $bib;
if ($format eq 'ARE' or $format eq 'BRE') {
- print OpenSRF::Utils::JSON->perl2JSON($bib);
+ print $outfh OpenSRF::Utils::JSON->perl2JSON($bib);
stats();
$count{did}++;
return;
if ($format eq 'XML') {
my $xml = $r->as_xml_record;
$xml =~ s/^<\?.+?\?>$//mo;
- print $xml;
+ print $outfh $xml;
} elsif ($format eq 'UNIMARC') {
- print $r->as_usmarc;
+ print $outfh $r->as_usmarc;
} elsif ($format eq 'USMARC') {
- print $r->as_usmarc;
+ print $outfh $r->as_usmarc;
}
$count{did}++;
if ($format eq 'XML') {
my $xml = $r->as_xml_record;
$xml =~ s/^<\?.+?\?>$//mo;
- print $xml;
+ print $outfh $xml;
} elsif ($format eq 'UNIMARC') {
- print $r->as_usmarc;
+ print $outfh $r->as_usmarc;
} elsif ($format eq 'USMARC') {
- print $r->as_usmarc;
+ print $outfh $r->as_usmarc;
}
} otherwise {
my $e = shift;
# if we see this setting, just skip that org
- next COPYMAP if( $localcfg->{'ExcludeEntireOrg'} );
+ if( $localcfg->{'ExcludeEntireOrg'} )
+ { skipnote($bib->id,"ExcludeEntireOrg"); next COPYMAP; }
# what follows are exclusion rules
if($localcfg->{'Flags'}){
# this little line is just forcing scalars into an array so we can 'use strict' with Config::Simple
my @flags = ( (ref($localcfg->{'Flags'}) eq "ARRAY") ? @{$localcfg->{'Flags'}} : ($localcfg->{'Flags'}));
- next COPYMAP if( grep { $_ eq 'reference' } @flags && $cp->ref eq 't');
- next COPYMAP if( grep { $_ eq 'unholdable' } @flags && $cp->holdable eq 'f');
- next COPYMAP if( grep { $_ eq 'circulate' } @flags && $cp->circulate eq 'f');
- next COPYMAP if( grep { $_ eq 'hidden' } @flags && $cp->opac_visible eq 'f');
+ if( grep { $_ eq 'reference' } @flags && $cp->ref eq 't')
+ { skipnote($bib->id,"Flags: reference"); next COPYMAP; }
+ elsif( grep { $_ eq 'unholdable' } @flags && $cp->holdable eq 'f')
+ { skipnote($bib->id,"Flags: unholdable"); next COPYMAP; }
+ elsif( grep { $_ eq 'circulate' } @flags && $cp->circulate eq 'f')
+ { skipnote($bib->id,"Flags: circulate"); next COPYMAP; }
+ elsif( grep { $_ eq 'hidden' } @flags && $cp->opac_visible eq 'f')
+ { skipnote($bib->id,"Flags: hidden"); next COPYMAP; }
}
# Excluded Circ Modifiers
if($localcfg->{'CircMods'}){
my $circmod = $cp->circ_modifier || "";
my @circmods = ( (ref($localcfg->{'CircMods'}) eq "ARRAY") ? @{$localcfg->{'CircMods'}} : ($localcfg->{'CircMods'}) );
- next COPYMAP if( grep { $_ eq $circmod } @circmods && @circmods);
+ if( grep { $_ eq $circmod } @circmods && @circmods)
+ { skipnote($bib->id,"CircMods"); next COPYMAP; }
}
# Inverse rule -- only include specified Circ Mods
if($localcfg->{'OnlyIncludeCircMods'}){
my $circmod = $cp->circ_modifier || "";
my @circmods = ( (ref($localcfg->{'CircMods'}) eq "ARRAY") ? @{$localcfg->{'CircMods'}} : ($localcfg->{'CircMods'}) );
- next COPYMAP unless( grep { $_ eq $circmod } @circmods && @circmods);
+ unless( grep { $_ eq $circmod } @circmods && @circmods)
+ { skipnote($bib->id,"OnlyIncludeCircMods"); next COPYMAP; }
}
# Excluded Copy Statuses
if($localcfg->{'Statuses'}){
my @statuses = ( (ref($localcfg->{'Statuses'}) eq "ARRAY") ? @{$localcfg->{'Statuses'}} : ($localcfg->{'Statuses'}) );
- next COPYMAP if( grep { $_ eq $statuses{$cp->status}->name } @statuses && @statuses);
+ if( grep { $_ eq $statuses{$cp->status}->name } @statuses && @statuses)
+ { skipnote($bib->id,"Statuses"); next COPYMAP; }
}
# Excluded Locations
if($localcfg->{'Locations'}){
my @locations = ( (ref($localcfg->{'Locations'}) eq "ARRAY") ? @{$localcfg->{'Locations'}} : ($localcfg->{'Locations'}) );
- next COPYMAP if( grep { $_ eq $shelves{$cp->location}->name } @locations && @locations);
+ if( grep { $_ eq $shelves{$cp->location}->name } @locations && @locations)
+ { skipnote($bib->id,"Locations"); next COPYMAP; }
}
# Inverse rule - Only use the specified locations
if($localcfg->{'OnlyIncludeLocations'}){
my @locations = ( (ref($localcfg->{'OnlyIncludeLocations'}) eq "ARRAY") ? @{$localcfg->{'OnlyIncludeLocations'}} : ($localcfg->{'Locations'}) );
- next COPYMAP unless( grep { $_ eq $shelves{$cp->location}->name } @locations && @locations);
+ unless( grep { $_ eq $shelves{$cp->location}->name } @locations && @locations)
+ { skipnote($bib->id,"OnlyIncludeLocations"); next COPYMAP; }
}
# exclude based on a regex match to location names
if($localcfg->{'LocationRegex'}){
my @locregex = ( (ref($localcfg->{'LocationRegex'}) eq "ARRAY") ? @{$localcfg->{'LocationRegex'}} : ($localcfg->{'LocationRegex'}) );
my $reg = $localcfg->{'LocationRegex'};
- next COPYMAP if( grep { $shelves{$cp->location}->name =~ m/($reg)/ } @locregex && @locregex);
+ if( grep { $shelves{$cp->location}->name =~ m/($reg)/ } @locregex && @locregex)
+ { skipnote($bib->id,"LocationRegex"); next COPYMAP; }
}
# include based on a regex match to location names
if($localcfg->{'OnlyIncludeLocationRegex'}){
my @locregex = ( (ref($localcfg->{'OnlyIncludeLocationRegex'}) eq "ARRAY") ? @{$localcfg->{'OnlyIncludeLocationRegex'}} : ($localcfg->{'OnlyIncludeLocationRegex'}) );
my $reg = $localcfg->{'OnlyIncludeLocationRegex'};
- next COPYMAP unless( grep { $shelves{$cp->location}->name =~ m/($reg)/ } @locregex && @locregex);
+ unless( grep { $shelves{$cp->location}->name =~ m/($reg)/ } @locregex && @locregex)
+ { skipnote($bib->id,"OnlyIncludeLocationRegex"); next COPYMAP; }
}
# Exclude based on a callno regex
if($localcfg->{'CallNoRegex'}){
my @callnoregex = ( (ref($localcfg->{'CallNoRegex'}) eq "ARRAY") ? @{$localcfg->{'CallNoRegex'}} : ($localcfg->{'CallNoRegex'}) );
my $reg = $localcfg->{'CallNoRegex'};
- next COPYMAP if( grep { $cn->label =~ m/($reg)/ } @callnoregex && @callnoregex);
+ if( grep { $cn->label =~ m/($reg)/ } @callnoregex && @callnoregex)
+ { skipnote($bib->id,"CallNoRegex"); next COPYMAP; }
}
# Include based on a callno regex
if($localcfg->{'OnlyIncludeCallNoRegex'}){
my @callnoregex = ( (ref($localcfg->{'OnlyIncludeCallNoRegex'}) eq "ARRAY") ? @{$localcfg->{'OnlyIncludeCallNoRegex'}} : ($localcfg->{'OnlyIncludeCallNoRegex'}) );
my $reg = $localcfg->{'OnlyIncludeCallNoRegex'};
- next COPYMAP unless( grep { $cn->label =~ m/($reg)/ } @callnoregex && @callnoregex);
+ unless( grep { $cn->label =~ m/($reg)/ } @callnoregex && @callnoregex)
+ { skipnote($bib->id,"OnlyIncludeCallNoRegex"); next COPYMAP; }
}
# Trim call number to a float and exclude based on Dewey Range
my $lt = $localcfg->{'DeweyLT'};
# FIXME if either config has an array just ditch for now
- next COPYMAP if (ref($gt) eq "ARRAY" or ref($lt) eq "ARRAY");
+ if (ref($gt) eq "ARRAY" or ref($lt) eq "ARRAY")
+ { skipnote($bib->id,""); next COPYMAP; }
$gt =~ s/[^0-9\.]//g if $gt; #trim off anything not deweyish
$lt =~ s/[^0-9\.]//g if $lt; #trim off anything not deweyish
# we have a range, exclude what's inbetween
if($lt && $gt){
- next COPYMAP if $callno > $gt and $callno < $lt;
+ if($callno > $gt and $callno < $lt)
+ { skipnote($bib->id,"Dewey LTGT"); next COPYMAP; }
# we only have a top threshold, exclude everything below it
} elsif ($lt){
- next COPYMAP if $callno < $lt;
+ if($callno < $lt)
+ { skipnote($bib->id,"Dewey LT"); next COPYMAP; }
# we only have a bottom threshold, exclude everything above it
} elsif ($gt){
- next COPYMAP if $callno > $gt;
+ if($callno > $gt)
+ { skipnote($bib->id,"Dewey GT"); next COPYMAP; }
}
}
} # if block
} # if block
} # sub
+
+sub skipnote {
+ printf(STDOUT "Skipped %s due to config: %s\n",$1,$2);
+}