34 private $translatorActivity;
36 private $langNameUtils;
38 private $loadBalancer;
40 private $configHelper;
42 private $contentLanguage;
44 private $progressStatsTableFactory;
46 private $progressStatsTable;
48 private $period = 180;
50 public const CONSTRUCTOR_OPTIONS = [
51 'TranslateMessageNamespaces',
54 public function __construct(
57 LanguageNameUtils $langNameUtils,
58 ILoadBalancer $loadBalancer,
60 Language $contentLanguage,
63 parent::__construct(
'SupportedLanguages' );
64 $this->options =
new ServiceOptions( self::CONSTRUCTOR_OPTIONS, $config );
65 $this->translatorActivity = $translatorActivity;
66 $this->langNameUtils = $langNameUtils;
67 $this->loadBalancer = $loadBalancer;
68 $this->configHelper = $configHelper;
69 $this->contentLanguage = $contentLanguage;
70 $this->progressStatsTableFactory = $progressStatsTableFactory;
73 protected function getGroupName() {
77 public function getDescription() {
78 return $this->msg(
'supportedlanguages' )->text();
81 public function execute( $par ) {
82 $out = $this->getOutput();
83 $lang = $this->getLanguage();
84 $this->progressStatsTable = $this->progressStatsTableFactory->newFromContext( $this->getContext() );
87 $out->addModuleStyles(
'ext.translate.specialpages.styles' );
90 'Help:Extension:Translate/Statistics_and_reporting#List_of_languages_and_translators'
93 $this->outputHeader(
'supportedlanguages-summary' );
94 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
95 $dbType = $dbr->getType();
96 if ( $dbType ===
'sqlite' || $dbType ===
'postgres' ) {
100 $out->msg(
'supportedlanguages-' . $dbType .
'-error' )->parse()
106 $out->addWikiMsg(
'supportedlanguages-localsummary' );
108 $names = $this->langNameUtils->getLanguageNames(
null,
'all' );
109 $languages = $this->languageCloud();
111 $languages = array_intersect_key( $languages, $names );
113 $this->outputLanguageCloud( $languages, $names );
114 $out->addWikiMsg(
'supportedlanguages-count', $lang->formatNum( count( $languages ) ) );
121 $language = strtolower( $par );
123 $data = $this->translatorActivity->inLanguage( $language );
126 $out->addHTML( Html::errorBox( $this->msg(
'generic-pool-error' )->parse() ) );
128 }
catch ( InvalidArgumentException $e ) {
129 $errorMessageHtml = $this->msg(
'translate-activelanguages-invalid-code' )
130 ->params( LanguageCode::bcp47( $language ) )
132 $out->addHTML( Html::errorBox( $errorMessageHtml ) );
136 $users = $data[
'users'];
137 $users = $this->filterUsers( $users, $language );
138 $this->preQueryUsers( $users );
139 $this->showLanguage( $language, $users, (
int)$data[
'asOfTime'] );
142 private function showLanguage(
string $code, array $users,
int $cachedAt ):
void {
143 $out = $this->getOutput();
144 $lang = $this->getLanguage();
145 $bcp47Code = LanguageCode::bcp47( $code );
149 $linkInfo[
'rc'][
'title'] = SpecialPage::getTitleFor(
'Recentchanges' );
150 $linkInfo[
'rc'][
'msg'] = $this->msg(
'supportedlanguages-recenttranslations' )->text();
151 $linkInfo[
'stats'][
'title'] = SpecialPage::getTitleFor(
'LanguageStats' );
152 $linkInfo[
'stats'][
'msg'] = $this->msg(
'languagestats' )->text();
154 $local = $this->langNameUtils->getLanguageName( $code, $lang->getCode(),
'all' );
155 $native = $this->langNameUtils->getLanguageName( $code,
null,
'all' );
157 if ( $local !== $native ) {
159 $headerText = $this->msg(
'supportedlanguages-portallink' )
160 ->params( $bcp47Code, $local, $native )->escaped();
163 $headerText = $this->msg(
'supportedlanguages-portallink-nocldr' )
164 ->params( $bcp47Code, $native )->escaped();
167 $out->addHTML( Html::rawElement(
'h2', [
'id' => $code ], $headerText ) );
171 $links[] = $this->getLinkRenderer()->makeKnownLink(
172 $linkInfo[
'stats'][
'title'],
173 $linkInfo[
'stats'][
'msg'],
177 'suppresscomplete' =>
'1'
180 $links[] = $this->getLinkRenderer()->makeKnownLink(
181 $linkInfo[
'rc'][
'title'],
182 $linkInfo[
'rc'][
'msg'],
185 'translations' =>
'only',
186 'trailer' =>
'/' . $code
189 $linkList = $lang->listToText( $links );
191 $out->addHTML(
'<p>' . $linkList .
"</p>\n" );
192 $this->makeUserList( $users );
194 $ageString = $this->getLanguage()->formatTimePeriod(
196 [
'noabbrevs' =>
true,
'avoid' =>
'avoidseconds' ]
198 $out->addWikiMsg(
'supportedlanguages-colorlegend', $this->getColorLegend() );
199 $out->addWikiMsg(
'translate-supportedlanguages-cached', $ageString );
202 private function languageCloud(): array {
204 $cache = ObjectCache::getInstance( CACHE_ANYTHING );
205 $cachekey = $cache->makeKey(
'translate-supportedlanguages-language-cloud',
'v2' );
207 $data = $cache->get( $cachekey );
208 if ( is_array( $data ) ) {
212 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
213 $tables = [
'recentchanges' ];
214 $fields = [
'substring_index(rc_title, \'/\', -1) as lang',
'count(*) as count' ];
215 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - 60 * 60 * 24 * $this->period );
217 'rc_timestamp > ' . $dbr->addQuotes( $timestamp ),
218 'rc_namespace' => $this->options->get(
'TranslateMessageNamespaces' ),
219 'rc_title' . $dbr->buildLike( $dbr->anyString(),
'/', $dbr->anyString() ),
221 $options = [
'GROUP BY' =>
'lang',
'HAVING' =>
'count > 20',
'ORDER BY' =>
'NULL' ];
223 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options );
226 foreach ( $res as $row ) {
227 $data[$row->lang] = (int)$row->count;
230 $cache->set( $cachekey, $data, 3600 );
235 protected function filterUsers( array $users,
string $code ): array {
236 foreach ( $users as $index => $user ) {
237 $username = $user[TranslatorActivityQuery::USER_NAME];
239 if ( $this->configHelper->isAuthorExcluded(
'#', $code, $username ) ) {
240 unset( $users[$index] );
247 protected function outputLanguageCloud( array $languages, array $names ) {
248 global $wgTranslateDocumentationLanguageCode;
250 $out = $this->getOutput();
252 $out->addHTML(
'<div class="tagcloud autonym">' );
254 foreach ( $languages as $k => $v ) {
257 $size = round( log( $v ) * 20 ) + 10;
259 if ( $langAttribute === $wgTranslateDocumentationLanguageCode ) {
260 $langAttribute = $this->contentLanguage->getHtmlCode();
264 'href' => $this->getPageTitle( $k )->getLocalURL(),
266 'style' =>
"font-size:$size%",
267 'lang' => $langAttribute,
270 $tag = Html::element(
'a', $params, $name );
271 $out->addHTML( $tag .
"\n" );
273 $out->addHTML(
'</div>' );
276 private function makeUserList( array $userStats ):
void {
281 $period = $this->period;
285 usort( $userStats,
static function ( $a, $b ) {
287 $a[TranslatorActivityQuery::USER_TRANSLATIONS]
289 $b[TranslatorActivityQuery::USER_TRANSLATIONS]
293 foreach ( $userStats as $stats ) {
294 $username = $stats[TranslatorActivityQuery::USER_NAME];
295 $title = Title::makeTitleSafe( NS_USER, $username );
297 LoggerFactory::getInstance(
'Translate' )->warning(
298 "T248125: Got Title-invalid username '{username}'",
299 [
'username' => $username ]
304 $count = $stats[TranslatorActivityQuery::USER_TRANSLATIONS];
305 $lastTranslationTimestamp = $stats[TranslatorActivityQuery::USER_LAST_ACTIVITY];
307 $enc = htmlspecialchars( $username );
311 $styles[
'font-size'] = round( log( $count, 10 ) * 30 ) + 70 .
'%';
313 $last = wfTimestamp( TS_UNIX ) - wfTimestamp( TS_UNIX, $lastTranslationTimestamp );
314 $last = round( $last / $day );
316 $this->msg(
'supportedlanguages-activity', $username )
317 ->numParams( $count, $last )
319 $last = max( 1, min( $period, $last ) );
320 $styles[
'border-bottom'] =
321 '3px solid #' . $this->progressStatsTable->getBackgroundColor( ( $period - $last ) / $period );
323 $stylestr = $this->formatStyle( $styles );
325 $attribs[
'style'] = $stylestr;
329 $this->getLinkRenderer()->makeLink( $title,
new HtmlArmor( $enc ), $attribs );
333 $usernameForGender =
'';
334 if ( count( $userStats ) === 1 ) {
335 $usernameForGender = $userStats[0][TranslatorActivityQuery::USER_NAME];
338 $linkList = $this->getLanguage()->listToText( $links );
339 $html =
"<p class='mw-translate-spsl-translators'>";
340 $html .= $this->msg(
'supportedlanguages-translators' )
341 ->rawParams( $linkList )
342 ->numParams( count( $links ) )
343 ->params( $usernameForGender )
346 $this->getOutput()->addHTML( $html );
349 protected function formatStyle( $styles ) {
351 foreach ( $styles as $key => $value ) {
352 $stylestr .=
"$key:$value;";
358 protected function preQueryUsers( array $users ):
void {
359 $lb =
new LinkBatch();
360 foreach ( $users as $data ) {
361 $username = $data[TranslatorActivityQuery::USER_NAME];
362 $user = Title::capitalize( $username, NS_USER );
363 $lb->add( NS_USER, $user );
364 $lb->add( NS_USER_TALK, $user );
369 protected function getColorLegend() {
371 $period = $this->period;
373 for ( $i = 0; $i <= $period; $i += 30 ) {
374 $iFormatted = htmlspecialchars( $this->getLanguage()->formatNum( $i ) );
375 $legend .=
'<span style="background-color:#' .
376 $this->progressStatsTable->getBackgroundColor( ( $period - $i ) / $period ) .
377 "\"> $iFormatted</span>";