From e517e34270a18172879e2e1376e19c755b7eb5b4 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 8 Jan 2019 13:13:46 -0300 Subject: [PATCH] Bug 21846: (follow-up) Add maintenance script This patch adds a maintenance script that generates the missing tags_approval entries based on the tags_all table, and then recalculates the weights for both tags_approval and tags_index tables. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Owen Leonard Signed-off-by: Josef Moravec Signed-off-by: Nick Clemens (cherry picked from commit 293eee9959925397e4fdc84ae0d4baea2cf7d94c) Signed-off-by: Martin Renvoize --- .../data/mysql/atomicupdate/bug_21846.perl | 2 + misc/maintenance/fix_tags_weight.pl | 140 ++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100755 misc/maintenance/fix_tags_weight.pl diff --git a/installer/data/mysql/atomicupdate/bug_21846.perl b/installer/data/mysql/atomicupdate/bug_21846.perl index 6396f19acb..1c41f3f0d4 100644 --- a/installer/data/mysql/atomicupdate/bug_21846.perl +++ b/installer/data/mysql/atomicupdate/bug_21846.perl @@ -23,4 +23,6 @@ if( CheckVersion( $DBversion ) ) { SetVersion( $DBversion ); print "Upgrade to $DBversion done (Bug 21846 - Using emoji as tags has broken weights)\n"; + my $maintenance_script = C4::Context->config("intranetdir") . "misc/maintenance/fix_tags_weight.pl"; + print "WARNING: (Bug 21846) You need to manually run $maintenance_script to fix possible issues with tags.\n"; } diff --git a/misc/maintenance/fix_tags_weight.pl b/misc/maintenance/fix_tags_weight.pl new file mode 100755 index 0000000000..549d170614 --- /dev/null +++ b/misc/maintenance/fix_tags_weight.pl @@ -0,0 +1,140 @@ +#!/usr/bin/perl + +# Copyright 2018 Theke 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 . + +use Modern::Perl; + +use C4::Context; +use C4::Tags; + +use Koha::Database; +use Koha::Tags; +use Koha::Tags::Approvals; +use Koha::Tags::Indexes; + +use Getopt::Long; +use Pod::Usage; + +=head1 NAME + +fix_tags_weight.pl - Fix weight for tags + +=head1 SYNOPSIS + +fix_tags_weight.pl [ --verbose or -v ] [ --help or -h ] + + Options: + --help or -h Brief usage message + --verbose or -v Be verbose + +=head1 DESCRIPTION + +This script fixes the calculated weights for the tags introduced by patrons. + +=over 8 + +=item B<--help> + +Prints a brief usage message and exits. + +=item B<--verbose> + +Prints information about the current weight, and the new one for each term/biblionumber. + +=back + +=cut + +binmode( STDOUT, ":encoding(UTF-8)" ); + +my $help; +my $verbose; + +my $result = GetOptions( + 'help|h' => \$help, + 'verbose|v' => \$verbose +); + +if ( not $result or $help ) { + pod2usage(); + exit 0; +} + +fix_tags_approval($verbose); +fix_tags_index( $verbose ); + +sub fix_tags_approval { + + my ($verbose) = @_; + + print "Fix tags_approval\n=================\n" if $verbose; + + my $dbh = C4::Context->dbh; + # Search the terms in tags_all that don't exist in tags_approval + my $sth = $dbh->prepare( + q{ + SELECT term + FROM ( + SELECT DISTINCT(tags_all.term) AS term, tags_approval.term AS approval FROM tags_all + LEFT JOIN tags_approval + ON (tags_all.term=tags_approval.term)) a + WHERE approval IS NULL; + } + ); + $sth->execute(); + my $approved = C4::Context->preference('TagsModeration') ? 0 : 1; + + # Add missing terms to tags_approval + while ( my $row = $sth->fetchrow_hashref ) { + my $term = $row->{term}; + C4::Tags::add_tag_approval( $term, 0, $approved ); + print "Added => $term\n"; + } + + my $approvals = Koha::Tags::Approvals->search; + # Recalculate weight_total for all tags_approval rows + while ( my $approval = $approvals->next ) { + my $count = Koha::Tags->search( { term => $approval->term } )->count; + print $approval->term . "\t|\t" . $approval->weight_total . "\t=>\t" . $count . "\n" + if $verbose; + $approval->weight_total($count)->store; + } +} + +sub fix_tags_index { + + my ($verbose) = @_; + my $indexes = Koha::Tags::Indexes->search; + + print "Fix tags_index\n==============\n" if $verbose; + + while ( my $index = $indexes->next ) { + my $count + = Koha::Tags->search( { term => $index->term, biblionumber => $index->biblionumber } ) + ->count; + print $index->term . "/" + . $index->biblionumber . "\t|\t" + . $index->weight + . "\t=>\t" + . $count . "\n" + if $verbose; + $index->weight($count)->store; + } +} + +1; -- 2.39.5