2 # Copyright (C) 2010 Laurentian University
3 # Author: Dan Scott <dscott@laurentian.ca>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
21 use MARC::File::XML (BinaryEncoding => 'UTF-8');
23 use OpenILS::Utils::Fieldmapper;
24 use OpenSRF::Utils::SettingsClient;
25 use OpenSRF::EX qw/:try/;
27 use Unicode::Normalize;
28 use OpenILS::Application::AppUtils;
30 use Pod::Usage qw/ pod2usage /;
32 my ($start_id, $end_id);
33 my $bootstrap = '/openils/conf/opensrf_core.xml';
37 my $result = GetOptions(
39 'configuration=s' => \$bootstrap,
40 'record=i' => \@records,
42 'start_id=i' => \$start_id,
43 'end_id=i' => \$end_id,
46 if (!$result or $options{help}) {
50 OpenSRF::System->bootstrap_client(config_file => $bootstrap);
51 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
53 # must be loaded and initialized after the IDL is parsed
54 use OpenILS::Utils::CStoreEditor;
55 OpenILS::Utils::CStoreEditor::init();
57 my $editor = OpenILS::Utils::CStoreEditor->new;
60 # get a list of all non-deleted records from Evergreen
61 # open-ils.cstore open-ils.cstore.direct.biblio.record_entry.id_list.atomic {"deleted":"f"}
62 $undeleted = $editor->request(
63 'open-ils.cstore.direct.biblio.record_entry.id_list.atomic',
64 [{deleted => 'f'}, {id => { '>' => 0}}]
66 @records = @$undeleted;
69 if ($start_id and $end_id) {
70 @records = ($start_id .. $end_id);
72 # print Dumper($undeleted, \@records);
74 # Hash of controlled fields & subfields in bibliographic records, and their
75 # corresponding controlling fields & subfields in the authority record
77 # So, if the bib 650$a can be controlled by an auth 150$a, that maps to:
78 # 650 => { a => { 150 => 'a'}}
80 100 => { a => { 100 => 'a' },
97 110 => { a => { 110 => 'a' },
112 111 => { a => { 111 => 'a' },
128 130 => { a => { 130 => 'a' },
143 600 => { a => { 100 => 'a' },
168 610 => { a => { 110 => 'a' },
190 611 => { a => { 111 => 'a' },
210 630 => { a => { 130 => 'a' },
229 648 => { a => { 148 => 'a' },
235 650 => { a => { 150 => 'a' },
242 651 => { a => { 151 => 'a' },
248 655 => { a => { 155 => 'a' },
254 700 => { a => { 100 => 'a' },
271 710 => { a => { 110 => 'a' },
286 711 => { a => { 111 => 'a' },
302 730 => { a => { 130 => 'a' },
317 751 => { a => { 151 => 'a' },
323 830 => { a => { 830 => 'a' },
340 foreach my $rec_id (@records) {
343 my $e = OpenILS::Utils::CStoreEditor->new();
344 # State variable; was the record changed?
348 my $record = $e->retrieve_biblio_record_entry($rec_id);
350 # print Dumper($record);
353 my $marc = MARC::Record->new_from_xml($record->marc());
355 # get the list of controlled fields
356 my @c_fields = keys %controllees;
358 foreach my $c_tag (@c_fields) {
359 my @c_subfields = keys %{$controllees{"$c_tag"}};
360 # print "Field: $field subfields: ";
361 # foreach (@subfields) { print "$_ "; }
363 # Get the MARCXML from the record and check for controlled fields/subfields
364 my @bib_fields = ($marc->field($c_tag));
365 foreach my $bib_field (@bib_fields) {
366 # print $_->as_formatted();
370 foreach my $c_subfield (@c_subfields) {
371 my $sf = $bib_field->subfield($c_subfield);
373 # Give me the first element of the list of authority controlling tags for this subfield
374 # XXX Will we need to support more than one controlling tag per subfield? Probably. That
375 # will suck. Oh well, leave that up to Ole to implement.
376 $match_subfields{$c_subfield} = (keys %{$controllees{$c_tag}{$c_subfield}})[0];
377 $match_tag = $match_subfields{$c_subfield};
378 push @searches, {term => $sf, subfield => $c_subfield};
381 # print Dumper(\%match_subfields);
384 my @tags = ($match_tag);
386 # print "Controlling tag: $c_tag and match tag $match_tag\n";
387 # print Dumper(\@tags, \@searches);
389 # Now we've built up a complete set of matching controlled
390 # subfields for this particular field; let's check to see if
391 # we have a matching authority record
392 my $session = OpenSRF::AppSession->create("open-ils.search");
393 my $validates = $session->request("open-ils.search.authority.validate.tag.id_list",
394 "tags", \@tags, "searches", \@searches
396 $session->disconnect();
398 # print Dumper($validates);
400 # Protect against failed (error condition) search request
402 print STDERR "Search for matching authority failed; record # $rec_id\n";
406 if (scalar(@$validates) == 0) {
410 # Iterate through the returned authority record IDs to delete any
411 # matching $0 subfields already in the bib record
412 foreach my $auth_zero (@$validates) {
413 $bib_field->delete_subfield(code => '0', match => qr/\)$auth_zero$/);
416 # Okay, we have a matching authority control; time to
417 # add the magical subfield 0. Use the first returned auth
419 my $auth_id = @$validates[0];
420 my $auth_rec = $e->retrieve_authority_record_entry($auth_id);
421 my $auth_marc = MARC::Record->new_from_xml($auth_rec->marc());
422 my $cni = $auth_marc->field('003')->data();
424 $bib_field->add_subfields('0' => "($cni)$auth_id");
429 my $editor = OpenILS::Utils::CStoreEditor->new(xact=>1);
430 # print $marc->as_formatted();
431 my $xml = $marc->as_xml_record();
433 $xml =~ s/^<\?xml.+\?\s*>//go;
434 $xml =~ s/>\s+</></go;
435 $xml =~ s/\p{Cc}//go;
436 $xml = OpenILS::Application::AppUtils->entityize($xml);
439 $editor->update_biblio_record_entry($record);
444 print STDERR "\nRecord # $rec_id : $err\n";
445 import MARC::File::XML; # reset SAX parser so that one bad record doesn't kill the entire process
453 authority_control_fields.pl - Controls fields in bibliographic records with authorities in Evergreen
457 C<authority_control_fields.pl> [B<--configuration>=I<opensrf_core.conf>]
458 [[B<--record>=I<record>[ B<--record>=I<record>]]] | [B<--all>] | [B<--start_id>=I<start-ID> B<--end_id>=I<end-ID>]
462 For a given set of records:
466 =item * Iterate through the list of fields that are controlled fields
468 =item * Iterate through the list of subfields that are controlled for
471 =item * Search for a matching authority record for that combination of
476 =item * If we find a match, then add a $0 subfield to that field identifying
477 the controlling authority record
479 =item * If we do not find a match, then insert a row into an "uncontrolled"
480 table identifying the record ID, field, and subfield(s) that were not controlled
484 =item * Iterate through the list of floating subdivisions
488 =item * If we find a match, then add a $0 subfield to that field identifying
489 the controlling authority record
491 =item * If we do not find a match, then insert a row into an "uncontrolled"
492 table identifying the record ID, field, and subfield(s) that were not controlled
496 =item * If we changed the record, update it in the database
504 =item * B<-c> I<config-file>, B<--configuration>=I<config-file>
506 Specifies the OpenSRF configuration file used to connect to the OpenSRF router.
507 Defaults to F</openils/conf/opensrf_core.xml>
509 =item * B<-r> I<record-ID>, B<--record>=I<record-ID>
511 Specifies the bibliographic record ID (found in the C<biblio.record_entry.id>
512 column) of the record to process. This option may be specified more than once
513 to process multiple records in a single run.
515 =item * B<-a>, B<--all>
517 Specifies that all bibliographic records should be processed. For large
518 databases, this may take an extraordinarily long amount of time.
520 =item * B<-s> I<start-ID>, B<--start_id>=I<start-ID>
522 Specifies the starting ID of the range of bibliographic records to process.
523 This option is ignored unless it is accompanied by the B<-e> or B<--end_id>
526 =item * B<-e> I<end-ID>, B<--end_id>=I<end-ID>
528 Specifies the ending ID of the range of bibliographic records to process.
529 This option is ignored unless it is accompanied by the B<-s> or B<--start>
536 authority_control_fields.pl --start_id 1 --end_id 50000
538 Processes the bibliographic records with IDs between 1 and 50,000 using the
539 default OpenSRF configuration file for connection information.
543 Dan Scott <dscott@laurentian.ca>
545 =head1 COPYRIGHT AND LICENSE
547 Copyright 2010-2011 by Dan Scott
549 This program is free software; you can redistribute it and/or
550 modify it under the terms of the GNU General Public License
551 as published by the Free Software Foundation; either version 2
552 of the License, or (at your option) any later version.
554 This program is distributed in the hope that it will be useful,
555 but WITHOUT ANY WARRANTY; without even the implied warranty of
556 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
557 GNU General Public License for more details.
559 You should have received a copy of the GNU General Public License
560 along with this program; if not, write to the Free Software
561 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.