Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialAutomaticTranslation
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
30
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
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getInitialLanguagesData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHtml
0.00% covered (danger)
0.00%
0 / 11
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
1<?php
2
3declare( strict_types = 1 );
4
5namespace ContentTranslation\Special;
6
7use ContentTranslation\SiteMapper;
8use MediaWiki\Html\TemplateParser;
9use MediaWiki\Languages\LanguageNameUtils;
10use MediaWiki\SpecialPage\SpecialPage;
11
12/**
13 * A special page for translating Wikipedia pages using MinT.
14 *
15 * This page provides the user the capability to search for an Wikipedia
16 * article in any language, and get the automatic (machine) translation for
17 * this article, by the MinT translation service.
18 *
19 * @author Nik Gkountas
20 * @license GPL-2.0-or-later
21 */
22class SpecialAutomaticTranslation extends SpecialPage {
23
24    private LanguageNameUtils $languageNameUtils;
25
26    public function __construct( LanguageNameUtils $languageNameUtils ) {
27        parent::__construct( 'AutomaticTranslation' );
28        $this->languageNameUtils = $languageNameUtils;
29    }
30
31    public function execute( $subPage ) {
32        parent::execute( $subPage );
33
34        $this->setHeaders();
35        $out = $this->getOutput();
36        $targetLanguageCode = SiteMapper::getCurrentLanguageCode();
37        $targetLanguage = $this->languageNameUtils->getLanguageName( $targetLanguageCode );
38        $out->addHTML( $this->getHtml( $targetLanguage ) );
39
40        $out->addModuleStyles( "mint.styles" );
41        $out->addModules( 'mint.app' );
42
43        // TODO: is there a way to get URL params in "getInitialLanguagesData" hook?
44        $out->addJsConfigVars( 'mintUrlSourceLanguageCode', $out->getRequest()->getVal( 'from' ) );
45        $out->addJsConfigVars( 'mintUrlTargetLanguageCode', $out->getRequest()->getVal( 'to' ) );
46    }
47
48    /**
49     * Callback used to share MinT initial languages JS code
50     */
51    public static function getInitialLanguagesData(): array {
52        return [
53            'mintInitialSourceLanguageCode' => 'all',
54            'mintInitialTargetLanguageCode' => SiteMapper::getCurrentLanguageCode()
55        ];
56    }
57
58    private function getHtml( string $targetLanguage ): string {
59        $templateParser = new TemplateParser( __DIR__ . '/../templates/minT' );
60        $data = [
61            'headerTitle' => $this->msg( 'mint-home-header-title' ),
62            'inputPlaceholder' => $this->msg( 'mint-home-input-placeholder' ),
63            'sourceLanguage' => $this->msg( 'mint-translation-list-all-languages-option-label' ),
64            'sourceLanguageCode' => 'all',
65            'targetLanguage' => $targetLanguage,
66            'infoPanelText' => $this->msg( 'mint-home-info-panel-text', $targetLanguage ),
67            'randomTopicButtonLabel' => $this->msg( 'mint-home-random-topic-button-label' )
68        ];
69
70        return $templateParser->processTemplate( 'Home', $data );
71    }
72
73    public function getDescription() {
74        return $this->msg( 'automatic-translation-special-page-description' );
75    }
76}