r36811 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r36810‎ | r36811 | r36812 >
Date:10:19, 30 June 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* Bugfixes
Modified paths:
  • /trunk/extensions/Translate/MessageChecks.php (modified) (history)
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateTasks.php (modified) (history)
  • /trunk/extensions/Translate/scripts/createCheckIndex.php (modified) (history)
  • /trunk/extensions/Translate/scripts/createMessageIndex.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -169,9 +169,16 @@
170170 );
171171 }
172172
 173+ // Can be either NULL or '', ARGH!
 174+ if ( $object->textbox1 === '' ) {
 175+ $editField = null;
 176+ } else {
 177+ $editField = $object->textbox1;
 178+ }
 179+
173180 if ( $xx !== null && $code !== 'en' ) {
174181 // Append translation from the file to edit area, if it's empty.
175 - if ($object->firsttime && $object->textbox1 === '' ) {
 182+ if ($object->firsttime && $editField === null ) {
176183 $object->textbox1 = $xx;
177184 }
178185 }
@@ -184,11 +191,12 @@
185192
186193
187194 // Some syntactic checks
188 - $translation = ($object->textbox1 !== '') ? $object->textbox1 : $xx;
 195+ $translation = ($editField !== null ) ? $editField : $xx;
189196 if ( $translation !== null ) {
190197 $message = new TMessage( $key, $en );
 198+ // Take the contents from edit field as a translation
191199 $message->database = $translation;
192 - $checks = MessageChecks::doChecks( $message, $group->getType() );
 200+ $checks = MessageChecks::doChecks( $message, $group->getType(), $code );
193201 if ( count($checks) ) {
194202 $checkMessages = array();
195203 foreach ( $checks as $checkParams ) {
Index: trunk/extensions/Translate/Translate.php
@@ -46,8 +46,8 @@
4747 $wgAvailableRights[] = 'translate';
4848
4949 define( 'TRANSLATE_FUZZY', '!!FUZZY!!' );
50 -define( 'TRANSLATE_INDEXFILE', $dir . 'messageindex.ser' );
51 -define( 'TRANSLATE_CHECKFILE', $dir . 'messagecheck.ser' );
 50+define( 'TRANSLATE_INDEXFILE', $dir . 'data/messageindex.ser' );
 51+define( 'TRANSLATE_CHECKFILE', $dir . 'data/messagecheck.ser' );
5252 define( 'TRANSLATE_ALIASFILE', $dir . 'aliases.txt' );
5353
5454 #
Index: trunk/extensions/Translate/scripts/createMessageIndex.php
@@ -33,6 +33,7 @@
3434 checkAndAdd( $g, true );
3535 }
3636
 37+wfMkdirParents( dirname(TRANSLATE_INDEXFILE) );
3738 file_put_contents( TRANSLATE_INDEXFILE, serialize( $hugearray ) );
3839
3940 function checkAndAdd( $g, $ignore = false ) {
Index: trunk/extensions/Translate/scripts/createCheckIndex.php
@@ -9,6 +9,7 @@
1010 * @file
1111 */
1212
 13+$optionsWithArgs = array('groups' );
1314 require( dirname(__FILE__) . '/cli.inc' );
1415
1516 $codes = Language::getLanguageNames( false );
@@ -20,15 +21,23 @@
2122 $codes = array_keys( $codes );
2223 sort( $codes );
2324
 25+if ( isset($options['groups'] ) ) {
 26+ $reqGroups = array_map( 'trim', explode( ',', $options['groups'] ) );
 27+} else {
 28+ $reqGroups = false;
 29+}
2430
25 -$supportedTypes = array('mediawiki');
26 -$problematic = array();
 31+$groups = MessageGroups::singleton()->getGroups();
2732
28 -$groups = MessageGroups::singleton()->getGroups();
2933 foreach ( $groups as $g ) {
 34+ $id = $g->getId();
 35+
 36+ // Skip groups that are not requested
 37+ if ( $reqGroups && !in_array($id, $reqGroups) ) continue;
 38+
 39+ $problematic = array();
3040 $type = $g->getType();
3141
32 - $id = $g->getId();
3342 STDOUT( "Working with $id: ", true );
3443
3544 foreach ( $codes as $code ) {
@@ -39,9 +48,8 @@
4049 $g->fillCollection( $collection );
4150 $namespace = $g->namespaces[0];
4251
43 - // Remove untranslated messages from the list
4452 foreach ( $collection->keys() as $key ) {
45 - $prob = MessageChecks::doFastChecks( $collection[$key], $type );
 53+ $prob = MessageChecks::doFastChecks( $collection[$key], $type, $code );
4654 if ( $prob ) {
4755 // Print it
4856 $nsText = $wgContLang->getNsText( $namespace );
@@ -54,7 +62,9 @@
5563 }
5664
5765 }
 66+
 67+ // Store the results
 68+ $file = TRANSLATE_CHECKFILE . "-$id";
 69+ wfMkdirParents( dirname($file) );
 70+ file_put_contents( $file, serialize( $problematic ) );
5871 }
59 -
60 -// Store the results
61 -file_put_contents( TRANSLATE_CHECKFILE, serialize( $problematic ) );
Index: trunk/extensions/Translate/TranslateTasks.php
@@ -182,7 +182,7 @@
183183
184184 }
185185
186 -class ViewProblematicTask extends ViewMessagesTask {
 186+class ViewProblematicTask extends ReviewMessagesTask {
187187 protected $id = 'problematic';
188188
189189 protected function setProcess() {
@@ -195,14 +195,15 @@
196196 }
197197
198198 protected function filterNonProblematic() {
199 - // Should never happen, DO WHAT I MEAN
200 - if ( !file_exists(TRANSLATE_CHECKFILE) ) {
 199+ $id = $this->group->getId();
 200+ $file = TRANSLATE_CHECKFILE . "-$id";
 201+ if ( !file_exists($file) ) {
201202 foreach ($this->collection->keys() as $key )
202203 unset( $this->collection[$key] );
203204 return;
204205 }
205206
206 - $problematic = unserialize( file_get_contents( TRANSLATE_CHECKFILE ) );
 207+ $problematic = unserialize( file_get_contents($file) );
207208
208209 $code = $this->options->getLanguage();
209210 if ( isset($problematic[$code]) ) {
@@ -438,11 +439,6 @@
439440 public static function getTasks() {
440441 global $wgTranslateTasks;
441442
442 - // Hide problematic task from the list if there is no check file.
443 - if ( !file_exists(TRANSLATE_CHECKFILE) ) {
444 - unset($wgTranslateTasks['problematic']);
445 - }
446 -
447443 return array_keys( $wgTranslateTasks );
448444 }
449445
Index: trunk/extensions/Translate/MessageChecks.php
@@ -10,36 +10,55 @@
1111 */
1212 class MessageChecks {
1313
14 - // Fastest first
15 - static $checksForType = array(
16 - 'mediawiki' => array(
17 - array( __CLASS__, 'checkPlural' ),
18 - array( __CLASS__, 'checkParameters' ),
19 - array( __CLASS__, 'checkUnknownParameters' ),
20 - array( __CLASS__, 'checkBalance' ),
21 - array( __CLASS__, 'checkLinks' ),
22 - array( __CLASS__, 'checkXHTML' ),
23 - ),
24 - 'freecol' => array(
25 - array( __CLASS__, 'checkFreeColMissingVars' ),
26 - array( __CLASS__, 'checkFreeColExtraVars' ),
27 - ),
28 - );
 14+ private function __construct() {
 15+ $file = dirname(__FILE__) . '/check-blacklist.php';
 16+ $this->blacklist =
 17+ ResourceLoader::loadVariableFromPHPFile( $file, 'checkBlacklist' );
2918
 19+ // Fastest first
 20+ $this->checksForType = array(
 21+ 'mediawiki' => array(
 22+ array( $this, 'checkPlural' ),
 23+ array( $this, 'checkParameters' ),
 24+ array( $this, 'checkUnknownParameters' ),
 25+ array( $this, 'checkBalance' ),
 26+ array( $this, 'checkLinks' ),
 27+ array( $this, 'checkXHTML' ),
 28+ ),
 29+ 'freecol' => array(
 30+ array( $this, 'checkFreeColMissingVars' ),
 31+ array( $this, 'checkFreeColExtraVars' ),
 32+ ),
 33+ );
 34+ }
 35+
 36+ public static function getInstance() {
 37+ static $obj = null;
 38+ if ( $obj === null ) {
 39+ $obj = new MessageChecks;
 40+ }
 41+ return $obj;
 42+ }
 43+
 44+ public function getChecks( $type ) {
 45+ if ( !isset($this->checksForType[$type]) ) return array();
 46+ return $this->checksForType[$type];
 47+ }
 48+
3049 /**
3150 * Entry point which runs all checks.
3251 *
3352 * @param $message Instance of TMessage.
3453 * @return Array of warning messages, html-format.
3554 */
36 - public static function doChecks( TMessage $message, $type ) {
 55+ public static function doChecks( TMessage $message, $type, $code ) {
3756 if ( $message->translation === null) return false;
38 - if ( !isset(self::$checksForType[$type])) return false;
 57+ $obj = new MessageChecks;
3958 $warnings = array();
4059
41 - foreach ( self::$checksForType[$type] as $check ) {
 60+ foreach ( $obj->getChecks( $type ) as $check ) {
4261 $warning = '';
43 - if ( call_user_func( $check, $message, &$warning ) ) {
 62+ if ( call_user_func( $check, $message, $code, &$warning ) ) {
4463 $warnings[] = $warning;
4564 }
4665 }
@@ -47,12 +66,12 @@
4867 return $warnings;
4968 }
5069
51 - public static function doFastChecks( TMessage $message, $type ) {
 70+ public static function doFastChecks( TMessage $message, $type, $code ) {
5271 if ( $message->translation === null) return false;
53 - if ( !isset(self::$checksForType[$type])) return false;
5472
55 - foreach ( self::$checksForType[$type] as $check ) {
56 - if ( call_user_func( $check, $message ) ) return true;
 73+ $obj = new MessageChecks;
 74+ foreach ( $obj->getChecks( $type ) as $check ) {
 75+ if ( call_user_func( $check, $message, $code ) ) return true;
5776 }
5877
5978 return false;
@@ -65,7 +84,7 @@
6685 * @param $message Instance of TMessage.
6786 * @return Array of unused parameters.
6887 */
69 - protected static function checkParameters( TMessage $message, &$desc = null ) {
 88+ protected function checkParameters( TMessage $message, $code, &$desc = null ) {
7089 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
7190
7291 $missing = array();
@@ -91,13 +110,13 @@
92111 return false;
93112 }
94113
95 - protected static function checkUnknownParameters( TMessage $message, &$desc = null ) {
 114+ protected function checkUnknownParameters( TMessage $message, $code, &$desc = null ) {
96115 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
97116
98117 $missing = array();
99118 $definition = $message->definition;
100119 $translation= $message->translation;
101 - if ( strpos( $definition, '$' ) === false ) return false;
 120+ if ( strpos( $translation, '$' ) === false ) return false;
102121
103122 for ( $i = 1; $i < 10; $i++ ) {
104123 $pattern = '/\$' . $i . '/s';
@@ -125,7 +144,7 @@
126145 * @return Array of unbalanced paranthesis pairs with difference of opening
127146 * and closing count as value.
128147 */
129 - protected static function checkBalance( TMessage $message, &$desc = null ) {
 148+ protected function checkBalance( TMessage $message, $code, &$desc = null ) {
130149 $translation = preg_replace( '/[^{}[\]()]/u', '', $message->translation );
131150 $counts = array( '{' => 0, '}' => 0, '[' => 0, ']' => 0, '(' => 0, ')' => 0 );
132151
@@ -162,7 +181,7 @@
163182 * @param $message Instance of TMessage.
164183 * @return Array of problematic links.
165184 */
166 - protected static function checkLinks( TMessage $message, &$desc = null ) {
 185+ protected function checkLinks( TMessage $message, $code, &$desc = null ) {
167186 $translation = $message->translation;
168187 if ( strpos( $translation, '[[' ) === false ) return false;
169188
@@ -197,7 +216,7 @@
198217 * @return Array of tags in invalid syntax with correction suggestions as
199218 * value.
200219 */
201 - protected static function checkXHTML( TMessage $message, &$desc = null ) {
 220+ protected function checkXHTML( TMessage $message, $code, &$desc = null ) {
202221 $translation = $message->translation;
203222 if ( strpos( $translation, '<' ) === false ) return false;
204223
@@ -236,7 +255,11 @@
237256 * @param $message Instance of TMessage.
238257 * @return True if plural magic word is missing.
239258 */
240 - protected static function checkPlural( TMessage $message, &$desc = null ) {
 259+ protected function checkPlural( TMessage $message, $code, &$desc = null ) {
 260+ if ( isset($this->blacklist[$code])
 261+ && in_array( 'plural', $this->blacklist[$code] ) )
 262+ return false;
 263+
241264 $definition = $message->definition;
242265 $translation = $message->translation;
243266 if ( stripos( $definition, '{{plural:' ) !== false &&
@@ -249,11 +272,11 @@
250273 }
251274
252275
253 - protected static function checkFreeColMissingVars( TMessage $message, &$desc = null ) {
254 - if ( !preg_match_all( '/%[^%]%/U', $message->definition, $defVars ) ) {
 276+ protected function checkFreeColMissingVars( TMessage $message, $code, &$desc = null ) {
 277+ if ( !preg_match_all( '/%[^% ]+%/U', $message->definition, $defVars ) ) {
255278 return false;
256279 }
257 - preg_match_all( '/%[^%]%/U', $message->translation, $transVars );
 280+ preg_match_all( '/%[^% ]+%/U', $message->translation, $transVars );
258281
259282 $missing = self::compareArrays( $defVars[0], $transVars[0] );
260283
@@ -262,7 +285,6 @@
263286 $desc = array( 'translate-checks-parameters',
264287 implode( ', ', $missing ),
265288 $wgLang->formatNum($count) );
266 - var_dump( $desc );
267289 return true;
268290 } else {
269291 return false;
@@ -270,11 +292,11 @@
271293 }
272294
273295
274 - protected static function checkFreeColExtraVars( TMessage $message, &$desc = null ) {
275 - if ( !preg_match_all( '/%[^%]%/U', $message->definition, $defVars ) ) {
 296+ protected function checkFreeColExtraVars( TMessage $message, $code, &$desc = null ) {
 297+ if ( !preg_match_all( '/%[^% ]+%/U', $message->definition, $defVars ) ) {
276298 return false;
277299 }
278 - preg_match_all( '/%[^%]%/U', $message->translation, $transVars );
 300+ preg_match_all( '/%[^% ]+%/U', $message->translation, $transVars );
279301
280302 $missing = self::compareArrays( $transVars[0], $defVars[0] );
281303
@@ -283,7 +305,6 @@
284306 $desc = array( 'translate-checks-parameters-unknown',
285307 implode( ', ', $missing ),
286308 $wgLang->formatNum($count) );
287 - var_dump( $desc );
288309 return true;
289310 } else {
290311 return false;
Index: trunk/extensions/Translate/MessageGroups.php
@@ -283,7 +283,8 @@
284284
285285 public function load( $code ) {
286286 $file = $this->getFileLocation( $code );
287 - $messages = $this->mangler->mangle(
 287+ // Can return null, convert to array
 288+ $messages = (array) $this->mangler->mangle(
288289 ResourceLoader::loadVariableFromPHPFile( $file, 'messages' ) );
289290 if ( $this->getId() !== 'core' && $code !== 'en' ) {
290291 // For branches, load trunk messages that are not in database

Status & tagging log