Add an explicit output file handle so STDOUT can be reused.
authorRobin H. Johnson <rjohnson@sitka.bclibraries.ca>
Wed, 5 Oct 2011 00:51:04 +0000 (17:51 -0700)
committerRobin H. Johnson <rjohnson@sitka.bclibraries.ca>
Wed, 5 Oct 2011 00:51:07 +0000 (17:51 -0700)
marc_export_custom/marc_export_custom

index f26e54b..6529c16 100755 (executable)
@@ -26,7 +26,7 @@ use Data::Dumper;
 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;
 
@@ -46,7 +46,7 @@ GetOptions(
         '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);
@@ -104,8 +104,17 @@ $type = lc($type);
 $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.  ".
@@ -137,7 +146,7 @@ my $ses = OpenSRF::AppSession->create('open-ils.cstore');
 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
@@ -180,7 +189,7 @@ if ($all_records) {
     }
 }
 
-print "</collection>\n" if ($format eq 'XML');
+print $outfh "</collection>\n" if ($format eq 'XML');
 
 $speed = $count{did} / (time - $start);
 my $time = time - $start;
@@ -219,7 +228,7 @@ sub export_record {
     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;
@@ -235,11 +244,11 @@ sub export_record {
         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}++;
@@ -262,11 +271,11 @@ sub export_record {
                 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;
@@ -405,7 +414,8 @@ sub add_bib_holdings {
 
                             # 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
                     
@@ -413,61 +423,74 @@ sub add_bib_holdings {
                             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
@@ -476,7 +499,8 @@ sub add_bib_holdings {
                                 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
 
@@ -487,13 +511,16 @@ sub add_bib_holdings {
 
                                 # 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; } 
                                 }
                             }
 
@@ -546,3 +573,7 @@ sub add_bib_holdings {
         } # if block
     } # if block
 } # sub
+
+sub skipnote { 
+       printf(STDOUT "Skipped %s due to config: %s\n",$1,$2); 
+}