Bug 14472: (follow-up) Wrong ISSN search index in record matching rules
[koha.git] / installer / data / mysql / backfill_statistics.pl
blob361f5447888315a376a473453c359f1522c181d8
1 #!/usr/bin/perl
3 # Part of the Koha Library Software www.koha-community.org
4 # Licensed under the GPL.
6 use strict;
7 use warnings;
9 # CPAN modules
10 use DBI;
11 use Getopt::Long;
13 # Koha modules
14 use C4::Context;
15 use C4::Items;
16 use C4::Debug;
17 use Data::Dumper;
19 use vars qw($debug $dbh $VERSION);
20 $dbh = C4::Context->dbh;
21 $VERSION = "1.00";
23 sub get_counts() {
24 my $query = q(
25 SELECT
26 (SELECT count(*) FROM statistics WHERE branch="NO_LIBRARY" ) AS NO_LIBRARY,
27 (SELECT count(*) FROM statistics WHERE branch IS NULL) AS NULL_BRANCH,
28 (SELECT count(*) FROM statistics WHERE itemtype IS NULL AND itemnumber IS NOT NULL) AS NULL_ITEMTYPE,
29 (SELECT count(*) FROM statistics WHERE usercode IS NULL) AS NULL_USERCODE,
30 (SELECT count(*) FROM statistics WHERE borrowernumber IS NULL) AS NULL_BORROWERNUMBER,
31 (SELECT count(*) FROM statistics WHERE associatedborrower IS NULL) AS NULL_ASSOCIATEDBORROWER,
32 (SELECT count(*) FROM statistics ) AS Total
34 my $sth = $dbh->prepare($query);
35 $sth->execute;
36 return $sth->fetchrow_hashref;
39 sub itemnumber_array() {
40 my $query = q(
41 SELECT DISTINCT itemnumber
42 FROM statistics
43 WHERE itemtype IS NULL
44 AND itemnumber IS NOT NULL
46 my $sth = $dbh->prepare($query);
47 $sth->execute;
48 my @itemnumbers = map {shift @$_} @{$sth->fetchall_arrayref};
49 return @itemnumbers;
51 sub null_borrower_lines() {
52 my $query = "SELECT * FROM statistics WHERE borrowernumber IS NULL";
53 my $sth = $dbh->prepare($query);
54 $sth->execute;
55 print "Number of lines with NULL_BORROWERNUMBER: ", scalar($sth->rows), "\n";
56 return $sth->fetchall_arrayref({});
59 sub show_counts() {
60 print "\nThe following counts represent the number of (potential) errors in your statistics table:\n";
61 my $counts = get_counts;
62 foreach (sort keys %$counts) {
63 $_ eq 'Total' and next;
64 $counts->{Error_Total} += $counts->{$_};
65 print sprintf("%30s : %3d \n",$_ ,$counts->{$_});
67 print sprintf("%30s : %3d (potential) errors in %d lines\n",'Error_Total',$counts->{Error_Total}, $counts->{'Total'});
70 ##### MAIN #####
71 print "This operation may take a while.\n";
72 (scalar @ARGV) or show_counts;
73 print "\nAttempting to populate missing data.\n";
75 my (@itemnumbers) = (scalar @ARGV) ? @ARGV : &itemnumber_array;
76 $debug and print "itemnumbers: ", Dumper(\@itemnumbers);
77 print "Number of distinct itemnumbers paired with NULL_ITEMTYPE: ", scalar(@itemnumbers), "\n";
79 my $query = "UPDATE statistics SET itemtype = ? WHERE itemnumber = ?";
80 my $update = $dbh->prepare($query);
81 # $debug and print "Update Query: $query\n";
82 foreach (@itemnumbers) {
83 my $item = GetItem($_);
84 unless ($item) {
85 print STDERR "\tNo item found for itemnumber $_\n";
86 next;
88 $update->execute($item->{itype},$_) or warn "Error in UPDATE execution";
89 printf "\titemnumber %5d : %7s (%s rows)\n", $_, $item->{itype}, $update->rows;
92 my $old_issues = $dbh->prepare("SELECT * FROM old_issues WHERE timestamp = ? AND itemnumber = ?");
93 my $issues = $dbh->prepare("SELECT * FROM issues WHERE timestamp = ? AND itemnumber = ?");
94 $update = $dbh->prepare("UPDATE statistics SET borrowernumber = ? WHERE datetime = ? AND itemnumber = ?");
95 my $nullborrs = null_borrower_lines;
96 $debug and print Dumper($nullborrs);
97 foreach (@$nullborrs) {
98 $old_issues->execute($_->{datetime},$_->{itemnumber});
99 my $issue;
100 if ($old_issues->rows != 1) {
101 print STDERR "Warning! Unexpected number of matches from old_issues: ",$old_issues->rows;
102 $issues->execute($_->{datetime},$_->{itemnumber});
103 if ($issues->rows != 1) {
104 print STDERR ", from issues: ",$issues->rows,"\tskipping this record\n";
105 next;
107 print STDERR "\n";
108 $issue = $issues->fetchrow_hashref;
109 } else {
110 $issue = $old_issues->fetchrow_hashref;
112 printf "\titemnumber: %5d at %20s -- borrowernumber: %5d\n", $_->{itemnumber}, $_->{datetime}, $issue->{borrowernumber};
113 $update->execute($issue->{borrowernumber},$_->{datetime},$_->{itemnumber});
116 print "\nOperations complete.\n";
117 (scalar @ARGV) or show_counts;