Teach marc_export about deleted and inactive records collab/miker/marc_export_enhancement
authorMike Rylander <mrylander@gmail.com>
Fri, 7 Sep 2012 14:55:49 +0000 (10:55 -0400)
committerMike Rylander <mrylander@gmail.com>
Fri, 7 Sep 2012 14:55:49 +0000 (10:55 -0400)
Per Galen Charlton's wishlist bug, give the marc_export command line tool the
ability to distinguish between deleted, non-deleted, active and inactive
records, and adjust the output based on user needs.

This also addresses an oversight in the existing code that causes the default
behaviour (output deleted records along with non-deleted) to miss deleted
records if they have ID values larger than the highest-ID'd non-delted record.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/support-scripts/marc_export.in

index 588f8dd..710e294 100755 (executable)
@@ -21,7 +21,7 @@ use Time::HiRes qw/time/;
 use Getopt::Long;
 
 
-my @formats = qw/USMARC UNIMARC XML BRE ARE/;
+my @formats = qw/USMARC UNIMARC XML BRE ARE SRE/;
 
 my $config = '@sysconfdir@/opensrf_core.xml';
 my $format = 'USMARC';
@@ -35,6 +35,10 @@ my $timeout = 0;
 my $export_mfhd = undef;
 my $type = 'biblio';
 my $all_records = undef;
+my $exclude_deleted = undef;
+my $exclude_inactive = undef;
+my $only_deleted = undef;
+my $only_inactive = undef;
 my $replace_001 = undef;
 my @library = ();
 
@@ -43,6 +47,10 @@ GetOptions(
         'items'      => \$holdings,
         'mfhd'       => \$export_mfhd,
         'all'        => \$all_records,
+        'exclude-deleted'  => \$exclude_deleted,
+        'exclude-inactive' => \$exclude_inactive,
+        'deleted'    => \$only_deleted,
+        'inactive'   => \$only_inactive,
         'replace_001'=> \$replace_001,
         'location=s' => \$location,
         'money=s'    => \$dollarsign,
@@ -73,19 +81,25 @@ in their ID sequences.
 Usage: $0 [options]
  --help or -h       This screen.
  --config or -c     Configuration file [@sysconfdir@/opensrf_core.xml]
- --format or -f     Output format (USMARC, UNIMARC, XML, BRE, ARE) [USMARC]
+ --format or -f     Output format (USMARC, UNIMARC, XML, BRE, ARE, SRE) [USMARC]
  --encoding or -e   Output encoding (UTF-8, ISO-8859-?, MARC8) [MARC8]
  --xml-idl or -x    Location of the IDL XML
  --timeout          Timeout for exporting a single record; increase if you
                     are using --holdings and are exporting records that
                     have a lot of items attached to them.
- --type or -t       Record type (BIBLIO, AUTHORITY) [BIBLIO]
+ --type or -t       Record type (BIBLIO, AUTHORITY, SERIAL) [BIBLIO]
  --all or -a        Export all records; ignores input list
  --library          Export the bibliographic records that have attached
                     holdings for the listed library or libraries as
                     identified by shortname
  --replace_001      Replace the 001 field value with the record ID
 
+ Modifiers to --all or input list:
+ --exclude-deleted  Include only non-deleted records in output
+ --exclude-inactive Include only active records in output
+ --deleted          Include only deleted records in output
+ --inactive         Include only active records in output
+
  Additional options for type = 'BIBLIO':
  --items or -i      Include items (holdings) in the output
  --money            Currency symbol to use in item price field [\$]
@@ -100,9 +114,9 @@ To export a set of USMARC records in a file named "output_file" based on the
 IDs contained in a file named "list_of_ids":
   cat list_of_ids | $0 > output_file
 
-To export a set of MARC21XML authority records in a file named "output.xml"
-for all authority records in the database:
-  $0 --format XML --type AUTHORITY --all > output.xml
+To export a set of active, non-deleted MARC21XML authority records in a file
+named "output.xml" for all authority records in the database:
+  $0 --format XML --type AUTHORITY --all --exclude-inactive --exclude-deleted > output.xml
 
 To export a set of USMARC bibliographic records encoded in UTF-8 in a file
 named "sys1_bibs.mrc" based on records which have attached callnumbers for the
@@ -119,6 +133,16 @@ if ($all_records && @library) {
         'records with a request for records by library');
 }
 
+if ($exclude_deleted && $only_deleted) {
+    die('Incompatible arguments: you cannot combine a request for deleted ' .
+        'records with a request excluding deleted records');
+}
+
+if ($exclude_inactive && $only_inactive) {
+    die('Incompatible arguments: you cannot combine a request for inactive ' .
+        'records with a request excluding inactive records');
+}
+
 $type = lc($type);
 $format = uc($format);
 $encoding = uc($encoding);
@@ -179,13 +203,15 @@ if ($all_records) {
     my $top_record = 0;
     if ($type eq 'biblio') {
         $top_record = $editor->search_biblio_record_entry([
-            {deleted => 'f'},
-            {order_by => { 'bre' => 'id DESC' }, limit => 1}
+            {}, {order_by => { 'bre' => 'id DESC' }, limit => 1}
         ])->[0]->id;
     } elsif ($type eq 'authority') {
         $top_record = $editor->search_authority_record_entry([
-            {deleted => 'f'},
-            {order_by => { 'are' => 'id DESC' }, limit => 1}
+            {}, {order_by => { 'are' => 'id DESC' }, limit => 1}
+        ])->[0]->id;
+    } elsif ($type eq 'serial') {
+        $top_record = $editor->search_serial_record_entry([
+            {}, {order_by => { 'sre' => 'id DESC' }, limit => 1}
         ])->[0]->id;
     }
     for (my $i = 0; $i++ < $top_record;) {
@@ -259,10 +285,17 @@ sub export_record {
     $bib = $s->content;
     $r->finish;
 
+    return if (
+        ( $exclude_deleted && $bib->deleted =~ /^t/i ) ||
+        ( $exclude_inactive && $bib->active =~ /^f/i ) ||
+        ( $only_deleted && $bib->deleted =~ /^f/i ) ||
+        ( $only_inactive && $bib->active =~ /^t/i )
+    );
+
     $count{bib}++;
     return unless $bib;
 
-    if ($format eq 'ARE' or $format eq 'BRE') {
+    if ($format eq 'ARE' or $format eq 'BRE' or $format eq 'SRE') {
         print OpenSRF::Utils::JSON->perl2JSON($bib);
         stats();
         $count{did}++;