aboutsummaryrefslogtreecommitdiffstats
path: root/stattrans.pl
blob: b794d64cb1975099e47a2354a1cf2b12acec89f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
#! /usr/bin/perl

# This script is still used in 2024

#   webwml-stattrans - Debian web site translation statistics
#   Copyright (c) 2001  Martin Schulze <joey@debian.org> and others

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.

#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

use POSIX qw(strftime);
use Getopt::Std;

#    These modules reside under webwml/Perl
use lib ($0 =~ m|(.*)/|, $1 or ".") ."/Perl";
use Local::VCS ':all';
use Webwml::Langs;
use Webwml::TransCheck;
use Webwml::TransIgnore;
use Debian::L10n::Db ('%LanguageList');
use Net::Domain qw(hostfqdn);
use Data::Dumper;
use JSON;

$| = 1;

$opt_h = "/srv/www.debian.org/webwml/english/devel/website/stats";
$opt_w = "/srv/www.debian.org/webwml";
$opt_p = "*.(wml|src)";
$opt_t = '<stats_title>';
$opt_v = 0;
$opt_d = "u";
$opt_l = undef;
$opt_b = ""; # Base URL, if not debian.org
# Should we try to repair any broken translation hash references?
$opt_r = 0;
# path of web stats JSON data
$opt_f = "/srv/www.debian.org/webwml/stats.json";
getopts('h:w:b:p:t:vd:l:f:r') || die;
#  Replace filename globbing by Perl regexps
$opt_p =~ s/\./\\./g;
$opt_p =~ s/\?/./g;
$opt_p =~ s/\*/.*/g;
$opt_p =~ s/$/\$/g;
%config = (
	   'htmldir' => $opt_h,
	   'wmldir'  => $opt_w,
	   'wmlpat'  => $opt_p,
	   'title'   => $opt_t,
	   'verbose' => $opt_v,
	   'difftype'=> $opt_d,
           'hit_file'=> $opt_f,
           'repair'  => $opt_r,
	   );

my $VCSHOST = "salsa";
my $VCSBASE = "https://salsa.debian.org/webmaster-team/webwml";

my $l = Webwml::Langs->new($opt_w);
my %langs = $l->name_iso();
my $VCS = Local::VCS->new();
if ($config{verbose}) {
    print "Caching VCS for performance...\n";
}
$VCS->cache_repo();
if ($config{verbose}) {
    print "   ...done\n";
}

my $transignore = Webwml::TransIgnore->new($opt_w);

chdir($config{'wmldir'}) or die "Can't chdir to $config{'wmldir'}: $!\n";

my %rev_info = $VCS->path_info("english",
			     'recursive' => 1,
			     'match_pat' => $config{'wmlpat'},
			     'skip_pat'  => "(template|/devel/website/stats/)");
my $cnt = scalar(keys %rev_info);
foreach (@{$transignore->global()}) {
	delete $rev_info{"english/$_"};
}

$max_versions_default = 5;
$min_versions_default = 1;

$date = strftime "%a %b %e %H:%M:%S %Y %z", localtime;

my %original;
my %transversion;
my %version;
my %files;
my %sizes;
my %min_versions;
my %max_versions;

print "Loading the coordination status databases\n" if ($config{verbose});
my %status_db = ();
opendir (DATADIR, "english/international/l10n/data")
	or die "Cannot open directory english/international/l10n/data: $!\n";
foreach (readdir (DATADIR)) {
	# Only check the status files
	next unless ($_ =~ m/^status\.(.*)$/);
	my $l = $1;
	next if (!defined $LanguageList{uc $l});
	if (-r "english/international/l10n/data/status.$l") {
		$status_db{$LanguageList{uc $l}} = Debian::L10n::Db->new();
		$status_db{$LanguageList{uc $l}}->read("english/international/l10n/data/status.$l", 0);
	}
}
closedir (DATADIR);

sub linklist {
	my ($file, $lang) = @_;
	my $add = "";
	if ($status_db{$lang}->has_package('www.debian.org')
	and $status_db{$lang}->has_status('www.debian.org')) {
		foreach my $statusline (@{$status_db{$lang}->status('www.debian.org')}) {
			my ($type, $statfile, $date, $status, $translator, $list, $url, $bug_nb) = @{$statusline};
			if ($file eq $statfile) {
				$date =~ s/\s*\+0000$//;
				$list =~ /^(\d\d\d\d)-(\d\d)-(\d\d\d\d\d)$/;
				$add = "<a href=\"https://lists.debian.org/debian-l10n-$lang/$1/debian-l10n-$lang-$1$2/msg$3.html\">$status</a>";
				$add = "<td>$add</td><td>$translator</td><td>$date</td>";
			}
		}
	}
	$add = '<td></td><td></td><td></td>' if (!length $add);
	return $add;
}

