removing no longer needed parameter
[migration-tools.git] / kmig.d / bin / mig-reporter
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use DBI;
7 use Data::Dumper;
8 use Getopt::Long;
9 use XML::LibXML;
10 use Env qw(
11     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
12         MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
13 );
14 use open ':encoding(utf8)';
15 use Cwd 'abs_path';
16 use Cwd qw(getcwd);
17 use FindBin;
18 my $mig_bin = "$FindBin::Bin/";
19 use lib "$FindBin::Bin/";
20 use KMig;
21
22 my $analyst = 'Equinox Open Library Initiative';
23 my $report_title;
24 my $reports_xml = 'stock_reports.xml';
25 my $tags = 'bibs.items.borrowers.circs.reserves.accounts.courses';
26 my $added_page_title;
27 my $added_page_file;
28 my $i = 0;
29 my $parser = XML::LibXML->new();
30 my $lines_per_page = 42;
31 my $koha_instance;
32 my $koha_conf_xml;
33
34 my $dbh = KMig::db_connect();
35
36 my $ret = GetOptions(
37     'tags:s'             => \$tags,    
38     'reports_xml:s'      => \$reports_xml,
39     'analyst:s'          => \$analyst,
40     'added_page_file:s'  => \$added_page_file,
41     'added_page_title:s' => \$added_page_title,
42     'report_title:s'     => \$report_title,
43     'title:s'            => \$report_title
44 );
45
46 my $mig_path = abs_path($0);
47 $mig_path =~ s|[^/]+$||;
48 $reports_xml = find_xml($reports_xml,$mig_path);
49 if (!defined $reports_xml) { abort("Can not find xml reports file."); }
50 my $dom = $parser->parse_file($reports_xml);
51
52 abort('must supply a --title parameter') unless defined $report_title;
53 if (defined $added_page_title) { abort ('must specify --added_page_file') unless defined $added_page_file; }
54 if (defined $added_page_file) { abort ('must specify --added_page_title') unless defined $added_page_title; }
55
56 my $report_file = create_report_name($report_title);
57 $report_file = $MIGGITDIR . $report_file;
58 open(my $fh, '>', $report_file) or die "Could not open report file!";
59
60 write_title_page($report_title,$fh,$analyst);
61
62 if (defined $added_page_file and defined $added_page_title) { 
63     print $fh "<<<\n";
64     print $fh "== $added_page_title\n";
65     print "$added_page_file\t$added_page_title\n";
66     open(my $an,'<:encoding(UTF-8)', $added_page_file) or die "Could not open $added_page_file !";
67     while ( my $line = <$an> ) {
68         print $fh $line;
69     }
70     print $fh "\n";
71     close $an;
72 }
73
74 my @report_tags = split(/\./,$tags);
75 foreach my $t (@report_tags) {
76     print "\n\n=========== Starting to process tag $t\n";
77     print   "==========================================\n";
78     print_section_header(ucfirst($t),$fh);
79     my $linecount = $lines_per_page;
80     my $r;
81
82     my @report_names;
83
84     foreach my $report ($dom->findnodes('//report')) {
85         if (index($report->findvalue('./tag'),$t) != -1) {
86             push @report_names, $report->findvalue('./name');
87         }
88     }
89     
90     print Dumper(@report_names);
91
92     #only has one level of failover now but could change to array of hashes and loops
93     foreach my $rname (@report_names) {
94         print "\nchecking for $rname ... ";
95         my %report = find_report($dom,$t,$rname);
96         $r = print_query($fh,%report);
97     }
98 }
99 # end of main logic
100
101 print "\n";
102 close $fh;
103
104 sub find_xml {
105     my $reports_xml = shift;
106     my $mig_path = shift;
107
108     if ($reports_xml =~ m/\//) { return $reports_xml; }
109
110     my $mig_test_file =  $mig_path . '/../xml/' . $reports_xml;
111     my $working_test_dir = getcwd();
112     my $working_test_file = $working_test_dir . '/' . $reports_xml;
113
114     if (-e $mig_test_file) { return $mig_test_file; }
115     if (-e $working_test_file) { return $working_test_file; }
116
117     return undef;
118 }
119
120 sub find_report {
121     my $dom = shift;
122     my $tag = shift;
123     my $name = shift;
124     my %report;
125
126     foreach my $node ($dom->findnodes('//report')) {
127         if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./name') eq $name) {
128             print "succeeded ... ";
129             %report = (
130                 name => $node->findvalue('./name'),
131                 report_title => $node->findvalue('./report_title'),
132                 query => $node->findvalue('./query'),
133                 heading => $node->findvalue('./heading'),
134                 tag => $node->findvalue('./tag'),
135                 note => $node->findvalue('./note'),
136             );
137             return %report;
138         }
139     }
140     print "failed ... ";
141     return %report = (
142         name => "eaten by grue"
143     );
144 }
145
146 sub print_section_header {
147     my $t = shift;
148     my $fh = shift;
149     $t =~ s/_/ /g;
150     #$t =~ s/(\w+)/\u$1/g;;
151     print $fh "<<<\n";
152     print $fh "== $t Reports\n";
153 }
154
155 sub create_report_name {
156     my $rt = shift;
157     my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
158     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
159     $year += 1900;
160     my $date = $year . '_' . $abbr[$mon] . '_' . $mday;
161     my $report_file = $rt . ' ' . $date . '.asciidoc';
162     $report_file =~ s/ /_/g;
163     return $report_file;
164 }
165
166 sub write_title_page {
167     my $rt = shift;
168     my $fh = shift;
169     my $a = shift;
170
171     my @abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
172     my $l = length($report_title);
173     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
174     $year += 1900;
175     print $fh "= $rt\n"; 
176     print $fh "$mday $abbr[$mon] $year\n";
177     print $fh "$a\n";
178     print $fh ":toc:\n";
179     print $fh "\n";
180 }
181
182 sub print_query {
183     my $fh = shift;
184     my %report = @_;
185     my $query = $report{query};
186     if (!defined $query) { print "No query defined, returning... \n"; return; }
187     print "$query\n";
188     my $sth = $dbh->prepare($query);
189     $sth->execute();
190
191     my $header_flag = 0;
192
193     while (my @row = $sth->fetchrow_array) {
194             if ($header_flag == 0) {
195                 print $fh "\n.*$report{report_title}*\n";
196                 print $fh "|===\n";
197                 my @h = split(/\./,$report{heading});
198                 my $h_length = @h;
199                 my $h_count = 1;
200                 while ($h_count <= $h_length) {
201                     print $fh "|$h[$h_count-1] ";
202                     $h_count++;
203                 }
204                 print $fh "\n";
205                 $header_flag = 1;
206             }
207             my $row_length = @row;
208             my $r = 1;
209             while ($r <= $row_length) {
210                 if (! defined $row[$r-1] ) {
211                     $row[$r-1] = 'none';
212                 }
213                 print $fh "|$row[$r-1] ";
214                 $r++;
215             }
216             print $fh "\n";
217         }
218     if ($header_flag == 1) { 
219         print $fh "|===\n\n"; 
220         print $fh $report{note};
221         print $fh "\n\n";
222     }
223     print "successfully wrote output for $report{name}.\n\n";
224 }
225
226 sub abort {
227     my $msg = shift;
228     print STDERR "$0: $msg", "\n";
229     print_usage();
230     exit 1;
231 }
232
233 sub print_usage {
234     print <<_USAGE_;
235
236   --tags            - period delimited these are the tags that it will 
237                       use to identify reports to run with (optional)
238   --report_title 
239
240 _USAGE_
241 }
242