r99248 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99247‎ | r99248 | r99249 >
Date:20:31, 7 October 2011
Author:nikerabbit
Status:deferred
Tags:
Comment:
Clean up to PageTranslationHooks, random documentation fixes
Modified paths:
  • /trunk/extensions/Translate/Groups.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateHooks.php (modified) (history)
  • /trunk/extensions/Translate/scripts/cli.inc (modified) (history)
  • /trunk/extensions/Translate/tag/PageTranslationHooks.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageHandle.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/cli.inc
@@ -69,8 +69,6 @@
7070 /**
7171 * Output a warning message to command line (if available).
7272 * @see STDOUT
73 - * @param $message
74 - * @param null $channel
7573 */
7674 function STDERR( $message, $channel = null ) {
7775 STDOUT( $message, $channel, true );
Index: trunk/extensions/Translate/tag/PageTranslationHooks.php
@@ -10,21 +10,13 @@
1111
1212 /**
1313 * Hooks for page translation.
14 - *
15 - * @todo Methods need documentation.
1614 * @ingroup PageTranslation
1715 */
1816 class PageTranslationHooks {
1917 // Uuugly hack
2018 static $allowTargetEdit = false;
2119
22 - /**
23 - * @static
24 - * @param $parser Parser
25 - * @param $text String
26 - * @param $state
27 - * @return bool
28 - */
 20+ /// Hook: ParserBeforeStrip
2921 public static function renderTagPage( $parser, &$text, $state ) {
3022 $title = $parser->getTitle();
3123
@@ -61,11 +53,8 @@
6254 /**
6355 * Set the right page content language for translated pages ("Page/xx").
6456 * Hook: PageContentLanguage
65 - * @param $title Title
66 - * @param $pageLang
67 - * @return bool
6857 */
69 - public static function onPageContentLanguage( $title, &$pageLang ) {
 58+ public static function onPageContentLanguage( Title $title, /*string*/ &$pageLang ) {
7059 // For translation pages, parse plural, grammar etc with correct language, and set the right direction
7160 if ( TranslatablePage::isTranslationPage( $title ) ) {
7261 list( , $code ) = TranslateUtils::figureMessage( $title->getText() );
@@ -74,51 +63,22 @@
7564 return true;
7665 }
7766
78 - /**
79 - * Only called from hook
80 - *
81 - * @param $outputpage
82 - * @param $text
83 - * @return bool
84 - */
85 - public static function injectCss( $outputpage, $text ) {
 67+ /// Hook: OutputPageBeforeHTML
 68+ public static function injectCss( OutputPage $outputpage, /*string*/ $text ) {
8669 $outputpage->addModules( 'ext.translate' );
87 -
8870 return true;
8971 }
9072
91 - /**
92 - * @param Title $title
93 - * @return types
94 - */
95 - public static function titleToGroup( Title $title ) {
96 - $namespace = $title->getNamespace();
97 - $text = $title->getDBkey();
98 - list( $key, ) = TranslateUtils::figureMessage( $text );
99 -
100 - return TranslateUtils::messageKeyToGroup( $namespace, $key );
101 - }
102 -
103 - /**
104 - * @param $article Article
105 - * @param $user User
106 - * @param $text
107 - * @param $summary
108 - * @param $minor
109 - * @param $_
110 - * @param $_
111 - * @param $flags
112 - * @param $revision
113 - * @return bool
114 - */
115 - public static function onSectionSave( $article, $user, $text, $summary, $minor,
116 - $_, $_, $flags, $revision ) {
 73+ /// Hook: ArticleSaveComplete
 74+ public static function onSectionSave( $article, User $user, $text, $summary,
 75+ $minor, $_, $_, $flags, $revision ) {
11776 $title = $article->getTitle();
11877
11978 // Some checks
 79+ $handle = new MessageHandle( $title );
12080
12181 // We are only interested in the translations namespace
122 - if ( $title->getNamespace() != NS_TRANSLATIONS ) {
 82+ if ( !$handle->isPageTranslation() || !$handle->isValid() ) {
12383 return true;
12484 }
12585
@@ -127,11 +87,8 @@
12888 return true;
12989 }
13090
131 - // Figure out the group
132 - $groupKey = self::titleToGroup( $title );
133 - $group = MessageGroups::getGroup( $groupKey );
 91+ $group = $handle->getGroup();
13492 if ( !$group instanceof WikiPageMessageGroup ) {
135 - SpecialPageTranslation::superDebug( __METHOD__, 'not wp-group', $title, $user, $groupKey );
13693 return true;
13794 }
13895
@@ -144,10 +101,8 @@
145102 }
146103
147104 // Update the target translation page
148 - list( , $code ) = TranslateUtils::figureMessage( $title->getDBkey() );
149 - global $wgTranslateDocumentationLanguageCode;
150 - if ( $code !== $wgTranslateDocumentationLanguageCode ) {
151 - SpecialPageTranslation::superDebug( __METHOD__, 'ok', $title, $user );
 105+ if ( !$handle->isDoc() ) {
 106+ $code = $handle->getCode();
152107 self::updateTranslationPage( $page, $code, $user, $flags, $summary );
153108 }
154109
@@ -291,7 +246,10 @@
292247 FOO;
293248 }
294249
295 - // To display nice error for editpage
 250+ /**
 251+ * Display nice error for editpage.
 252+ * Hook: EditFilterMerged
 253+ */
296254 public static function tpSyntaxCheckForEditPage( $editpage, $text, &$error, $summary ) {
297255 if ( strpos( $text, '<translate>' ) === false ) {
298256 return true;
@@ -310,16 +268,7 @@
311269 /**
312270 * When attempting to save, last resort. Edit page would only display
313271 * edit conflict if there wasn't tpSyntaxCheckForEditPage
314 - * @param $article Article
315 - * @param $user User
316 - * @param $text
317 - * @param $summary string
318 - * @param $minor bool
319 - * @param $_
320 - * @param $_
321 - * @param $flags
322 - * @param $status
323 - * @return bool
 272+ * Hook: ArticleSave
324273 */
325274 public static function tpSyntaxCheck( $article, $user, $text, $summary,
326275 $minor, $_, $_, $flags, $status ) {
@@ -339,6 +288,7 @@
340289 return true;
341290 }
342291
 292+ /// Hook: ArticleSaveComplete
343293 public static function addTranstag( $article, $user, $text, $summary,
344294 $minor, $_, $_, $flags, $revision ) {
345295 // We are not interested in null revisions
@@ -360,23 +310,25 @@
361311 return true;
362312 }
363313
364 - /// Prevent editing of unknown pages in Translations namespace
365 - public static function preventUnknownTranslations( $title, $user, $action, &$result ) {
366 - if ( $title->getNamespace() == NS_TRANSLATIONS && $action === 'edit' ) {
367 - $group = self::titleToGroup( $title );
368 - if ( $group === null ) {
369 - // No group means that the page is currently not
370 - // registered to any page translation message groups
371 - $result = array( 'tpt-unknown-page' );
372 - return false;
373 - }
 314+ /**
 315+ * Prevent editing of unknown pages in Translations namespace.
 316+ * Hook: getUserPermissionsErrorsExpensive
 317+ */
 318+ public static function preventUnknownTranslations( Title $title, User $user, $action, &$result ) {
 319+ $handle = new MessageHandle( $title );
 320+ if ( $handle->isPageTranslation() && $action === 'edit' && !$handle->isValid() ) {
 321+ $result = array( 'tpt-unknown-page' );
 322+ return false;
374323 }
375324
376325 return true;
377326 }
378327
379 - /// Prevent editing of translation pages directly
380 - public static function preventDirectEditing( $title, $user, $action, &$result ) {
 328+ /**
 329+ * Prevent editing of translation pages directly.
 330+ * Hook: getUserPermissionsErrorsExpensive
 331+ */
 332+ public static function preventDirectEditing( Title $title, User $user, $action, &$result ) {
381333 $page = TranslatablePage::isTranslationPage( $title );
382334 if ( $page !== false && $action !== 'delete' ) {
383335 if ( self::$allowTargetEdit ) {
@@ -398,30 +350,35 @@
399351 return true;
400352 }
401353
402 - public static function disableDelete( $article, $wgOut, &$reason ) {
403 - if ( TranslatablePage::isSourcePage( $article->mTitle ) || TranslatablePage::isTranslationPage( $article->mTitle ) ) {
404 - $new = SpecialPage::getTitleFor( 'PageTranslationDeletePage', $article->mTitle->getPrefixedText() );
405 - $wgOut->redirect( $new->getFullUrl() );
 354+ /**
 355+ * Redirects the delete action to our own for translatable pages.
 356+ * Hook: ArticleConfirmDelete
 357+ */
 358+ public static function disableDelete( $article, $out, &$reason ) {
 359+ $title = $article->getTitle();
 360+ if ( TranslatablePage::isSourcePage( $title ) || TranslatablePage::isTranslationPage( $title ) ) {
 361+ $new = SpecialPage::getTitleFor( 'PageTranslationDeletePage', $title->getPrefixedText() );
 362+ $out->redirect( $new->getFullUrl() );
406363 }
407364 return true;
408365 }
409366
410 - /// @todo: fix method name.
411 - public static function test( &$article, &$outputDone, &$pcache ) {
412 - if ( !$article->getOldID() ) {
413 - self::header( $article->getTitle() );
 367+ /// Hook: ArticleViewHeader
 368+ public static function translatablePageHeader( &$article, &$outputDone, &$pcache ) {
 369+ if ( $article->getOldID() ) {
 370+ return true;
414371 }
415372
416 - return true;
417 - }
 373+ $title = $article->getTitle();
418374
419 - public static function header( Title $title ) {
420375 if ( TranslatablePage::isTranslationPage( $title ) ) {
421376 self::translationPageHeader( $title );
422377 } else {
423378 // Check for pages that are tagged or marked
424379 self::sourcePageHeader( $title );
425380 }
 381+
 382+ return true;
426383 }
427384
428385 protected static function sourcePageHeader( Title $title ) {
@@ -527,24 +484,17 @@
528485 $wgOut->addHTML( '<hr />' );
529486 }
530487
 488+ /// Hook: LinksUpdate
531489 public static function preventCategorization( $updater ) {
532 - global $wgTranslateDocumentationLanguageCode;
533 - $title = $updater->getTitle();
534 - list( , $code ) = TranslateUtils::figureMessage( $title );
535 - if ( $title->getNamespace() == NS_TRANSLATIONS && $code !== $wgTranslateDocumentationLanguageCode ) {
 490+ $handle = new MessageHandle( $updater->getTitle() );
 491+ if ( $handle->isPageTranslation() && !$handle->isDoc() ) {
536492 $updater->mCategories = array();
537493 }
538494 return true;
539495 }
540496
541497 /**
542 - * @static
543 - * @param $type
544 - * @param $action
545 - * @param $title Title
546 - * @param $forUI
547 - * @param $params
548 - * @return String
 498+ * @return string
549499 */
550500 public static function formatLogEntry( $type, $action, $title, $forUI, $params ) {
551501 global $wgLang, $wgContLang;
@@ -580,13 +530,15 @@
581531 return '';
582532 }
583533
 534+ /// Hook: SpecialPage_initList
584535 public static function replaceMovePage( &$list ) {
585536 $old = is_array( $list['Movepage'] );
586537 $list['Movepage'] = array( 'SpecialPageTranslationMovePage', $old );
587538 return true;
588539 }
589540
590 - public static function lockedPagesCheck( $title, $user, $action, &$result ) {
 541+ /// Hook: getUserPermissionsErrorsExpensive
 542+ public static function lockedPagesCheck( Title $title, User $user, $action, &$result ) {
591543 global $wgMemc;
592544 $key = wfMemcKey( 'pt-lock', $title->getPrefixedText() );
593545 if ( $wgMemc->get( $key ) === true ) {
@@ -597,6 +549,7 @@
598550 return true;
599551 }
600552
 553+ /// Hook: SkinSubPageSubtitle
601554 public static function replaceSubtitle( &$subpages, $skin = null , $out = null ) {
602555 global $wgOut;
603556 // $out was only added in some MW version
Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -344,7 +344,7 @@
345345 }
346346
347347 /**
348 - * @Hook: ArticleSaveComplete
 348+ * Hook: ArticleSaveComplete
349349 * @param $article Article
350350 * @param $user User
351351 * @param $text string
Index: trunk/extensions/Translate/TranslateHooks.php
@@ -128,7 +128,7 @@
129129 $wgHooks['getUserPermissionsErrorsExpensive'][] = 'PageTranslationHooks::lockedPagesCheck';
130130
131131 // Our custom header for translation pages
132 - $wgHooks['ArticleViewHeader'][] = 'PageTranslationHooks::test';
 132+ $wgHooks['ArticleViewHeader'][] = 'PageTranslationHooks::translatablePageHeader';
133133
134134 // Prevent section pages appearing in categories
135135 $wgHooks['LinksUpdate'][] = 'PageTranslationHooks::preventCategorization';
Index: trunk/extensions/Translate/utils/MessageHandle.php
@@ -65,6 +65,23 @@
6666 return $this->code;
6767 }
6868
 69+ /**
 70+ * Determine whether the current handle is for message documentation.
 71+ * @return bool
 72+ */
 73+ public function isDoc() {
 74+ global $wgTranslateDocumentationLanguageCode;
 75+ return $this->getCode() === $wgTranslateDocumentationLanguageCode;
 76+ }
 77+
 78+ /**
 79+ * Determine whether the current handle is for page translation feature.
 80+ * @return bool
 81+ */
 82+ public function isPageTranslation() {
 83+ return $this->getTitle()->getNamespace() == NS_TRANSLATIONS;
 84+ }
 85+
6986 public function getGroupIds() {
7087 if ( $this->groupIds === null ) {
7188 $this->groupIds = TranslateUtils::messageKeyToGroups( $this->getTitle()->getNamespace(), $this->getKey() );
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -638,9 +638,8 @@
639639 return null;
640640 }
641641
642 - global $wgUser;
643 -
644 - $title = $wgUser->getSkin()->link(
 642+ $linker = class_exists( 'DummyLinker' ) ? new DummyLinker : new Linker;
 643+ $title = $linker->link(
645644 SpecialPage::getTitleFor( 'Translate' ),
646645 htmlspecialchars( $this->group->getLabel() ),
647646 array(),
Index: trunk/extensions/Translate/Groups.php
@@ -106,7 +106,6 @@
107107 /**
108108 * Returns message tags. If type is given, only messages keys with that
109109 * tag is returnted. Otherwise an array[tag => keys] is returnted.
110 - * @param null $type
111110 * @return array
112111 */
113112 public function getTags( $type = null );
@@ -114,10 +113,9 @@
115114 /**
116115 * Returns the definition or translation for given message key in given
117116 * language code.
118 - * @todo Is this needed in the interface?
119 - * @param $key
120 - * @param $code
121 - * @return string or null
 117+ * @param $key string
 118+ * @param $code string
 119+ * @return string|null
122120 */
123121 public function getMessage( $key, $code );
124122

Status & tagging log