r99251 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99250‎ | r99251 | r99252 >
Date:20:53, 7 October 2011
Author:nikerabbit
Status:deferred
Tags:
Comment:
Cleanup TranslateEditAddons:
* preserveWhitespaces unused
* removed docs that do not tell anything new
* moved some functions to TranslateHooks
* some changes to make code more clear
Modified paths:
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateHooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -20,12 +20,8 @@
2121 /**
2222 * Add some tabs for navigation for users who do not use Ajax interface.
2323 * Hooks: SkinTemplateNavigation, SkinTemplateTabs
24 - * @param $skin Skin
25 - * @param $tabs Array
26 - *
27 - * @return bool
2824 */
29 - static function addNavigationTabs( $skin, &$tabs ) {
 25+ static function addNavigationTabs( Skin $skin, array &$tabs ) {
3026 global $wgRequest;
3127
3228 $title = $skin->getTitle();
@@ -47,20 +43,20 @@
4844
4945 $next = $prev = null;
5046
 47+ $match = -100;
 48+
5149 foreach ( $keys as $index => $tkey ) {
5250 if ( $key === strtolower( strtr( $tkey, ' ', '_' ) ) ) {
 51+ $match = $index;
5352 break;
5453 }
55 - if ( $index === $count -1 ) {
56 - $index = -666;
57 - }
5854 }
5955
60 - if ( isset( $keys[$index -1] ) ) {
61 - $prev = $keys[$index -1];
 56+ if ( isset( $keys[$match -1] ) ) {
 57+ $prev = $keys[$match -1];
6258 }
63 - if ( isset( $keys[$index + 1] ) ) {
64 - $next = $keys[$index + 1];
 59+ if ( isset( $keys[$match + 1] ) ) {
 60+ $next = $keys[$match + 1];
6561 }
6662
6763 $id = $group->getId();
@@ -122,10 +118,8 @@
123119 /**
124120 * Keep the usual diiba daaba hidden from translators.
125121 * Hook: AlternateEdit
126 - * @param $editpage EditPage
127 - * @return bool
128122 */
129 - public static function intro( $editpage ) {
 123+ public static function intro( EditPage $editpage ) {
130124 $editpage->suppressIntro = true;
131125
132126 $msg = wfMsgForContent( 'translate-edit-tag-warning' );
@@ -139,10 +133,8 @@
140134 /**
141135 * Adds the translation aids and navigation to the normal edit page.
142136 * Hook: EditPage::showEditForm:initial
143 - * @param $object
144 - * @return bool
145137 */
146 - static function addTools( $object ) {
 138+ static function addTools( EditPage $object ) {
147139 $handle = new MessageHandle( $object->mTitle );
148140 if ( !$handle->isValid() ) {
149141 return true;
@@ -156,23 +148,17 @@
157149 * Replace the normal save button with one that says if you are editing
158150 * message documentation to try to avoid accidents.
159151 * Hook: EditPageBeforeEditButtons
160 - * @param $editpage EditPage
161 - * @param $buttons
162 - * @param $tabindex
163 - * @return bool
164152 */
165 - static function buttonHack( $editpage, &$buttons, $tabindex ) {
166 - global $wgTranslateDocumentationLanguageCode, $wgLang;
 153+ static function buttonHack( EditPage $editpage, &$buttons, $tabindex ) {
 154+ global $wgLang;
167155
168156 $handle = new MessageHandle( $editpage->mTitle );
169157 if ( !$handle->isValid() ) {
170158 return true;
171159 }
172160
173 - $code = $handle->getCode();
174 -
175 - if ( $code === $wgTranslateDocumentationLanguageCode ) {
176 - $name = TranslateUtils::getLanguageName( $code, false, $wgLang->getCode() );
 161+ if ( $handle->isDoc() ) {
 162+ $name = TranslateUtils::getLanguageName( $handle->getCode(), false, $wgLang->getCode() );
177163 $temp = array(
178164 'id' => 'wpSave',
179165 'name' => 'wpSave',
@@ -270,7 +256,7 @@
271257 * @param $object
272258 * @return String
273259 */
274 - private static function editBoxes( $object ) {
 260+ private static function editBoxes( EditPage $object ) {
275261 global $wgOut, $wgRequest;
276262
277263 $th = new TranslationHelpers( $object->mTitle );
@@ -310,28 +296,20 @@
311297 /**
312298 * Removes protection tab for message namespaces - not useful.
313299 * Hook: SkinTemplateTabs
314 - * @param $skin Skin
315 - * @param $tabs
316 - * @return bool
317300 */
318 - public static function tabs( $skin, &$tabs ) {
 301+ public static function tabs( Skin $skin, &$tabs ) {
319302 $handle = new MessageHandle( $skin->getTitle() );
320 - if ( !$handle->isMessageNamespace() ) {
321 - return true;
 303+ if ( $handle->isMessageNamespace() ) {
 304+ unset( $tabs['protect'] );
322305 }
323306
324 - unset( $tabs['protect'] );
325 -
326307 return true;
327308 }
328309
329310 /**
330311 * Hook: EditPage::showEditForm:fields
331 - * @param $edit
332 - * @param $out OutputPage
333 - * @return bool
334312 */
335 - public static function keepFields( $edit, $out ) {
 313+ public static function keepFields( EditPage $edit, OutputPage $out ) {
336314 global $wgRequest;
337315
338316 $out->addHTML( "\n" .
@@ -344,17 +322,8 @@
345323 }
346324
347325 /**
 326+ * Runs message checks, adds tp:transver tags and updates statistics.
348327 * Hook: ArticleSaveComplete
349 - * @param $article Article
350 - * @param $user User
351 - * @param $text string
352 - * @param $summary string
353 - * @param $minor bool
354 - * @param $_
355 - * @param $_
356 - * @param $flags
357 - * @param $revision Revision
358 - * @return bool
359328 */
360329 public static function onSave( $article, $user, $text, $summary,
361330 $minor, $_, $_, $flags, $revision
@@ -385,18 +354,14 @@
386355 }
387356
388357 /**
389 - * @param $handle MessageHandle
390 - * @param $text
391358 * @return bool
392359 */
393 - protected static function checkNeedsFuzzy( MessageHandle $handle, $text ) {
 360+ protected static function checkNeedsFuzzy( MessageHandle $handle, /*string*/$text ) {
394361 // Check for explicit tag.
395362 $fuzzy = self::hasFuzzyString( $text );
396363
397364 // Docs are exempt for checks
398 - global $wgTranslateDocumentationLanguageCode;
399 - $code = $handle->getCode();
400 - if ( $code === $wgTranslateDocumentationLanguageCode ) {
 365+ if ( $handle->isDoc() ) {
401366 return $fuzzy;
402367 }
403368
@@ -407,6 +372,7 @@
408373 return $fuzzy;
409374 }
410375
 376+ $code = $handle->getCode();
411377 $key = $handle->getKey();
412378 $en = $group->getMessage( $key, $group->getSourceLanguage() );
413379 $message = new FatMessage( $key, $en );
@@ -422,8 +388,8 @@
423389
424390 /**
425391 * @param $title Title
426 - * @param $revision
427 - * @param $fuzzy
 392+ * @param $revision int
 393+ * @param $fuzzy bool
428394 */
429395 protected static function updateFuzzyTag( Title $title, $revision, $fuzzy ) {
430396 $dbw = wfGetDB( DB_MASTER );
@@ -485,47 +451,11 @@
486452 }
487453
488454 /**
489 - * @param $text string
490 - * @return string
491 - */
492 - public static function preserveWhitespaces( $text ) {
493 - $text = wfEscapeWikiText( $text );
494 - $text = preg_replace( '/^ /m', ' ', $text );
495 - $text = preg_replace( '/ $/m', ' ', $text );
496 - $text = preg_replace( '/ /', '  ', $text );
497 - $text = str_replace( "\n", '<br />', $text );
498 - return $text;
499 - }
500 -
501 - /**
502 - * Hook: LanguageGetTranslatedLanguageNames
503 - * @param $names
504 - * @param $code
505 - * @return bool
506 - */
507 - public static function translateMessageDocumentationLanguage( &$names, $code ) {
508 - global $wgTranslateDocumentationLanguageCode;
509 - if ( $wgTranslateDocumentationLanguageCode ) {
510 - $names[$wgTranslateDocumentationLanguageCode] =
511 - wfMessage( 'translate-documentation-language' )->inLanguage( $code )->plain();
512 - }
513 - return true;
514 - }
515 -
516 - /**
517455 * Hook: ArticlePrepareTextForEdit
518 - * @param $article Article
519 - * @param $popts ParserOptions
520 - * @return bool
521456 */
522 - public static function disablePreSaveTransform( $article, $popts ) {
523 - global $wgTranslateDocumentationLanguageCode;
524 -
 457+ public static function disablePreSaveTransform( $article, ParserOptions $popts ) {
525458 $handle = new MessageHandle( $article->getTitle() );
526 - $code = $handle->getCode();
527 - if ( $handle->isMessageNamespace()
528 - && $code !== $wgTranslateDocumentationLanguageCode ) {
529 -
 459+ if ( $handle->isMessageNamespace() && !$handle->isDoc() ) {
530460 $popts->setPreSaveTransform( false );
531461 }
532462 return true;
@@ -533,11 +463,8 @@
534464
535465 /**
536466 * Hook: ArticleContentOnDiff
537 - * @param $de DifferenceEngine
538 - * @param $out OutputPage
539 - * @return bool
540467 */
541 - public static function displayOnDiff( $de, $out ) {
 468+ public static function displayOnDiff( DifferenceEngine $de, OutputPage $out ) {
542469 $title = $de->getTitle();
543470 $handle = new MessageHandle( $title );
544471
@@ -562,98 +489,4 @@
563490 return false;
564491 }
565492
566 - /**
567 - * Hook: SpecialSearchProfiles
568 - * @param $profiles array
569 - * @return bool
570 - */
571 - public static function searchProfile( &$profiles ) {
572 - global $wgTranslateMessageNamespaces;
573 - $insert = array();
574 - $insert['translation'] = array(
575 - 'message' => 'translate-searchprofile',
576 - 'tooltip' => 'translate-searchprofile-tooltip',
577 - 'namespaces' => $wgTranslateMessageNamespaces,
578 - );
579 -
580 - $profiles = wfArrayInsertAfter( $profiles, $insert, 'help' );
581 - return true;
582 - }
583 -
584 - /**
585 - * Hook: SpecialSearchProfileForm
586 - * @param $search
587 - * @param $form
588 - * @param $profile
589 - * @param $term
590 - * @param $opts
591 - * @return bool
592 - */
593 - public static function searchProfileForm( $search, &$form, $profile, $term, $opts ) {
594 - if ( $profile !== 'translation' ) {
595 - return true;
596 - }
597 -
598 - if ( !$search->getSearchEngine()->supports( 'title-suffix-filter' ) ) {
599 - return false;
600 - }
601 -
602 - $hidden = '';
603 - foreach ( $opts as $key => $value ) {
604 - $hidden .= Html::hidden( $key, $value );
605 - }
606 -
607 - $context = $search->getContext();
608 - $code = $context->getLang()->getCode();
609 - $selected = $context->getRequest()->getVal( 'languagefilter' );
610 -
611 - if ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
612 - $languages = LanguageNames::getNames( $code,
613 - LanguageNames::FALLBACK_NORMAL,
614 - LanguageNames::LIST_MW
615 - );
616 - } else {
617 - $languages = Language::getLanguageNames( false );
618 - }
619 -
620 - ksort( $languages );
621 -
622 - $selector = new HTMLSelector( 'languagefilter', 'languagefilter', $selected );
623 - $selector->addOption( wfMessage( 'translate-search-nofilter' ), '-' );
624 - foreach ( $languages as $code => $name ) {
625 - $selector->addOption( "$code - $name", $code );
626 - }
627 -
628 - $selector = $selector->getHTML();
629 -
630 - $label = Xml::label( wfMessage( 'translate-search-languagefilter' ), 'languagefilter' ) . '&#160;';
631 - $params = array( 'id' => 'mw-searchoptions' );
632 -
633 - $form = Xml::fieldset( false, false, $params ) .
634 - $hidden . $label . $selector .
635 - Html::closeElement( 'fieldset' );
636 - return false;
637 - }
638 -
639 - /**
640 - * Hook: SpecialSearchSetupEngine
641 - * @param $search
642 - * @param $profile
643 - * @param $engine
644 - * @return bool
645 - */
646 - public static function searchProfileSetupEngine( $search, $profile, $engine ) {
647 - if ( $profile !== 'translation' ) {
648 - return true;
649 - }
650 -
651 - $context = $search->getContext();
652 - $selected = $context->getRequest()->getVal( 'languagefilter' );
653 - if ( $selected !== '-' && $selected ) {
654 - $engine->setFeatureData( 'title-suffix-filter', "/$selected" );
655 - $search->setExtraParam( 'languagefilter', $selected );
656 - }
657 - return true;
658 - }
659 -
660493 }
Index: trunk/extensions/Translate/Translate.php
@@ -94,7 +94,7 @@
9595 $wgHooks['EditPageBeforeEditButtons'][] = 'TranslateEditAddons::buttonHack';
9696 $wgHooks['EditPage::showEditForm:fields'][] = 'TranslateEditAddons::keepFields';
9797 $wgHooks['SkinTemplateTabs'][] = 'TranslateEditAddons::tabs';
98 -$wgHooks['LanguageGetTranslatedLanguageNames'][] = 'TranslateEditAddons::translateMessageDocumentationLanguage';
 98+$wgHooks['LanguageGetTranslatedLanguageNames'][] = 'TranslateHooks::translateMessageDocumentationLanguage';
9999 $wgHooks['ArticlePrepareTextForEdit'][] = 'TranslateEditAddons::disablePreSaveTransform';
100100 // Fuzzy tags for speed.
101101 $wgHooks['ArticleSaveComplete'][] = 'TranslateEditAddons::onSave';
@@ -120,9 +120,9 @@
121121 $wgHooks['ArticleContentOnDiff'][] = 'TranslateEditAddons::displayOnDiff';
122122
123123 // Search profile
124 -$wgHooks['SpecialSearchProfiles'][] = 'TranslateEditAddons::searchProfile';
125 -$wgHooks['SpecialSearchProfileForm'][] = 'TranslateEditAddons::searchProfileForm';
126 -$wgHooks['SpecialSearchSetupEngine'][] = 'TranslateEditAddons::searchProfileSetupEngine';
 124+$wgHooks['SpecialSearchProfiles'][] = 'TranslateHooks::searchProfile';
 125+$wgHooks['SpecialSearchProfileForm'][] = 'TranslateHooks::searchProfileForm';
 126+$wgHooks['SpecialSearchSetupEngine'][] = 'TranslateHooks::searchProfileSetupEngine';
127127
128128 $wgHooks['LinkBegin'][] = 'SpecialMyLanguage::linkfix';
129129
Index: trunk/extensions/Translate/TranslateHooks.php
@@ -215,4 +215,97 @@
216216 return true;
217217 }
218218
 219+ /**
 220+ * Hook: LanguageGetTranslatedLanguageNames
 221+ */
 222+ public static function translateMessageDocumentationLanguage( &$names, $code ) {
 223+ global $wgTranslateDocumentationLanguageCode;
 224+ if ( $wgTranslateDocumentationLanguageCode ) {
 225+ $names[$wgTranslateDocumentationLanguageCode] =
 226+ wfMessage( 'translate-documentation-language' )->inLanguage( $code )->plain();
 227+ }
 228+ return true;
 229+ }
 230+
 231+ /**
 232+ * Hook: SpecialSearchProfiles
 233+ */
 234+ public static function searchProfile( array &$profiles ) {
 235+ global $wgTranslateMessageNamespaces;
 236+ $insert = array();
 237+ $insert['translation'] = array(
 238+ 'message' => 'translate-searchprofile',
 239+ 'tooltip' => 'translate-searchprofile-tooltip',
 240+ 'namespaces' => $wgTranslateMessageNamespaces,
 241+ );
 242+
 243+ $profiles = wfArrayInsertAfter( $profiles, $insert, 'help' );
 244+ return true;
 245+ }
 246+
 247+ /**
 248+ * Hook: SpecialSearchProfileForm
 249+ */
 250+ public static function searchProfileForm( $search, &$form, /*string*/ $profile, $term, $opts ) {
 251+ if ( $profile !== 'translation' ) {
 252+ return true;
 253+ }
 254+
 255+ if ( !$search->getSearchEngine()->supports( 'title-suffix-filter' ) ) {
 256+ return false;
 257+ }
 258+
 259+ $hidden = '';
 260+ foreach ( $opts as $key => $value ) {
 261+ $hidden .= Html::hidden( $key, $value );
 262+ }
 263+
 264+ $context = $search->getContext();
 265+ $code = $context->getLang()->getCode();
 266+ $selected = $context->getRequest()->getVal( 'languagefilter' );
 267+
 268+ if ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
 269+ $languages = LanguageNames::getNames( $code,
 270+ LanguageNames::FALLBACK_NORMAL,
 271+ LanguageNames::LIST_MW
 272+ );
 273+ } else {
 274+ $languages = Language::getLanguageNames( false );
 275+ }
 276+
 277+ ksort( $languages );
 278+
 279+ $selector = new HTMLSelector( 'languagefilter', 'languagefilter', $selected );
 280+ $selector->addOption( wfMessage( 'translate-search-nofilter' ), '-' );
 281+ foreach ( $languages as $code => $name ) {
 282+ $selector->addOption( "$code - $name", $code );
 283+ }
 284+
 285+ $selector = $selector->getHTML();
 286+
 287+ $label = Xml::label( wfMessage( 'translate-search-languagefilter' ), 'languagefilter' ) . '&#160;';
 288+ $params = array( 'id' => 'mw-searchoptions' );
 289+
 290+ $form = Xml::fieldset( false, false, $params ) .
 291+ $hidden . $label . $selector .
 292+ Html::closeElement( 'fieldset' );
 293+ return false;
 294+ }
 295+
 296+ /// Hook: SpecialSearchSetupEngine
 297+ public static function searchProfileSetupEngine( $search, /*string*/ $profile, SearchEngine $engine ) {
 298+ if ( $profile !== 'translation' ) {
 299+ return true;
 300+ }
 301+
 302+ $context = $search->getContext();
 303+ $selected = $context->getRequest()->getVal( 'languagefilter' );
 304+ if ( $selected !== '-' && $selected ) {
 305+ $engine->setFeatureData( 'title-suffix-filter', "/$selected" );
 306+ $search->setExtraParam( 'languagefilter', $selected );
 307+ }
 308+ return true;
 309+ }
 310+
 311+
219312 }

Status & tagging log