Bug 28386: Fix Pawel history notes
[koha.git] / reports / catalogue_stats.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Koha;
27 use C4::Reports;
28 use C4::Circulation;
29 use C4::Biblio qw/GetMarcSubfieldStructureFromKohaField/;
30
31 use Koha::AuthorisedValues;
32 use Koha::DateUtils;
33 use Koha::ItemTypes;
34
35 =head1 NAME
36
37 plugin that shows a stats on borrowers
38
39 =head1 DESCRIPTION
40
41 =cut
42
43 our $debug = 0;
44 my $input = new CGI;
45 my $fullreportname = "reports/catalogue_stats.tt";
46 my $do_it       = $input->param('do_it');
47 my $line        = $input->param("Line");
48 my $column      = $input->param("Column");
49 my $cellvalue      = $input->param("Cellvalue"); # one of 'items', 'biblios', 'deleteditems'
50 my @filters     = $input->multi_param("Filter");
51 my $cotedigits  = $input->param("cotedigits");
52 my $output      = $input->param("output");
53 my $basename    = $input->param("basename");
54 our $sep        = $input->param("sep");
55 $sep = "\t" if ($sep eq 'tabulation');
56 my $item_itype;
57 if(C4::Context->preference('item-level_itypes')) {
58         $item_itype = "items\.itype"
59 } else {
60         $item_itype = "itemtype";
61 }
62 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($line=~ /publicationyear/ )) {
63     $line = "copyrightdate";
64 }
65 if(C4::Context->preference('marcflavour') ne "UNIMARC" && ($column =~ /publicationyear/ )) {
66     $column = "copyrightdate";
67 }
68
69 my ($template, $borrowernumber, $cookie)
70         = get_template_and_user({template_name => $fullreportname,
71                                 query => $input,
72                                 type => "intranet",
73                                 flagsrequired => {reports => '*'},
74                                 debug => 1,
75                                 });
76 $template->param(do_it => $do_it);
77 if ($do_it) {
78     my $results = calculate( $line, $column, $cellvalue, $cotedigits, \@filters );
79     if ( $output eq "screen" ) {
80         $template->param( mainloop => $results );
81         output_html_with_http_headers $input, $cookie, $template->output;
82         exit;
83     } else {
84         print $input->header(
85             -type       => 'text/csv',
86             -encoding   => 'utf-8',
87             -attachment => "$basename.csv",
88             -name       => "$basename.csv"
89         );
90         my $cols  = @$results[0]->{loopcol};
91         my $lines = @$results[0]->{looprow};
92         print @$results[0]->{line} . "/" . @$results[0]->{column} . $sep;
93         foreach my $col (@$cols) {
94             print $col->{coltitle} . $sep;
95         }
96         print "Total\n";
97         foreach my $line (@$lines) {
98             my $x = $line->{loopcell};
99             print $line->{rowtitle} . $sep;
100             foreach my $cell (@$x) {
101                 print $cell->{value} . $sep;
102             }
103             print $line->{totalrow};
104             print "\n";
105         }
106         print "TOTAL";
107         $cols = @$results[0]->{loopfooter};
108         foreach my $col (@$cols) {
109             print $sep. $col->{totalcol};
110         }
111         print $sep. @$results[0]->{total};
112         exit;
113     }
114 } else {
115         my $dbh = C4::Context->dbh;
116         my @values;
117         my %labels;
118         my $count=0;
119         my $req;
120         my @select;
121
122     my $itemtypes = Koha::ItemTypes->search_with_localization;
123
124     my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } );
125     my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } );
126
127     foreach my $kohafield (qw(items.notforloan items.materials)) {
128         my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield);
129         if($subfield_structure) {
130             my $avlist;
131             my $avcategory = $subfield_structure->{authorised_value};
132             if($avcategory) {
133                 $avlist = GetAuthorisedValues($avcategory);
134             }
135             my $kf = $kohafield;
136             $kf =~ s/^items\.//;
137             $template->param(
138                 $kf => 1,
139                 $kf."_label" => $subfield_structure->{liblibrarian},
140                 $kf."_avlist" => $avlist
141             );
142         }
143     }
144
145     my @mime  = ( map { +{type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
146
147     $template->param(
148         itemtypes    => $itemtypes,
149         locationloop => \@locations,
150         authvals     => \@authvals,
151         CGIextChoice => \@mime,
152         CGIsepChoice => GetDelimiterChoices,
153         item_itype   => $item_itype,
154     );
155
156 }
157 output_html_with_http_headers $input, $cookie, $template->output;
158
159 ## End of Main Body
160
161
162 sub calculate {
163     my ( $line, $column, $cellvalue, $cotedigits, $filters ) = @_;
164     my @mainloop;
165     my @loopfooter;
166     my @loopcol;
167     my @loopline;
168     my @looprow;
169     my %globalline;
170     my $grantotal     = 0;
171     my $barcodelike   = @$filters[16];
172     my $barcodefilter = @$filters[17];
173     my $not;
174     my $itemstable = ($cellvalue eq 'deleteditems') ? 'deleteditems' : 'items';
175
176     my $dbh = C4::Context->dbh;
177
178     # if barcodefilter is empty set as %
179     if ($barcodefilter) {
180
181         # Check if barcodefilter is "like" or "not like"
182         if ( !$barcodelike ) {
183             $not = "not";
184         }
185
186         # Change * to %
187         $barcodefilter =~ s/\*/%/g;
188     }
189
190     # Filters
191     # Checking filters
192     #
193     my @loopfilter;
194     for ( my $i = 0 ; $i <= @$filters ; $i++ ) {
195         my %cell;
196         if ( defined @$filters[$i] and @$filters[$i] ne '' and $i != 11 ) {
197             if ( ( ( $i == 1 ) or ( $i == 5 ) ) and ( @$filters[ $i - 1 ] ) ) {
198                 $cell{err} = 1 if ( @$filters[$i] < @$filters[ $i - 1 ] );
199             }
200             $cell{filter} .= @$filters[$i];
201             $cell{crit} .=
202                 ( $i == 0 )  ? "Item CallNumber From"
203               : ( $i == 1 )  ? "Item CallNumber To"
204               : ( $i == 2 )  ? "Item type"
205               : ( $i == 3 )  ? "Publisher"
206               : ( $i == 4 )  ? "Publication year From"
207               : ( $i == 5 )  ? "Publication year To"
208               : ( $i == 6 ) ? "Library"
209               : ( $i == 7 ) ? "Shelving Location"
210               : ( $i == 8 ) ? "Collection Code"
211               : ( $i == 9 ) ? "Status"
212               : ( $i == 10 ) ? "Materials"
213               : ( $i == 12 and $filters->[11] == 0 ) ? "Barcode (not like)"
214               : ( $i == 12 and $filters->[11] == 1 ) ? "Barcode (like)"
215               : ( $i == 13 ) ? "Date acquired (item) from"
216               : ( $i == 14 ) ? "Date acquired (item) to"
217               : ( $i == 15 ) ? "Date deleted (item) from"
218               : ( $i == 16 ) ? "Date deleted (item) to"
219               :                '';
220
221             push @loopfilter, \%cell;
222         }
223     }
224
225     @$filters[13] = dt_from_string(@$filters[13])->date() if @$filters[13];
226     @$filters[14] = dt_from_string(@$filters[14])->date() if @$filters[14];
227     @$filters[15] = dt_from_string(@$filters[15])->date() if @$filters[15];
228     @$filters[16] = dt_from_string(@$filters[16])->date() if @$filters[16];
229
230     my @linefilter;
231     $linefilter[0] = @$filters[0] if ( $line =~ /items\.itemcallnumber/ );
232     $linefilter[1] = @$filters[1] if ( $line =~ /items\.itemcallnumber/ );
233     if ( C4::Context->preference('item-level_itypes') ) {
234         $linefilter[0] = @$filters[2] if ( $line =~ /items\.itype/ );
235     } else {
236         $linefilter[0] = @$filters[2] if ( $line =~ /itemtype/ );
237     }
238     $linefilter[0] = @$filters[3] if ( $line =~ /publishercode/ );
239     $linefilter[0] = @$filters[4] if ( $line =~ /publicationyear/ );
240     $linefilter[1] = @$filters[5] if ( $line =~ /publicationyear/ );
241
242     $linefilter[0] = @$filters[6] if ( $line =~ /items\.homebranch/ );
243     $linefilter[0] = @$filters[7] if ( $line =~ /items\.location/ );
244     $linefilter[0] = @$filters[8] if ( $line =~ /items\.ccode/ );
245     $linefilter[0] = @$filters[9] if ( $line =~ /items\.notforloan/ );
246     $linefilter[0] = @$filters[10] if ( $line =~ /items\.materials/ );
247     $linefilter[0] = @$filters[13] if ( $line =~ /items\.dateaccessioned/ );
248     $linefilter[1] = @$filters[14] if ( $line =~ /items\.dateaccessioned/ );
249     $linefilter[0] = @$filters[15] if ( $line =~ /deleteditems\.timestamp/ );
250     $linefilter[1] = @$filters[16] if ( $line =~ /deleteditems\.timestamp/ );
251
252     my @colfilter;
253     $colfilter[0] = @$filters[0] if ( $column =~ /items\.itemcallnumber/ );
254     $colfilter[1] = @$filters[1] if ( $column =~ /items\.itemcallnumber/ );
255     if ( C4::Context->preference('item-level_itypes') ) {
256         $colfilter[0] = @$filters[2] if ( $column =~ /items\.itype/ );
257     } else {
258         $colfilter[0] = @$filters[2] if ( $column =~ /itemtype/ );
259     }
260     $colfilter[0] = @$filters[3]  if ( $column =~ /publishercode/ );
261     $colfilter[0] = @$filters[4]  if ( $column =~ /publicationyear/ );
262     $colfilter[1] = @$filters[5]  if ( $column =~ /publicationyear/ );
263     $colfilter[0] = @$filters[6] if ( $column =~ /items\.homebranch/ );
264     $colfilter[0] = @$filters[7] if ( $column =~ /items\.location/ );
265     $colfilter[0] = @$filters[8] if ( $column =~ /items\.ccode/ );
266     $colfilter[0] = @$filters[9] if ( $column =~ /items\.notforloan/ );
267     $colfilter[0] = @$filters[10] if ( $column =~ /items\.materials/ );
268     $colfilter[0] = @$filters[13] if ( $column =~ /items.dateaccessioned/ );
269     $colfilter[1] = @$filters[14] if ( $column =~ /items\.dateaccessioned/ );
270     $colfilter[0] = @$filters[15] if ( $column =~ /deleteditems\.timestamp/ );
271     $colfilter[1] = @$filters[16] if ( $column =~ /deleteditems\.timestamp/ );
272
273     # 1st, loop rows.
274     my $origline = $line;
275     $line =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
276     my $linefield;
277     if ( ( $line =~ /itemcallnumber/ ) and ($cotedigits) ) {
278         $linefield = "left($line,$cotedigits)";
279     } elsif ( $line =~ /^deleteditems\.timestamp$/ ) {
280         $linefield = "DATE($line)";
281     } else {
282         $linefield = $line;
283     }
284
285     my $strsth = "SELECT DISTINCTROW $linefield FROM $itemstable
286                     LEFT JOIN biblioitems USING (biblioitemnumber)
287                     LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
288                   WHERE 1 ";
289     $strsth .= " AND barcode $not LIKE ? " if ($barcodefilter);
290     if (@linefilter) {
291         if ( $linefilter[1] ) {
292             $strsth .= " AND $line >= ? ";
293             $strsth .= " AND $line <= ? ";
294         } elsif ( defined $linefilter[0] and $linefilter[0] ne '' ) {
295             $linefilter[0] =~ s/\*/%/g;
296             $strsth .= " AND $line LIKE ? ";
297         }
298     }
299     $strsth .= " ORDER BY $linefield";
300     $debug and print STDERR "catalogue_stats SQL: $strsth\n";
301
302     my $sth = $dbh->prepare($strsth);
303     if ( $barcodefilter and (@linefilter) and ( $linefilter[1] ) ) {
304         $sth->execute( $barcodefilter, $linefilter[0], $linefilter[1] );
305     } elsif ( (@linefilter) and ( $linefilter[1] ) ) {
306         $sth->execute( $linefilter[0], $linefilter[1] );
307     } elsif ( $barcodefilter and $linefilter[0] ) {
308         $sth->execute( $barcodefilter, $linefilter[0] );
309     } elsif ( $linefilter[0] ) {
310         $sth->execute($linefilter[0]);
311     } elsif ($barcodefilter) {
312         $sth->execute($barcodefilter);
313     } else {
314         $sth->execute();
315     }
316     my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) };
317     while ( my ($celvalue) = $sth->fetchrow ) {
318         my %cell;
319         if (defined $celvalue and $celvalue ne '') {
320             if($rowauthvals and $rowauthvals->{$celvalue}) {
321                 $cell{rowtitle} = $rowauthvals->{$celvalue};
322             } else {
323                 $cell{rowtitle} = $celvalue;
324             }
325             $cell{value} = $celvalue;
326         }
327         else {
328             $cell{rowtitle} = "NULL";
329             $cell{value} = "zzEMPTY";
330         }
331         $cell{totalrow} = 0;
332         push @loopline, \%cell;
333     }
334
335     # 2nd, loop cols.
336     my $origcolumn = $column;
337     $column =~ s/^items\./deleteditems./ if($cellvalue eq "deleteditems");
338     my $colfield;
339     if ( ( $column =~ /itemcallnumber/ ) and ($cotedigits) ) {
340         $colfield = "left($column,$cotedigits)";
341     } elsif ( $column =~ /^deleteditems\.timestamp$/ ) {
342         $colfield = "DATE($column)";
343     } else {
344         $colfield = $column;
345     }
346
347     my $strsth2 = "
348         SELECT distinctrow $colfield
349         FROM   $itemstable
350         LEFT JOIN biblioitems
351             USING (biblioitemnumber)
352         LEFT JOIN biblio
353             ON (biblioitems.biblionumber = biblio.biblionumber)
354         WHERE 1 ";
355     $strsth2 .= " AND barcode $not LIKE ?" if $barcodefilter;
356
357     if ( (@colfilter) and ( $colfilter[1] ) ) {
358         $strsth2 .= " AND $column >= ? AND $column <= ?";
359     } elsif ( defined $colfilter[0] and $colfilter[0] ne '' ) {
360         $colfilter[0] =~ s/\*/%/g;
361         $strsth2 .= " AND $column LIKE ? ";
362     }
363     $strsth2 .= " ORDER BY $colfield";
364     $debug and print STDERR "SQL: $strsth2";
365     my $sth2 = $dbh->prepare($strsth2);
366     if ( $barcodefilter and (@colfilter) and ( $colfilter[1] ) ) {
367         $sth2->execute( $barcodefilter, $colfilter[0], $colfilter[1] );
368     } elsif ( (@colfilter) and ( $colfilter[1] ) ) {
369         $sth2->execute( $colfilter[0], $colfilter[1] );
370     } elsif ( $barcodefilter && $colfilter[0] ) {
371         $sth2->execute( $barcodefilter , $colfilter[0] );
372     } elsif ( $colfilter[0]) {
373         $sth2->execute( $colfilter[0] );
374     } elsif ($barcodefilter) {
375         $sth2->execute($barcodefilter);
376     } else {
377         $sth2->execute();
378     }
379     my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) };
380     while ( my ($celvalue) = $sth2->fetchrow ) {
381         my %cell;
382         if (defined $celvalue and $celvalue ne '') {
383             if($colauthvals and $colauthvals->{$celvalue}) {
384                 $cell{coltitle} = $colauthvals->{$celvalue};
385             } else {
386                 $cell{coltitle} = $celvalue;
387             }
388             $cell{value} = $celvalue;
389         }
390         else {
391             $cell{coltitle} = "NULL";
392             $cell{value} = "zzEMPTY";
393         }
394         $cell{totalcol} = 0;
395         push @loopcol, \%cell;
396     }
397
398     my $i = 0;
399     my @totalcol;
400     my $hilighted = -1;
401
402     #Initialization of cell values.....
403     my %table;
404
405     foreach my $row (@loopline) {
406         foreach my $col (@loopcol) {
407             $table{ $row->{value} }->{ $col->{value} } = 0;
408         }
409         $table{ $row->{value} }->{totalrow} = 0;
410     }
411
412     # preparing calculation
413     my $select_cellvalue = " COUNT(*) ";
414     $select_cellvalue = " COUNT(DISTINCT biblioitems.biblionumber) " if($cellvalue eq 'biblios');
415     my $strcalc = "
416         SELECT $linefield, $colfield, $select_cellvalue
417         FROM $itemstable
418         LEFT JOIN biblioitems ON ($itemstable.biblioitemnumber = biblioitems.biblioitemnumber)
419         LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
420         WHERE 1 ";
421
422     my @sqlargs;
423
424     if ($barcodefilter) {
425         $strcalc .= "AND barcode $not like ? ";
426         push @sqlargs, $barcodefilter;
427     }
428
429     if ( @$filters[0] ) {
430         $strcalc .= " AND $itemstable.itemcallnumber >= ? ";
431         @$filters[0] =~ s/\*/%/g;
432         push @sqlargs, @$filters[0];
433     }
434
435     if ( @$filters[1] ) {
436         $strcalc .= " AND $itemstable.itemcallnumber <= ? ";
437         @$filters[1] =~ s/\*/%/g;
438         push @sqlargs, @$filters[1];
439     }
440
441     if ( @$filters[2] ) {
442         $strcalc .= " AND " . ( C4::Context->preference('item-level_itypes') ? "$itemstable.itype" : 'biblioitems.itemtype' ) . " LIKE ? ";
443         @$filters[2] =~ s/\*/%/g;
444         push @sqlargs, @$filters[2];
445     }
446
447     if ( @$filters[3] ) {
448         $strcalc .= " AND biblioitems.publishercode LIKE ? ";
449         @$filters[3] =~ s/\*/%/g;
450         @$filters[3] .= "%" unless @$filters[3] =~ /%/;
451         push @sqlargs, @$filters[3];
452     }
453     if ( @$filters[4] ) {
454         $strcalc .= " AND " .
455         (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
456         . "> ? ";
457         @$filters[4] =~ s/\*/%/g;
458         push @sqlargs, @$filters[4];
459     }
460     if ( @$filters[5] ) {
461         @$filters[5] =~ s/\*/%/g;
462         $strcalc .= " AND " .
463         (C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'publicationyear' : 'copyrightdate')
464         . "< ? ";
465         push @sqlargs, @$filters[5];
466     }
467     if ( @$filters[6] ) {
468         $strcalc .= " AND $itemstable.homebranch LIKE ? ";
469         @$filters[6] =~ s/\*/%/g;
470         push @sqlargs, @$filters[6];
471     }
472     if ( @$filters[7] ) {
473         $strcalc .= " AND $itemstable.location LIKE ? ";
474         @$filters[7] =~ s/\*/%/g;
475         push @sqlargs, @$filters[7];
476     }
477     if ( @$filters[8] ) {
478         $strcalc .= " AND $itemstable.ccode  LIKE ? ";
479         @$filters[8] =~ s/\*/%/g;
480         push @sqlargs, @$filters[8];
481     }
482     if ( defined @$filters[9] and @$filters[9] ne '' ) {
483         $strcalc .= " AND $itemstable.notforloan  LIKE ? ";
484         @$filters[9] =~ s/\*/%/g;
485         push @sqlargs, @$filters[9];
486     }
487     if ( defined @$filters[10] and @$filters[10] ne '' ) {
488         $strcalc .= " AND $itemstable.materials  LIKE ? ";
489         @$filters[10] =~ s/\*/%/g;
490         push @sqlargs, @$filters[10];
491     }
492     if ( @$filters[13] ) {
493         $strcalc .= " AND $itemstable.dateaccessioned >= ? ";
494         @$filters[13] =~ s/\*/%/g;
495         push @sqlargs, @$filters[13];
496     }
497     if ( @$filters[14] ) {
498         $strcalc .= " AND $itemstable.dateaccessioned <= ? ";
499         @$filters[14] =~ s/\*/%/g;
500         push @sqlargs, @$filters[14];
501     }
502     if ( $cellvalue eq 'deleteditems' and @$filters[15] ) {
503         $strcalc .= " AND DATE(deleteditems.timestamp) >= ? ";
504         @$filters[15] =~ s/\*/%/g;
505         push @sqlargs, @$filters[15];
506     }
507     if ( $cellvalue eq 'deleteditems' and @$filters[16] ) {
508         @$filters[16] =~ s/\*/%/g;
509         $strcalc .= " AND DATE(deleteditems.timestamp) <= ?";
510         push @sqlargs, @$filters[16];
511     }
512     $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
513     $debug and warn "SQL: $strcalc";
514     my $dbcalc = $dbh->prepare($strcalc);
515     $dbcalc->execute(@sqlargs);
516
517     while ( my ( $row, $col, $value ) = $dbcalc->fetchrow ) {
518
519         $col      = "zzEMPTY" if ( !defined($col) );
520         $row      = "zzEMPTY" if ( !defined($row) );
521
522         $table{$row}->{$col}     += $value;
523         $table{$row}->{totalrow} += $value;
524         $grantotal               += $value;
525     }
526
527     foreach my $row ( @loopline ) {
528         my @loopcell;
529
530         #@loopcol ensures the order for columns is common with column titles
531         # and the number matches the number of columns
532         foreach my $col (@loopcol) {
533             my $value = $table{$row->{value}}->{ $col->{value} };
534             push @loopcell, { value => $value };
535         }
536         push @looprow,
537           { 'rowtitle' => $row->{rowtitle},
538             'value'    => $row->{value},
539             'loopcell' => \@loopcell,
540             'hilighted' => ( $hilighted *= -1 > 0 ),
541             'totalrow' => $table{$row->{value}}->{totalrow}
542           };
543     }
544
545     foreach my $col (@loopcol) {
546         my $total = 0;
547         foreach my $row (@looprow) {
548             $total += $table{ $row->{value} }->{ $col->{value} };
549         }
550
551         push @loopfooter, { 'totalcol' => $total };
552     }
553
554     # the header of the table
555     $globalline{loopfilter} = \@loopfilter;
556
557     # the core of the table
558     $globalline{looprow} = \@looprow;
559     $globalline{loopcol} = \@loopcol;
560
561     # the foot (totals by borrower type)
562     $globalline{loopfooter} = \@loopfooter;
563     $globalline{total}      = $grantotal;
564     $globalline{line}       = $line;
565     $globalline{column}     = $column;
566     push @mainloop, \%globalline;
567     return \@mainloop;
568 }