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