r108034 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108033‎ | r108034 | r108035 >
Date:15:13, 4 January 2012
Author:nikerabbit
Status:resolved (Comments)
Tags:
Comment:
* Refactored TranslationHelpers to have MessageHandle as store variable, instead of separate title, language and message key
* Refactored TranslationHelpers to throw exceptions, which are converted to html comments to aid debugging
* Refactored TranslationHelpers to not rely on $wgRequest
* Added getMessageContent to RecentMessageGroup to allow passing the message handle, getMessage only takes message key which does not include names, and thus was unable to know which message to load
* Added getGroupIds and getPrimaryGroupId to MessageIndex which accept MessageHandles, equivalents in TranslationUtils::messageKeyToGroup(s) should be phased out
Modified paths:
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageIndex.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationEditPage.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/MessageGroups.php
@@ -974,6 +974,17 @@
975975 return null;
976976 }
977977
 978+ /**
 979+ * Subpage language of any in the title is not used.
 980+ */
 981+ public function getMessageContent( MessageHandle $handle, $code ) {
 982+ $groupId = MessageIndex::getPrimaryGroupId( $handle );
 983+ $group = MessageGroups::getGroup( $groupId );
 984+ if ( $group ) {
 985+ return $group->getMessage( $handle->getKey(), $code );
 986+ }
 987+ }
 988+
978989 }
979990
980991 /**
Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -269,7 +269,8 @@
270270 private static function editBoxes( EditPage $object ) {
271271 global $wgOut, $wgRequest;
272272
273 - $th = new TranslationHelpers( $object->mTitle );
 273+ $groupId = $wgRequest->getText( 'loadgroup', '' );
 274+ $th = new TranslationHelpers( $object->mTitle, $groupId );
274275 if ( $object->firsttime && !$wgRequest->getCheck( 'oldid' ) && !$wgRequest->getCheck( 'undo' ) ) {
275276 $object->textbox1 = (string) $th->getTranslation();
276277 } else {
Index: trunk/extensions/Translate/utils/MessageIndex.php
@@ -20,9 +20,6 @@
2121 /// @var MessageIndex
2222 protected static $instance;
2323
24 - /// @var array
25 - protected $index;
26 -
2724 /**
2825 * @return MessageIndex
2926 */
@@ -36,6 +33,33 @@
3734 return self::$instance;
3835 }
3936
 37+
 38+ /**
 39+ * @since 2012-01-14
 40+ * @return array
 41+ */
 42+ public static function getGroupIds( MessageHandle $handle ) {
 43+ $namespace = $handle->getTitle()->getNamespace();
 44+ $key = $handle->getKey();
 45+ $normkey = strtr( strtolower( "$namespace:$key" ), " ", "_" );
 46+
 47+ $index = self::singleton()->retrieve();
 48+ if ( isset( $index[$normkey] ) ) {
 49+ return (array) $index[$normkey];
 50+ } else {
 51+ return array();
 52+ }
 53+ }
 54+
 55+ /**
 56+ * @since 2012-01-14
 57+ * @return MessageGroup|null
 58+ */
 59+ public static function getPrimaryGroupId( MessageHandle $handle ) {
 60+ $groups = self::getGroupIds( $handle );
 61+ return count( $groups ) ? array_shift( $groups ) : null;
 62+ }
 63+
