removing miker-filter series; superceded by filter_record_ids, which has far less...
[migration-tools.git] / filter_record.ids
index 99c6171..de88ae9 100644 (file)
@@ -3,53 +3,46 @@ use warnings;
 use strict;
 
 use Getopt::Long;
-use Time::HiRes qw/time/;
-use MARC::Record;
-use MARC::File::XML ( BinaryEncoding => 'utf-8' );
-
-# THIS FILE EXTRACTS NONMATCHING RECORDS
+#use MARC::Record;
+#use MARC::File::XML ( BinaryEncoding => 'utf-8' );
 
 # configuration hashref
-my $conf  = ();
-#initialize($conf);
-
-my $idfile = shift;
-my $marcfile = shift;
-my $import = shift;
-my $shelve = shift;
+my $conf  = {};
+initialize($conf);
 
 my %id;
 
-open F, "<$idfile";
+open F, "<", $conf->{idfile};
 while (<F>) {
        chomp;
        $id{$_} = 1;
 }
-
 close F;
 
 my $M; my $I; my $S;
-open $M, '<:utf8', $marcfile;
-open $I, '>:utf8', $import;
-open $S, '>:utf8', $shelve;
-
-my $starttime = time;
-my $count = 0;
-my $icount = 0;
-my $scount = 0;
+open $M, '<:utf8', $conf->{marcfile}
+  or die "Can't open marcfile '",$conf->{marcfile},"'\n";
+open $I, '>:utf8', $conf->{'output-import'}
+  or die "Can't open import file '",$conf->{'output-import'},"'\n";
+open $S, '>:utf8', $conf->{'output-shelved'}
+  or die "Can't open shelf file '",$conf->{'output-shelve'},"'\n";
+
 while (<$M>) {
-    /tag="903" ind1=" " ind2=" ">.*?<subfield code="a">(\d+)</;
-    if ( $id{$1} ) {
-        print $S $_;
-        $scount++;
+    my $tag = $conf->{tag};
+    my $sub = $conf->{subfield};
+
+    /tag="$tag" ind1=" " ind2=" ">.*?<subfield code="$sub">(\d+)</;
+    if ($conf->{mode} eq "exclude") {
+        print $S $_ if ($id{$1});
+        print $I $_ unless ($id{$1});;
     } else {
-        print $I $_;
-        $icount++;
+        print $S $_ unless ($id{$1});
+        print $I $_ if ($id{$1});;
     }
-    $count++;
+    $conf->{count}++;
 
-    unless ($count && $count % 100) {
-        print STDERR "\r$count\t(shelved: $scount, import: $icount)\t". $count / (time - $starttime);
+    unless ($conf->{count} % 100) {
+        print STDERR "\rProcessed: ",$conf->{count};
     }
 }
 
@@ -68,35 +61,31 @@ sub initialize {
     binmode(STDIN, ':utf8');
 
     my $rc = GetOptions( $c,
-                         'incoming',
-                         'incumbent',
-                         'incoming-tag|incot=i',
-                         'incoming-subfield|incos=s',
-                         'incumbent-tag|incut=i',
-                         'incumbent-subfield|incus=s',
-                         'output|o=s',
+                         'mode=s',
+                         'include',
+                         'tag|t=i',
+                         'subfield|s=s',
+                         'idfile|i=s',
+                         'marcfile|m=s',
+                         'output-import|oi=s',
+                         'output-shelved|os=s',
                          'help|h',
                        );
     show_help() unless $rc;
     show_help() if ($c->{help});
 
-    $c->{'incoming-tag'}         = 903;
-    $c->{'incoming-subfield'}    = 'a';
-    $c->{'incoming-matchfile'}   = '';
-    $c->{'incoming-nomatchfile'} = '';
-    $c->{'incumbent-tag'}         = 901;
-    $c->{'incumbent-subfield'}    = 'a';
-    $c->{'incumbent-matchfile'}   = '';
-    $c->{'incumbent-nomatchfile'} = '';
     my @keys = keys %{$c};
-    show_help() unless (@ARGV and @keys);
-    for my $key ('renumber-from', 'tag', 'subfield', 'output')
+    for my $key ('mode', 'idfile', 'marcfile', 'tag', 'subfield',
+                 'output-import', 'output-shelved')
       { push @missing, $key unless $c->{$key} }
     if (@missing) {
         print "Required option: ", join(', ', @missing), " missing!\n";
         show_help();
     }
-
+    unless ($c->{mode} eq "include" or $c->{mode} eq "exclude") {
+        print "Unknown run mode '", $c->{mode}, "'\n";
+        show_help();
+    }
 }
 
 
@@ -108,14 +97,29 @@ Display usage message when things go wrong
 
 sub show_help {
 print <<HELP;
-Usage is: $0 [REQUIRED ARGS]
-Req'd Arguments
-  --renumber-from=N        -rf First id# of new sequence
-  --tag=N                  -t  Which tag to use
-  --subfield=X             -s  Which subfield to use
-  --output=<file>          -o  Output filename
-
-Any number of input files may be specified; one output file will result.
+
+Usage is: $0 [ARGS]
+
+  --mode=MODE           Runmode to use (exclude, include; usually exclude)
+  --idfile         -i   File of record ids to use as source for matchpoints
+  --marcfile       -m   MARCXML source file
+  --tag            -t   MARC tag to use as matchpoint (903 when matching
+                        incoming records, 901 when matching incumbents)
+  --subfield       -s   Subfield of tag to use ('a' for incoming, 'c'
+                        for incumbent)
+  --output-import  -oi  Output MARCXML file for records to be imported
+  --output-shelved -os  Output MARCXML file for records to be ignored
+
+If '--mode=exclude' is specified, the record ids in the file specified
+by -idfile will be used as EXCLUSION data.
+
+That is, the given record ids will be treated as records which match
+incumbent records, are being compressed into existing data, and so
+WILL NOT be imported. The --output-import file will contain records
+whose ids DO NOT occur in --idfile; --output-shelve will contain the
+records which DO occur.
+
+If '--mode=include' is specified, the reverse occurs.
 HELP
 exit 1;
 }