3 # Copyright 2018 Theke Solutions
5 # This file is part of Koha.
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.
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.
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>.
27 use Koha::Tags::Approvals;
28 use Koha::Tags::Indexes;
35 fix_tags_weight.pl - Fix weight for tags
39 fix_tags_weight.pl [ --verbose or -v ] [ --help or -h ]
42 --help or -h Brief usage message
43 --verbose or -v Be verbose
47 This script fixes the calculated weights for the tags introduced by patrons.
53 Prints a brief usage message and exits.
57 Prints information about the current weight, and the new one for each term/biblionumber.
63 binmode( STDOUT, ":encoding(UTF-8)" );
70 'verbose|v' => \$verbose
73 pod2usage(1) if $help;
75 fix_tags_approval($verbose);
76 fix_tags_index( $verbose );
78 sub fix_tags_approval {
82 print "Fix tags_approval\n=================\n" if $verbose;
84 my $dbh = C4::Context->dbh;
85 # Search the terms in tags_all that don't exist in tags_approval
86 my $sth = $dbh->prepare(
90 SELECT DISTINCT(tags_all.term) AS term, tags_approval.term AS approval FROM tags_all
91 LEFT JOIN tags_approval
92 ON (tags_all.term=tags_approval.term)) a
93 WHERE approval IS NULL;
97 my $approved = C4::Context->preference('TagsModeration') ? 0 : 1;
99 # Add missing terms to tags_approval
100 while ( my $row = $sth->fetchrow_hashref ) {
101 my $term = $row->{term};
102 C4::Tags::add_tag_approval( $term, 0, $approved );
103 print "Added => $term\n";
106 my $approvals = Koha::Tags::Approvals->search;
107 # Recalculate weight_total for all tags_approval rows
108 while ( my $approval = $approvals->next ) {
109 my $count = Koha::Tags->search( { term => $approval->term } )->count;
110 print $approval->term . "\t|\t" . $approval->weight_total . "\t=>\t" . $count . "\n"
112 $approval->weight_total($count)->store;
119 my $indexes = Koha::Tags::Indexes->search;
121 print "Fix tags_index\n==============\n" if $verbose;
123 while ( my $index = $indexes->next ) {
125 = Koha::Tags->search( { term => $index->term, biblionumber => $index->biblionumber } )
127 print $index->term . "/"
128 . $index->biblionumber . "\t|\t"
133 $index->weight($count)->store;