|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2018 Theke Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use C4::Context; |
| 23 |
use C4::Tags; |
| 24 |
|
| 25 |
use Koha::Database; |
| 26 |
use Koha::Tags; |
| 27 |
use Koha::Tags::Approvals; |
| 28 |
use Koha::Tags::Indexes; |
| 29 |
|
| 30 |
use Getopt::Long; |
| 31 |
use Pod::Usage; |
| 32 |
|
| 33 |
=head1 NAME |
| 34 |
|
| 35 |
fix_tags_weight.pl - Fix weight for tags |
| 36 |
|
| 37 |
=head1 SYNOPSIS |
| 38 |
|
| 39 |
fix_tags_weight.pl [ --verbose or -v ] [ --help or -h ] |
| 40 |
|
| 41 |
Options: |
| 42 |
--help or -h Brief usage message |
| 43 |
--verbose or -v Be verbose |
| 44 |
|
| 45 |
=head1 DESCRIPTION |
| 46 |
|
| 47 |
This script fixes the calculated weights for the tags introduced by patrons. |
| 48 |
|
| 49 |
=over 8 |
| 50 |
|
| 51 |
=item B<--help> |
| 52 |
|
| 53 |
Prints a brief usage message and exits. |
| 54 |
|
| 55 |
=item B<--verbose> |
| 56 |
|
| 57 |
Prints information about the current weight, and the new one for each term/biblionumber. |
| 58 |
|
| 59 |
=back |
| 60 |
|
| 61 |
=cut |
| 62 |
|
| 63 |
binmode( STDOUT, ":encoding(UTF-8)" ); |
| 64 |
|
| 65 |
my $help; |
| 66 |
my $verbose; |
| 67 |
|
| 68 |
my $result = GetOptions( |
| 69 |
'help|h' => \$help, |
| 70 |
'verbose|v' => \$verbose |
| 71 |
); |
| 72 |
|
| 73 |
if ( not $result or $help ) { |
| 74 |
pod2usage(); |
| 75 |
exit 0; |
| 76 |
} |
| 77 |
|
| 78 |
fix_tags_approval($verbose); |
| 79 |
fix_tags_index( $verbose ); |
| 80 |
|
| 81 |
sub fix_tags_approval { |
| 82 |
|
| 83 |
my ($verbose) = @_; |
| 84 |
|
| 85 |
print "Fix tags_approval\n=================\n" if $verbose; |
| 86 |
|
| 87 |
my $dbh = C4::Context->dbh; |
| 88 |
# Search the terms in tags_all that don't exist in tags_approval |
| 89 |
my $sth = $dbh->prepare( |
| 90 |
q{ |
| 91 |
SELECT term |
| 92 |
FROM ( |
| 93 |
SELECT DISTINCT(tags_all.term) AS term, tags_approval.term AS approval FROM tags_all |
| 94 |
LEFT JOIN tags_approval |
| 95 |
ON (tags_all.term=tags_approval.term)) a |
| 96 |
WHERE approval IS NULL; |
| 97 |
} |
| 98 |
); |
| 99 |
$sth->execute(); |
| 100 |
my $approved = C4::Context->preference('TagsModeration') ? 0 : 1; |
| 101 |
|
| 102 |
# Add missing terms to tags_approval |
| 103 |
while ( my $row = $sth->fetchrow_hashref ) { |
| 104 |
my $term = $row->{term}; |
| 105 |
C4::Tags::add_tag_approval( $term, 0, $approved ); |
| 106 |
print "Added => $term\n"; |
| 107 |
} |
| 108 |
|
| 109 |
my $approvals = Koha::Tags::Approvals->search; |
| 110 |
# Recalculate weight_total for all tags_approval rows |
| 111 |
while ( my $approval = $approvals->next ) { |
| 112 |
my $count = Koha::Tags->search( { term => $approval->term } )->count; |
| 113 |
print $approval->term . "\t|\t" . $approval->weight_total . "\t=>\t" . $count . "\n" |
| 114 |
if $verbose; |
| 115 |
$approval->weight_total($count)->store; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
sub fix_tags_index { |
| 120 |
|
| 121 |
my ($verbose) = @_; |
| 122 |
my $indexes = Koha::Tags::Indexes->search; |
| 123 |
|
| 124 |
print "Fix tags_index\n==============\n" if $verbose; |
| 125 |
|
| 126 |
while ( my $index = $indexes->next ) { |
| 127 |
my $count |
| 128 |
= Koha::Tags->search( { term => $index->term, biblionumber => $index->biblionumber } ) |
| 129 |
->count; |
| 130 |
print $index->term . "/" |
| 131 |
. $index->biblionumber . "\t|\t" |
| 132 |
. $index->weight |
| 133 |
. "\t=>\t" |
| 134 |
. $count . "\n" |
| 135 |
if $verbose; |
| 136 |
$index->weight($count)->store; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
1; |