From 6e8e347c7c2f69b4ed7f89e514075c56e6a49b5c 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 | 181 ++++++++++++++++++++ 1 file changed, 181 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..df78a0f82e --- /dev/null +++ b/misc/maintenance/update_opac_suppression.pl @@ -0,0 +1,181 @@ +#!/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 Pod::Usage qw( pod2usage ); + +use C4::Biblio qw( GetMarcBiblio ModBiblio ); +use C4::Context; +use Koha::Bilblios; + +my ( $sqlwhere, $confirm, $verbose, $want_help ); +GetOptions( + 'confirm' => \$confirm, + 'verbose' => \$verbose, + 'marc:s' => \$os_marc_input, + 'where:s' => \$sqlwhere, + 'help|h' => \$want_help, +) || podusage(1); + +pod2usage(1) if $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 ); +} + +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, $os_subfield ) = _read_marc_code($os_marc_input); + +my $os_rules = C4::Context->yaml_preference('OpacHiddenItems') // {}; + +my $dbh = C4::Context->dbh; +my $querysth = q{ + SELECT items.biblionumber,biblio.frameworkcode FROM items + LEFT JOIN biblioitems ON (items.biblioitemnumber = biblioitems.biblioitemnumber) + LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber) +}; +$querysth .= qq{ WHERE $sqlwhere } if ($sqlwhere); +$querysth .= q{ GROUP BY items.biblionumber ORDER BY items.biblionumber }; +my $query = $dbh->prepare($querysth); +$query->execute; +while ( my ( $biblionumber, $frameworkcode ) = $query->fetchrow ) { + $count++; + say "$count processed" if ( $verbose && $count % 100 ); + my $marc_record = GetMarcBiblio( { biblionumber => $biblionumber } ); + if ($marc_record) { + eval { + + my $bibio_object = Koha::Bilios->find($biblionumber); + my @items = $bibio_object->items->as_list; + my $os_new_value; + + if (@items) { + if ( any { !$_->hidden_in_opac( { rules => $rules } ) } @items ) { + $os_new_value = 0; # Do not hide because there is at least one visible item + } + else { + $os_new_value = 1; # Hide because all items are hidden + } + } + else { + $os_new_value = 0; # Do not hide if no items + } + + my $os_old_value; + my $os_marc_field = $record->field($os_marc_field); + if ($os_marc_field) { + $os_old_value = + defined $os_subfield + ? $os_marc_field->subfield($os_subfield) + : $os_marc_field->data(); + } + $os_old_value = $os_old_value ? 1 : 0; + + if ( $confirm && $os_new_value != $os_old_value ) { + my $modified = C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode ); + if ( $verbose and $modified ) { + 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 : $@"; + } + } + else { + print "Error in biblio $biblionumber : can't parse record"; + } +} + +print "\n\n$count records processed.\n" 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(items.timestamp)) <= 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<--where> + +Limits the search on bibliographic records with a user-specified WHERE clause. + +The columns from items, biblioitems and biblio tables are available. + +=back + +=cut + -- 2.35.3