# Count wml files in given directory
#
sub getwmlfiles
{
    my $lang = shift;
    my $dir = "$lang";
#    my $cutfrom = length ($config{'wmldir'})+length($lang)+2;
    my $count = 0;
    my $size = 0;
    my $is_english = ($lang eq "english")?1:0;
    my ( $file, $v );
    my @listfiles;
    my %altrev_info;

    if (! -d "$dir") {
      print "$0: can't find $dir! Skipping ...\n";
      return;
    }
    if ($is_english) {
        @listfiles = sort keys(%rev_info);
    } else {
	%altrev_info = $VCS->path_info($dir,
				       'recursive' => 1,
				       'match_pat' => $config{'wmlpat'},
				       'skip_pat'  => "template");
        @listfiles = sort keys(%altrev_info);
    }
#    print "cutfrom is $cutfrom\n";
#    print "Looking at @listfiles\n";
#    open (LIST, ">$config{'htmldir'}/$lang.list")
#	|| die "Can't open $config{'htmldir'}/$lang.list";
    foreach my $file (@listfiles) {
#	print LIST "$file\n";
#	$file = substr ($f, $cutfrom);
#	print "looking at $file\n";
	next if $transignore->is_global($file);
	$files{$file} = 1;
	$wmlfiles{$lang} .= " " . $file;
	my $transcheck = Webwml::TransCheck->new("$dir/$file");
	if ($transcheck->revision()) {
	    $transversion{"$lang/$file"} = $transcheck->revision();
	    $original{"$lang/$file"} ||= $transcheck->original();
	    $min_versions{"$lang/$file"} = $transcheck->mindelta() || $min_versions_default;
	    $max_versions{"$lang/$file"} = $transcheck->maxdelta() || $max_versions_default;
	}
	if ($is_english) {
	    $version{"$lang/$file"} = $rev_info{"$file"}{'cmt_rev'};
	} else {
	    $version{"$lang/$file"} = $altrev_info{"$file"}{'cmt_rev'};
	    if (!$transcheck->revision()) {
	        $transcheckenglish = Webwml::TransCheck->new("english/$file");
		if (!$transcheckenglish->revision() and (-e "english/$file")) {
		    $transversion{"$lang/$file"} = $VCS->get_oldest_revision("english/$file");
		    $original{"$lang/$file"} = "english";
		} else {
		    $original{"english/$file"} = $lang;
		    $transversion{"english/$file"} = $VCS->get_oldest_revision("$lang/$file");
		}
	    }
	}
	if ($transcheck->maintainer()) {
	    # Escape HTML special characters to make the resulting page valid.
	    # This is needed for maintainers who use their e-mail address
	    # within angle brackets.
	    my %_escape_table = (
		'&' => '&amp;', '>' => '&gt;', '<' => '&lt;',
		q{"} => '&quot;', q{'} => '&#39;',
		q{`} => '&#96;', '{' => '&#123;',
		'}' => '&#125;'
	    );
	    $maintainer{"$lang/$file"} = $transcheck->maintainer();
	    $maintainer{"$lang/$file"} =~ s/([&><"'`{}])/$_escape_table{$1}/ge;
	}
	$count++;
	$sizes{$file} = (stat "".($original{"english/$file"}||"english")."/".$file)[7];
	$size += $sizes{$file};
    }
#    close LIST;
    $wmlfiles{$lang} .= " ";
    $wml{$lang} = $count;
    $wml_s{$lang} = $size;
    if ($config{verbose}) {
	print "  $lang: $count wml files, $size bytes\n";
    }
}

sub get_color
{
    my $percent = shift;

    if ($percent < 50) {
    	return sprintf ("#FF%02x00", (255/50) * $percent);
    } else {
	return sprintf ("#%02xFF00", (255/50) * (100 - $percent));
    }
}

# Update the translation-check metadata header in a wml file
sub update_file_metadata
{
    my $file = shift;
    my $revision = shift;
    my $hash = shift;
    my $text = "";

    open (IN, "< $file") or die "Can't open $file for reading: $!\n";
    while (<IN>) {
	if (m/^#use wml::debian::translation-check/) {
	    s/(translation="?)($revision)("?)/$1$hash$3/;
	}
	$text .= $_;
    }
    close(IN);
    open(OUT, "> $file") or die "Can't open $file for writing: $!\n";
    print OUT $text;
    close OUT;
}

sub check_translation
{
    my ($translation, $version, $file, $orig_file, $min_versions, $max_versions) = @_;

    my ( @version_numbers, $major_number, $last_number );
#    print "  check_translation: looking at translation $translation, english version $version, file $file, orig_file $orig_file\n";

    if ( $version && $translation ) {
	# Here we compare the original version with the translated one and print
	# a note for the user if their first or last numbers are too far apart
	# From translation-check.wml
	my $version_diff = $VCS->count_changes($orig_file, $translation, $version);
	if (!defined $version_diff) {
	    print "check_translation: error from count_changes() for orig_file $orig_file, file $file\n";
	    if ($config{'repair'}) {
		# Replace the translation reference with the most
		# recent version of the original? Yes, they're all
		# either really old or files with only one commit...
		my $latest_rev_orig = $VCS->get_newest_revision($orig_file);
		print "  fixing: using rev $latest_rev_orig instead of $translation\n";
		update_file_metadata($file, $translation, $latest_rev_orig);
	    }
	} else {
	    if ($version_diff < 0) {
		return '<gettext domain="stats">Wrong translation version</gettext>';
	    } elsif ( $version_diff >= $max_versions ) {
		return '<gettext domain="stats">This translation is too out of date</gettext>';		
	    } elsif ( $version_diff >= $min_versions ) {
		return '<gettext domain="stats">The original is newer than this translation</gettext>';
	    }
	}
    } elsif ( !$version && $translation) {
	return '<gettext domain="stats">The original no longer exists</gettext>';
    }
    return "";
}

print "Collecting data:\n" if ($config{'verbose'});
if ($opt_l) {
  getwmlfiles ($opt_l);
  getwmlfiles ('english');
} else {
  foreach $lang (sort keys %langs) {
    getwmlfiles ($lang);
  }
}
print "\n" if ($config{'verbose'});

my @search_in;
if ($opt_l) {
  @search_in = ( 'english', $opt_l );
} else {
  @search_in = sort keys %langs;
}

# Compute stats about gettext files
print "Computing statistics in gettext files... " if ($config{'verbose'});
my ( %po_translated, %po_fuzzy, %po_untranslated, %po_total );
my ( %percent_po_t, %percent_po_u, %percent_po_f );
foreach $lang (@search_in) {
    next if $lang eq 'english';
    $po_translated{"total"}{$lang} = $po_fuzzy{"total"}{$lang} = $po_untranslated{"total"}{$lang} = 0;
    $percent_po_t{'total'}{$lang} = 0;
    $percent_po_f{'total'}{$lang} = 0;
    $percent_po_u{'total'}{$lang} = 100;
    if (! -d "$lang/po") {
      print "$0: can't find $lang/po! Skipping ...\n";
      next;
    }
    my @status = qx,LC_ALL=C make -C $lang/po stats 2>&1,;
    foreach $line (@status) {
        chomp $line;
        next if($line =~ /make: (Enter|Leav)ing directory/);
        ($domain = $line) =~ s/\..*//;
        $po_translated{$domain}{$lang} = ($line =~ /(\d+) translated/ ? $1 : "0");
        $po_fuzzy{$domain}{$lang} = ($line =~ /(\d+) fuzzy/ ? $1 : "0");
        $po_untranslated{$domain}{$lang} = ($line =~ /(\d+) untranslated/ ? $1 : "0");

        $po_total{$domain} = $po_translated{$domain}{$lang} + $po_fuzzy{$domain}{$lang} + $po_untranslated{$domain}{$lang};

        $po_translated{"total"}{$lang} += $po_translated{$domain}{$lang};
	$po_fuzzy{"total"}{$lang} += $po_fuzzy{$domain}{$lang};
	$po_untranslated{"total"}{$lang} += $po_untranslated{$domain}{$lang};

        if ($po_total{$domain} > 0) {
            $percent_po_t{$domain}{$lang} = int ($po_translated{$domain}{$lang}/$po_total{$domain} * 100 + .5);
            $percent_po_f{$domain}{$lang} = int ($po_fuzzy{$domain}{$lang}/$po_total{$domain} * 100 + .5);
            $percent_po_u{$domain}{$lang} = int ($po_untranslated{$domain}{$lang}/$po_total{$domain} * 100 + .5);
        } else {
            $percent_po_t{$domain}{$lang} = 0;
            $percent_po_f{$domain}{$lang} = 0;
            $percent_po_u{$domain}{$lang} = 0; # or 100 if po/ doesn't exist
        }
    }
    $po_total{"total"} = $po_translated{"total"}{$lang} + $po_fuzzy{"total"}{$lang} + $po_untranslated{"total"}{$lang};

    if ($po_total{'total'} > 0) {
	$percent_po_t{'total'}{$lang} = int ($po_translated{'total'}{$lang}/$po_total{'total'} * 100 + .5);
	$percent_po_f{'total'}{$lang} = int ($po_fuzzy{'total'}{$lang}/$po_total{'total'} * 100 + .5);
	$percent_po_u{'total'}{$lang} = int ($po_untranslated{'total'}{$lang}/$po_total{'total'} * 100 + .5);
    }
}
print "done.\n" if ($config{'verbose'});

# =============== Create HTML files ===============
mkdir ($config{'htmldir'}, 02775) if (! -d $config{'htmldir'});

# Read website hit statistics, if available
my %hits;
my $file_sorter = sub($$) { $_[0] cmp $_[1] };
if ($config{'hit_file'}) {
    printf("Retrieving hit data from [%s].\n", $config{'hit_file'}) if ($config{'verbose'});
    open( my $fh, '<', $config{'hit_file'} );
    local $/;
    my $json = <$fh>;
    close $fh;
    if ($json) {
        my $perl = JSON::from_json($json, {utf8 => 1});
        foreach my $e (@{$perl}) {
            my ($count, $url) = @$e;
            last if $count < 3; # URLS with 2 or 1 hits are most likely mistakes; let's not waste RAM on them
            $hits{substr($url, 1)} = $count;
        }
    }
    if (%hits) {
        $file_sorter = sub($$) {
            my ($a, $b) = @_;
            $a =~ s/\.wml$//o;
            $b =~ s/\.wml$//o;
            $hits{$b} <=> $hits{$a}
        };
    } else {
        print "Tables will be sorted alphabetically.\n" if ($config{'verbose'});
    }
} else {
    print "No hit file specified. Tables will be sorted alphabetically.\n" if ($config{'verbose'});
}

my @filenames = sort $file_sorter keys %files;
my $nfiles = scalar @filenames;
$nsize += $sizes{$_} foreach (@filenames);

# 'u' == 'unidiff', 'h' == 'colored diff'
my $firstdifftype;
my $seconddifftype;
if ($config{'difftype'} eq 'u') {
    $firstdifftype = 'u';
    $seconddifftype = 'h';
} else {
    $firstdifftype = 'h';
    $seconddifftype = 'u';
}

sub vcs_log_url {
    my ($path, $rev) = @_;

    if ($VCSHOST =~ m/salsa/) {
	return "$VCSBASE/commits/$rev/$path";
    } else {
	die "Unknown/unsupported VCSHOST $VCSHOST - ABORT\n";
    }
}

sub vcs_diff_url {
    my ( $path, $r1, $r2, $diff_format ) = @_;

    if ($VCSHOST =~ m/salsa/) {
	return "$VCSBASE/compare?from=$r1&amp;to=$r2";
    } else {
	die "Unknown/unsupported VCSHOST $VCSHOST - ABORT\n";
    }
}

sub vcs_view_url {
    my ($path) = @_;

    if ($VCSHOST =~ m/salsa/) {
	return "$VCSBASE/blob/master/$path";
    } else {
	die "Unknown/unsupported VCSHOST $VCSHOST - ABORT\n";
    }
}

sub vcs_raw_url {
    my ($path) = @_;

    if ($VCSHOST =~ m/salsa/) {
	return "$VCSBASE/raw/master/$path";
    } else {
	die "Unknown/unsupported VCSHOST $VCSHOST - ABORT\n";
    }
}

# Return a shortened version string to make things less unwieldy
sub short {
    my $in = shift;
    if ($VCSHOST =~ m/salsa/) {
	return substr $in, 0, 12; # 12 is a common length people use for git
    } else {
	return $in;
    }
}

print "Creating files: " if ($config{'verbose'});
foreach $lang (@search_in) {
    my @processed_langs = ($langs{$lang});
    @processed_langs = ("zh-cn", "zh-tw") if $langs{$lang} eq "zh";
    foreach $l (sort @processed_langs) {
        print "$l.wml " if ($config{'verbose'});
        $t_body = $u_body = $ui_body = $un_body = $uu_body = $o_body = "";
        $translated{$lang} = $outdated{$lang} = $untranslated{$lang} = 0;

        # get stats about files
        foreach $file (@filenames) {
            next if ($file eq "");
            (my $base = $file) =~ s/\.wml$//;
            my $hits = exists $hits{$base} ? $hits{$base}.' <gettext domain="stats">hits</gettext>' : '<gettext domain="stats">hit count N/A</gettext>';
	    my $todo = '<td></td><td></td><td></td>';
	    $todo = linklist($file, $lang) if defined $status_db{$lang};
	    # Translated pages or already WIP in translation list
            if ((index ($wmlfiles{$lang}, " $file ") >= 0) or (($todo ne '<td></td><td></td><td></td>') and ($transversion{"$lang/$file"} ne $version{"$orig/$file"}))) {
                $translated{$lang}++;
		$translated_s{$lang} += $sizes{$file};
                $orig = $original{"$lang/$file"} || "english";
                # Outdated translations
                $msg = check_translation ($transversion{"$lang/$file"}, $version{"$orig/$file"}, "$lang/$file", "$orig/$file", $min_versions{"$lang/$file"}, $max_versions{"$lang/$file"});
                if (length ($msg) or (($todo ne '<td></td><td></td><td></td>') and ($transversion{"$lang/$file"} ne $version{"$orig/$file"}))) {
                    $o_body .= "<tr>";
                    if (($file !~ /\.wml$/)
                        || ($file eq "devel/wnpp/wnpp.wml")) {
                        $o_body .= sprintf "<td>%s</td>", $file;
                    } else {
                        $o_body .= sprintf "<td><a title=\"%s\" href=\"$opt_b/%s\">%s</a></td>", $hits, $base, $base;
                    }
		  if (!defined $status_db{$lang}) {
                    $o_body .= sprintf "<td>%s</td>", $msg;
	          }
		    if ($msg eq '<gettext domain="stats">Wrong translation version</gettext>' || $msg eq '<gettext domain="stats">The original no longer exists</gettext>') {
		      if (defined $status_db{$lang}) {
			$o_body .= sprintf "<td>%d (%.2f&nbsp;&permil;)</td>", $sizes{$file}, $sizes{$file}/$nsize * 1000;
		      } else {
		        $o_body .= "<td></td><td></td>";
		      }
		    } else {
			if (defined $status_db{$lang}) {
			    if ($transversion{"$lang/$file"} ne ''){
				$o_body .= sprintf "<td>git diff %s..%s -- %s</td>",
				    short($transversion{"$lang/$file"}), short($version{"$orig/$file"}), "$orig/$file";
			    } else {
				$o_body .= sprintf "<td>%d (%.2f&nbsp;&permil;)</td>", $sizes{$file}, $sizes{$file}/$nsize * 1000;
			    }
			} else {
			    $o_body .= sprintf "<td>git diff %s..%s -- %s</td>",
				short($transversion{"$lang/$file"}), short($version{"$orig/$file"}), "$orig/$file";
			}
		  }
		    $o_body .= sprintf "<td><a title=\"%s\" href=\"%s\">[L]</a></td>", $msg, vcs_log_url("$orig/$file", $version{"$orig/$file"});
                    $o_body .= sprintf "<td><a href=\"%s\">[V]</a>\&nbsp;", vcs_view_url("$lang/$file");
                    $o_body .= sprintf "<a href=\"%s\">[F]</a></td>", vcs_raw_url("$lang/$file");
                    $o_body .= sprintf "<td align=center>%s</td>", $maintainer{"$lang/$file"} || "";
		    $o_body .= $todo if (defined $status_db{$lang});
                    $o_body .= "</tr>\n";
                    $outdated{$lang}++;
		    $outdated_s{$lang} += $sizes{$file};
                # Up-to-date translations
                } else {
                    if (($file !~ /\.wml$/)
                        || ($file eq "devel/wnpp/wnpp.wml")) {
                        $t_body .= sprintf "<li>%s</li>\n", $file;
                    } else {
                        $t_body .= sprintf "<li><a title=\"%s\" href=\"$opt_b/%s.%s.html\">%s</a></li>\n", $hits, $base, $l, $base;
                    }
                }
            }
            # Untranslated pages
            else {
	        my $u_tmp;
                if (($file !~ /\.wml$/)
                    || ($file eq "devel/wnpp/wnpp.wml")) {
                    $u_tmp = sprintf "<tr><td>%s</td><td>&nbsp;</td></tr>\n", $file;
                } else {
                    $u_tmp = sprintf "<tr><td><a title=\"%s\" href=\"$opt_b/%s\">%s&nbsp;&nbsp;(%s)</a></td><td align=\"right\">%d</td><td>(%.2f&nbsp;&permil;)</td></tr>\n", $hits, $base, $base, $version{"$orig/$file"}, $sizes{$file}, $sizes{$file}/$nsize * 1000;
                }
		if (($file =~ /international\//) &&
		    ($file !~ /international\/index.wml$/) &&
		    ($file !~ /international\/l10n\//)) {
		    $ui_body .=$u_tmp;
		} elsif (($file =~ /(News|events|security|vote)\/[0-9]{4}\//) ||
		         ($file =~ /(News\/weekly\/[0-9]{4}\/|security\/undated)/)) {
		    $un_body .= $u_tmp;
		} elsif ($file =~ /(consultants|users\/(com|edu|gov|org))\//) {
		    $uu_body .=$u_tmp;
		} else {
		    $u_body .= $u_tmp;
		}
                $untranslated{$lang}++;
		$untranslated_s{$lang} += $sizes{$file};
            }
        }


        # this is where we discard the files that the translation directory contains
        # but which don't exist in the English directory
        #   print "extra files: ".$wml{$lang}-$translated{$lang}."\n";
        $wml{$lang} = $translated{$lang};
	$wml_s{$lang} = $translated_s{$lang};
        $translated{$lang} = $translated{$lang} - $outdated{$lang};
	$translated_s{$lang} = $translated_s{$lang} - $outdated_s{$lang};

        if ($nfiles > 0) {
            $percent_a{$lang} = $wml{$lang}/$nfiles * 100;
        } else {
            $percent_a{$lang} = 0;
        }
        if ($nsize > 0) {
            $percent_as{$lang} = $wml_s{$lang}/$nsize * 100;
        } else {
	    $percent_as{$lang} = 0;
        }
        if ($wml{$lang} > 0) {
            $percent_t{$lang} = $translated{$lang}/$wml{$lang} * 100;
        } else {
            $percent_t{$lang} = 0;
        }
        if ($wml_s{$lang} > 0) {
            $percent_ts{$lang} = $translated_s{$lang}/$wml_s{$lang} * 100;
        } else {
	    $percent_ts{$lang} = 0;
        }
        $percent_o{$lang} = 100 - $percent_t{$lang};
	$percent_os{$lang} = 100 - $percent_ts{$lang};
        $percent_u{$lang} = 100 - $percent_a{$lang};
	$percent_us{$lang} = 100 - $percent_as{$lang};

        if (open (HTML, ">$config{'htmldir'}/$l.wml")) {
	    printf HTML "#use wml::debian::template title=\"<:=\$trans{\$CUR_ISO_LANG}{%s}:>\"\n", $lang;
	    print HTML "#use wml::debian::toc\n";
        printf HTML qq|<define-tag transstatslink><a href="%s">webwml-stattrans</a></define-tag>\n|,
            vcs_view_url('stattrans.pl');
        print HTML "<define-tag createdwith><address>\n";
        print HTML '<gettext domain="stats">Created with <transstatslink></gettext>';
        print HTML "</address></define-tag>\n";
            print HTML "<script src=\"diffstat.js\" type=\"text/javascript\"></script>\n\n";
            $color = get_color ($percent_a{$lang});

            printf HTML '<table summary="<gettext domain="stats">Translation summary for</gettext> <:=$trans{$CUR_ISO_LANG}{'.$lang.'} :>" style="background-color: %s; width: 100%; font-weight: bold; margin: 0; text-align: center;">'."\n", $color;
            print HTML "<colgroup span=\"4\" width=\"25%\"></colgroup>\n";

            print HTML '<tr><th><gettext domain="stats">Translated</gettext></th><th><gettext domain="stats">Up to date</gettext></th><th><gettext domain="stats">Outdated</gettext></th><th><gettext domain="stats">Not translated</gettext></th></tr>'."\n<tr>\n";
            printf HTML '<td>%d <gettext domain="stats">files</gettext> (%.1f%%)</td>', $wml{$lang}, $percent_a{$lang};
            printf HTML '<td>%d <gettext domain="stats">files</gettext> (%.1f%%)</td>', $translated{$lang}, $percent_t{$lang};
            printf HTML '<td>%d <gettext domain="stats">files</gettext> (%.1f%%)</td>', $outdated{$lang}, $percent_o{$lang};
            printf HTML '<td>%d <gettext domain="stats">files</gettext> (%.1f%%)</td>', $untranslated{$lang}, $percent_u{$lang};
            print HTML "</tr>\n";
	    print HTML "<tr>\n";
	    printf HTML '<td>%d <gettext domain="stats">bytes</gettext> (%.1f%%)</td>', $wml_s{$lang}, $percent_as{$lang};
	    printf HTML '<td>%d <gettext domain="stats">bytes</gettext> (%.1f%%)</td>', $translated_s{$lang}, $percent_ts{$lang};
	    printf HTML '<td>%d <gettext domain="stats">bytes</gettext> (%.1f%%)</td>', $outdated_s{$lang}, $percent_os{$lang};
	    printf HTML '<td>%d <gettext domain="stats">bytes</gettext> (%.1f%%)</td>', $nsize-$wml_s{$lang}, $percent_us{$lang};
	    print HTML "</tr>\n";
            print HTML "</table>\n";

            # Make the table of content
	    print HTML "<toc-display/>\n";
            if (%hits) {
                print HTML '<p><gettext domain="stats">Note: the lists of pages are sorted by popularity. Hover over the page name to see the number of hits.</gettext>';
                print HTML "</p>\n";
            }

            # outputs the content
            if ($o_body) {
                print HTML '<toc-add-entry name="outdated"><gettext domain="stats">Outdated translations</gettext></toc-add-entry>'."\n";
                print HTML "<table summary=\"Outdated translations\" border=0 cellpadding=1 cellspacing=1>\n";
                print HTML '<tr><th><gettext domain="stats">File</gettext></th>'."\n";
	      if (defined $status_db{$lang}) {
		print HTML '<th><gettext domain="stats">Diff</gettext></th>';
	      } else {
		print HTML '<th><gettext domain="stats">Comment</gettext></th>'."\n";
		print HTML '<th><gettext domain="stats">Git command line</gettext></th>';
	      }
                print HTML '<th><gettext domain="stats">Log</gettext></th>';
                print HTML '<th><gettext domain="stats">Translation</gettext></th>';
                print HTML '<th><gettext domain="stats">Maintainer</gettext></th>';
	      if (defined $status_db{$lang}) {
		print HTML '<th><gettext domain="stats">Status</gettext></th>';
		print HTML '<th><gettext domain="stats">Translator</gettext></th>';
		print HTML '<th><gettext domain="stats">Date</gettext></th>';
	      }
                print HTML "</tr>\n";
                print HTML $o_body;
                print HTML "</table>\n";
            }
            if ($u_body) {
                print HTML '<toc-add-entry name="untranslated"><gettext domain="stats">General pages not translated</gettext></toc-add-entry>'."\n";
                print HTML '<table summary="<gettext domain="stats">Untranslated general pages</gettext>">'."\n";
                print HTML $u_body;
                print HTML "</table>\n";
            }
            if ($un_body) {
                print HTML '<toc-add-entry name="untranslated-news"><gettext domain="stats">News items not translated (low priority)</gettext></toc-add-entry>'."\n";
                print HTML '<table summary="<gettext domain="stats">Untranslated news items</gettext>">'."\n";
                print HTML $un_body;
                print HTML "</table>\n";
            }
            if ($uu_body) {
                print HTML '<toc-add-entry name="untranslated-user"><gettext domain="stats">Consultant/user pages not translated (low priority)</gettext></toc-add-entry>'."\n";
                print HTML '<table summary="<gettext domain="stats">Untranslated consultant/user pages</gettext>">'."\n";
                print HTML $uu_body;
                print HTML "</table>\n";
            }
            if ($ui_body) {
                print HTML '<toc-add-entry name="untranslated-l10n"><gettext domain="stats">International pages not translated (very low priority)</gettext></toc-add-entry>'."\n";
                print HTML '<table summary="<gettext domain="stats">Untranslated international pages</gettext>">'."\n";
                print HTML $ui_body;
                print HTML "</table>\n";
            }
            if ($t_body) {
                print HTML '<toc-add-entry name="uptodate"><gettext domain="stats">Translated pages (up-to-date)</gettext></toc-add-entry>'."\n";
                print HTML "<ul class=\"discless\">\n";
                print HTML $t_body;
                print HTML "</ul>\n";
            }
            # outputs the gettext stats
            if ($lang ne 'english') {
                print HTML '<toc-add-entry name="gettext"><gettext domain="stats">Translated templates (PO files)</gettext></toc-add-entry>'."\n";
                print HTML '<table summary="<gettext domain="stats">PO Translation Statistics</gettext>" width="100%">'."\n";
		print HTML '<tr><th><gettext domain="stats">File</gettext></th>'."\n";
		print HTML '<th><gettext domain="stats">Up to date</gettext></th>'."\n";
		print HTML '<th><gettext domain="stats">Fuzzy</gettext></th>'."\n";
		print HTML '<th><gettext domain="stats">Untranslated</gettext></th>'."\n";
		print HTML '<th><gettext domain="stats">Total</gettext></th></tr>'."\n";
                foreach my $domain (sort keys %po_total) {
                    next if $domain eq 'total';
                    print HTML "<tr>";

                    $color_t = get_color ($percent_po_t{$domain}{$lang});
                    $color_f = get_color (100 - $percent_po_f{$domain}{$lang});
                    $color_u = get_color (100 - $percent_po_u{$domain}{$lang});

                    print HTML "<td>$domain.$langs{$lang}.po</td>";
                    printf HTML "<td style=\"background-color: %s\" align=right>%d (%s%%)</td>", $color_t, $po_translated{$domain}{$lang},   $percent_po_t{$domain}{$lang};
                    printf HTML "<td style=\"background-color: %s\" align=right>%d (%s%%)</td>", $color_f, $po_fuzzy{$domain}{$lang},        $percent_po_f{$domain}{$lang};
                    printf HTML "<td style=\"background-color: %s\" align=right>%d (%s%%)</td>", $color_u, $po_untranslated{$domain}{$lang}, $percent_po_u{$domain}{$lang};
                    printf HTML "<td align=right>%d</td>", $po_total{$domain};
                    print HTML "</tr>\n";
                }
                print HTML "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
		print HTML '<tr><th><gettext domain="stats">Total:</gettext></th>';
                $color_t = get_color ($percent_po_t{'total'}{$lang});
                $color_f = get_color (100 - $percent_po_f{'total'}{$lang});
                $color_u = get_color (100 - $percent_po_u{'total'}{$lang});
                printf HTML "<td style=\"background-color: %s\" align=right>%d (%d%%)</td>", $color_t, $po_translated{'total'}{$lang}, $percent_po_t{'total'}{$lang};
                printf HTML "<td style=\"background-color: %s\" align=right>%d (%d%%)</td>", $color_f, $po_fuzzy{'total'}{$lang}, $percent_po_f{'total'}{$lang};
                printf HTML "<td style=\"background-color: %s\" align=right>%d (%d%%)</td>", $color_u, $po_untranslated{'total'}{$lang}, $percent_po_u{'total'}{$lang};
                printf HTML "<td align=right>%d</td>", $po_total{'total'};
                print HTML "</tr>\n";
                print HTML "</table>\n";
            }

            print HTML "<createdwith/>\n";
            close (HTML);
        } else {
            print "Can't open $config{'htmldir'}/$l.wml\n";
        }
    }
}
print "\n" if ($config{'verbose'});

# =============== Creating index.html ===============
print "Creating index.wml... " if ($config{'verbose'});

open (HTMLI, ">$config{'htmldir'}/index.wml")
    || die "Can't open $config{'htmldir'}/index.wml";

print HTMLI "#use wml::debian::stats_tags\n";
printf HTMLI "#use wml::debian::template title=\"%s\"\n\n", $config{'title'};
printf HTMLI qq|<define-tag transstatslink><a href="%s">webwml-stattrans</a></define-tag>\n|,
       vcs_view_url('stattrans.pl');
print HTMLI "<define-tag createdwith><address>\n";
print HTMLI '<gettext domain="stats">Created with <transstatslink></gettext>';
print HTMLI "</address></define-tag>\n";
print HTMLI '<h2><gettext domain="stats">Translated web pages</gettext></h2>'."\n";
printf HTMLI "<p><stats_pages %d></p>\n",($wml{'english'}+$untranslated{'english'});

print HTMLI '<table summary="<gettext domain="stats">Translation Statistics by Page Count</gettext>" class="stattrans">'."\n";
print HTMLI "<colgroup width=\"20%\">\n";
print HTMLI "<col>\n";
print HTMLI "</colgroup>";
print HTMLI "<colgroup width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "<col width=\"10%\">\n";
print HTMLI "</colgroup>";
print HTMLI "<thead>";
print HTMLI '<tr><th><gettext domain="stats">Language</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Translations</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Up to date</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Outdated</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Not translated</gettext></th></tr>'."\n";
print HTMLI "</thead>";
print HTMLI "<tbody>";
foreach $lang (@search_in) {
    my @processed_langs = ($langs{$lang});
    @processed_langs = ("zh-cn", "zh-tw") if $langs{$lang} eq "zh";
    foreach $l (@processed_langs) {
        $color_a = get_color ($percent_a{$lang});
        $color_t = get_color ($percent_t{$lang});
        $color_o = get_color (100 - $percent_o{$lang});
        $color_u = get_color (100 - $percent_u{$lang});

        print HTMLI "<tr>";
        printf HTMLI "<th><a href=\"%s\"><:=\$trans{\$CUR_ISO_LANG}{%s} :></a> (%s)</th>", $l, $lang, $l;
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_a, $wml{$lang},          $percent_a{$lang};
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_t, $translated{$lang},   $percent_t{$lang};
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_o, $outdated{$lang},     $percent_o{$lang};
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_u, $untranslated{$lang}, $percent_u{$lang};
        print HTMLI "</tr>\n";
    }
}
print HTMLI "</tbody>";
print HTMLI "</table>\n";

print HTMLI '<h2><gettext domain="stats">Translated web pages (by size)</gettext></h2>'."\n";
printf HTMLI "<p><stats_bytes %d></p>\n",($wml_s{'english'}+$untranslated_s{'english'});

print HTMLI '<table summary="<gettext domain="stats">Translation Statistics by Page Size</gettext>" class="stattrans">'."\n";
print HTMLI "<colgroup span=\"1\">\n";
print HTMLI "<col width=\"20%\">\n";
print HTMLI "</colgroup>";
print HTMLI "<colgroup span=\"8\">\n";
print HTMLI "<col width=\"13%\">\n";
print HTMLI "<col width=\"7%\">\n";
print HTMLI "<col width=\"13%\">\n";
print HTMLI "<col width=\"7%\">\n";
print HTMLI "<col width=\"13%\">\n";
print HTMLI "<col width=\"7%\">\n";
print HTMLI "<col width=\"13%\">\n";
print HTMLI "<col width=\"7%\">\n";
print HTMLI "</colgroup>";
print HTMLI "<thead>";
print HTMLI '<tr><th><gettext domain="stats">Language</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Translations</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Up to date</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Outdated</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Not translated</gettext></th></tr>'."\n";
print HTMLI "</thead>";
print HTMLI "<tbody>";

foreach $lang (@search_in) {
    my @processed_langs = ($langs{$lang});
    @processed_langs = ("zh-cn", "zh-tw") if $langs{$lang} eq "zh";
    foreach $l (@processed_langs) {
	$color_a = get_color ($percent_a{$lang});
	$color_t = get_color ($percent_t{$lang});
	$color_o = get_color (100 - $percent_o{$lang});
	$color_u = get_color (100 - $percent_u{$lang});

	print HTMLI "<tr>";
	printf HTMLI "<th><a href=\"%s\"><:=\$trans{\$CUR_ISO_LANG}{%s} :></a> (%s)</th>", $l, $lang, $l;
	printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_a, $wml_s{$lang},                   $percent_as{$lang};
	printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_t, $translated_s{$lang},            $percent_ts{$lang};
	printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_o, $outdated_s{$lang},              $percent_os{$lang};
	printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%.1f%%)</td>", $color_u, $wml_s{"english"}+$untranslated_s{'english'}-$wml_s{$lang}, $percent_us{$lang};
	print HTMLI "</tr>\n";
    }
}
print HTMLI "</tbody>";
print HTMLI "</table>\n";

print HTMLI '<h2><gettext domain="stats">Translated templates (PO files)</gettext></h2>'."\n";
printf HTMLI "<p><stats_strings %d></p>\n",$po_total{'total'};

print HTMLI '<table summary="<gettext domain="stats">PO Translation Statistics</gettext>" class="stattrans">'."\n";
print HTMLI "<colgroup span=\"1\"width=\"28%\">\n";
print HTMLI "</colgroup>";
print HTMLI "<colgroup span=\"6\" width=\"12%\">\n";
print HTMLI "<col width=\"12%\">\n";
print HTMLI "<col width=\"12%\">\n";
print HTMLI "<col width=\"12%\">\n";
print HTMLI "<col width=\"12%\">\n";
print HTMLI "<col width=\"12%\">\n";
print HTMLI "<col width=\"12%\">\n";
print HTMLI "</colgroup>";
print HTMLI "<thead>";
print HTMLI '<tr><th><gettext domain="stats">Language</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Up to date</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Fuzzy</gettext></th>'."\n";
print HTMLI '<th colspan="2"><gettext domain="stats">Not translated</gettext></th></tr>'."\n";
print HTMLI "</thead>";
print HTMLI "<tbody>";
foreach $lang (@search_in) {
    next if $lang eq 'english';
    my @processed_langs = ($langs{$lang});
    @processed_langs = ("zh-cn", "zh-tw") if $langs{$lang} eq "zh";
    foreach $l (@processed_langs) {
        print HTMLI "<tr>";
        printf HTMLI "<th><a href=\"%s#gettext\"><:=\$trans{\$CUR_ISO_LANG}{%s} :></a> (%s)</th>", $l, $lang, $l;
        $color_t = get_color ($percent_po_t{'total'}{$lang});
        $color_f = get_color (100 - $percent_po_f{'total'}{$lang});
        $color_u = get_color (100 - $percent_po_u{'total'}{$lang});
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%d%%)</td>", $color_t, $po_translated{'total'}{$lang},   $percent_po_t{'total'}{$lang};
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%d%%)</td>", $color_f, $po_fuzzy{'total'}{$lang},        $percent_po_f{'total'}{$lang};
        printf HTMLI "<td style=\"background-color: %s\">%d</td><td>(%d%%)</td>", $color_u, $po_untranslated{'total'}{$lang}, $percent_po_u{'total'}{$lang};
        print HTMLI "</tr>\n";
    }
}

print HTMLI "</tbody>";
print HTMLI "</table>\n";

print HTMLI "<createdwith/>\n";
close (HTMLI);

print "done.\n" if ($config{'verbose'});

# Note:
#   Translated pages on ll.html may be higher than in index.html.
#   This is due to the fact that some english pages were removed.

# printf "%s\n", join ("\n", keys %version);
# printf "%s - %s\n", $version{'german/devel/index'}, $version{'english/devel/index'};

© 2014-2024 Faster IT GmbH | imprint | privacy policy