Bug 30477: Add new UNIMARC installer translation files
[koha.git] / misc / maintenance / fix_tags_weight.pl
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::Script;
26 use Koha::Database;
27 use Koha::Tags;
28 use Koha::Tags::Approvals;
29 use Koha::Tags::Indexes;
30
31 use Getopt::Long qw( GetOptions );
32 use Pod::Usage qw( pod2usage );
33
34 =head1 NAME
35
36 fix_tags_weight.pl - Fix weight for tags
37
38 =head1 SYNOPSIS
39
40 fix_tags_weight.pl [ --verbose or -v ] [ --help or -h ]
41
42  Options:
43    --help or -h         Brief usage message
44    --verbose or -v      Be verbose
45
46 =head1 DESCRIPTION
47
48 This script fixes the calculated weights for the tags introduced by patrons.
49
50 =over 8
51
52 =item B<--help>
53
54 Prints a brief usage message and exits.
55
56 =item B<--verbose>
57
58 Prints information about the current weight, and the new one for each term/biblionumber.
59
60 =back
61
62 =cut
63
64 binmode( STDOUT, ":encoding(UTF-8)" );
65
66 my $help;
67 my $verbose;
68
69 GetOptions(
70     'help|h'    => \$help,
71     'verbose|v' => \$verbose
72 ) or pod2usage(2);
73
74 pod2usage(1) if $help;
75
76 fix_tags_approval($verbose);
77 fix_tags_index( $verbose );
78
79 sub fix_tags_approval {
80
81     my ($verbose) = @_;
82
83     print "Fix tags_approval\n=================\n" if $verbose;
84
85     my $dbh = C4::Context->dbh;
86     # Search the terms in tags_all that don't exist in tags_approval
87     my $sth = $dbh->prepare(
88         q{
89         SELECT term
90         FROM (
91             SELECT DISTINCT(tags_all.term) AS term, tags_approval.term AS approval FROM tags_all
92             LEFT JOIN tags_approval
93             ON (tags_all.term=tags_approval.term)) a
94         WHERE approval IS NULL;
95     }
96     );
97     $sth->execute();
98     my $approved = C4::Context->preference('TagsModeration') ? 0 : 1;
99
100     # Add missing terms to tags_approval
101     while ( my $row = $sth->fetchrow_hashref ) {
102         my $term = $row->{term};
103         C4::Tags::add_tag_approval( $term, 0, $approved );
104         print "Added => $term\n";
105     }
106
107     my $approvals = Koha::Tags::Approvals->search;
108     # Recalculate weight_total for all tags_approval rows
109     while ( my $approval = $approvals->next ) {
110         my $count = Koha::Tags->search( { term => $approval->term } )->count;
111         print $approval->term . "\t|\t" . $approval->weight_total . "\t=>\t" . $count . "\n"
112             if $verbose;
113         $approval->weight_total($count)->store;
114     }
115 }
116
117 sub fix_tags_index {
118
119     my ($verbose) = @_;
120     my $indexes = Koha::Tags::Indexes->search;
121
122     print "Fix tags_index\n==============\n" if $verbose;
123
124     while ( my $index = $indexes->next ) {
125         my $count
126             = Koha::Tags->search( { term => $index->term, biblionumber => $index->biblionumber } )
127             ->count;
128         print $index->term . "/"
129             . $index->biblionumber . "\t|\t"
130             . $index->weight
131             . "\t=>\t"
132             . $count . "\n"
133             if $verbose;
134         $index->weight($count)->store;
135     }
136 }
137
138 1;