Koha/misc/maintenance/update_localuse_from_statistics.pl
Nick Clemens 1ba5e028cb
Bug 37790: Add skip indexing and holds queue options and verbosity to update localuse script
This patch skips record indexing and real time holds queue updates when updating the localuse field
from statistics. A note is added to the script that the user should reindex if the localuse field is mapped.

Additionally a verbose option is added to the script, and doubled use of GetOptions is removed.

Lastly, a check is added to confirm the items value is being changed before the value is stored.

To test:
 1 - Enable the real time holds queue
 2 - Enable Elasticsearch
 3 - perl misc/maintenance/update_localuse_from_statistics.pl --confirm
 4 - Note all items are touched and reported
 5 - Check the background jobs table - there are many jobs generated
 6 - Apply patch
 7 - perl misc/maintenance/update_localuse_from_statistics.pl --confirm
 8 - Note 0 items are reported updated, no new background jobs
 9 - perl misc/maintenance/update_localuse_from_statistics.pl --confirm --verbose
10 - No items reported
11 - Update some items in the DB
     UPDATE items SET localuse = 99 WHERE itemnumber LIKE '%9';
12 - perl misc/maintenance/update_localuse_from_statistics.pl --confirm
13 - Only the number of items changed above reported
14 - UPDATE items SET localuse = 99 WHERE itemnumber LIKE '%9';
15 - perl misc/maintenance/update_localuse_from_statistics.pl --confirm --verbose
16 - Each item changed reported, and the amounts, and the total items updated.

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-11-18 17:04:32 +01:00

109 lines
2.7 KiB
Perl
Executable file

#!/usr/bin/perl
#
# Copyright (C) 2023 ByWater Solutions
#
# 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 strict;
use warnings;
use Koha::Script;
use C4::Context;
use Koha::Items;
use Koha::Statistics;
use Getopt::Long qw( GetOptions );
use Pod::Usage qw( pod2usage );
my ($params);
GetOptions(
'c|confirm' => \$params->{confirm},
'v|verbose' => \$params->{verbose},
'help' => \$params->{help},
);
if ( $params->{help} ) {
pod2usage( -verbose => 2 );
exit;
}
my $updated;
if ( $params->{confirm} ) {
$updated = update_localuse( { verbose => $params->{verbose} } );
}
print "Updated $updated items.\n";
sub update_localuse {
my $params = shift;
my $verbose = $params->{verbose};
my $dbh = C4::Context->dbh;
my $items = Koha::Items->search();
my $updated = 0;
# Loop through each item and update it with statistics info
while ( my $item = $items->next ) {
my $itemnumber = $item->itemnumber;
my $localuse_count = Koha::Statistics->search( { itemnumber => $itemnumber, type => 'localuse' } )->count;
my $item_localuse = $item->localuse // 0;
next if $item_localuse == $localuse_count;
$item->localuse($localuse_count);
$item->store( { skip_record_index => 1, skip_holds_queue => 1 } );
$updated++;
print "Updated item $itemnumber with localuse statistics info. Was $item_localuse, now $localuse_count\n"
if $verbose;
}
return $updated;
}
=head1 NAME
update_local_use_from_statistics.pl
=head1 SYNOPSIS
update_localuse_from_statistics.pl
update_localuse_from_statistics.pl --help
update_localuse_from_statistics.pl --confirm
update_localuse_from_statistics.pl --verbose
=head1 DESCRIPTION
This script updates the items.localuse column with data from the statistics table to make sure the two tables are congruent.
NOTE: If you have mapped the items.localuse field in your search engine you must reindex your records after running this script.
=head1 OPTIONS
=over
=item B<-h--help>
Prints this help message
=item B<-c|--confirm>
Confirm to run the script.
=item B<-v|--verbose>
Add verbosity to output.
=back
=cut