This changes the behavior of --items
authorJason Etheridge <jason@esilibrary.com>
Fri, 19 Dec 2014 20:11:14 +0000 (15:11 -0500)
committerJason Etheridge <jason@esilibrary.com>
Fri, 19 Dec 2014 21:07:17 +0000 (16:07 -0500)
And adds an option for --exclude-itemless-bibs.  Previously, bib records without
items would not be exported at all with the --items argument.  Now they will (*),
but the --exclude-itemless-bibs argument will restore the original behavior.

* - This change was accomplished by performing a LEFT JOIN against the call number
and copy tables, but if the --library option is used, then the original vanilla
JOIN is still used, so whether an itemless bib will export or not with that
argument will depend on whether there is a non-deleted call number on the bib.

Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Open-ILS/src/support-scripts/marc_export.in

index 348c4b3..cc12de9 100755 (executable)
@@ -82,6 +82,7 @@ sub new {
     GetOptions(\%opts,
                'help',
                'items',
+               'exclude-itemless-bibs',
                'mfhd',
                'all',
                'replace_001',
@@ -128,15 +129,16 @@ Usage: $0 [options]
  --since            Export records modified since a certain date and time.
 
  Additional options for type = 'BIBLIO':
- --items or -i      Include items (holdings) in the output
- --money            Currency symbol to use in item price field [\$]
- --mfhd             Export serial MFHD records for associated bib records
-                    Not compatible with --format=BRE
- --location or -l   MARC Location Code for holdings from
-                    http://www.loc.gov/marc/organizations/orgshome.html
- --library          Export the bibliographic records that have attached
-                    holdings for the listed library or libraries as
-                    identified by shortname
+ --items or -i            Include items (holdings) in the output
+ --exclude-itemless-bibs  Don't output bibs without holdings (requires --items)
+ --money                  Currency symbol to use in item price field [\$]
+ --mfhd                   Export serial MFHD records for associated bib records
+                          Not compatible with --format=BRE
+ --location or -l         MARC Location Code for holdings from
+                          http://www.loc.gov/marc/organizations/orgshome.html
+ --library                Export the bibliographic records that have attached
+                          holdings for the listed library or libraries as
+                          identified by shortname
 
 Examples:
 
@@ -195,6 +197,9 @@ HELP
     if ($opts{format} eq 'BRE' && $opts{items}) {
         die "Format BRE is not compatible with exporting holdings."
     }
+    if ($opts{'exclude-itemless-bibs'} && ! $opts{items}) {
+        die "If excluding itemless bibs, must export holdings.";
+    }
 
     if ($opts{mfhd}) {
         if ($opts{type} ne 'biblio') {
@@ -375,10 +380,10 @@ ACN_JOIN
 
     if ($Marque::config->option_value('items')) {
         unless ($acn_joined) {
-            $from .= "\njoin $acnTable on $acnTable.record = $breTable.id";
+            $from .= "\nleft join $acnTable on $acnTable.record = $breTable.id";
             $from .= "\nand $acnTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
         }
-        $from .= "\njoin $acpTable on $acpTable.call_number = $acnTable.id";
+        $from .= "\nleft join $acpTable on $acpTable.call_number = $acnTable.id";
         $from .= "\nand $acpTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
     }
 
@@ -458,8 +463,10 @@ sub next {
                 }
                 if ($Marque::config->option_value('items')) {
                     my @acps = $self->acps_for_bre($r);
+                    my $item_count = 0;
                     foreach my $acp (@acps) {
                         next unless ($acp);
+                        $item_count++;
                         my $location = $Marque::config->option_value('location');
                         my $price = ($acp->price() ? $Marque::config->option_value('money').$acp->price() : '');
                         $marc->insert_grouped_field(
@@ -480,6 +487,10 @@ sub next {
                                 (!$U->is_true($acp->opac_visible()) ? (x => 'hidden') : ())
                             ));
                     }
+                    if ($item_count == 0 && $Marque::config->option_value('exclude-itemless-bibs')) {
+                        import MARC::File::XML; # Reset SAX Parser.
+                        return $self->next(); # skip this bib
+                    }
                 }
                 if ($Marque::config->option_value('mfhd')) {
                     $self->{mfhds} = [$self->sres_for_bre($r)];