r62260 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62259‎ | r62260 | r62261 >
Date:20:04, 10 February 2010
Author:siebrand
Status:deferred
Tags:
Comment:
Remove some code duplication, fix bug in importing fuzzied message in MessageWebImporter
* doAction(), getFuzzyBot() and makeTitle() from SpecialManageGroups.php now use methods of class MessageWebImporter. These methods are now public.
* documented doAction() (had to add two parameters for the code merge)
* add MessageWebImporter::makeTextFuzzy() to safe add TRANSLATE_FUZZY to text
* in doAction() "$action === 'fuzzy' && $code != 'en'" did not have a Title created, causing an error. That case also shouldn't call doFuzzy(). Text is now fuzzied and sent to doImport
* rename $flags parameter of MessageWebImporter::doImport() to $editFlags for clarity

Needs review.
Modified paths:
  • /trunk/extensions/Translate/SpecialManageGroups.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageWebImporter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/SpecialManageGroups.php
@@ -1,5 +1,12 @@
22 <?php
3 -if ( !defined( 'MEDIAWIKI' ) ) die();
 3+/**
 4+ * @addtogroup Extensions
 5+ *
 6+ * @author Niklas Laxstr�m
 7+ * @author Siebrand Mazeland
 8+ * @copyright Copyright � 2009-2010, Niklas Laxstr�m, Siebrand Mazeland
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
411
512 class SpecialManageGroups {
613 protected $skin, $user, $out;
@@ -188,7 +195,17 @@
189196 $this->time = wfTimestamp();
190197 }
191198
192 - $message = $this->doAction( $action, $group, $key, $code, $value );
 199+ $fuzzybot = MessageWebImporter::getFuzzyBot();
 200+ $message = MessageWebImporter::doAction(
 201+ $action,
 202+ $group,
 203+ $key,
 204+ $code,
 205+ $value,
 206+ '', /* default edit summary */
 207+ $fuzzybot,
 208+ EDIT_FORCE_BOT
 209+ );
