msg( 'supportedlanguages' )->text(); } public function execute( $par ) { $out = $this->getOutput(); $lang = $this->getLanguage(); // Only for manual debugging nowdays $this->purge = false; $this->setHeaders(); $out->addModules( 'ext.translate.special.supportedlanguages' ); $out->addModuleStyles( 'ext.translate.special.supportedlanguages' ); $out->addHelpLink( 'Help:Extension:Translate/Statistics_and_reporting#List_of_languages_and_translators' ); $this->outputHeader( 'supportedlanguages-summary' ); $dbr = wfGetDB( DB_REPLICA ); if ( $dbr->getType() === 'sqlite' ) { $out->wrapWikiMsg( '
' . $linkList . "
\n" ); $this->makeUserList( $users, $userStats ); } protected function languageCloud() { global $wgTranslateMessageNamespaces; $cache = wfGetCache( CACHE_ANYTHING ); $cachekey = wfMemcKey( 'translate-supportedlanguages-language-cloud' ); if ( $this->purge ) { $cache->delete( $cachekey ); } else { $data = $cache->get( $cachekey ); if ( is_array( $data ) ) { return $data; } } $dbr = wfGetDB( DB_REPLICA ); $tables = [ 'recentchanges' ]; $fields = [ 'substring_index(rc_title, \'/\', -1) as lang', 'count(*) as count' ]; $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - 60 * 60 * 24 * $this->period ); $conds = [ # Without the quotes the rc_timestamp index isn't used and this query is much slower "rc_timestamp > '$timestamp'", 'rc_namespace' => $wgTranslateMessageNamespaces, 'rc_title' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() ), ]; $options = [ 'GROUP BY' => 'lang', 'HAVING' => 'count > 20', 'ORDER BY' => 'NULL' ]; $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options ); $data = []; foreach ( $res as $row ) { $data[$row->lang] = $row->count; } $cache->set( $cachekey, $data, 3600 ); return $data; } /** * Fetch the translators for a language with caching * * @param string $code * @return array|bool Map of (user name => page count) or false on failure */ public function fetchTranslators( $code ) { $cache = wfGetCache( CACHE_ANYTHING ); $cachekey = wfMemcKey( 'translate-supportedlanguages-translator-list-v1', $code ); if ( $this->purge ) { $cache->delete( $cachekey ); $data = false; } else { $staleCutoffUnix = time() - 3600; $data = $cache->get( $cachekey ); if ( is_array( $data ) && $data['asOfTime'] > $staleCutoffUnix ) { return $data['users']; } } $that = $this; $work = new PoolCounterWorkViaCallback( 'TranslateFetchTranslators', "TranslateFetchTranslators-$code", [ 'doWork' => function () use ( $that, $code, $cache, $cachekey ) { $users = $that->loadTranslators( $code ); $newData = [ 'users' => $users, 'asOfTime' => time() ]; $cache->set( $cachekey, $newData, 86400 ); return $users; }, 'doCachedWork' => function () use ( $cache, $cachekey ) { $newData = $cache->get( $cachekey ); // Use new cache value from other thread return is_array( $newData ) ? $newData['users'] : false; }, 'fallback' => function () use ( $data ) { // Use stale cache if possible return is_array( $data ) ? $data['users'] : false; } ] ); return $work->execute(); } /** * Fetch the translators for a language * * @param string $code * @return array Map of (user name => page count) */ public function loadTranslators( $code ) { global $wgTranslateMessageNamespaces; $dbr = wfGetDB( DB_REPLICA, 'vslow' ); if ( class_exists( ActorMigration::class ) ) { $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' ); } else { $actorQuery = [ 'tables' => [], 'fields' => [ 'rev_user_text' => 'rev_user_text' ], 'joins' => [], ]; } $tables = [ 'page', 'revision' ] + $actorQuery['tables']; $fields = [ 'rev_user_text' => $actorQuery['fields']['rev_user_text'], 'count(page_id) as count' ]; $conds = [ 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code ), 'page_namespace' => $wgTranslateMessageNamespaces, ]; $options = [ 'GROUP BY' => $actorQuery['fields']['rev_user_text'], 'ORDER BY' => 'NULL' ]; $joins = [ 'revision' => [ 'JOIN', 'page_id=rev_page' ], ] + $actorQuery['joins']; $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $joins ); $data = []; foreach ( $res as $row ) { $data[$row->rev_user_text] = $row->count; } return $data; } protected function filterUsers( array $users, $code, $blacklist ) { foreach ( array_keys( $users ) as $username ) { # We do not know the group $hash = "#;$code;$username"; $blacklisted = false; foreach ( $blacklist as $rule ) { list( $type, $regex ) = $rule; if ( preg_match( $regex, $hash ) ) { if ( $type === 'white' ) { $blacklisted = false; break; } else { $blacklisted = true; } } } if ( $blacklisted ) { unset( $users[$username] ); } } return $users; } protected function outputLanguageCloud( array $languages, array $names ) { $out = $this->getOutput(); $out->addHTML( '"; $html .= $this->msg( 'supportedlanguages-translators' ) ->rawParams( $linkList ) ->numParams( count( $links ) ) ->params( $username ) ->escaped(); $html .= "
\n"; $this->getOutput()->addHTML( $html ); } protected function getUserStats( $users ) { $cache = wfGetCache( CACHE_ANYTHING ); $dbr = wfGetDB( DB_REPLICA ); $keys = []; foreach ( $users as $username ) { $keys[] = wfMemcKey( 'translate', 'sl-usertats', $username ); } $cached = $cache->getMulti( $keys ); $data = []; foreach ( $users as $index => $username ) { $cachekey = $keys[$index]; if ( !$this->purge && isset( $cached[$cachekey] ) ) { $data[$username] = $cached[$cachekey]; continue; } if ( class_exists( ActorMigration::class ) ) { $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' ); $tables = [ 'user', 'r' => [ 'revision' ] + $actorQuery['tables'] ]; $joins = [ 'r' => [ 'JOIN', 'user_id = rev_user' ], ] + $actorQuery['joins']; } else { $tables = [ 'user', 'revision' ]; $joins = [ 'revision' => [ 'JOIN', 'user_id = rev_user' ] ]; } $fields = [ 'user_name', 'user_editcount', 'MAX(rev_timestamp) as lastedit' ]; $conds = [ 'user_name' => $username, ]; $res = $dbr->selectRow( $tables, $fields, $conds, __METHOD__, [], $joins ); $data[$username] = [ $res->user_editcount, $res->lastedit ]; $cache->set( $cachekey, $data[$username], 3600 ); } return $data; } protected function formatStyle( $styles ) { $stylestr = ''; foreach ( $styles as $key => $value ) { $stylestr .= "$key:$value;"; } return $stylestr; } protected function preQueryUsers( $users ) { $lb = new LinkBatch; foreach ( $users as $user => $count ) { $user = Title::capitalize( $user, NS_USER ); $lb->add( NS_USER, $user ); $lb->add( NS_USER_TALK, $user ); } $lb->execute(); } protected function getColorLegend() { $legend = ''; $period = $this->period; $statsTable = new StatsTable(); for ( $i = 0; $i <= $period; $i += 30 ) { $iFormatted = htmlspecialchars( $this->getLanguage()->formatNum( $i ) ); $legend .= ' $iFormatted"; } return $legend; } }