summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/translator-stats.php
blob: 1847f46dce7fa40bd56709a73188eafeef92bb5f (plain)
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
<?php
/**
 * Script to gather translator stats.
 *
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

class TS extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Script to gather translator stats in tsv format. ' .
			'You can further process the output with translate-stats-process.php';
	}

	public function execute() {
		global $wgDisableUserGroupExpiry;

		$dbr = wfGetDB( DB_REPLICA );
		$users = $dbr->select(
			[ 'user', 'user_groups' ],
			[
				'user_name',
				'user_registration',
				'user_editcount',
				'ug_group',
			],
			[
				'user_registration is not null'
			],
			__METHOD__,
			[
				'ORDER BY' => 'user_id ASC',
			],
			[
				'user_groups' => [
					'LEFT JOIN',
					[
						'user_id=ug_user',
						'ug_group' => 'translator',
						( isset( $wgDisableUserGroupExpiry ) && !$wgDisableUserGroupExpiry ) ?
							'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ) :
							''
					]
				]
			]
		);

		echo "username\tregistration ts\tedit count\tis translator?\tpromoted ts\tmethod\n";

		$rejected = $dbr->select(
			[ 'logging' ],
			[
				'log_title',
				'log_timestamp',
			],
			[
				'log_type' => 'translatorsandbox',
				'log_action' => 'rejected',
			],
			__METHOD__
		);

		foreach ( $rejected as $r ) {
			echo "{$r->log_title}\t{$r->log_timestamp}\t0\t\t\tsandbox\n";
		}

		foreach ( $users as $u ) {
			$logs = $dbr->select(
				'logging',
				[
					'log_type',
					'log_action',
					'log_timestamp',
					'log_params',
				],
				[
					'log_title' => $u->user_name,
					'log_type' => [ 'rights', 'translatorsandbox' ],
				],
				__METHOD__,
				[
					'ORDER BY' => 'log_id ASC',
				]
			);

			$promoted = null;
			$method = 'normal';
			foreach ( $logs as $log ) {
				if ( $log->log_action === 'promoted' ) {
					$promoted = $log->log_timestamp;
					$method = 'sandbox';
					break;
				} elseif ( $log->log_action === 'rights' ) {
					Wikimedia\suppressWarnings();
					$data = unserialize( $log->log_params );
					Wikimedia\restoreWarnings();
					if ( $data === false ) {
						$lines = explode( "\n", $log->log_params );
						if ( strpos( $lines[1], 'translator' ) !== false ) {
							$promoted = $log->log_timestamp;
							break;
						}
					} elseif (
						isset( $data['5::newgroups'] ) &&
						in_array( 'translator', $data['5::newgroups'] )
					) {
						$promoted = $log->log_timestamp;
						break;
					}
				}
			}

			echo "{$u->user_name}\t{$u->user_registration}\t{$u->user_editcount}" .
			"\t{$u->ug_group}\t{$promoted}\t{$method}\n";
		}
	}
}

$maintClass = TS::class;
require_once RUN_MAINTENANCE_IF_MAIN;