Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
LegacyInterfaceHookHandler.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface;
5
6use MediaWiki\Context\RequestContext;
7use MediaWiki\Diff\DifferenceEngine;
8use MediaWiki\Diff\Hook\ArticleContentOnDiffHook;
9use MediaWiki\EditPage\EditPage;
12use MediaWiki\Hook\AlternateEditHook;
13use MediaWiki\Hook\EditPage__showEditForm_initialHook;
14use MediaWiki\Hook\TitleGetEditNoticesHook;
15use MediaWiki\Language\LanguageFactory;
16use MediaWiki\Output\OutputPage;
17use MediaWiki\Skin\Components\SkinComponentUtils;
18use MediaWiki\Skin\Hook\SidebarBeforeOutputHook;
19use MediaWiki\Skin\Hook\SkinTemplateNavigation__UniversalHook;
20use Wikimedia\ArrayUtils\ArrayUtils;
21
28 implements
29 AlternateEditHook,
30 ArticleContentOnDiffHook,
31 EditPage__showEditForm_initialHook,
32 TitleGetEditNoticesHook,
33 SidebarBeforeOutputHook,
34 SkinTemplateNavigation__UniversalHook
35{
36
37 public function __construct(
38 private readonly LanguageFactory $languageFactory,
39 ) {
40 }
41
46 public function onAlternateEdit( $editPage ): void {
47 $handle = new MessageHandle( $editPage->getTitle() );
48 if ( $handle->isValid() ) {
49 $editPage->suppressIntro = true;
50 }
51 }
52
58 public function onEditPage__showEditForm_initial( $editPage, $out ): void {
59 // phpcs:enable
60 $handle = new MessageHandle( $editPage->getTitle() );
61 if ( !$handle->isValid() ) {
62 return;
63 }
64
65 $context = $out->getContext();
66 $request = $context->getRequest();
67
68 if ( $editPage->firsttime && !$request->getCheck( 'oldid' ) &&
69 !$request->getCheck( 'undo' ) ) {
70 if ( $handle->isFuzzy() ) {
71 $editPage->textbox1 = MessageHandle::makeFuzzyString( $editPage->textbox1 );
72 }
73 }
74 }
75
77 public function onTitleGetEditNotices( $title, $oldid, &$notices ) {
78 $handle = new MessageHandle( $title );
79 if ( !$handle->isValid() ) {
80 return;
81 }
82
83 // The context is required for loading style modules. This won't work in
84 // an API context e.g. when loading VisualEditor.
85 $context = RequestContext::getMain();
86
87 $th = new LegacyTranslationAids( $handle, $context, $this->languageFactory );
88 $notices[] = $th->getBoxes();
89 }
90
96 public function onArticleContentOnDiff( $diffEngine, $output ): void {
97 $title = $diffEngine->getTitle();
98 $handle = new MessageHandle( $title );
99
100 if ( !$handle->isValid() ) {
101 return;
102 }
103
104 $th = new LegacyTranslationAids( $handle, $diffEngine->getContext(), $this->languageFactory );
105 $output->addHTML( $th->getBoxes() );
106 }
107
115 public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
116 $title = $skin->getTitle();
117 $handle = new MessageHandle( $title );
118
119 if ( !$handle->isValid() ) {
120 return;
121 }
122
123 $message = $title->getNsText() . ':' . $handle->getKey();
124 $url = SkinComponentUtils::makeSpecialUrl( 'Translations', [ 'message' => $message ] );
125
126 // Add the actual toolbox entry.
127 $sidebar['TOOLBOX'][ 'alltrans' ] = [
128 'href' => $url,
129 'id' => 't-alltrans',
130 'msg' => 'translate-sidebar-alltrans',
131 ];
132 }
133
140 public function onSkinTemplateNavigation__Universal( $skin, &$tabs ): void {
141 $title = $skin->getTitle();
142 $handle = new MessageHandle( $title );
143
144 if ( !$handle->isValid() ) {
145 return;
146 }
147
148 // Don't add language parameter if viewing the message in the source language
149 $isSourceLanguage = $handle->getCode() === $handle->getGroup()->getSourceLanguage();
150
151 $tab = [
152 'text' => $skin->msg( 'tpt-tab-translate' )->text(),
153 'href' => Utilities::getEditorUrl(
154 handle: $handle,
155 action_source: 'message_page_tab',
156 includeLanguageParam: !$isSourceLanguage
157 ),
158 ];
159
160 $tabs['views'] = ArrayUtils::insertAfter( $tabs[ 'views' ], [ 'translate' => $tab ], 'view' );
161 }
162}
Class for pointing to messages, like Title class is for titles.
Integration point to MediaWiki for the legacy translation aids.
onEditPage__showEditForm_initial( $editPage, $out)
Enhances the action=edit view for wikitext editor with some translation aids.
onArticleContentOnDiff( $diffEngine, $output)
Enhances the action=diff view with some translations aids.
onAlternateEdit( $editPage)
Do not show the usual introductory messages on edit page for messages.
onSidebarBeforeOutput( $skin, &$sidebar)
Adds toolbox menu item to pages, showing all other available translations for a message.
Provides minimal translation aids which integrate with the edit page and on diffs for translatable me...
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30