Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
translator-stats-process.php
Go to the documentation of this file.
1<?php
10// Standard boilerplate to define $IP
11if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
12 $IP = getenv( 'MW_INSTALL_PATH' );
13} else {
14 $dir = __DIR__;
15 $IP = "$dir/../../..";
16}
17require_once "$IP/maintenance/Maintenance.php";
18
19class TSP extends Maintenance {
20 public function __construct() {
21 parent::__construct();
22 $this->addDescription( 'Script to calculate monthly stats about tsv data produced ' .
23 'by translator-stats.php.' );
24 $this->addArg(
25 'file',
26 'tsv file to process'
27 );
28 }
29
30 protected function median( $a ) {
31 sort( $a );
32 $len = count( $a );
33 if ( $len === 0 ) {
34 return 0;
35 } elseif ( $len === 1 ) {
36 return $a[0];
37 } elseif ( $len % 2 === 0 ) {
38 return $a[$len / 2];
39 } else {
40 return ( $a[(int)floor( $len / 2 )] + $a[(int)ceil( $len / 2 )] ) / 2;
41 }
42 }
43
44 public function execute() {
45 $handle = fopen( $this->getArg( 0 ), 'r' );
46 // remove heading
47 // @phan-suppress-next-line PhanPluginUseReturnValueInternalKnown
48 fgets( $handle );
49
50 $data = [];
51 while ( true ) {
52 $l = fgets( $handle );
53 if ( $l === false ) {
54 break;
55 }
56
57 $fields = explode( "\t", trim( $l, "\n" ) );
58 $reg = $fields[1];
59 $month = substr( $reg, 0, 4 ) . '-' . substr( $reg, 4, 2 ) . '-01';
60 $data[$month][] = $fields;
61 }
62
63 fclose( $handle );
64
65 ksort( $data );
66
67 echo "period\tnew\tpromoted\tgood\tmedian promotion time\t" .
68 "avg promotion time\tsandbox approval rate\n";
69
70 foreach ( $data as $key => $period ) {
71 $total = 0;
72 $promoted = 0;
73 $good = 0;
74 $delay = [];
75 $avg = 'N/A';
76 $sbar = [];
77
78 foreach ( $period as $p ) {
79 [ , $reg, $edits, $translator, $promtime, $method ] = $p;
80 $total++;
81 if ( $translator === 'translator' ) {
82 $promoted++;
83 }
84
85 if ( $edits > 100 ) {
86 $good++;
87 }
88
89 if ( $promtime ) {
90 $delay[] = (int)wfTimestamp( TS_UNIX, $promtime ) - (int)wfTimestamp( TS_UNIX, $reg );
91 }
92
93 if ( $method === 'sandbox' ) {
94 if ( $promtime ) {
95 $sbar[] = true;
96 } else {
97 $sbar[] = false;
98 }
99 }
100
101 }
102
103 $median = round( $this->median( $delay ) / 3600 );
104 if ( count( $delay ) ) {
105 $avg = round( array_sum( $delay ) / count( $delay ) / 3600 );
106 }
107
108 if ( $sbar === [] ) {
109 $sbar = 'N/A';
110 } else {
111 $sbar = count( array_filter( $sbar ) ) / count( $sbar );
112 }
113
114 echo "$key\t$total\t$promoted\t$good\t$median\t$avg\t$sbar\n";
115 }
116 }
117}
118
119$maintClass = TSP::class;
120require_once RUN_MAINTENANCE_IF_MAIN;