r44058 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44057‎ | r44058 | r44059 >
Date:08:07, 30 November 2008
Author:ialex
Status:ok
Tags:
Comment:
svn:eol-style native
Modified paths:
  • /trunk/extensions/Translate/SpecialLanguageStats.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/SpecialLanguageStats.php
@@ -1,212 +1,212 @@
2 -<?php
3 -if ( !defined( 'MEDIAWIKI' ) ) die();
4 -/**
5 - * Implements a special page which givens translation statistics for a given set
6 - * of message groups. Message group names can be entered (pipe separated) into
7 - * the form, or added as a parameter in the URL.
8 - *
9 - * Some of the code is based on the statistics code in phase3/maintenance/language
10 - *
11 - * @author Siebrand Mazeland
12 - * @copyright Copyright © 2008 Siebrand Mazeland
13 - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
14 - */
15 -
16 -#class SpecialLanguageStats extends IncludableSpecialPage {
17 -class SpecialLanguageStats extends UnlistedSpecialPage {
18 - function __construct() {
19 - parent::__construct( 'LanguageStats' );
20 - }
21 -
22 - function execute( $par ) {
23 - global $wgRequest, $wgOut;
24 -
25 - wfLoadExtensionMessages( 'Translate' );
26 -
27 - $this->setHeaders();
28 - $this->outputHeader();
29 -
30 - if( !$this->including() ) {
31 - $wgOut->addHTML( $this->languageForm( $par ) );
32 - }
33 -
34 - $out = '';
35 -
36 - #if( $this->including() ) {
37 -# if( isset( Language::getLanguageNames( true ) ) ) {
38 - if( 1 ) {
39 - $out .= $this->getGroupStats( $par );
40 - } else {
41 - $wgOut->addWikiMsg( 'translate-page-no-such-language' );
42 - }
43 - #}
44 - $wgOut->addHTML( $out );
45 - }
46 -
47 - /**
48 - * HTML for the top form
49 - * @param integer $code A language code (default empty, example: 'en').
50 - */
51 - function languageForm( $code = '' ) {
52 - global $wgScript;
53 - $t = $this->getTitle();
54 -
55 - $out = Xml::openElement( 'div', array( 'class' => 'languagecode' ) );
56 - $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
57 - $out .= Xml::hidden( 'title', $t->getPrefixedText() );
58 - $out .= Xml::openElement( 'fieldset' );
59 - $out .= Xml::element( 'legend', null, wfMsg( 'translate-language-code' ) );
60 - $out .= Xml::openElement( 'table', array( 'id' => 'langcodeselect', 'class' => 'allpages' ) );
61 - $out .= "<tr>
62 - <td class='mw-label'>" .
63 - Xml::label( wfMsg( 'translate-language-code-field-name' ), 'code' ) .
64 - "</td>
65 - <td class='mw-input'>" .
66 - Xml::input( 'code', 30, str_replace('_',' ',$code), array( 'id' => 'code' ) ) .
67 - "</td>
68 - <td class='mw-input'>" .
69 - Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
70 - "</td>
71 - </tr>";
72 - $out .= Xml::closeElement( 'table' );
73 - $out .= Xml::closeElement( 'fieldset' );
74 - $out .= Xml::closeElement( 'form' );
75 - $out .= Xml::closeElement( 'div' );
76 - return $out;
77 - }
78 -
79 - # Statistics table heading
80 - function heading() {
81 - return '<table class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear:both;" width="100%">' . "\n";
82 - }
83 -
84 - # Statistics table footer
85 - function footer() {
86 - return "</table>\n";
87 - }
88 -
89 - # Statistics table row start
90 - function blockstart() {
91 - return "\t<tr>\n";
92 - }
93 -
94 - # Statistics table row end
95 - function blockend() {
96 - return "\t</tr>\n";
97 - }
98 -
99 - # Statistics table element (heading or regular cell)
100 - function element( $in, $heading = false, $bgcolor = '' ) {
101 - if( $heading ) {
102 - $element = '<th>' . $in . '</th>';
103 - } else if ( $bgcolor ) {
104 - $element = '<td bgcolor="#' . $bgcolor . '">' . $in . '</td>';
105 - } else {
106 - $element = '<td>' . $in . '</td>';
107 - }
108 - return "\t\t" . $element . "\n";
109 - }
110 -
111 - function getGroups() {
112 - $groups = wfMsgForContent( 'translate-languagestats-groups' );
113 -
114 - if( $groups ) {
115 - // Make the group names clean
116 - // Should contain one valid group name per line
117 - // All invalid group names should be ignored
118 - // Return all group names if there are no valid group names at all
119 - // FIXME: implement the above here
120 - $cleanGroups = '';
121 -
122 - if( $cleanGroups ) {
123 - return $cleanGroups;
124 - }
125 - }
126 -
127 - return MessageGroups::singleton()->getGroups();
128 - }
129 -
130 - function getBackgroundColour( $subset, $total, $fuzzy = false ) {
131 - $v = @round(255 * $subset / $total);
132 -
133 - if ( $fuzzy ) {
134 - # weight fuzzy with factor 20
135 - $v = $v * 20;
136 - if( $v > 255 ) $v = 255;
137 - $v = 255 - $v;
138 - }
139 -
140 - if ( $v < 128 ) {
141 - # Red to Yellow
142 - $red = 'FF';
143 - $green = sprintf( '%02X', 2 * $v );
144 - } else {
145 - # Yellow to Green
146 - $red = sprintf('%02X', 2 * ( 255 - $v ) );
147 - $green = 'FF';
148 - }
149 - $blue = '00';
150 -
151 - return $red . $green . $blue;
152 - }
153 -
154 - // copied and adaped from groupStatistics.php by Nikerabbit
155 - function getGroupStats( $code ) {
156 - global $wgUser;
157 -
158 - $out = '';
159 -
160 - $out .= '<!-- ' . $code . " -->\n";
161 - $out .= '<!-- ' . TranslateUtils::getLanguageName( $code, false ) . " -->\n";
162 -
163 - # Create header
164 - $out .= $this->heading();
165 - $out .= $this->blockstart();
166 - $out .= $this->element( wfMsg( 'translate-page-group', true ) );
167 - $out .= $this->element( wfMsg( 'translate-percentage-complete', true ) );
168 - $out .= $this->element( wfMsg( 'translate-percentage-fuzzy', true ) );
169 - $out .= $this->blockend();
170 -
171 - # fetch groups stats have to be displayed for
172 - $groups = $this->getGroups();
173 -
174 - # Get statistics for the message groups
175 - foreach ( $groups as $g ) {
176 - $out .= $this->blockstart();
177 -
178 - $translateTitle = SpecialPage::getTitleFor( 'Translate' );
179 - $pageParameters = "group=" . $g->getId() . "&language=" . $code;
180 - $translateGroupLink = $wgUser->getSkin()->makeKnownLinkObj( $translateTitle, $g->getLabel(), $pageParameters );
181 -
182 - $out .= $this->element( $translateGroupLink );
183 -
184 - // Initialise messages
185 - $collection = $g->initCollection( $code );
186 - $collection->filter( 'optional' );
187 - // Store the count of real messages for later calculation.
188 - $total = count( $collection );
189 -
190 - // Fill translations in for counting
191 - $g->fillCollection( $collection );
192 -
193 - // Count fuzzy first
194 - $collection->filter( 'fuzzy' );
195 - $fuzzy = $total - count( $collection );
196 -
197 - // Count the completion percent
198 - $collection->filter( 'translated', false );
199 - $translated = count( $collection );
200 -
201 - $translatedPercentage = @sprintf( '%.2f%%', 100 * $translated / $total );
202 - $fuzzyPercentage = @sprintf( '%.2f%%', 100 * $fuzzy / $total );
203 -
204 - $out .= $this->element( $translatedPercentage, false, $this->getBackgroundColour( $translated, $total ) );
205 - $out .= $this->element( $fuzzyPercentage, false, $this->getBackgroundColour( $fuzzy, $total, true ) );
206 - $out .= $this->blockend();
207 - }
208 -
209 - $out .= $this->footer();
210 -
211 - return $out;
212 - }
213 -}
 2+<?php
 3+if ( !defined( 'MEDIAWIKI' ) ) die();
 4+/**
 5+ * Implements a special page which givens translation statistics for a given set
 6+ * of message groups. Message group names can be entered (pipe separated) into
 7+ * the form, or added as a parameter in the URL.
 8+ *
 9+ * Some of the code is based on the statistics code in phase3/maintenance/language
 10+ *
 11+ * @author Siebrand Mazeland
 12+ * @copyright Copyright © 2008 Siebrand Mazeland
 13+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 14+ */
 15+
 16+#class SpecialLanguageStats extends IncludableSpecialPage {
 17+class SpecialLanguageStats extends UnlistedSpecialPage {
 18+ function __construct() {
 19+ parent::__construct( 'LanguageStats' );
 20+ }
 21+
 22+ function execute( $par ) {
 23+ global $wgRequest, $wgOut;
 24+
 25+ wfLoadExtensionMessages( 'Translate' );
 26+
 27+ $this->setHeaders();
 28+ $this->outputHeader();
 29+
 30+ if( !$this->including() ) {
 31+ $wgOut->addHTML( $this->languageForm( $par ) );
 32+ }
 33+
 34+ $out = '';
 35+
 36+ #if( $this->including() ) {
 37+# if( isset( Language::getLanguageNames( true ) ) ) {
 38+ if( 1 ) {
 39+ $out .= $this->getGroupStats( $par );
 40+ } else {
 41+ $wgOut->addWikiMsg( 'translate-page-no-such-language' );
 42+ }
 43+ #}
 44+ $wgOut->addHTML( $out );
 45+ }
 46+
 47+ /**
 48+ * HTML for the top form
 49+ * @param integer $code A language code (default empty, example: 'en').
 50+ */
 51+ function languageForm( $code = '' ) {
 52+ global $wgScript;
 53+ $t = $this->getTitle();
 54+
 55+ $out = Xml::openElement( 'div', array( 'class' => 'languagecode' ) );
 56+ $out .= Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
 57+ $out .= Xml::hidden( 'title', $t->getPrefixedText() );
 58+ $out .= Xml::openElement( 'fieldset' );
 59+ $out .= Xml::element( 'legend', null, wfMsg( 'translate-language-code' ) );
 60+ $out .= Xml::openElement( 'table', array( 'id' => 'langcodeselect', 'class' => 'allpages' ) );
 61+ $out .= "<tr>
 62+ <td class='mw-label'>" .
 63+ Xml::label( wfMsg( 'translate-language-code-field-name' ), 'code' ) .
 64+ "</td>
 65+ <td class='mw-input'>" .
 66+ Xml::input( 'code', 30, str_replace('_',' ',$code), array( 'id' => 'code' ) ) .
 67+ "</td>
 68+ <td class='mw-input'>" .
 69+ Xml::submitButton( wfMsg( 'allpagessubmit' ) ) .
 70+ "</td>
 71+ </tr>";
 72+ $out .= Xml::closeElement( 'table' );
 73+ $out .= Xml::closeElement( 'fieldset' );
 74+ $out .= Xml::closeElement( 'form' );
 75+ $out .= Xml::closeElement( 'div' );
 76+ return $out;
 77+ }
 78+
 79+ # Statistics table heading
 80+ function heading() {
 81+ return '<table class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear:both;" width="100%">' . "\n";
 82+ }
 83+
 84+ # Statistics table footer
 85+ function footer() {
 86+ return "</table>\n";
 87+ }
 88+
 89+ # Statistics table row start
 90+ function blockstart() {
 91+ return "\t<tr>\n";
 92+ }
 93+
 94+ # Statistics table row end
 95+ function blockend() {
 96+ return "\t</tr>\n";
 97+ }
 98+
 99+ # Statistics table element (heading or regular cell)
 100+ function element( $in, $heading = false, $bgcolor = '' ) {
 101+ if( $heading ) {
 102+ $element = '<th>' . $in . '</th>';
 103+ } else if ( $bgcolor ) {
 104+ $element = '<td bgcolor="#' . $bgcolor . '">' . $in . '</td>';
 105+ } else {
 106+ $element = '<td>' . $in . '</td>';
 107+ }
 108+ return "\t\t" . $element . "\n";
 109+ }
 110+
 111+ function getGroups() {
 112+ $groups = wfMsgForContent( 'translate-languagestats-groups' );
 113+
 114+ if( $groups ) {
 115+ // Make the group names clean
 116+ // Should contain one valid group name per line
 117+ // All invalid group names should be ignored
 118+ // Return all group names if there are no valid group names at all
 119+ // FIXME: implement the above here
 120+ $cleanGroups = '';
 121+
 122+ if( $cleanGroups ) {
 123+ return $cleanGroups;
 124+ }
 125+ }
 126+
 127+ return MessageGroups::singleton()->getGroups();
 128+ }
 129+
 130+ function getBackgroundColour( $subset, $total, $fuzzy = false ) {
 131+ $v = @round(255 * $subset / $total);
 132+
 133+ if ( $fuzzy ) {
 134+ # weight fuzzy with factor 20
 135+ $v = $v * 20;
 136+ if( $v > 255 ) $v = 255;
 137+ $v = 255 - $v;
 138+ }
 139+
 140+ if ( $v < 128 ) {
 141+ # Red to Yellow
 142+ $red = 'FF';
 143+ $green = sprintf( '%02X', 2 * $v );
 144+ } else {
 145+ # Yellow to Green
 146+ $red = sprintf('%02X', 2 * ( 255 - $v ) );
 147+ $green = 'FF';
 148+ }
 149+ $blue = '00';
 150+
 151+ return $red . $green . $blue;
 152+ }
 153+
 154+ // copied and adaped from groupStatistics.php by Nikerabbit
 155+ function getGroupStats( $code ) {
 156+ global $wgUser;
 157+
 158+ $out = '';
 159+
 160+ $out .= '<!-- ' . $code . " -->\n";
 161+ $out .= '<!-- ' . TranslateUtils::getLanguageName( $code, false ) . " -->\n";
 162+
 163+ # Create header
 164+ $out .= $this->heading();
 165+ $out .= $this->blockstart();
 166+ $out .= $this->element( wfMsg( 'translate-page-group', true ) );
 167+ $out .= $this->element( wfMsg( 'translate-percentage-complete', true ) );
 168+ $out .= $this->element( wfMsg( 'translate-percentage-fuzzy', true ) );
 169+ $out .= $this->blockend();
 170+
 171+ # fetch groups stats have to be displayed for
 172+ $groups = $this->getGroups();
 173+
 174+ # Get statistics for the message groups
 175+ foreach ( $groups as $g ) {
 176+ $out .= $this->blockstart();
 177+
 178+ $translateTitle = SpecialPage::getTitleFor( 'Translate' );
 179+ $pageParameters = "group=" . $g->getId() . "&language=" . $code;
 180+ $translateGroupLink = $wgUser->getSkin()->makeKnownLinkObj( $translateTitle, $g->getLabel(), $pageParameters );
 181+
 182+ $out .= $this->element( $translateGroupLink );
 183+
 184+ // Initialise messages
 185+ $collection = $g->initCollection( $code );
 186+ $collection->filter( 'optional' );
 187+ // Store the count of real messages for later calculation.
 188+ $total = count( $collection );
 189+
 190+ // Fill translations in for counting
 191+ $g->fillCollection( $collection );
 192+
 193+ // Count fuzzy first
 194+ $collection->filter( 'fuzzy' );
 195+ $fuzzy = $total - count( $collection );
 196+
 197+ // Count the completion percent
 198+ $collection->filter( 'translated', false );
 199+ $translated = count( $collection );
 200+
 201+ $translatedPercentage = @sprintf( '%.2f%%', 100 * $translated / $total );
 202+ $fuzzyPercentage = @sprintf( '%.2f%%', 100 * $fuzzy / $total );
 203+
 204+ $out .= $this->element( $translatedPercentage, false, $this->getBackgroundColour( $translated, $total ) );
 205+ $out .= $this->element( $fuzzyPercentage, false, $this->getBackgroundColour( $fuzzy, $total, true ) );
 206+ $out .= $this->blockend();
 207+ }
 208+
 209+ $out .= $this->footer();
 210+
 211+ return $out;
 212+ }
 213+}
Property changes on: trunk/extensions/Translate/SpecialLanguageStats.php
___________________________________________________________________
Added: svn:eol-style
214214 + native

Status & tagging log