From d5787d96afb6ed2db02d7a211f2c08975d3da54f Mon Sep 17 00:00:00 2001 From: phette23 Date: Thu, 12 Dec 2024 08:57:49 -0800 Subject: [PATCH] Bug 29238: (follow up) tidy 3 scripts --- misc/cronjobs/merge_authorities.pl | 43 ++++++----- misc/link_bibs_to_authorities.pl | 74 +++++++++---------- .../remove_unused_authorities.pl | 55 +++++++------- 3 files changed, 89 insertions(+), 83 deletions(-) diff --git a/misc/cronjobs/merge_authorities.pl b/misc/cronjobs/merge_authorities.pl index f8f8f457db..0ecb7a1623 100755 --- a/misc/cronjobs/merge_authorities.pl +++ b/misc/cronjobs/merge_authorities.pl @@ -2,26 +2,26 @@ use Modern::Perl; use Getopt::Long qw( GetOptions ); -use Pod::Usage qw( pod2usage ); -use Time::HiRes qw( gettimeofday ); +use Pod::Usage qw( pod2usage ); +use Time::HiRes qw( gettimeofday ); use Koha::Script -cron; use C4::AuthoritiesMarc qw( GetAuthority merge ); -use C4::Log qw( cronlogaction ); +use C4::Log qw( cronlogaction ); use Koha::Authority::MergeRequests; use constant RESET_HOURS => 24; use constant REMOVE_DAYS => 30; -my ( $params ); -my $command_line_options = join(" ", @ARGV); +my ($params); +my $command_line_options = join( " ", @ARGV ); GetOptions( 'h' => \$params->{help}, 'v' => \$params->{verbose}, 'b' => \$params->{batch}, ); -$|=1; # flushes output +$| = 1; # flushes output if ( $params->{batch} ) { cronlogaction( { info => $command_line_options } ); handle_batch($params); @@ -30,34 +30,39 @@ if ( $params->{batch} ) { } sub handle_batch { - my $params = shift; + my $params = shift; my $verbose = $params->{verbose}; my $starttime = gettimeofday; print "Started merging\n" if $verbose; - Koha::Authority::MergeRequests->cron_cleanup({ reset_hours => RESET_HOURS, remove_days => REMOVE_DAYS }); + Koha::Authority::MergeRequests->cron_cleanup( { reset_hours => RESET_HOURS, remove_days => REMOVE_DAYS } ); my $rs = Koha::Authority::MergeRequests->search( - { done => 0 }, - { order_by => { -asc => 'id' }}, # IMPORTANT + { done => 0 }, + { order_by => { -asc => 'id' } }, # IMPORTANT ); + # For best results, postponed merges should be applied in right order. # Similarly, we do not only select the last one for a specific id. - while( my $req = $rs->next ) { + while ( my $req = $rs->next ) { $req->done(2)->store; print "Merging auth " . $req->authid . " to " . ( $req->authid_new // 'NULL' ) . ".\n" if $verbose; - my $newmarc = $req->authid_new + my $newmarc = + $req->authid_new ? GetAuthority( $req->authid_new ) : undef; + # Following merge call handles both modifications and deletes - merge({ - mergefrom => $req->authid, - MARCfrom => scalar $req->oldmarc, - mergeto => $req->authid_new, - MARCto => $newmarc, - override_limit => 1, - }); + merge( + { + mergefrom => $req->authid, + MARCfrom => scalar $req->oldmarc, + mergeto => $req->authid_new, + MARCto => $newmarc, + override_limit => 1, + } + ); $req->done(1)->store; } my $timeneeded = gettimeofday - $starttime; diff --git a/misc/link_bibs_to_authorities.pl b/misc/link_bibs_to_authorities.pl index 650de1d8e3..7137a6e54a 100755 --- a/misc/link_bibs_to_authorities.pl +++ b/misc/link_bibs_to_authorities.pl @@ -26,10 +26,10 @@ use C4::Biblio qw( ); use C4::Log qw( cronlogaction ); use Koha::Biblios; -use Getopt::Long qw( GetOptions ); -use Pod::Usage qw( pod2usage ); -use Time::HiRes qw( time ); -use POSIX qw( ceil strftime ); +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 ); use Koha::Database; @@ -54,8 +54,8 @@ my $commit = 100; my $tagtolink; my $allowrelink = C4::Context->preference("LinkerRelink") // ''; -my $command_line_options = join(" ", @ARGV); -my $result = GetOptions( +my $command_line_options = join( " ", @ARGV ); +my $result = GetOptions( 'v|verbose' => \$verbose, 't|test' => \$test_only, 'l|link-report' => \$link_report, @@ -74,8 +74,7 @@ if ( not $result or $want_help ) { cronlogaction( { info => $command_line_options } ); -my $linker_module = - "C4::Linker::" . ( C4::Context->preference("LinkerModule") || 'Default' ); +my $linker_module = "C4::Linker::" . ( C4::Context->preference("LinkerModule") || 'Default' ); unless ( can_load( modules => { $linker_module => undef } ) ) { $linker_module = 'C4::Linker::Default'; unless ( can_load( modules => { $linker_module => undef } ) ) { @@ -96,27 +95,26 @@ my $num_bad_bibs = 0; my %unlinked_headings; my %linked_headings; my %fuzzy_headings; -my $dbh = C4::Context->dbh; +my $dbh = C4::Context->dbh; my @updated_biblios = (); -my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); +my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); my $schema = Koha::Database->schema; $schema->txn_begin; -process_bibs( $linker, $bib_limit, $auth_limit, $commit, { tagtolink => $tagtolink, allowrelink => $allowrelink }); +process_bibs( $linker, $bib_limit, $auth_limit, $commit, { tagtolink => $tagtolink, allowrelink => $allowrelink } ); exit 0; sub process_bibs { my ( $linker, $bib_limit, $auth_limit, $commit, $args ) = @_; - my $tagtolink = $args->{tagtolink}; + my $tagtolink = $args->{tagtolink}; my $allowrelink = $args->{allowrelink}; - my $bib_where = ''; - my $starttime = time(); + my $bib_where = ''; + my $starttime = time(); if ($bib_limit) { $bib_where = "WHERE $bib_limit"; } - my $sql = - "SELECT biblionumber FROM biblio $bib_where ORDER BY biblionumber ASC"; + my $sql = "SELECT biblionumber FROM biblio $bib_where ORDER BY biblionumber ASC"; my $sth = $dbh->prepare($sql); $sth->execute(); my $linker_args = { tagtolink => $tagtolink, allowrelink => $allowrelink }; @@ -141,10 +139,10 @@ sub process_bibs { for ( values %unlinked_headings ) { $headings_unlinked += $_; } for ( values %fuzzy_headings ) { $headings_fuzzy += $_; } - my $endtime = time(); - my $totaltime = ceil (($endtime - $starttime) * 1000); - $starttime = strftime('%D %T', localtime($starttime)); - $endtime = strftime('%D %T', localtime($endtime)); + my $endtime = time(); + my $totaltime = ceil( ( $endtime - $starttime ) * 1000 ); + $starttime = strftime( '%D %T', localtime($starttime) ); + $endtime = strftime( '%D %T', localtime($endtime) ); my $summary = <<_SUMMARY_; @@ -173,9 +171,7 @@ Linked headings (from most frequent to least): _LINKED_HEADER_ - @keys = sort { - $linked_headings{$b} <=> $linked_headings{$a} or "\L$a" cmp "\L$b" - } keys %linked_headings; + @keys = sort { $linked_headings{$b} <=> $linked_headings{$a} or "\L$a" cmp "\L$b" } keys %linked_headings; foreach my $key (@keys) { print "$key:\t" . $linked_headings{$key} . " occurrences\n"; } @@ -187,10 +183,7 @@ Unlinked headings (from most frequent to least): _UNLINKED_HEADER_ - @keys = sort { - $unlinked_headings{$b} <=> $unlinked_headings{$a} - or "\L$a" cmp "\L$b" - } keys %unlinked_headings; + @keys = sort { $unlinked_headings{$b} <=> $unlinked_headings{$a} or "\L$a" cmp "\L$b" } keys %unlinked_headings; foreach my $key (@keys) { print "$key:\t" . $unlinked_headings{$key} . " occurrences\n"; } @@ -202,9 +195,7 @@ Fuzzily-matched headings (from most frequent to least): _FUZZY_HEADER_ - @keys = sort { - $fuzzy_headings{$b} <=> $fuzzy_headings{$a} or "\L$a" cmp "\L$b" - } keys %fuzzy_headings; + @keys = sort { $fuzzy_headings{$b} <=> $fuzzy_headings{$a} or "\L$a" cmp "\L$b" } keys %fuzzy_headings; foreach my $key (@keys) { print "$key:\t" . $fuzzy_headings{$key} . " occurrences\n"; } @@ -215,12 +206,13 @@ _FUZZY_HEADER_ sub process_bib { my $linker = shift; my $biblionumber = shift; - my $args = shift; + my $args = shift; my $tagtolink = $args->{tagtolink}; - my $allowrelink = $args->{allowrelink}; - my $biblio = Koha::Biblios->find($biblionumber); + my $allowrelink = $args->{allowrelink}; + my $biblio = Koha::Biblios->find($biblionumber); my $record; eval { $record = $biblio->metadata->record; }; + unless ( defined $record ) { warn "Could not retrieve bib $biblionumber from the database - record is corrupt."; $num_bad_bibs++; @@ -262,12 +254,18 @@ sub process_bib { ); } if ( not $test_only ) { - ModBiblio( $record, $biblionumber, $frameworkcode, { - disable_autolink => 1, - skip_holds_queue => 1, - skip_record_index =>1 - }); + ModBiblio( + $record, + $biblionumber, + $frameworkcode, + { + disable_autolink => 1, + skip_holds_queue => 1, + skip_record_index => 1 + } + ); push @updated_biblios, $biblionumber; + #Last param is to note ModBiblio was called from linking script and bib should not be linked again $num_bibs_modified++; } diff --git a/misc/migration_tools/remove_unused_authorities.pl b/misc/migration_tools/remove_unused_authorities.pl index 4283cea2f2..19cbc5c4a4 100755 --- a/misc/migration_tools/remove_unused_authorities.pl +++ b/misc/migration_tools/remove_unused_authorities.pl @@ -25,14 +25,14 @@ use Modern::Perl; use Koha::Script; use C4::Context; use C4::AuthoritiesMarc qw( DelAuthority ); -use C4::Log qw( cronlogaction ); -use Getopt::Long qw( GetOptions ); +use C4::Log qw( cronlogaction ); +use Getopt::Long qw( GetOptions ); use Koha::SearchEngine::Search; my @authtypes; -my ($confirm, $test, $want_help); -my $command_line_options = join(" ", @ARGV); +my ( $confirm, $test, $want_help ); +my $command_line_options = join( " ", @ARGV ); GetOptions( 'aut|authtypecode:s' => \@authtypes, 'c|confirm' => \$confirm, @@ -40,7 +40,7 @@ GetOptions( 'h|help' => \$want_help, ); -if ( $want_help || !($test || $confirm) ) { +if ( $want_help || !( $test || $confirm ) ) { print_usage(); exit 0; } @@ -51,56 +51,59 @@ if ($test) { print "*** Testing only, authorities will not be deleted. ***\n"; } if (@authtypes) { - print "Restricted to authority type(s) : ".join(',', @authtypes).".\n"; + print "Restricted to authority type(s) : " . join( ',', @authtypes ) . ".\n"; } my $searcher = Koha::SearchEngine::Search->new( { index => 'biblios' } ); my $checksearch; if ( C4::Context->preference("SearchEngine") eq 'Zebra' ) { + # Check server state - my $errZebraConnection = C4::Context->Zconn("biblioserver",0)->errcode(); + my $errZebraConnection = C4::Context->Zconn( "biblioserver", 0 )->errcode(); if ( $errZebraConnection == 10000 ) { die "Zebra server seems not to be available. This script needs Zebra runs."; - } elsif ( $errZebraConnection ) { + } elsif ($errZebraConnection) { die "Error from Zebra: $errZebraConnection"; } $checksearch = q{an,alwaysmatches=''}; -} -else { +} else { $checksearch = q{an:*}; } + # Check search on authority number as at least one result -my ($err,$res,$nb) = $searcher->simple_search_compat($checksearch,0,10); -unless ($nb > 0) { +my ( $err, $res, $nb ) = $searcher->simple_search_compat( $checksearch, 0, 10 ); +unless ( $nb > 0 ) { die "Searching authority number in biblio records seems not to be available : $checksearch"; } -my $dbh=C4::Context->dbh; +my $dbh = C4::Context->dbh; + # prepare the request to retrieve all authorities of the requested types my $rqsql = q{ SELECT authid,authtypecode FROM auth_header }; -$rqsql .= q{ WHERE authtypecode IN (}.join(',',map{ '?' }@authtypes).')' if @authtypes; +$rqsql .= q{ WHERE authtypecode IN (} . join( ',', map { '?' } @authtypes ) . ')' if @authtypes; my $rqselect = $dbh->prepare($rqsql); -$|=1; +$| = 1; $rqselect->execute(@authtypes); -my $counter=0; -my $totdeleted=0; -my $totundeleted=0; -while (my $data=$rqselect->fetchrow_hashref){ +my $counter = 0; +my $totdeleted = 0; +my $totundeleted = 0; +while ( my $data = $rqselect->fetchrow_hashref ) { $counter++; - print 'authid='.$data->{'authid'}; - print ' type='.$data->{'authtypecode'}; - my $bibliosearch = 'an:'.$data->{'authid'}; + print 'authid=' . $data->{'authid'}; + print ' type=' . $data->{'authtypecode'}; + my $bibliosearch = 'an:' . $data->{'authid'}; + # search for biblios mapped - my ($err,$res,$used) = $searcher->simple_search_compat($bibliosearch,0,10); - if (defined $err) { + my ( $err, $res, $used ) = $searcher->simple_search_compat( $bibliosearch, 0, 10 ); + if ( defined $err ) { print "\n"; warn "Error: $err on search for biblios $bibliosearch\n"; next; } - unless ($used > 0){ + unless ( $used > 0 ) { unless ($test) { - DelAuthority({ authid => $data->{'authid'}, skip_merge => 1 }); + DelAuthority( { authid => $data->{'authid'}, skip_merge => 1 } ); print " : deleted"; } else { print " : can be deleted"; -- 2.47.1