diff options
Diffstat (limited to 'www/wiki/extensions/Translate/utils/StatsTable.php')
-rw-r--r-- | www/wiki/extensions/Translate/utils/StatsTable.php | 165 |
1 files changed, 62 insertions, 103 deletions
diff --git a/www/wiki/extensions/Translate/utils/StatsTable.php b/www/wiki/extensions/Translate/utils/StatsTable.php index 284508a9..0e9bc937 100644 --- a/www/wiki/extensions/Translate/utils/StatsTable.php +++ b/www/wiki/extensions/Translate/utils/StatsTable.php @@ -1,23 +1,17 @@ <?php /** - * Contains logic for special page Special:LanguageStats. - * * @file * @author Siebrand Mazeland * @author Niklas Laxström * @copyright Copyright © 2008-2013 Siebrand Mazeland, Niklas Laxström - * @license GPL-2.0+ + * @license GPL-2.0-or-later */ /** - * Implements includable special page Special:LanguageStats which provides - * translation statistics for all defined message groups. + * Implements generation of HTML stats table. * * Loosely based on the statistics code in phase3/maintenance/language * - * Use {{Special:LanguageStats/nl/1}} to show for 'nl' and suppres completely - * translated groups. - * * @ingroup Stats */ class StatsTable { @@ -39,7 +33,7 @@ class StatsTable { /** * @var Message[] */ - protected $extraColumns = array(); + protected $extraColumns = []; public function __construct() { $this->lang = RequestContext::getMain()->getLanguage(); @@ -49,13 +43,13 @@ class StatsTable { /** * Statistics table element (heading or regular cell) * - * @param $in \string Element contents. - * @param $bgcolor \string Backround color in ABABAB format. - * @param $sort \string Value used for sorting. - * @return \string Html td element. + * @param string $in Element contents. + * @param string $bgcolor Backround color in ABABAB format. + * @param string $sort Value used for sorting. + * @return string Html td element. */ public function element( $in, $bgcolor = '', $sort = '' ) { - $attributes = array(); + $attributes = []; if ( $sort ) { $attributes['data-sort-value'] = $sort; @@ -63,7 +57,6 @@ class StatsTable { if ( $bgcolor ) { $attributes['style'] = 'background-color: #' . $bgcolor; - $attributes['class'] = 'hover-color'; } $element = Html::element( 'td', $attributes, $in ); @@ -71,34 +64,28 @@ class StatsTable { return $element; } - public function getBackgroundColor( $subset, $total, $fuzzy = false ) { - wfSuppressWarnings(); - $v = round( 255 * $subset / $total ); - wfRestoreWarnings(); - + public function getBackgroundColor( $percentage, $fuzzy = false ) { if ( $fuzzy ) { - // Weigh fuzzy with factor 20. - $v = $v * 20; - - if ( $v > 255 ) { - $v = 255; - } - - $v = 255 - $v; + // Steeper scale for fuzzy + // (0), [0-2), [2-4), ... [12-100) + $index = min( 7, ceil( 50 * $percentage ) ); + $colors = [ + '', 'fedbd7', 'fecec8', 'fec1b9', + 'fcb5ab', 'fba89d', 'f89b8f', 'f68d81' + ]; + return $colors[ $index ]; } - if ( $v < 128 ) { - // Red to Yellow - $red = 'FF'; - $green = sprintf( '%02X', 2 * $v ); - } else { - // Yellow to Green - $red = sprintf( '%02X', 2 * ( 255 - $v ) ); - $green = 'FF'; - } - $blue = '00'; + // https://gka.github.io/palettes/#colors=#36c,#eaf3ff|steps=20|bez=1|coL=1 + // Color groups for (0-10], (10-20], ... (90-100], (100) + $index = floor( $percentage * 10 ); + $colors = [ + 'eaf3ff', 'e2ebfc', 'dae3fa', 'd2dbf7', 'c9d4f5', + 'c1ccf2', 'b8c4ef', 'b1bced', 'a8b4ea', '9fade8', + '96a6e5' + ]; - return $red . $green . $blue; + return $colors[ $index ]; } /** @@ -117,11 +104,10 @@ class StatsTable { /** * @param Message $msg - * * @return string HTML */ public function createColumnHeader( Message $msg ) { - return Html::element( 'th', array(), $msg->text() ); + return Html::element( 'th', [], $msg->text() ); } public function addExtraColumn( Message $column ) { @@ -132,12 +118,13 @@ class StatsTable { * @return Message[] */ public function getOtherColumnHeaders() { - return array_merge( array( + return array_merge( [ wfMessage( 'translate-total' ), wfMessage( 'translate-untranslated' ), wfMessage( 'translate-percentage-complete' ), + wfMessage( 'translate-percentage-proofread' ), wfMessage( 'translate-percentage-fuzzy' ), - ), $this->extraColumns ); + ], $this->extraColumns ); } /** @@ -147,7 +134,7 @@ class StatsTable { // Create table header $out = Html::openElement( 'table', - array( 'class' => 'statstable wikitable mw-sp-translate-table' ) + [ 'class' => 'statstable' ] ); $out .= "\n\t" . Html::openElement( 'thead' ); @@ -172,7 +159,7 @@ class StatsTable { */ public function makeTotalRow( Message $message, $stats ) { $out = "\t" . Html::openElement( 'tr' ); - $out .= "\n\t\t" . Html::element( 'td', array(), $message->text() ); + $out .= "\n\t\t" . Html::element( 'td', [], $message->text() ); $out .= $this->makeNumberColumns( $stats ); $out .= "\n\t" . Xml::closeElement( 'tr' ) . "\n"; @@ -188,9 +175,10 @@ class StatsTable { $total = $stats[MessageGroupStats::TOTAL]; $translated = $stats[MessageGroupStats::TRANSLATED]; $fuzzy = $stats[MessageGroupStats::FUZZY]; + $proofread = $stats[MessageGroupStats::PROOFREAD]; if ( $total === null ) { - $na = "\n\t\t" . Html::element( 'td', array( 'data-sort-value' => -1 ), '...' ); + $na = "\n\t\t" . Html::element( 'td', [ 'data-sort-value' => -1 ], '...' ); $nap = "\n\t\t" . $this->element( '...', 'AFAFAF', -1 ); $out = $na . $na . $nap . $nap; @@ -198,27 +186,33 @@ class StatsTable { } $out = "\n\t\t" . Html::element( 'td', - array( 'data-sort-value' => $total ), + [ 'data-sort-value' => $total ], $this->lang->formatNum( $total ) ); $out .= "\n\t\t" . Html::element( 'td', - array( 'data-sort-value' => $total - $translated ), + [ 'data-sort-value' => $total - $translated ], $this->lang->formatNum( $total - $translated ) ); if ( $total === 0 ) { $transRatio = 0; $fuzzyRatio = 0; + $proofRatio = 0; } else { $transRatio = $translated / $total; $fuzzyRatio = $fuzzy / $total; + $proofRatio = $translated === 0 ? 0 : $proofread / $translated; } $out .= "\n\t\t" . $this->element( $this->formatPercentage( $transRatio, 'floor' ), - $this->getBackgroundColor( $translated, $total ), + $this->getBackgroundColor( $transRatio ), sprintf( '%1.5f', $transRatio ) ); + $out .= "\n\t\t" . $this->element( $this->formatPercentage( $proofRatio, 'floor' ), + $this->getBackgroundColor( $proofRatio ), + sprintf( '%1.5f', $proofRatio ) ); + $out .= "\n\t\t" . $this->element( $this->formatPercentage( $fuzzyRatio, 'ceil' ), - $this->getBackgroundColor( $fuzzy, $total, true ), + $this->getBackgroundColor( $fuzzyRatio, true ), sprintf( '%1.5f', $fuzzyRatio ) ); return $out; @@ -226,8 +220,8 @@ class StatsTable { /** * Makes a nice print from plain float. - * @param $num float - * @param $to string floor or ceil + * @param number $num + * @param string $to floor or ceil * @return string Plain text */ public function formatPercentage( $num, $to = 'floor' ) { @@ -239,7 +233,7 @@ class StatsTable { /** * Gets the name of group with some extra formatting. - * @param $group MessageGroup + * @param MessageGroup $group * @return string Html */ public function getGroupLabel( MessageGroup $group ) { @@ -247,7 +241,7 @@ class StatsTable { // Bold for meta groups. if ( $group->isMeta() ) { - $groupLabel = Html::rawElement( 'b', array(), $groupLabel ); + $groupLabel = Html::rawElement( 'b', [], $groupLabel ); } return $groupLabel; @@ -255,20 +249,18 @@ class StatsTable { /** * Gets the name of group linked to translation tool. - * @param $group MessageGroup - * @param $code string Language code - * @param $params array Any extra query parameters. + * @param MessageGroup $group + * @param string $code Language code + * @param array $params Any extra query parameters. * @return string Html */ public function makeGroupLink( MessageGroup $group, $code, $params ) { - $queryParameters = $params + array( + $queryParameters = $params + [ 'group' => $group->getId(), 'language' => $code - ); + ]; - $attributes = array( - 'title' => $this->getGroupDescription( $group ) - ); + $attributes = []; $translateGroupLink = Linker::link( $this->translate, $this->getGroupLabel( $group ), $attributes, $queryParameters @@ -278,43 +270,10 @@ class StatsTable { } /** - * Gets the description of a group. This is a bit slow thing to do for - * thousand+ groups, so some caching is involved. - * @param $group MessageGroup - * @return string Plain text - */ - public function getGroupDescription( MessageGroup $group ) { - $code = $this->lang->getCode(); - - $cache = wfGetCache( CACHE_ANYTHING ); - $key = wfMemcKey( "translate-groupdesc-$code-" . $group->getId() ); - $desc = $cache->get( $key ); - - if ( is_string( $desc ) ) { - return $desc; - } - - $realFunction = array( 'MessageCache', 'singleton' ); - - if ( is_callable( $realFunction ) ) { - $mc = MessageCache::singleton(); - } else { - global $wgMessageCache; - - $mc = $wgMessageCache; - } - - $desc = $mc->transform( $group->getDescription(), true, $this->lang ); - $cache->set( $key, $desc ); - - return $desc; - } - - /** * Check whether translations in given group in given language * has been disabled. - * @param $groupId string Message group id - * @param $code string Language code + * @param string $groupId Message group id + * @param string $code Language code * @return bool */ public function isBlacklisted( $groupId, $code ) { @@ -322,11 +281,11 @@ class StatsTable { $blacklisted = null; - $checks = array( + $checks = [ $groupId, strtok( $groupId, '-' ), '*' - ); + ]; foreach ( $checks as $check ) { if ( isset( $wgTranslateBlacklist[$check] ) && isset( $wgTranslateBlacklist[$check][$code] ) ) { @@ -344,7 +303,7 @@ class StatsTable { $blacklisted = true; } - $include = Hooks::run( 'Translate:MessageGroupStats:isIncluded', array( $groupId, $code ) ); + $include = Hooks::run( 'Translate:MessageGroupStats:isIncluded', [ $groupId, $code ] ); if ( !$include ) { $blacklisted = true; } @@ -355,17 +314,17 @@ class StatsTable { /** * Used to circumvent ugly tooltips when newlines are used in the * message content ("x\ny" becomes "x y"). - * @param $text + * @param string $text * @return string */ public static function formatTooltip( $text ) { $wordSeparator = wfMessage( 'word-separator' )->text(); - $text = strtr( $text, array( + $text = strtr( $text, [ "\n" => $wordSeparator, "\r" => $wordSeparator, "\t" => $wordSeparator, - ) ); + ] ); return $text; } |