Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
LanguageEditStatsMaintenanceScript.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
9use MediaWiki\MediaWikiServices;
10
21 public function __construct() {
22 parent::__construct();
23 $this->addDescription( 'Script to show number of edits per language for all message groups.' );
24 $this->addOption(
25 'top',
26 '(optional) Show given number of language codes (default: 10)',
27 self::OPTIONAL,
28 self::HAS_ARG
29 );
30 $this->addOption(
31 'days',
32 '(optional) Calculate for given number of days (default: 7)',
33 self::OPTIONAL,
34 self::HAS_ARG
35 );
36 $this->addOption(
37 'bots',
38 '(optional) Include bot edits'
39 );
40 $this->addOption(
41 'ns',
42 '(optional) Comma separated list of namespace IDs',
43 self::OPTIONAL,
44 self::HAS_ARG
45 );
46 $this->requireExtension( 'Translate' );
47 }
48
49 public function execute(): void {
50 $days = (int)$this->getOption( 'days' ) ?: 7;
51 $top = (int)$this->getOption( 'top' ) ?: 10;
52 $bots = $this->hasOption( 'bots' );
53
54 $namespaces = [];
55 if ( $this->hasOption( 'ns' ) ) {
56 $input = explode( ',', $this->getOption( 'ns' ) );
57
58 foreach ( $input as $namespace ) {
59 if ( is_numeric( $namespace ) ) {
60 $namespaces[] = $namespace;
61 }
62 }
63 }
64
65 // Select set of edits to report on
66 $rows = $this->translationChanges( $days, $bots, $namespaces );
67
68 // Get counts for edits per language code
69 $codes = [];
70 foreach ( $rows as $row ) {
71 [ , $code ] = Utilities::figureMessage( $row );
72
73 if ( !isset( $codes[$code] ) ) {
74 $codes[$code] = 0;
75 }
76
77 $codes[$code]++;
78 }
79
80 // Sort counts and report descending up to $top rows.
81 arsort( $codes );
82 $i = 0;
83 foreach ( $codes as $code => $num ) {
84 if ( $i++ === $top ) {
85 break;
86 }
87
88 $this->output( "$code\t$num\n" );
89 }
90 }
91
100 private function translationChanges( int $days, bool $bots, array $ns ): array {
101 global $wgTranslateMessageNamespaces;
102 $dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA );
103
104 $cutoff = $dbr->timestamp( time() - ( $days * 24 * 3600 ) );
105
106 $conds = [
107 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff ),
108 'rc_namespace' => $ns ?: $wgTranslateMessageNamespaces,
109 'actor_name <> ' . $dbr->addQuotes( FuzzyBot::getName() )
110 ];
111 if ( $bots ) {
112 $conds['rc_bot'] = 0;
113 }
114
115 return $dbr->newSelectQueryBuilder()
116 ->select( [ 'rc_title' ] )
117 ->from( 'recentchanges' )
118 ->join( 'actor', null, 'actor_id=rc_actor' )
119 ->where( $conds )
120 ->caller( __METHOD__ )
121 ->fetchFieldValues();
122 }
123}
Shows a top list of language codes with edits in a given time period.
FuzzyBot - the misunderstood workhorse.
Definition FuzzyBot.php:15
Constants for making code for maintenance scripts more readable.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31