193210
194211 $key = array_shift( $message );
195212 $params = $message;
@@ -364,69 +381,10 @@
365382 }
366383 }
367384
368 - protected function doAction( $action, $group, $key, $code, $message, $comment = '' ) {
369 - if ( $action === 'import' || $action === 'conflict' ) {
370 - if ( $action === 'import' ) {
371 - $comment = wfMsgForContentNoTrans( 'translate-manage-import-summary' );
372 - } else {
373 - $comment = wfMsgForContentNoTrans( 'translate-manage-conflict-summary' );
374 - $message = TRANSLATE_FUZZY . $message;
375 - }
376 -
377 - $title = self::makeTitle( $group, $key, $code );
378 - $fuzzybot = self::getFuzzyBot();
379 -
380 - return MessageWebImporter::doImport(
381 - $title,
382 - $message,
383 - $comment,
384 - $fuzzybot,
385 - EDIT_FORCE_BOT
386 - );
387 - } elseif ( $action === 'ignore' ) {
388 - return array( 'translate-manage-import-ignore', $key );
389 - } elseif ( $action === 'fuzzy' && $code != 'en' ) {
390 - global $wgUser;
391 - $title = Title::makeTitleSafe( $group->getNamespace(), $key );
392 - $comment = "[{$wgUser->getName()}] ";
393 - $comment .= wfMsgForContentNoTrans( 'translate-manage-fuzzy-summary' );
394 -
395 - return MessageWebImporter::doFuzzy(
396 - $title,
397 - $message,
398 - $comment,
399 - null,
400 - EDIT_FORCE_BOT
401 - );
402 - } else {
403 - throw new MWException( "Unhandled action $action" );
404 - }
405 - }
406 -
407385 protected function checkProcessTime() {
408386 return wfTimestamp() - $this->time >= $this->processingTime;
409387 }
410388
411 - // FIXME: Duplicate code. See ChangeSyncer::getImportUser() in scripts/sync-group.php
412 - protected static function getFuzzyBot() {
413 - global $wgTranslateFuzzyBotName;
414 -
415 - $user = User::newFromName( $wgTranslateFuzzyBotName );
416 -
417 - if ( !$user->isLoggedIn() ) {
418 - $user->addToDatabase();
419 - }
420 -
421 - return $user;
422 - }
423 -
424 - protected static function makeTitle( $group, $key, $code ) {
425 - $ns = $group->getNamespace();
426 - $titlekey = "$key/$code";
427 -
428 - return Title::makeTitleSafe( $ns, $titlekey );
429 - }
430 -
431389 protected function setSubtitle( $group, $code ) {
432390 global $wgLang;
433391 $links[] = $this->skin->link(
Index: trunk/extensions/Translate/utils/MessageWebImporter.php
@@ -6,7 +6,8 @@
77 * @addtogroup Extensions
88 *
99 * @author Niklas Laxström
10 - * @copyright Copyright © 2009, Niklas Laxström
 10+ * @author Siebrand Mazeland
 11+ * @copyright Copyright © 2009-2010, Niklas Laxström, Siebrand Mazeland
1112 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1213 */
1314
@@ -142,13 +143,6 @@
143144
144145 if ( isset( $collection[$key] ) ) {
145146 $old = $collection[$key]->translation();
146 - /* Fuzzy state of the in-wiki message is of no
147 - * concern. Default action should be 'import'.
148 - * If the string is marked fuzzy in the gettext,
149 - * fuzzy tag is added anyway on import.
150 - $fuzzy = TranslateEditAddons::hasFuzzyString( $old ) ||
151 - TranslateEditAddons::isFuzzy( self::makeTitle( $group, $key, $code ) );
152 - */
153147 }
154148
155149 // No changes at all, ignore
@@ -185,7 +179,13 @@
186180 // Check processing time
187181 if ( !isset( $this->time ) ) $this->time = wfTimestamp();
188182
189 - $message = $this->doAction( $action, $group, $key, $code, $value );
 183+ $message = self::doAction(
 184+ $action,
 185+ $group,
 186+ $key,
 187+ $code,
 188+ $value
 189+ );
190190
191191 $key = array_shift( $message );
192192 $params = $message;
@@ -272,23 +272,40 @@
273273 return $alldone;
274274 }
275275
276 - // FIXME: lot of duplication with SpecialManageGroups::doAction()
277 - protected function doAction( $action, $group, $key, $code, $message, $comment = '' ) {
 276+ /**
 277+ * Perform an action on a given group/key/code
 278+ *
 279+ * @param $action String: import/conflict/ignore
 280+ * @param $group Object: group object
 281+ * @param $key String: message key
 282+ * @param $code String: language code
 283+ * @param $message String: contents for the $key/code combination
 284+ * @param $comment String: edit summary (default: empty) - see Article::doEdit
 285+ * @param $user Object: object of user that will make the edit (default: null - $wgUser) - see Article::doEdit
 286+ * @param $editFlags Integer bitfield: see Article::doEdit
 287+ *
 288+ * @return String: action result
 289+ */
 290+ public static function doAction( $action, $group, $key, $code, $message, $comment = '', $user = null, $editFlags = 0 ) {
278291 if ( $action === 'import' || $action === 'conflict' ) {
279292 if ( $action === 'import' ) {
280293 $comment = wfMsgForContentNoTrans( 'translate-manage-import-summary' );
281294 } else {
282295 $comment = wfMsgForContentNoTrans( 'translate-manage-conflict-summary' );
283 - $message = TRANSLATE_FUZZY . $message;
 296+ $message = self::makeMessageFuzzy( $message );
284297 }
285298
286299 $title = self::makeTitle( $group, $key, $code );
287300
288 - return self::doImport( $title, $message, $comment );
 301+ return self::doImport( $title, $message, $comment, $user, $editFlags );
289302 } elseif ( $action === 'ignore' ) {
290303 return array( 'translate-manage-import-ignore', $key );
291304 } elseif ( $action === 'fuzzy' && $code != 'en' ) {
292 - return self::doFuzzy( $title, $message, $comment );
 305+ $title = self::makeTitle( $group, $key, $code );
 306+
 307+ $message = self::makeMessageFuzzy( $message );
 308+
 309+ return self::doImport( $title, $message, $comment, $user, $editFlags );
293310 } else {
294311 throw new MWException( "Unhandled action $action" );
295312 }
@@ -298,9 +315,9 @@
299316 return wfTimestamp() - $this->time >= $this->processingTime;
300317 }
301318
302 - public static function doImport( $title, $message, $comment, $user = null, $flags = false ) {
 319+ public static function doImport( $title, $message, $comment, $user = null, $editFlags = 0 ) {
303320 $article = new Article( $title );
304 - $status = $article->doEdit( $message, $comment, $flags, false, $user );
 321+ $status = $article->doEdit( $message, $comment, $editFlags, false, $user );
305322 $success = $status->isOK();
306323
307324 if ( $success ) {
@@ -340,10 +357,8 @@
341358 foreach ( $rows as $row ) {
342359 $ttitle = Title::makeTitle( $row->page_namespace, $row->page_title );
343360
344 - // Avoid double-fuzzying
345361 $text = Revision::getRevisionText( $row );
346 - $text = str_replace( TRANSLATE_FUZZY, '', $text );
347 - $text = TRANSLATE_FUZZY . $text;
 362+ $text = self::makeTextFuzzy( $text );
348363
349364 $changed[] = self::doImport(
350365 $ttitle,
@@ -383,8 +398,7 @@
384399 return array( 'translate-manage-import-fuzzy', "\n" . $text );
385400 }
386401
387 - // FIXME: duplicate of SpecialManageGroups::getFuzzyBot()
388 - protected static function getFuzzyBot() {
 402+ public static function getFuzzyBot() {
389403 global $wgTranslateFuzzyBotName;
390404
391405 $user = User::newFromName( $wgTranslateFuzzyBotName );
@@ -396,8 +410,7 @@
397411 return $user;
398412 }
399413
400 - // FIXME: duplicate of SpecialManageGroups::makeTitle()
401 - protected static function makeTitle( $group, $key, $code ) {
 414+ public static function makeTitle( $group, $key, $code ) {
402415 $ns = $group->getNamespace();
403416 $titlekey = "$key/$code";
404417
@@ -414,4 +427,16 @@
415428 Xml::tags( 'div', $contentParams, $content )
416429 );
417430 }
 431+
 432+ /**
 433+ * Safe add fuzzy tag to text
 434+ *
 435+ * @param $message String: message content
 436+ *
 437+ * @return $message prefixed with TRANSLATE_FUZZY tag
 438+ */
 439+ public static function makeTextFuzzy( $message ) {
 440+ $message = str_replace( TRANSLATE_FUZZY, '', $message );
 441+ return TRANSLATE_FUZZY . $message;
 442+ }
418443 }

Status & tagging log