4064 /** @return array */
4165 abstract public function retrieve();
4266 abstract protected function store( array $array );
@@ -157,6 +181,9 @@
158182 * Storage on serialized file.
159183 */
160184 class FileCachedMessageIndex extends MessageIndex {
 185+ /// @var array
 186+ protected $index;
 187+
161188 protected $filename = 'translate_messageindex.ser';
162189
163190 /** @return array */
@@ -187,6 +214,9 @@
188215 protected $key = 'translate-messageindex';
189216 protected $cache;
190217
 218+ /// @var array
 219+ protected $index;
 220+
191221 protected function __construct( array $params ) {
192222 $this->cache = wfGetCache( CACHE_ANYTHING );
193223 }
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -16,22 +16,16 @@
1717 */
1818 class TranslationHelpers {
1919 /**
20 - * Title of the message
 20+ * @var MessageHandle
 21+ * @since 2012-01-04
2122 */
22 - protected $title;
 23+ protected $handle;
2324 /**
24 - * Name of the message without namespace or language code.
25 - */
26 - protected $page;
27 - /**
28 - * The language we are translating into.
29 - */
30 - protected $targetLanguage;
31 - /**
3225 * The group object of the message (or null if there isn't any)
3326 * @var MessageGroup
3427 */
3528 protected $group;
 29+
3630 /**
3731 * The current translation as a string.
3832 */
@@ -51,45 +45,28 @@
5246 protected $editMode = 'true';
5347
5448 /**
55 - * @param $title Title Title of a page that holds a translation.
 49+ * @param $title Title: Title of a page that holds a translation.
 50+ * @param $group String: Group id that should be used, otherwise autodetected from title.
5651 */
57 - public function __construct( Title $title ) {
58 - $this->title = $title;
59 - $this->init();
 52+ public function __construct( Title $title, $groupId ) {
 53+ $this->handle = new MessageHandle( $title );
 54+ $this->group = $this->getMessageGroup( $this->handle, $groupId );
6055 }
6156
6257 /**
63 - * Initializes member variables.
64 - */
65 - protected function init() {
66 - $title = $this->title;
67 - list( $page, $code ) = TranslateEditAddons::figureMessage( $title );
68 -
69 - $this->page = $page;
70 - $this->targetLanguage = $code;
71 - $this->group = self::getMessageGroup( $title->getNamespace(), $page );
72 - }
73 -
74 - /**
7558 * Tries to determine to which group this message belongs. It tries to get
7659 * group id from loadgroup GET-paramater, but fallbacks to messageIndex file
7760 * if no valid group was provided.
78 - * @param $namespace int The namespace where the page is.
79 - * @param $key string The message key we are interested in.
 61+ *
8062 * @return MessageGroup which the key belongs to, or null.
8163 */
82 - protected static function getMessageGroup( $namespace, $key ) {
83 - global $wgRequest;
 64+ protected function getMessageGroup( MessageHandle $handle, $groupId ) {
 65+ $mg = MessageGroups::getGroup( $groupId );
8466
85 - $group = $wgRequest->getText( 'loadgroup', '' );
86 - $mg = MessageGroups::getGroup( $group );
87 -
88 - # If we were not given group
 67+ # If we were not given (a valid) group
8968 if ( $mg === null ) {
90 - $group = TranslateUtils::messageKeyToGroup( $namespace, $key );
91 - if ( $group ) {
92 - $mg = MessageGroups::getGroup( $group );
93 - }
 69+ $groupId = MessageIndex::getPrimaryGroupId( $handle );
 70+ $mg = MessageGroups::getGroup( $groupId );
9471 }
9572
9673 return $mg;
@@ -128,12 +105,14 @@
129106 return $this->definition;
130107 }
131108
132 - if ( $this->group === null ) {
133 - return '';
 109+ $this->mustBeKnownMessage();
 110+
 111+ if ( method_exists( $this->group, 'getMessageContent' ) ) {
 112+ $this->definition = $this->group->getMessageContent( $this->handle, $this->group->getSourceLanguage() );
 113+ } else {
 114+ $this->definition = $this->group->getMessage( $this->handle->getKey(), $this->group->getSourceLanguage() );
134115 }
135116
136 - $this->definition = $this->group->getMessage( $this->page, $this->group->getSourceLanguage() );
137 -
138117 return $this->definition;
139118 }
140119
@@ -148,17 +127,18 @@
149128 }
150129
151130 // Shorter names
152 - $page = $this->page;
153 - $code = $this->targetLanguage;
 131+ $title = $this->handle->getTitle();
 132+ $page = $this->handle->getKey();
 133+ $code = $this->handle->getCode();
154134 $group = $this->group;
155135
156136 // Try database first
157137 $translation = TranslateUtils::getMessageContent(
158 - $page, $code, $this->title->getNamespace()
 138+ $page, $code, $title->getNamespace()
159139 );
160140
161141 if ( $translation !== null ) {
162 - if ( !TranslateEditAddons::hasFuzzyString( $translation ) && TranslateEditAddons::isFuzzy( $this->title ) ) {
 142+ if ( !TranslateEditAddons::hasFuzzyString( $translation ) && TranslateEditAddons::isFuzzy( $title ) ) {
163143 $translation = TRANSLATE_FUZZY . $translation;
164144 }
165145 } elseif ( $group && !$group instanceof FileBasedMessageGroup ) {
@@ -184,10 +164,19 @@
185165 }
186166
187167 /**
188 - * Get target language code
 168+ * Gets the linguistically correct language code for translation
189169 */
190170 public function getTargetLanguage() {
191 - return $this->targetLanguage;
 171+ global $wgLanguageCode, $wgTranslateDocumentationLanguageCode;
 172+
 173+ $code = $this->handle->getCode();
 174+ if ( !$code ) {
 175+ $this->mustBeKnownMessage();
 176+ $code = $group->getSourceLanguage();
 177+ }
 178+ if ( $code === $wgTranslateDocumentationLanguageCode ) {
 179+ return $wgLanguageCode;
 180+ }
192181 }
193182
194183 /**
@@ -217,7 +206,11 @@
218207
219208 $boxes = array();
220209 foreach ( $all as $type => $cb ) {
221 - $box = call_user_func( $cb );
 210+ try {
 211+ $box = call_user_func( $cb );
 212+ } catch ( TranslationHelperExpection $e ) {
 213+ $box = "<!-- Box $type not available: {$e->getMessage()} -->";
 214+ }
222215
223216 if ( $box ) {
224217 $boxes[$type] = $box;
@@ -254,22 +247,17 @@
255248 * @return string Html snippet which contains the suggestions.
256249 */
257250 protected function getTmBox( $serviceName, $config ) {
258 - if ( !$this->targetLanguage ) {
259 - return null;
260 - }
 251+ $this->mustHaveDefinition();
 252+ self::checkTranslationServiceFailure( $serviceName );
261253
262 - if ( strval( $this->getDefinition() ) === '' ) {
263 - return null;
 254+ // Needed data
 255+ $page = $this->handle->getKey();
 256+ $code = $this->handle->getCode();
 257+ if ( !$code ) {
 258+ $code = $this->group->getSourceLanguage();
264259 }
265 -
266 - if ( self::checkTranslationServiceFailure( $serviceName ) ) {
267 - return null;
268 - }
269 -
270 - // Needed data
271 - $code = $this->targetLanguage;
272260 $definition = $this->getDefinition();
273 - $ns = $this->title->getNsText();
 261+ $ns = $this->handle->getTitle()->getNsText();
274262
275263 // Fetch suggestions
276264 $server = $config['server'];
@@ -283,47 +271,46 @@
284272 // Parse suggestions, but limit to three (in case there would be more)
285273 $boxes = array();
286274
287 - if ( $suggestions !== false ) {
288 - $suggestions = FormatJson::decode( $suggestions, true );
 275+ if ( $suggestions === false ) {
 276+ // Assume timeout
 277+ self::reportTranslationServiceFailure( $serviceName );
 278+ }
 279+ $suggestions = FormatJson::decode( $suggestions, true );
289280
290 - foreach ( $suggestions as $s ) {
291 - // No use to suggest them what they are currently viewing
292 - if ( $s['context'] === "$ns:{$this->page}" ) {
293 - continue;
294 - }
 281+ foreach ( $suggestions as $s ) {
 282+ // No use to suggest them what they are currently viewing
 283+ if ( $s['context'] === "$ns:$page" ) {
 284+ continue;
 285+ }
295286
296 - $accuracy = wfMsgHtml( 'translate-edit-tmmatch' , sprintf( '%.2f', $s['quality'] ) );
297 - $legend = array( $accuracy => array() );
 287+ $accuracy = wfMsgHtml( 'translate-edit-tmmatch' , sprintf( '%.2f', $s['quality'] ) );
 288+ $legend = array( $accuracy => array() );
298289
299 - $source_page = Title::newFromText( $s['context'] . "/$code" );
300 - if ( $source_page ) {
301 - $legend[$accuracy][] = self::ajaxEditLink( $source_page, '•' );
302 - }
 290+ $source_page = Title::newFromText( $s['context'] . "/$code" );
 291+ if ( $source_page ) {
 292+ $legend[$accuracy][] = self::ajaxEditLink( $source_page, '•' );
 293+ }
303294
304 - $text = $this->suggestionField( $s['target'] );
305 - $params = array( 'class' => 'mw-sp-translate-edit-tmsug', 'title' => $s['source'] );
 295+ $text = $this->suggestionField( $s['target'] );
 296+ $params = array( 'class' => 'mw-sp-translate-edit-tmsug', 'title' => $s['source'] );
306297
307 - if ( isset( $sugFields[$s['target']] ) ) {
308 - $sugFields[$s['target']][2] = array_merge_recursive( $sugFields[$s['target']][2], $legend );
309 - } else {
310 - $sugFields[$s['target']] = array( $text, $params, $legend );
311 - }
 298+ if ( isset( $sugFields[$s['target']] ) ) {
 299+ $sugFields[$s['target']][2] = array_merge_recursive( $sugFields[$s['target']][2], $legend );
 300+ } else {
 301+ $sugFields[$s['target']] = array( $text, $params, $legend );
312302 }
 303+ }
313304
314 - foreach ( $sugFields as $field ) {
315 - list( $text, $params, $label ) = $field;
316 - $legend = array();
 305+ foreach ( $sugFields as $field ) {
 306+ list( $text, $params, $label ) = $field;
 307+ $legend = array();
317308
318 - foreach ( $label as $acc => $links ) {
319 - $legend[] = $acc . ' ' . implode( " ", $links );
320 - }
 309+ foreach ( $label as $acc => $links ) {
 310+ $legend[] = $acc . ' ' . implode( " ", $links );
 311+ }
321312
322 - $legend = implode( ' | ', $legend );
323 - $boxes[] = Html::rawElement( 'div', $params, self::legend( $legend ) . $text . self::clear() ) . "\n";
324 - }
325 - } else {
326 - // Assume timeout
327 - self::reportTranslationServiceFailure( $serviceName );
 313+ $legend = implode( ' | ', $legend );
 314+ $boxes[] = Html::rawElement( 'div', $params, self::legend( $legend ) . $text . self::clear() ) . "\n";
328315 }
329316
330317 $boxes = array_slice( $boxes, 0, 3 );
@@ -378,18 +365,17 @@
379366 protected function getGoogleSuggestion( $serviceName, $config ) {
380367 global $wgMemc;
381368
382 - if ( self::checkTranslationServiceFailure( $serviceName ) ) {
383 - return null;
384 - }
 369+ $this->mustHaveDefinition();
 370+ self::checkTranslationServiceFailure( $serviceName );
385371
386 - $code = $this->targetLanguage;
 372+ $code = $this->handle->getCode();
387373 $definition = trim( strval( $this->getDefinition() ) );
388374 $definition = self::wrapUntranslatable( $definition );
389375
390376 $memckey = wfMemckey( 'translate-tmsug-badcodes-' . $serviceName );
391377 $unsupported = $wgMemc->get( $memckey );
392378
393 - if ( $definition === '' || isset( $unsupported[$code] ) ) {
 379+ if ( isset( $unsupported[$code] ) ) {
394380 return null;
395381 }
396382
@@ -407,8 +393,6 @@
408394 if ( $json === false ) {
409395 // Most likely a timeout or other general error
410396 self::reportTranslationServiceFailure( $serviceName );
411 -
412 - return null;
413397 } elseif ( !is_object( $response ) ) {
414398 error_log( __METHOD__ . ': Unable to parse reply: ' . strval( $json ) );
415399 return null;
@@ -424,10 +408,9 @@
425409 $wgMemc->set( $memckey, $unsupported, 60 * 60 * 8 );
426410 } else {
427411 // Unknown error, assume the worst
428 - self::reportTranslationServiceFailure( $serviceName );
429412 wfWarn( __METHOD__ . "($serviceName): " . $response->responseDetails );
430413 error_log( __METHOD__ . "($serviceName): " . $response->responseDetails );
431 - return null;
 414+ self::reportTranslationServiceFailure( $serviceName );
432415 }
433416
434417 return null;
@@ -457,18 +440,17 @@
458441 protected function getMicrosoftSuggestion( $serviceName, $config ) {
459442 global $wgMemc;
460443
461 - if ( self::checkTranslationServiceFailure( $serviceName ) ) {
462 - return null;
463 - }
 444+ $this->mustHaveDefinition();
 445+ self::checkTranslationServiceFailure( $serviceName );
464446
465 - $code = $this->targetLanguage;
 447+ $code = $this->handle->getCode();
466448 $definition = trim( strval( $this->getDefinition() ) );
467449 $definition = self::wrapUntranslatable( $definition );
468450
469451 $memckey = wfMemckey( 'translate-tmsug-badcodes-' . $serviceName );
470452 $unsupported = $wgMemc->get( $memckey );
471453
472 - if ( $definition === '' || isset( $unsupported[$code] ) ) {
 454+ if ( isset( $unsupported[$code] ) ) {
473455 return null;
474456 }
475457
@@ -514,7 +496,6 @@
515497 }
516498 // Most likely a timeout or other general error
517499 self::reportTranslationServiceFailure( $serviceName );
518 - return null;
519500 }
520501
521502 $ret = $req->getContent();
@@ -541,13 +522,11 @@
542523 protected function getApertiumSuggestion( $serviceName, $config ) {
543524 global $wgMemc;
544525
545 - if ( self::checkTranslationServiceFailure( $serviceName ) ) {
546 - return null;
547 - }
 526+ self::checkTranslationServiceFailure( $serviceName );
548527
549 - $page = $this->page;
550 - $code = $this->targetLanguage;
551 - $ns = $this->title->getNamespace();
 528+ $page = $this->handle->getKey();
 529+ $code = $this->handle->getCode();
 530+ $ns = $this->handle->getTitle()->getNamespace();
552531
553532 $memckey = wfMemckey( 'translate-tmsug-pairs-' . $serviceName );
554533 $pairs = $wgMemc->get( $memckey );
@@ -560,7 +539,6 @@
561540
562541 if ( $json === false ) {
563542 self::reportTranslationServiceFailure( $serviceName );
564 - return null;
565543 } elseif ( !is_object( $response ) ) {
566544 error_log( __METHOD__ . ': Unable to parse reply: ' . strval( $json ) );
567545 return null;
@@ -614,7 +592,6 @@
615593 $response = FormatJson::decode( $json );
616594 if ( $json === false || !is_object( $response ) ) {
617595 self::reportTranslationServiceFailure( $serviceName );
618 - break; // Too slow, back off
619596 } elseif ( $response->responseStatus !== 200 ) {
620597 error_log( __METHOD__ . " (HTTP {$response->responseStatus}) with ($serviceName ($candidate|$code)): " . $response->responseDetails );
621598 } else {
@@ -637,10 +614,8 @@
638615 }
639616
640617 public function getDefinitionBox() {
641 - $en = strval( $this->getDefinition() );
642 - if ( $en === '' ) {
643 - return null;
644 - }
 618+ $this->mustHaveDefinition();
 619+ $en = $this->getDefinition();
645620
646621 $linker = class_exists( 'DummyLinker' ) ? new DummyLinker : new Linker;
647622 $title = $linker->link(
@@ -649,7 +624,7 @@
650625 array(),
651626 array(
652627 'group' => $this->group->getId(),
653 - 'language' => $this->targetLanguage
 628+ 'language' => $this->handle->getCode()
654629 )
655630 );
656631
@@ -695,17 +670,15 @@
696671 }
697672
698673 public function getCheckBox() {
 674+ $this->mustBeKnownMessage();
 675+
699676 global $wgTranslateDocumentationLanguageCode;
700677
701678 $placeholder = Html::element( 'div', array( 'class' => 'mw-translate-messagechecks' ) );
702679
703 - if ( $this->group === null ) {
704 - return null;
705 - }
706 -
707 - $page = $this->page;
 680+ $page = $this->handle->getKey();
708681 $translation = $this->getTranslation();
709 - $code = $this->targetLanguage;
 682+ $code = $this->handle->getCode();
710683 $en = $this->getDefinition();
711684
712685 if ( strval( $translation ) === '' ) {
@@ -746,9 +719,9 @@
747720 public function getOtherLanguagesBox() {
748721 global $wgLang;
749722
750 - $code = $this->targetLanguage;
751 - $page = $this->page;
752 - $ns = $this->title->getNamespace();
 723+ $code = $this->handle->getCode();
 724+ $page = $this->handle->getKey();
 725+ $ns = $this->handle->getTitle()->getNamespace();
753726
754727 $boxes = array();
755728 foreach ( self::getFallbacks( $code ) as $fbcode ) {
@@ -807,8 +780,8 @@
808781 return null;
809782 }
810783
811 - $page = $this->page;
812 - $ns = $this->title->getNamespace();
 784+ $page = $this->handle->getKey();
 785+ $ns = $this->handle->getTitle()->getNamespace();
813786
814787 $title = Title::makeTitle( $ns, $page . '/' . $wgTranslateDocumentationLanguageCode );
815788 $edit = self::ajaxEditLink( $title, wfMsgHtml( 'translate-edit-contribute' ) );
@@ -848,7 +821,7 @@
849822 $ffs = $this->group->getFFS();
850823 if ( $ffs instanceof GettextFFS ) {
851824 global $wgContLang;
852 - $mykey = $wgContLang->lcfirst( $this->page );
 825+ $mykey = $wgContLang->lcfirst( $this->handle->getKey() );
853826 $data = $ffs->read( $this->group->getSourceLanguage() );
854827 $help = $data['TEMPLATE'][$mykey]['comments'];
855828 // Do not display an empty comment. That's no help and takes up unnecessary space.
@@ -881,21 +854,24 @@
882855 }
883856
884857 protected function getPageDiff() {
885 - if ( $this->group instanceof WikiPageMessageGroup || $this->group === null ) {
 858+ $this->mustBeKnownMessage();
 859+
 860+ $group = $this->group;
 861+ $title = $this->handle->getTitle();
 862+ $key = $this->handle->getKey();
 863+
 864+ if ( $group instanceof WikiPageMessageGroup ) {
886865 return null;
887866 }
888867
889 - // Shortcuts
890 - $key = $this->page;
891 -
892 - $definitionTitle = Title::makeTitleSafe( $this->title->getNamespace(), "$key/en" );
 868+ $definitionTitle = Title::makeTitleSafe( $title->getNamespace(), "$key/en" );
893869 if ( !$definitionTitle || !$definitionTitle->exists() ) {
894870 return null;
895871 }
896872
897873 $db = wfGetDB( DB_MASTER );
898874 $conds = array(
899 - 'rt_page' => $this->title->getArticleId(),
 875+ 'rt_page' => $title->getArticleId(),
900876 'rt_type' => RevTag::getType( 'tp:transver' ),
901877 );
902878 $options = array(
@@ -934,19 +910,21 @@
935911 }
936912
937913 protected function getTranslationPageDiff() {
 914+
938915 global $wgEnablePageTranslation;
939916
940917 if ( !$wgEnablePageTranslation ) {
941918 return null;
942919 }
943920
 921+ $this->mustBeKnownMessage();
944922 if ( !$this->group instanceof WikiPageMessageGroup ) {
945923 return null;
946924 }
947925
948926 // Shortcuts
949 - $code = $this->targetLanguage;
950 - $key = $this->page;
 927+ $code = $this->handle->getCode();
 928+ $key = $this->handle->getKey();
951929
952930 // TODO: encapsulate somewhere
953931 $page = TranslatablePage::newFromTitle( $this->group->getTitle() );
@@ -987,7 +965,7 @@
988966
989967 protected function getLastDiff() {
990968 // Shortcuts
991 - $title = $this->title;
 969+ $title = $this->handle->getTitle();
992970 $latestRevId = $title->getLatestRevID();
993971 $previousRevId = $title->getPreviousRevisionID( $latestRevId );
994972
@@ -1002,7 +980,7 @@
1003981 if ( $previous !== $latest ) {
1004982 $diff = new DifferenceEngine;
1005983 if ( method_exists( 'DifferenceEngine', 'setTextLanguage' ) ) {
1006 - $diff->setTextLanguage( $this->targetLanguage );
 984+ $diff->setTextLanguage( $this->getTargetLanguage() );
1007985 }
1008986 $diff->setText( $previous, $latest );
1009987 $diff->setReducedLineNumbers();
@@ -1032,7 +1010,7 @@
10331011 $text = wfMessage( 'translate-dynagroup-last', $user )->escaped();
10341012 }
10351013 }
1036 -
 1014+
10371015 return TranslateUtils::fieldset( $text, $diffText, array( 'class' => 'mw-sp-translate-latestchange' ) );
10381016 }
10391017
@@ -1144,13 +1122,14 @@
11451123 * @return null|string
11461124 */
11471125 public function getLazySuggestionBox() {
1148 - if ( $this->group === null || !$this->targetLanguage ) {
 1126+ $this->mustBeKnownMessage();
 1127+ if ( !$this->handle->getCode() ) {
11491128 return null;
11501129 }
11511130
11521131 $url = SpecialPage::getTitleFor( 'Translate', 'editpage' )->getLocalUrl( array(
11531132 'suggestions' => 'only',
1154 - 'page' => $this->title->getPrefixedDbKey(),
 1133+ 'page' => $this->handle->getTitle()->getPrefixedDbKey(),
11551134 'loadgroup' => $this->group->getId(),
11561135 ) );
11571136 $url = Xml::encodeJsVar( $url );
@@ -1167,22 +1146,22 @@
11681147 * @return string
11691148 */
11701149 public function dialogID() {
1171 - $hash = sha1( $this->title->getPrefixedDbKey() );
 1150+ $hash = sha1( $this->handle->getTitle()->getPrefixedDbKey() );
11721151 return substr( $hash, 0, 4 );
11731152 }
11741153
11751154 /**
1176 - * @param $source
1177 - * @param $lang
 1155+ * @param $source jQuery selector for element containing the source
 1156+ * @param $lang Language code or object
11781157 * @return string
11791158 */
1180 - public function adder( $source, $lang = null ) {
 1159+ public function adder( $source, $lang ) {
11811160 if ( !$this->editMode ) {
11821161 return '';
11831162 }
11841163 $target = self::jQueryPathId( $this->getTextareaId() );
11851164 $source = self::jQueryPathId( $source );
1186 - $dir = wfGetLangObj( $lang ? $lang : $this->targetLanguage )->getDir();
 1165+ $dir = wfGetLangObj( $lang )->getDir();
11871166 $params = array(
11881167 'onclick' => "jQuery($target).val(jQuery($source).text()).focus(); return false;",
11891168 'href' => '#',
@@ -1209,15 +1188,17 @@
12101189 public function suggestionField( $text ) {
12111190 static $counter = 0;
12121191
 1192+ $code = $this->getTargetLanguage();
 1193+
12131194 $counter++;
12141195 $dialogID = $this->dialogID();
12151196 $id = Sanitizer::escapeId( "tmsug-$dialogID-$counter" );
1216 - $contents = Html::rawElement( 'div', array( 'lang' => $this->targetLanguage,
1217 - 'dir' => Language::factory( $this->targetLanguage )->getDir() ),
 1197+ $contents = Html::rawElement( 'div', array( 'lang' => $code,
 1198+ 'dir' => Language::factory( $code )->getDir() ),
12181199 TranslateUtils::convertWhiteSpaceToHTML( $text ) );
12191200 $contents .= $this->wrapInsert( $id, $text );
12201201
1221 - return $this->adder( $id ) . "\n" . $contents;
 1202+ return $this->adder( $id, $code ) . "\n" . $contents;
12221203 }
12231204
12241205 /**
@@ -1227,14 +1208,14 @@
12281209 * @return link
12291210 */
12301211 public static function ajaxEditLink( $target, $text ) {
1231 - list( $page, ) = TranslateEditAddons::figureMessage( $target );
1232 - $group = TranslateUtils::messageKeyToGroup( $target->getNamespace(), $page );
 1212+ $handle = new MessageHandle( $target );
 1213+ $groupId = MessageIndex::getPrimaryGroupId( $handle );
12331214
12341215 $params = array();
12351216 $params['action'] = 'edit';
1236 - $params['loadgroup'] = $group;
 1217+ $params['loadgroup'] = $groupId;
12371218
1238 - $jsEdit = TranslationEditPage::jsEdit( $target, $group );
 1219+ $jsEdit = TranslationEditPage::jsEdit( $target, $groupId );
12391220
12401221 $linker = class_exists( 'DummyLinker' ) ? new DummyLinker() : new Linker();
12411222 return $linker->link( $target, $text, $jsEdit, $params );
@@ -1287,7 +1268,9 @@
12881269 return false;
12891270 }
12901271
1291 - return $count >= self::$serviceFailureCount;
 1272+ if ( $count >= self::$serviceFailureCount ) {
 1273+ throw new TranslationHelperExpection( "web service $service is temporarily disabled" );
 1274+ }
12921275 }
12931276
12941277 /**
@@ -1314,11 +1297,10 @@
13151298 } elseif ( $count > self::$serviceFailureCount ) {
13161299 error_log( "Translation service $service still suspended" );
13171300 }
 1301+
 1302+ throw new TranslationHelperExpection( "web service $service failed to provide valid response" );
13181303 }
13191304
1320 - /**
1321 - * @param $out OutputPage
1322 - */
13231305 public static function addModules( OutputPage $out ) {
13241306 $out->addModules( 'ext.translate.quickedit' );
13251307
@@ -1329,4 +1311,28 @@
13301312 $diff = new DifferenceEngine;
13311313 $diff->showDiffStyle();
13321314 }
 1315+
 1316+ /// @since 2012-01-04
 1317+ protected function mustBeKnownMessage() {
 1318+ if ( !$this->group ) {
 1319+ throw new TranslationHelperExpection( 'unknown group' );
 1320+ }
 1321+ }
 1322+ /// @since 2012-01-04
 1323+ protected function mustBeTranslation() {
 1324+ if ( !$this->handle->getCode() ) {
 1325+ throw new TranslationHelperExpection( 'editing source language' );
 1326+ }
 1327+ }
 1328+
 1329+ /// @since 2012-01-04
 1330+ protected function mustHaveDefinition() {
 1331+ if ( strval( $this->getDefinition() ) === '' ) {
 1332+ throw new TranslationHelperExpection( 'message does not have definition' );
 1333+ }
 1334+ }
 1335+
13331336 }
 1337+
 1338+/// @since 2012-01-04
 1339+class TranslationHelperExpection extends MWException {}
Index: trunk/extensions/Translate/utils/TranslationEditPage.php
@@ -60,12 +60,13 @@
6161 * disabled all other output.
6262 */
6363 public function execute() {
64 - global $wgOut, $wgServer, $wgScriptPath, $wgUser;
 64+ global $wgOut, $wgServer, $wgScriptPath, $wgUser, $wgRequest;
6565
6666 $wgOut->disable();
6767
6868 $data = $this->getEditInfo();
69 - $helpers = new TranslationHelpers( $this->getTitle() );
 69+ $groupId = $wgRequest->getText( 'loadgroup', '' );
 70+ $helpers = new TranslationHelpers( $this->getTitle(), $groupId );
7071
7172 $id = "tm-target-{$helpers->dialogID()}";
7273 $helpers->setTextareaId( $id );

Follow-up revisions

RevisionCommit summaryAuthorDate
r108038Missed one instance were constructor changed, ping r108034nikerabbit15:23, 4 January 2012
r108046It's useful to catch the errors we might throw, ping r108034nikerabbit16:15, 4 January 2012
r110265follow-up r108034: actually return the value of $code in getTargetLanguage (t...robin03:46, 30 January 2012

Comments

#Comment by Siebrand (talk | contribs)   15:38, 4 January 2012
  • @since 2012-01-14

What was it like over there?

#Comment by Nikerabbit (talk | contribs)   15:53, 4 January 2012

There was snow everywhere. I had to type with my mittens on.

Status & tagging log