From fada0642ad5f9eb92e6e9a3d55d50eb513bed4f1 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Thu, 23 Jun 2022 13:52:30 -1000 Subject: [PATCH] Bug 31035: Script to update OPACSuppression field depending on hidden items --- misc/maintenance/update_opac_suppression.pl | 215 ++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100755 misc/maintenance/update_opac_suppression.pl diff --git a/misc/maintenance/update_opac_suppression.pl b/misc/maintenance/update_opac_suppression.pl new file mode 100755 index 0000000000..cdb1f70ada --- /dev/null +++ b/misc/maintenance/update_opac_suppression.pl @@ -0,0 +1,215 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +BEGIN { + use FindBin (); + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +use Koha::Script; + +use Getopt::Long qw( GetOptions ); + +use List::MoreUtils qw( any ); +use MARC::Field; +use Pod::Usage qw( pod2usage ); + +use C4::Biblio qw( ModBiblio ); +use C4::Context; +use Koha::Biblios; + +my ( $confirm, $verbose, $os_marc_input, $hide_no_items, $sqlwhere, $want_help ); +GetOptions( + 'confirm' => \$confirm, + 'verbose' => \$verbose, + 'marc:s' => \$os_marc_input, + 'hide-no-items' => \$hide_no_items, + 'where:s' => \$sqlwhere, + 'help|h' => \$want_help, +) || podusage(1); + +pod2usage(1) if $want_help; +pod2usage("Parameter marc is mandatory") unless $os_marc_input; + +sub _read_marc_code { + my $input = shift; + my ( $field, $subfield ); + if ( $input =~ /^(\d{3})$/ ) { + $field = $1; + } elsif ( $input =~ /^(\d{3})(\w)$/ ) { + $field = $1; + $subfield = $2; + } + return ( $field, $subfield ); +} + +sub _get_new_value { + my ( $biblio, $rules ) = @_; + my $val; + my @items = $biblio->items->as_list; + if (@items) { + if ( any { !$_->hidden_in_opac( { rules => $rules } ) } @items ) { + $val = 0; # Do not hide because there is at least one visible item + } else { + $val = 1; # Hide because all items are hidden + } + } else { + $val = $hide_no_items ? 1 : 0; # No items + } + return $val; +} + +sub _get_old_value { + my ( $marc_record, $f, $sf ) = @_; + my $val; + my $field = $marc_record->field($f); + if ($field) { + $val = + defined $sf + ? $field->subfield($sf) + : $field->data(); + } + $val = $val ? 1 : 0; + return $val; +} + +sub _set_new_value { + my ( $marc_record, $f, $sf, $val ) = @_; + my $field = $marc_record->field($f); + if ($field) { + if ( defined $sf ) { + $field->update( $sf, $val ); + } else { + $field->update($val); + } + } else { + if ( defined $sf ) { + $marc_record->add_fields( MARC::Field->new( $f, ' ', ' ', $sf => $val ) ); + } else { + $marc_record->add_fields( MARC::Field->new( $f, $val ) ); + } + } +} + +say "Updating OPAC suppression MARC data in $os_marc_input" if $verbose; +say "Confirm flag not passed, running in dry-run mode..." unless $confirm; + +my ( $os_field_input, $os_subfield_input ) = _read_marc_code($os_marc_input); + +my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {}; + +my $dbh = C4::Context->dbh; +my $querysth = q{ + SELECT biblio.biblionumber FROM biblio + JOIN biblio_metadata ON (biblio_metadata.biblionumber = biblio.biblionumber) + JOIN biblioitems ON (biblioitems.biblionumber = biblio.biblionumber) +}; +$querysth .= qq{ WHERE $sqlwhere } if ($sqlwhere); +$querysth .= q{ ORDER BY biblio.biblionumber }; +my $query = $dbh->prepare($querysth); +$query->execute; +my $count_total = 0; +my $count_modified = 0; + +while ( my ($biblionumber) = $query->fetchrow ) { + $count_total++; + my $biblio = Koha::Biblios->find($biblionumber); + unless ($biblio) { + warn "Error in biblio $biblionumber : not found"; + next; + } + my $marc_record = $biblio->metadata->record; + unless ($marc_record) { + warn "Error in biblio $biblionumber : can't parse record"; + next; + } + eval { + my $os_new_value = _get_new_value( $biblio, $rules ); + my $os_old_value = _get_old_value( $marc_record, $os_field_input, $os_subfield_input ); + if ( $os_new_value != $os_old_value ) { + if ($confirm) { + _set_new_value( $marc_record, $os_field_input, $os_subfield_input, $os_new_value ); + C4::Biblio::ModBiblio( $marc_record, $biblionumber, $biblio->frameworkcode, { disable_autolink => 1 } ); + $count_modified++; + if ($verbose) { + say "Bibliographic record $biblionumber changed to " . ( $os_new_value ? "hidden" : "visible" ) . " in OPAC"; + } + } elsif ($verbose) { + say "Bibliographic record $biblionumber would have been changed to " . ( $os_new_value ? "hidden" : "visible" ) . " in OPAC"; + } + } + }; + if ($@) { + warn "Problem with biblio $biblionumber : $@"; + } +} + +say "$count_total records processed, $count_modified modified" if $verbose; + +=head1 NAME + +update_opac_suppression.pl + +=head1 SYNOPSIS + + perl update_opac_suppression.pl --help + + perl update_opac_suppression.pl --confirm --marc 942n + + perl update_opac_suppression.pl --verbose --confirm --marc 942n --where "TO_DAYS(NOW()) - TO_DAYS(DATE(biblio.datecreated)) <= 1" + +=head1 DESCRIPTION + +Update MARC field for OPACSuppression depending on hidden items. + +=head1 OPTIONS + +=over 8 + +=item B<--help> + +Prints this help + +=item B<--confirm> + +Confirmation flag, the script will be running in dry-run mode if set not. + +=item B<--verbose> + +Verbose mode. + +=item B<--marc> + +MARC field (optionaly subfield) of OPACSuppression search field. +Usually 942n. + +=item B<--hide-no-items> + +Hide records with no items. By default there are not hidden. + +=item B<--where> + +Limits the search on bibliographic records with a user-specified WHERE clause. + +The columns from biblio, biblioitems and biblio_metadata tables are available. + +=back + +=cut + -- 2.39.0