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