r97171 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97170‎ | r97171 | r97172 >
Date:16:10, 15 September 2011
Author:nikerabbit
Status:deferred
Tags:
Comment:
Convert code to use MessageHandle class.
Introduced Translate:newTranslation hook to reduce code duplication
Modified paths:
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationMemoryUpdater.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -27,16 +27,15 @@
2828 global $wgRequest;
2929
3030 $title = $skin->getTitle();
 31+ $handle = new MessageHandle( $title );
3132
32 - if ( !self::isMessageNamespace( $title ) ) {
 33+ if ( !$handle->isValid() ) {
3334 return true;
3435 }
3536
36 - list( $key, $code, $group ) = self::getKeyCodeGroup( $title );
37 - if ( !$group || !$code ) {
38 - return true;
39 - }
40 -
 37+ $group = $handle->getGroup();
 38+ $key = $handle->getKey();
 39+ $code = $handle->getCode();
4140 $collection = $group->initCollection( $group->getSourceLanguage() );
4241 $collection->filter( 'optional' );
4342 $keys = array_keys( $collection->keys() );
@@ -138,12 +137,12 @@
139138 * Hook: EditPage::showEditForm:initial
140139 */
141140 static function addTools( $object ) {
142 - if ( !self::isMessageNamespace( $object->mTitle ) ) {
 141+ $handle = new MessageHandle( $object->mTitle );
 142+ if ( !$handle->isValid() ) {
143143 return true;
144144 }
145145
146146 $object->editFormTextTop .= self::editBoxes( $object );
147 -
148147 return true;
149148 }
150149
@@ -290,24 +289,12 @@
291290 }
292291
293292 /**
294 - * Check if a title is in a message namespace.
295 - * @param $title Title
296 - * @return \bool If title is in a message namespace.
297 - */
298 - public static function isMessageNamespace( Title $title ) {
299 - global $wgTranslateMessageNamespaces;
300 -
301 - $namespace = $title->getNamespace();
302 -
303 - return in_array( $namespace, $wgTranslateMessageNamespaces, true );
304 - }
305 -
306 - /**
307293 * Removes protection tab for message namespaces - not useful.
308294 * Hook: SkinTemplateTabs
309295 */
310296 public static function tabs( $skin, &$tabs ) {
311 - if ( !self::isMessageNamespace( $skin->getTitle() ) ) {
 297+ $handle = new MessageHandle( $skin->getTitle() );
 298+ if ( !$handle->isMessageNamespace() ) {
312299 return true;
313300 }
314301
@@ -352,8 +339,7 @@
353340 MessageGroupStats::clear( $handle );
354341
355342 if ( $fuzzy === false ) {
356 - // Fuzzy versions are not real translations
357 - self::updateTransverTag( $handle, $rev );
 343+ wfRunHooks( 'Translate:newTranslation', array( $handle, $revision, $text, $user ) );
358344 }
359345
360346 return true;
@@ -365,6 +351,7 @@
366352
367353 // Docs are exempt for checks
368354 global $wgTranslateDocumentationLanguageCode;
 355+ $code = $handle->getCode();
369356 if ( $code === $wgTranslateDocumentationLanguageCode ) {
370357 return $fuzzy;
371358 }
@@ -376,6 +363,7 @@
377364 return $fuzzy;
378365 }
379366
 367+ $key = $handle->getKey();
380368 $en = $group->getMessage( $key, $group->getSourceLanguage() );
381369 $message = new FatMessage( $key, $en );
382370 // Take the contents from edit field as a translation.
@@ -411,8 +399,9 @@
412400 * Adds tag which identifies the revision of source message at that time.
413401 * This is used to show diff against current version of source message
414402 * when updating a translation.
 403+ * Hook: Translate:newTranslation
415404 */
416 - protected static function updateTransverTag( MessageHandle $handle, $revision ) {
 405+ public static function updateTransverTag( MessageHandle $handle, $revision, $text, User $user ) {
417406 $group = $handle->getGroup();
418407 if ( $group instanceof WikiPageMessageGroup ) {
419408 // WikiPageMessageGroup has different method
@@ -420,8 +409,8 @@
421410 }
422411
423412 $title = $handle->getTitle();
424 - $fullKey = $handle->getKey() . '/' . $group->getSourceLanguage();
425 - $definitionTitle = Title::makeTitleSafe( $title->getNamespace(), $fullkey );
 413+ $name = $handle->getKey() . '/' . $group->getSourceLanguage();
 414+ $definitionTitle = Title::makeTitleSafe( $title->getNamespace(), $name );
426415 if ( !$definitionTitle || !$definitionTitle->exists() ) {
427416 return;
428417 }
@@ -462,9 +451,12 @@
463452 /// Hook: ArticlePrepareTextForEdit
464453 public static function disablePreSaveTransform( $article, $popts ) {
465454 global $wgTranslateDocumentationLanguageCode;
466 - $keycodegroup = self::getKeyCodeGroup( $article->getTitle() );
467 - if ( self::isMessageNamespace( $article->getTitle() )
468 - && $keycodegroup[1] !== $wgTranslateDocumentationLanguageCode ) {
 455+
 456+ $handle = new MessageHandle( $article->getTitle() );
 457+ $code = $handle->getCode();
 458+ if ( $handle->isMessageNamespace()
 459+ && $code !== $wgTranslateDocumentationLanguageCode ) {
 460+
469461 $popts->setPreSaveTransform( false );
470462 }
471463 return true;
Index: trunk/extensions/Translate/Translate.php
@@ -96,6 +96,7 @@
9797 $wgHooks['LoadExtensionSchemaUpdates'][] = 'PageTranslationHooks::schemaUpdates';
9898 // Fuzzy tags for speed.
9999 $wgHooks['ArticleSaveComplete'][] = 'TranslateEditAddons::onSave';
 100+$wgHooks['Translate:newTranslation'][] = 'TranslateEditAddons::updateTransverTag';
100101
101102 // Custom preferences
102103 $wgDefaultUserOptions['translate'] = 0;
@@ -111,7 +112,7 @@
112113 $wgHooks['SkinTemplateToolboxEnd'][] = 'TranslateToolbox::toolboxAllTranslations';
113114
114115 // Translation memory updates
115 -$wgHooks['ArticleSaveComplete'][] = 'TranslationMemoryUpdater::update';
 116+$wgHooks['Translate:newTranslation'][] = 'TranslationMemoryUpdater::update';
116117
117118 // Translation display related
118119 $wgHooks['ArticleContentOnDiff'][] = 'TranslateEditAddons::displayOnDiff';
Index: trunk/extensions/Translate/utils/TranslationMemoryUpdater.php
@@ -13,19 +13,10 @@
1414 */
1515 class TranslationMemoryUpdater {
1616 /**
17 - * @static
18 - * @param $article Article
19 - * @param $user User
20 - * @param $text
21 - * @param $summary
22 - * @param $minor
23 - * @param $_
24 - * @param $_
25 - * @param $flags
26 - * @param $revision
27 - * @return bool
 17+ * Shovels the new translation into translation memory.
 18+ * Hook: Translate:newTranslation
2819 */
29 - public static function update( $article, $user, $text, $summary, $minor, $_, $_, $flags, $revision ) {
 20+ public static function update( MessageHandle $handle, $revision, $text, User $user ) {
3021 global $wgContLang;
3122
3223 $dbw = self::getDatabaseHandle();
@@ -34,29 +25,15 @@
3526 return true;
3627 }
3728
38 - $title = $article->getTitle();
39 - // Something we are not interested in at all
40 - if ( !TranslateEditAddons::isMessageNamespace( $title ) ) {
41 - return true;
42 - }
43 -
44 - list( $key, $code, $group ) = TranslateEditAddons::getKeyCodeGroup( $title );
45 - // Unknown message, we cannot handle. We need definition.
46 - if ( !$group || !$code ) {
47 - return true;
48 - }
49 -
5029 // Skip definitions to not slow down mass imports etc.
5130 // These will be added when first translation is made
52 - if ( $code === 'en' ) {
 31+ if ( $handle->getCode() === 'en' ) {
5332 return true;
5433 }
5534
56 - // Skip fuzzy messages
57 - if ( TranslateEditAddons::hasFuzzyString( $text ) ) {
58 - return true;
59 - }
60 -
 35+ $group = $handle->getGroup();
 36+ $key = $handle->getKey();
 37+ $code = $handle->getCode();
6138 $ns_text = $wgContLang->getNsText( $group->getNamespace() );
6239 $definition = $group->getMessage( $key, 'en' );
6340 if ( !is_string( $definition ) || !strlen( $definition ) ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r97243updateTransverTag is now a hook, which needs to return true....nikerabbit06:34, 16 September 2011

Status & tagging log