Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ActiveLanguagesSpecialPage.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use Config;
7use Html;
8use HtmlArmor;
9use InvalidArgumentException;
10use Language;
11use LanguageCode;
12use MediaWiki\Cache\LinkBatchFactory;
13use MediaWiki\Config\ServiceOptions;
15use MediaWiki\Languages\LanguageFactory;
16use MediaWiki\Languages\LanguageNameUtils;
17use MediaWiki\Logger\LoggerFactory;
18use ObjectCache;
19use SpecialPage;
20use Title;
21use Wikimedia\Rdbms\ILoadBalancer;
22
31class ActiveLanguagesSpecialPage extends SpecialPage {
32 private ServiceOptions $options;
33 private TranslatorActivity $translatorActivity;
34 private LanguageNameUtils $langNameUtils;
35 private ILoadBalancer $loadBalancer;
36 private ConfigHelper $configHelper;
37 private Language $contentLanguage;
38 private ProgressStatsTableFactory $progressStatsTableFactory;
39 private StatsTable $progressStatsTable;
40 private LinkBatchFactory $linkBatchFactory;
41 private LanguageFactory $languageFactory;
43 private int $period = 180;
44
45 public const CONSTRUCTOR_OPTIONS = [
46 'TranslateMessageNamespaces',
47 ];
48
49 public function __construct(
50 Config $config,
51 TranslatorActivity $translatorActivity,
52 LanguageNameUtils $langNameUtils,
53 ILoadBalancer $loadBalancer,
54 ConfigHelper $configHelper,
55 Language $contentLanguage,
56 ProgressStatsTableFactory $progressStatsTableFactory,
57 LinkBatchFactory $linkBatchFactory,
58 LanguageFactory $languageFactory
59 ) {
60 parent::__construct( 'SupportedLanguages' );
61 $this->options = new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
62 $this->translatorActivity = $translatorActivity;
63 $this->langNameUtils = $langNameUtils;
64 $this->loadBalancer = $loadBalancer;
65 $this->configHelper = $configHelper;
66 $this->contentLanguage = $contentLanguage;
67 $this->progressStatsTableFactory = $progressStatsTableFactory;
68 $this->linkBatchFactory = $linkBatchFactory;
69 $this->languageFactory = $languageFactory;
70 }
71
72 protected function getGroupName() {
73 return 'translation';
74 }
75
76 public function getDescription() {
77 return $this->msg( 'supportedlanguages' );
78 }
79
80 public function execute( $par ): void {
81 $out = $this->getOutput();
82 $lang = $this->getLanguage();
83 $this->progressStatsTable = $this->progressStatsTableFactory->newFromContext( $this->getContext() );
84
85 $this->setHeaders();
86 $out->addModuleStyles( 'ext.translate.specialpages.styles' );
87
88 $out->addHelpLink(
89 'Help:Extension:Translate/Statistics_and_reporting#List_of_languages_and_translators'
90 );
91
92 $this->outputHeader( 'supportedlanguages-summary' );
93 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
94 $dbType = $dbr->getType();
95 if ( $dbType === 'sqlite' || $dbType === 'postgres' ) {
96 $out->addHTML(
97 Html::errorBox(
98 // Messages used: supportedlanguages-sqlite-error, supportedlanguages-postgres-error
99 $out->msg( 'supportedlanguages-' . $dbType . '-error' )->parse()
100 )
101 );
102 return;
103 }
104
105 $out->addWikiMsg( 'supportedlanguages-localsummary' );
106
107 $names = $this->langNameUtils->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::ALL );
108 $languages = $this->languageCloud();
109 // There might be all sorts of subpages which are not languages
110 $languages = array_intersect_key( $languages, $names );
111
112 $this->outputLanguageCloud( $languages, $names );
113 $out->addWikiMsg( 'supportedlanguages-count', $lang->formatNum( count( $languages ) ) );
114
115 if ( !$par ) {
116 return;
117 }
118
119 // Convert formatted language tag like zh-Hant to internal format like zh-hant
120 $language = strtolower( $par );
121 try {
122 $data = $this->translatorActivity->inLanguage( $language );
123 } catch ( StatisticsUnavailable $e ) {
124 // generic-pool-error is from MW core
125 $out->addHTML( Html::errorBox( $this->msg( 'generic-pool-error' )->parse() ) );
126 return;
127 } catch ( InvalidArgumentException $e ) {
128 $errorMessageHtml = $this->msg( 'translate-activelanguages-invalid-code' )
129 ->params( LanguageCode::bcp47( $language ) )
130 ->parse();
131 $out->addHTML( Html::errorBox( $errorMessageHtml ) );
132 return;
133 }
134
135 $users = $data['users'];
136 $users = $this->filterUsers( $users, $language );
137 $this->preQueryUsers( $users );
138 $this->showLanguage( $language, $users, (int)$data['asOfTime'] );
139 }
140
141 private function showLanguage( string $code, array $users, int $cachedAt ): void {
142 $out = $this->getOutput();
143 $lang = $this->getLanguage();
144
145 // Information to be used inside the foreach loop.
146 $linkInfo = [];
147 $linkInfo['rc']['title'] = SpecialPage::getTitleFor( 'Recentchanges' );
148 $linkInfo['rc']['msg'] = $this->msg( 'supportedlanguages-recenttranslations' )->text();
149 $linkInfo['stats']['title'] = SpecialPage::getTitleFor( 'LanguageStats' );
150 $linkInfo['stats']['msg'] = $this->msg( 'languagestats' )->text();
151
152 $local = $this->langNameUtils->getLanguageName( $code, $lang->getCode() );
153 $native = $this->langNameUtils->getLanguageName( $code );
154 $statLanguage = $this->languageFactory->getLanguage( $code );
155 $bcp47Code = $statLanguage->getHtmlCode();
156
157 $span = Html::rawElement( 'span', [ 'lang' => $bcp47Code, 'dir' => $statLanguage->getDir() ], $native );
158
159 if ( $local !== $native ) {
160
161 $headerText = $this->msg( 'supportedlanguages-portallink' )
162 ->params( $bcp47Code, $local, $span )->parse();
163 } else {
164 // No CLDR, so a less localised header and link title.
165 $headerText = $this->msg( 'supportedlanguages-portallink-nocldr' )
166 ->params( $bcp47Code, $span )->parse();
167 }
168
169 $out->addHTML( Html::rawElement( 'h2', [ 'id' => $code ], $headerText ) );
170
171 // Add useful links for language stats and recent changes for the language.
172 $links = [];
173 $links[] = $this->getLinkRenderer()->makeKnownLink(
174 $linkInfo['stats']['title'],
175 $linkInfo['stats']['msg'],
176 [],
177 [
178 'code' => $code,
179 'suppresscomplete' => '1'
180 ]
181 );
182 $links[] = $this->getLinkRenderer()->makeKnownLink(
183 $linkInfo['rc']['title'],
184 $linkInfo['rc']['msg'],
185 [],
186 [
187 'translations' => 'only',
188 'trailer' => '/' . $code
189 ]
190 );
191 $linkList = $lang->listToText( $links );
192
193 $out->addHTML( '<p>' . $linkList . "</p>\n" );
194 $this->makeUserList( $users );
195
196 $ageString = $this->getLanguage()->formatTimePeriod(
197 time() - $cachedAt,
198 [ 'noabbrevs' => true, 'avoid' => 'avoidseconds' ]
199 );
200 $out->addWikiMsg( 'supportedlanguages-colorlegend', $this->getColorLegend() );
201 $out->addWikiMsg( 'translate-supportedlanguages-cached', $ageString );
202 }
203
204 private function languageCloud(): array {
205 // TODO: Inject a factory when such a thing is available in MediaWiki core
206 $cache = ObjectCache::getInstance( CACHE_ANYTHING );
207 $cachekey = $cache->makeKey( 'translate-supportedlanguages-language-cloud', 'v2' );
208
209 $data = $cache->get( $cachekey );
210 if ( is_array( $data ) ) {
211 return $data;
212 }
213
214 $dbr = $this->loadBalancer->getConnection( DB_REPLICA );
215 $tables = [ 'recentchanges' ];
216 $fields = [ 'substring_index(rc_title, \'/\', -1) as lang', 'count(*) as count' ];
217 $timestamp = $dbr->timestamp( (int)wfTimestamp() - 60 * 60 * 24 * $this->period );
218 $conds = [
219 'rc_timestamp > ' . $dbr->addQuotes( $timestamp ),
220 'rc_namespace' => $this->options->get( 'TranslateMessageNamespaces' ),
221 'rc_title' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() ),
222 ];
223 $options = [ 'GROUP BY' => 'lang', 'HAVING' => 'count > 20', 'ORDER BY' => 'NULL' ];
224
225 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options );
226
227 $data = [];
228 foreach ( $res as $row ) {
229 $data[$row->lang] = (int)$row->count;
230 }
231
232 $cache->set( $cachekey, $data, 3600 );
233
234 return $data;
235 }
236
237 protected function filterUsers( array $users, string $code ): array {
238 foreach ( $users as $index => $user ) {
239 $username = $user[TranslatorActivityQuery::USER_NAME];
240 // We do not know the group
241 if ( $this->configHelper->isAuthorExcluded( '#', $code, $username ) ) {
242 unset( $users[$index] );
243 }
244 }
245
246 return $users;
247 }
248
249 protected function outputLanguageCloud( array $languages, array $names ) {
250 global $wgTranslateDocumentationLanguageCode;
251
252 $out = $this->getOutput();
253
254 $out->addHTML( '<div class="tagcloud autonym">' );
255
256 foreach ( $languages as $k => $v ) {
257 $name = $names[$k];
258 $langAttribute = $k;
259 $size = round( log( $v ) * 20 ) + 10;
260
261 if ( $langAttribute === $wgTranslateDocumentationLanguageCode ) {
262 $langAttribute = $this->contentLanguage->getHtmlCode();
263 }
264
265 $params = [
266 'href' => $this->getPageTitle( $k )->getLocalURL(),
267 'class' => 'tag',
268 'style' => "font-size:$size%",
269 'lang' => $langAttribute,
270 ];
271
272 $tag = Html::element( 'a', $params, $name );
273 $out->addHTML( $tag . "\n" );
274 }
275 $out->addHTML( '</div>' );
276 }
277
278 private function makeUserList( array $userStats ): void {
279 $day = 60 * 60 * 24;
280
281 // Scale of the activity colors, anything
282 // longer than this is just inactive
283 $period = $this->period;
284
285 $links = [];
286 // List users in descending order by number of translations in this language
287 usort( $userStats, static function ( $a, $b ) {
288 return -(
289 $a[TranslatorActivityQuery::USER_TRANSLATIONS]
290 <=>
291 $b[TranslatorActivityQuery::USER_TRANSLATIONS]
292 );
293 } );
294
295 foreach ( $userStats as $stats ) {
296 $username = $stats[TranslatorActivityQuery::USER_NAME];
297 $title = Title::makeTitleSafe( NS_USER, $username );
298 if ( !$title ) {
299 LoggerFactory::getInstance( 'Translate' )->warning(
300 "T248125: Got Title-invalid username '{username}'",
301 [ 'username' => $username ]
302 );
303 continue;
304 }
305
306 $count = $stats[TranslatorActivityQuery::USER_TRANSLATIONS];
307 $lastTranslationTimestamp = $stats[TranslatorActivityQuery::USER_LAST_ACTIVITY];
308
309 $enc = htmlspecialchars( $username );
310
311 $attribs = [];
312 $styles = [];
313 $styles['font-size'] = round( log( $count, 10 ) * 30 ) + 70 . '%';
314
315 $last = (int)wfTimestamp() - (int)wfTimestamp( TS_UNIX, $lastTranslationTimestamp );
316 $last = round( $last / $day );
317 $attribs['title'] =
318 $this->msg( 'supportedlanguages-activity', $username )
319 ->numParams( $count, $last )
320 ->text();
321 $last = max( 1, min( $period, $last ) );
322 $styles['border-bottom'] =
323 '3px solid #' . $this->progressStatsTable->getBackgroundColor( ( $period - $last ) / $period );
324
325 $stylestr = $this->formatStyle( $styles );
326 if ( $stylestr ) {
327 $attribs['style'] = $stylestr;
328 }
329
330 $links[] =
331 $this->getLinkRenderer()->makeLink( $title, new HtmlArmor( $enc ), $attribs );
332 }
333
334 // for GENDER support
335 $usernameForGender = '';
336 if ( count( $userStats ) === 1 ) {
337 $usernameForGender = $userStats[0][TranslatorActivityQuery::USER_NAME];
338 }
339
340 $linkList = $this->getLanguage()->listToText( $links );
341 $html = "<p class='mw-translate-spsl-translators'>";
342 $html .= $this->msg( 'supportedlanguages-translators' )
343 ->rawParams( $linkList )
344 ->numParams( count( $links ) )
345 ->params( $usernameForGender )
346 ->escaped();
347 $html .= "</p>\n";
348 $this->getOutput()->addHTML( $html );
349 }
350
351 protected function formatStyle( $styles ) {
352 $stylestr = '';
353 foreach ( $styles as $key => $value ) {
354 $stylestr .= "$key:$value;";
355 }
356
357 return $stylestr;
358 }
359
360 protected function preQueryUsers( array $users ): void {
361 $lb = $this->linkBatchFactory->newLinkBatch();
362 foreach ( $users as $data ) {
363 $username = $data[TranslatorActivityQuery::USER_NAME];
364 $user = Title::capitalize( $username, NS_USER );
365 $lb->add( NS_USER, $user );
366 $lb->add( NS_USER_TALK, $user );
367 }
368 $lb->execute();
369 }
370
371 protected function getColorLegend() {
372 $legend = '';
373 $period = $this->period;
374
375 for ( $i = 0; $i <= $period; $i += 30 ) {
376 $iFormatted = htmlspecialchars( $this->getLanguage()->formatNum( $i ) );
377 $legend .= '<span style="background-color:#' .
378 $this->progressStatsTable->getBackgroundColor( ( $period - $i ) / $period ) .
379 "\"> $iFormatted</span>";
380 }
381
382 return $legend;
383 }
384}
This special page shows active languages and active translators per language.
Implements generation of HTML stats table.
A helper class added to work with configuration values of the Translate Extension.