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