Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
14.29% |
18 / 126 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
SpecialExternalGuidance | |
14.29% |
18 / 126 |
|
0.00% |
0 / 4 |
177.21 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
mtContextGuidance | |
15.13% |
18 / 119 |
|
0.00% |
0 / 1 |
116.33 |
1 | <?php |
2 | /** |
3 | * Contains the special page Special:ExternalGuidance. |
4 | * |
5 | * @copyright See AUTHORS.txt |
6 | * @license GPL-2.0-or-later |
7 | */ |
8 | |
9 | namespace MediaWiki\Extension\ExternalGuidance; |
10 | |
11 | use ErrorPageError; |
12 | use MediaWiki\Html\Html; |
13 | use MediaWiki\Languages\LanguageNameUtils; |
14 | use MediaWiki\MainConfigNames; |
15 | use MediaWiki\Output\OutputPage; |
16 | use MediaWiki\Request\WebRequest; |
17 | use MediaWiki\SpecialPage\SpecialPage; |
18 | use MediaWiki\Title\MalformedTitleException; |
19 | use MediaWiki\Title\Title; |
20 | |
21 | /** |
22 | * Welcoming page from an ExternalGuidance contribution entry point |
23 | */ |
24 | class SpecialExternalGuidance extends SpecialPage { |
25 | |
26 | /** @var LanguageNameUtils */ |
27 | private $languageNameUtils; |
28 | |
29 | /** |
30 | * @param LanguageNameUtils $languageNameUtils |
31 | */ |
32 | public function __construct( |
33 | LanguageNameUtils $languageNameUtils |
34 | ) { |
35 | parent::__construct( 'ExternalGuidance', '', false ); |
36 | $this->languageNameUtils = $languageNameUtils; |
37 | } |
38 | |
39 | /** |
40 | * @inheritDoc |
41 | */ |
42 | public function getDescription() { |
43 | return $this->msg( 'externalguidance-specialpage-title' ); |
44 | } |
45 | |
46 | /** |
47 | * @inheritDoc |
48 | */ |
49 | public function execute( $parameters ) { |
50 | $request = $this->getRequest(); |
51 | $this->setHeaders(); |
52 | $this->outputHeader(); |
53 | // Currently we have only one context - Machine translation. So using it unconditionally. |
54 | $this->mtContextGuidance( $request, $this->getOutput() ); |
55 | } |
56 | |
57 | /** |
58 | * Machine translation context based guidance rendering |
59 | * @param WebRequest $request |
60 | * @param OutputPage $out |
61 | */ |
62 | public function mtContextGuidance( $request, $out ) { |
63 | $targetPageTitle = null; |
64 | $pageExists = false; |
65 | |
66 | $sourceLanguage = $request->getVal( 'from' ); |
67 | $targetLanguage = $request->getVal( 'to' ); |
68 | $sourcePage = $request->getVal( 'page' ); |
69 | $targetPage = $request->getVal( 'targettitle' ); |
70 | $service = $request->getVal( 'service' ); |
71 | |
72 | if ( !$service || !$sourcePage || !$sourceLanguage || !$targetLanguage ) { |
73 | throw new ErrorPageError( |
74 | 'externalguidance-specialpage-title', |
75 | 'externalguidance-specialpage-param-missing' |
76 | ); |
77 | } |
78 | |
79 | if ( !$this->languageNameUtils->isKnownLanguageTag( $sourceLanguage ) ) { |
80 | throw new ErrorPageError( |
81 | 'externalguidance-specialpage-title', |
82 | 'externalguidance-specialpage-invalid-language', |
83 | [ $sourceLanguage ] |
84 | ); |
85 | } |
86 | if ( !$this->languageNameUtils->isKnownLanguageTag( $targetLanguage ) ) { |
87 | throw new ErrorPageError( |
88 | 'externalguidance-specialpage-title', |
89 | 'externalguidance-specialpage-invalid-language', |
90 | [ $targetLanguage ] |
91 | ); |
92 | } |
93 | $config = $this->getConfig(); |
94 | if ( !in_array( $service, $config->get( 'ExternalGuidanceKnownServices' ) ) ) { |
95 | throw new ErrorPageError( |
96 | 'externalguidance-specialpage-title', |
97 | 'externalguidance-specialpage-invalid-service', |
98 | [ $service ] |
99 | ); |
100 | } |
101 | |
102 | // Create the title instance after validation. |
103 | try { |
104 | $sourcePageTitle = Title::newFromTextThrow( $sourcePage ); |
105 | if ( $targetPage ) { |
106 | $targetPageTitle = Title::newFromTextThrow( $targetPage ); |
107 | // This wiki should match the target language since the "contribute" link takes the user |
108 | // to this special page in target language. |
109 | $pageExists = $targetPageTitle->isKnown(); |
110 | } |
111 | } catch ( MalformedTitleException $e ) { |
112 | // Invalid user input. T353469 |
113 | throw new ErrorPageError( |
114 | 'externalguidance-specialpage-title', |
115 | $e->getMessageObject() |
116 | ); |
117 | } |
118 | |
119 | $out->addHTML( '<div class="eg-sp">' ); |
120 | |
121 | $siteName = $config->get( MainConfigNames::Sitename ); |
122 | if ( $pageExists ) { |
123 | $out->addWikiMsg( 'externalguidance-specialpage-mt-intro-pageexist', |
124 | $siteName, |
125 | $this->languageNameUtils->getLanguageName( $sourceLanguage ) |
126 | ); |
127 | $out->addWikiMsg( "externalguidance-specialpage-mt-pageexist", |
128 | $this->languageNameUtils->getLanguageName( $targetLanguage ) ); |
129 | } else { |
130 | $out->addWikiMsg( 'externalguidance-specialpage-mt-intro', |
131 | $siteName, |
132 | $this->languageNameUtils->getLanguageName( $sourceLanguage ), |
133 | $this->languageNameUtils->getLanguageName( $targetLanguage ) |
134 | ); |
135 | } |
136 | $out->addHTML( '<ul>' ); |
137 | $out->wrapWikiMsg( |
138 | "<li class='eg-sp-intro-machine'>" . |
139 | "<span class='eg-icon-robot'></span>" . |
140 | '<div>$1</div></li>', |
141 | 'externalguidance-specialpage-intro-machine' ); |
142 | $out->wrapWikiMsg( |
143 | "<li class='eg-sp-intro-human'>" . |
144 | "<span class='eg-icon-user'></span>" . |
145 | '<div>$1</div></li>', |
146 | 'externalguidance-specialpage-intro-human' ); |
147 | $out->addHTML( '</ul>' ); |
148 | $out->wrapWikiMsg( "<h3 class='eg-sp-ways-to-contribute'>\n$1\n</h3>", |
149 | 'externalguidance-specialpage-contribute-title' ); |
150 | $editParams = [ |
151 | // Invoke VisualEditor |
152 | 'veaction' => 'edit', |
153 | // See T212405 and T209132 |
154 | 'campaign' => 'external-machine-translation' |
155 | ]; |
156 | |
157 | if ( $pageExists ) { |
158 | $actionLabel = $this->msg( 'externalguidance-specialpage-contribute-expand-action' )->text(); |
159 | $out->addHTML( Html::element( |
160 | 'button', |
161 | [ |
162 | 'class' => 'eg-sp-contribute-expand cdx-button ' |
163 | . 'cdx-button--action-progressive cdx-button--weight-primary', |
164 | 'disabled' => true |
165 | ], |
166 | $actionLabel |
167 | ) ); |
168 | $out->addWikiMsg( 'externalguidance-specialpage-contribute-expand' ); |
169 | } else { |
170 | $actionLabel = $this->msg( 'externalguidance-specialpage-contribute-create-action' )->text(); |
171 | $out->addHTML( Html::element( |
172 | 'button', |
173 | [ |
174 | 'class' => 'eg-sp-contribute-create cdx-button cdx-button--action-progressive', |
175 | 'disabled' => true |
176 | ], |
177 | $actionLabel |
178 | ) ); |
179 | $out->addWikiMsg( 'externalguidance-specialpage-contribute-create', |
180 | $this->languageNameUtils->getLanguageName( $targetLanguage ) ); |
181 | |
182 | } |
183 | |
184 | $actionLabel = $this->msg( 'externalguidance-specialpage-contribute-improve-source-action', |
185 | $this->languageNameUtils->getLanguageName( $sourceLanguage ) )->text(); |
186 | $out->addHTML( Html::element( |
187 | 'button', |
188 | [ |
189 | 'class' => 'eg-sp-contribute-to-original cdx-button cdx-button--action-progressive ' |
190 | . 'cdx-button--weight-quiet', |
191 | 'disabled' => true |
192 | ], |
193 | $actionLabel |
194 | ) ); |
195 | $out->addWikiMsg( 'externalguidance-specialpage-contribute-improve-source' ); |
196 | $out->addHTML( '</div>' ); |
197 | |
198 | $out->addModules( 'mw.externalguidance.special' ); |
199 | $out->addJsConfigVars( [ |
200 | 'wgExternalGuidanceSourcePage' => $sourcePageTitle->getPrefixedText(), |
201 | 'wgExternalGuidanceSourceLanguage' => $sourceLanguage, |
202 | 'wgExternalGuidanceTargetLanguage' => $targetLanguage, |
203 | 'wgExternalGuidanceService' => $service, |
204 | ] ); |
205 | if ( $pageExists ) { |
206 | $out->addJsConfigVars( |
207 | 'wgExternalGuidanceTargetPage', $targetPageTitle->getPrefixedText() |
208 | ); |
209 | } |
210 | } |
211 | |
212 | } |