Bugzilla – Attachment 136472 Details for
Bug 31035
Script to update OPACSuppression field depending on hidden items
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 31035: Script to update OPACSuppression field depending on hidden items
Bug-31035-Script-to-update-OPACSuppression-field-d.patch (text/plain), 6.78 KB, created by
Fridolin Somers
on 2022-06-24 07:00:18 UTC
(
hide
)
Description:
Bug 31035: Script to update OPACSuppression field depending on hidden items
Filename:
MIME Type:
Creator:
Fridolin Somers
Created:
2022-06-24 07:00:18 UTC
Size:
6.78 KB
patch
obsolete
>From 986934ceef2a8ac811a200f3a0d5bd20055d3b66 Mon Sep 17 00:00:00 2001 >From: Fridolin Somers <fridolin.somers@biblibre.com> >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 | 217 ++++++++++++++++++++ > 1 file changed, 217 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..184ad786a8 >--- /dev/null >+++ b/misc/maintenance/update_opac_suppression.pl >@@ -0,0 +1,217 @@ >+#!/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 <http://www.gnu.org/licenses>. >+ >+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( GetMarcBiblio ModBiblio ); >+use C4::Context; >+use Koha::Biblios; >+ >+my ( $confirm, $verbose, $os_marc_input, $sqlwhere, $want_help ); >+GetOptions( >+ 'confirm' => \$confirm, >+ 'verbose' => \$verbose, >+ 'marc:s' => \$os_marc_input, >+ '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 ( $biblionumber, $rules ) = @_; >+ my $val; >+ my $bibio_object = Koha::Biblios->find($biblionumber); >+ my @items = $bibio_object->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 = 0; # Do not hide if 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 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; >+my $count_total = 0; >+my $count_modified = 0; >+ >+while ( my ( $biblionumber, $frameworkcode ) = $query->fetchrow ) { >+ $count_total++; >+ my $marc_record = GetMarcBiblio( { biblionumber => $biblionumber } ); >+ if ($marc_record) { >+ eval { >+ my $os_new_value = _get_new_value( $biblionumber, $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, $frameworkcode ); >+ $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 : $@"; >+ } >+ } >+ else { >+ say "Error in biblio $biblionumber : can't parse record"; >+ } >+} >+ >+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(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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 31035
:
136466
|
136472
|
144793
|
154852