All pastes #2065719 Raw Edit

bib creation

public text v1 · immutable
#2065719 ·published 2011-05-20 13:59 UTC
rendered paste body
# Keep a count of the loaded records.
my $count = 0;
# We are now ready to process the downloaded MARC file.
# my $marc_file = MARC::File::USMARC->in($fname); # For reals.
my $marc_file = MARC::File::USMARC->in("/home/jason/safari/FULL-2011-05-18.mrc"); # For development.
while (my $record = $marc_file->next()) {
    $count++;
# Get the record status to determine what to do with it.
    my $status = substr($record->leader(), 5, 1);

# Do our configured transformations:
    unless ($status eq 'd') {
        my $transforms = $config->find("/config/marc/*");
        foreach my $trans ($transforms->get_nodelist()) {
            next unless ($trans->isa('XML::XPath::Node::Element'));
            # Currently, we only support a replace specification. I
            # plan to add a delete and an add specification in the
            # future.
            if ($trans->getLocalName() eq 'replace') {
                my $tag = $trans->getAttribute('tag');
                next unless($tag);
                my $ind1 = $trans->getAttribute('ind1');
                my $ind2 = $trans->getAttribute('ind2');
                my $subs = $config->find('subfield', $trans);
                if ($subs->size()) {
                    foreach my $sub ($subs->get_nodelist) {
                        my $code = $sub->getAttribute('code');
                        next unless ($code);
                        my $match = $config->findvalue('match', $sub)->value();
                        my $with = $config->findvalue('with', $sub)->value();
                        foreach my $field ($record->field($tag)) {
                            next if ($ind1 && $field->indicator(1) ne $ind1);
                            next if ($ind2 && $field->indicator(2) ne $ind2);
                            if ($match) {
                                my $value = $field->subfield($code);
                                if ($value) {
                                    $value =~ s/$match/$with/;
                                    $field->update($code => $value);
                                }
                            }
                            else {
                                $field->update($code => $with) if ($field->subfield($code));
                            }
                        }
                    }
                }
                else {
                    # This isn't implemented, yet, but should be.
                }
            }
        }
    }

# If updating, overlay matching records by deleting them first.
    if ($download_type eq 'update') {
        # This still needs to be implemented. It needs to match
        # records based on some criteria and then delete, replace, or
        # overlay existing records.
    }

# Insert our new record.
    unless ($status eq 'd') {
        print("$count ");
# Convert the record to marcxml.
        (my $xml = $record->as_xml_record()) =~ s/\n//sog;
        $xml =~ s/^<\?xml.+\?\s*>//go;
        $xml =~ s/>\s+</></go;
        $xml =~ s/\p{Cc}//go;
        $xml = OpenILS::Application::AppUtils->entityize($xml);
        $xml =~ s/[\x00-\x1f]//go;

        my $r = OpenILS::Application::AppUtils->simplereq('open-ils.cat', 'open-ils.cat.biblio.record.xml.create',
                                                          $authtoken, $xml, $source->id);
        if (ref($r) eq 'HASH') {
            print($r->{textcode} . "\n");
        }
        elsif (ref($r) eq 'Fieldmapper::biblio::record_entry') {
            print($r->id . ' ' . $r->source . "\n");
        }
        else {
            print("$r\n");
        }
    }
}