# find Koha's Perl modules
# test carefully before changing this
- use FindBin;
+ use FindBin ();
eval { require "$FindBin::Bin/kohalib.pl" };
}
+use Koha::Script;
use C4::Context;
-use C4::Biblio;
-use Getopt::Long;
-use Pod::Usage;
-use Data::Dumper;
-use Time::HiRes qw/time/;
-use POSIX qw/strftime ceil/;
+use C4::Biblio qw(
+ GetFrameworkCode
+ GetMarcBiblio
+ LinkBibHeadingsToAuthorities
+ ModBiblio
+);
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
+use Time::HiRes qw( time );
+use POSIX qw( ceil strftime );
+use Module::Load::Conditional qw( can_load );
sub usage {
pod2usage( -verbose => 2 );
my $auth_limit;
my $bib_limit;
my $commit = 100;
+my $tagtolink;
+my $allowrelink = C4::Context->preference("CatalogModuleRelink") || '';
my $result = GetOptions(
'v|verbose' => \$verbose,
'a|auth-limit=s' => \$auth_limit,
'b|bib-limit=s' => \$bib_limit,
'c|commit=i' => \$commit,
+ 'g|tagtolink=i' => \$tagtolink,
'h|help' => \$want_help
);
-binmode( STDOUT, ":utf8" );
+binmode( STDOUT, ":encoding(UTF-8)" );
if ( not $result or $want_help ) {
usage();
my $linker_module =
"C4::Linker::" . ( C4::Context->preference("LinkerModule") || 'Default' );
-eval { eval "require $linker_module"; };
-if ($@) {
+unless ( can_load( modules => { $linker_module => undef } ) ) {
$linker_module = 'C4::Linker::Default';
- eval "require $linker_module";
-}
-if ($@) {
- die "Unable to load linker module. Aborting.";
+ unless ( can_load( modules => { $linker_module => undef } ) ) {
+ die "Unable to load linker module. Aborting.";
+ }
}
my $linker = $linker_module->new(
my %fuzzy_headings;
my $dbh = C4::Context->dbh;
$dbh->{AutoCommit} = 0;
-process_bibs( $linker, $bib_limit, $auth_limit, $commit );
+process_bibs( $linker, $bib_limit, $auth_limit, $commit, { tagtolink => $tagtolink, allowrelink => $allowrelink });
$dbh->commit();
exit 0;
sub process_bibs {
- my ( $linker, $bib_limit, $auth_limit, $commit ) = @_;
+ my ( $linker, $bib_limit, $auth_limit, $commit, $args ) = @_;
+ my $tagtolink = $args->{tagtolink};
+ my $allowrelink = $args->{allowrelink};
my $bib_where = '';
my $starttime = time();
if ($bib_limit) {
"SELECT biblionumber FROM biblio $bib_where ORDER BY biblionumber ASC";
my $sth = $dbh->prepare($sql);
$sth->execute();
+ my $linker_args = { tagtolink => $tagtolink, allowrelink => $allowrelink };
while ( my ($biblionumber) = $sth->fetchrow_array() ) {
$num_bibs_processed++;
- process_bib( $linker, $biblionumber );
+ process_bib( $linker, $biblionumber, $linker_args );
if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
print_progress_and_commit($num_bibs_processed);
sub process_bib {
my $linker = shift;
my $biblionumber = shift;
-
- my $bib = GetMarcBiblio($biblionumber);
+ my $args = shift;
+ my $tagtolink = $args->{tagtolink};
+ my $allowrelink = $args->{allowrelink};
+ my $bib = GetMarcBiblio({ biblionumber => $biblionumber });
unless ( defined $bib ) {
print
"\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
return;
}
+ my $frameworkcode = GetFrameworkCode($biblionumber);
+
my ( $headings_changed, $results ) =
- LinkBibHeadingsToAuthorities( $linker, $bib,
- GetFrameworkCode($biblionumber) );
+ LinkBibHeadingsToAuthorities( $linker, $bib, $frameworkcode, $allowrelink, $tagtolink );
foreach my $key ( keys %{ $results->{'unlinked'} } ) {
$unlinked_headings{$key} += $results->{'unlinked'}->{$key};
}
if ($headings_changed) {
if ($verbose) {
my $title = substr( $bib->title, 0, 20 );
- print
-"Bib $biblionumber ($title): $headings_changed headings changed\n";
+ printf(
+ "Bib %12d (%-20s): %3d headings changed\n",
+ $biblionumber,
+ $title,
+ $headings_changed
+ );
}
if ( not $test_only ) {
- ModBiblio( $bib, $biblionumber, GetFrameworkCode($biblionumber) );
+ ModBiblio( $bib, $biblionumber, $frameworkcode, 1 );
+ #Last param is to note ModBiblio was called from linking script and bib should not be linked again
$num_bibs_modified++;
}
}
link_bibs_to_authorities.pl --commit=1000
link_bibs_to_authorities.pl --auth-limit=STRING
link_bibs_to_authorities.pl --bib-limit=STRING
+ link_bibs_to_authorities.pl -g=700
=head1 DESCRIPTION
Commit the results to the database after every N records are processed.
+=item B<-g=N>
+
+Only process those headings found in MARC field N.
+
=item B<--test>
Only test the authority linking and report the results; do not change the bib